fix(core): update
This commit is contained in:
parent
84e1624e64
commit
6a8946f2b1
6
package-lock.json
generated
6
package-lock.json
generated
@ -233,9 +233,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"@pushrocks/smartpromise": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.2.tgz",
|
||||
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ=="
|
||||
"version": "3.0.5",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.5.tgz",
|
||||
"integrity": "sha512-9kHBWyDFjQ6cV1rseOfge02EH6huh/mrtqxlFoJoxnMaGWf5F8H3UEsskBBUGI6QKE1Bl8evr74AIKWwJ0r/bA=="
|
||||
},
|
||||
"@pushrocks/smartrequest": {
|
||||
"version": "1.1.23",
|
||||
|
@ -20,6 +20,7 @@
|
||||
"@types/node": "^12.7.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartpromise": "^3.0.5",
|
||||
"@pushrocks/smartrequest": "^1.1.23"
|
||||
},
|
||||
"files": [
|
||||
|
30
test/test.ts
30
test/test.ts
@ -5,15 +5,15 @@ let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
|
||||
|
||||
import * as slackme from '../ts/index';
|
||||
|
||||
let testSlackme: slackme.SlackAccount;
|
||||
let testSlackAccount: slackme.SlackAccount;
|
||||
let testSlackMessage: slackme.SlackMessage;
|
||||
|
||||
tap.test('should create a valid slackme instance', async (tools) => {
|
||||
testSlackme = new slackme.SlackAccount(testQenv.getEnvVarOnDemand('SLACK_TOKEN'));
|
||||
testSlackAccount = new slackme.SlackAccount(testQenv.getEnvVarOnDemand('SLACK_TOKEN'));
|
||||
});
|
||||
|
||||
tap.test('should send a message to Slack', async (tools) => {
|
||||
testSlackMessage = new slackme.SlackMessage({
|
||||
const messageOptions = {
|
||||
author_name: 'GitLab CI',
|
||||
author_link: 'https://gitlab.com/',
|
||||
pretext: '*Good News*: Build successfull!',
|
||||
@ -30,16 +30,17 @@ tap.test('should send a message to Slack', async (tools) => {
|
||||
short: true
|
||||
}
|
||||
]
|
||||
});
|
||||
await testSlackme.sendMessage({
|
||||
};
|
||||
await testSlackAccount.sendMessage({
|
||||
channelArg: 'random',
|
||||
messageOptionsArg: testSlackMessage.messageOptions,
|
||||
messageOptions: messageOptions,
|
||||
mode: 'new'
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('should send a message to Slack by directly calling the message', async (tools) => {
|
||||
testSlackMessage = new slackme.SlackMessage(
|
||||
testSlackAccount,
|
||||
{
|
||||
author_name: 'GitLab CI',
|
||||
author_link: 'https://gitlab.com/',
|
||||
@ -57,8 +58,7 @@ tap.test('should send a message to Slack by directly calling the message', async
|
||||
short: true
|
||||
}
|
||||
]
|
||||
},
|
||||
testSlackme
|
||||
}
|
||||
);
|
||||
await testSlackMessage.sendToRoom('random');
|
||||
await tools.delayFor(1000);
|
||||
@ -118,4 +118,18 @@ tap.test('should send a message to Slack by directly calling the message', async
|
||||
})
|
||||
});
|
||||
|
||||
tap.test('should send logs', async () => {
|
||||
const slackLog = new slackme.SlackLog({
|
||||
slackAccount: testSlackAccount,
|
||||
channelName: 'random'
|
||||
})
|
||||
for (let i = 0; i < 30; i++) {
|
||||
await slackLog.sendLogLine('hi there');
|
||||
await slackLog.sendLogLine('so awesome');
|
||||
await slackLog.sendLogLine('really');
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
||||
tap.start();
|
||||
|
@ -1,2 +1,3 @@
|
||||
export * from './slack.classes.slackaccount';
|
||||
export * from './slack.classes.slackmessage';
|
||||
export * from './slack.classes.slacklog';
|
||||
|
@ -10,15 +10,32 @@ export class SlackAccount {
|
||||
}
|
||||
|
||||
async sendMessage(optionsArg: {
|
||||
messageOptionsArg: IMessageOptions;
|
||||
messageOptions: IMessageOptions;
|
||||
channelArg: string;
|
||||
ts?: string;
|
||||
mode: 'new' | 'threaded' | 'update';
|
||||
}) {
|
||||
|
||||
let requestBody: any = {
|
||||
channel: optionsArg.channelArg,
|
||||
attachments: [optionsArg.messageOptionsArg]
|
||||
text: optionsArg.messageOptions.text,
|
||||
};
|
||||
|
||||
if (optionsArg.messageOptions.fields) {
|
||||
requestBody = {
|
||||
...requestBody,
|
||||
attachments: [{
|
||||
pretext: optionsArg.messageOptions.pretext,
|
||||
fields: optionsArg.messageOptions.fields,
|
||||
ts: optionsArg.messageOptions.ts,
|
||||
color: optionsArg.messageOptions.color
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
let postUrl = this.postUrl;
|
||||
|
||||
switch (true) {
|
||||
@ -35,7 +52,11 @@ export class SlackAccount {
|
||||
thread_ts: optionsArg.ts
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
console.log(requestBody);
|
||||
|
||||
const response = await plugins.smartrequest.postJson(postUrl, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.slackToken}`
|
||||
|
32
ts/slack.classes.slacklog.ts
Normal file
32
ts/slack.classes.slacklog.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { SlackAccount } from "./slack.classes.slackaccount";
|
||||
import { SlackMessage } from "./slack.classes.slackmessage";
|
||||
|
||||
export class SlackLog {
|
||||
public slackAccount: SlackAccount;
|
||||
public slackMessage: SlackMessage;
|
||||
public channelName: string;
|
||||
|
||||
public completeLog = ``;
|
||||
|
||||
constructor(optionsArg: {
|
||||
slackAccount: SlackAccount;
|
||||
channelName: string;
|
||||
}) {
|
||||
this.slackAccount = optionsArg.slackAccount;
|
||||
this.channelName = optionsArg.channelName;
|
||||
}
|
||||
public async sendLogLine(logText: string) {
|
||||
if (!this.slackMessage) {
|
||||
this.slackMessage = new SlackMessage(this.slackAccount, {
|
||||
text: '``` log is loading... ```'
|
||||
});
|
||||
await this.slackMessage.sendToRoom(this.channelName);
|
||||
}
|
||||
const date = new Date();
|
||||
this.completeLog += `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()} - ` + logText + '\n';
|
||||
await this.slackMessage.updateAndSend({
|
||||
text: '```\n' + this.completeLog + '\n```'
|
||||
})
|
||||
}
|
||||
|
||||
};
|
@ -53,15 +53,19 @@ export interface IMessageOptions {
|
||||
}
|
||||
|
||||
export class SlackMessage {
|
||||
slackmeRef: SlackAccount;
|
||||
messageOptions: IMessageOptions;
|
||||
channel: string;
|
||||
ts: string;
|
||||
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: SlackAccount) {
|
||||
if (slackmeArg) {
|
||||
this.slackmeRef = slackmeArg;
|
||||
public slackAccountRef: SlackAccount;
|
||||
public messageOptions: IMessageOptions;
|
||||
public channel: string;
|
||||
public ts: string;
|
||||
|
||||
public requestRunning = plugins.smartpromise.defer();
|
||||
|
||||
constructor(slackAccountArg: SlackAccount, messageOptionsArg: IMessageOptions) {
|
||||
if (slackAccountArg) {
|
||||
this.slackAccountRef = slackAccountArg;
|
||||
}
|
||||
this.messageOptions = messageOptionsArg;
|
||||
this.requestRunning.resolve();
|
||||
}
|
||||
|
||||
async updateAndSend(messageOptionsArg: IMessageOptions) {
|
||||
@ -76,15 +80,17 @@ export class SlackMessage {
|
||||
|
||||
async sendToRoom(channelNameArg: string, modeArg: 'new' | 'update' | 'threaded' = 'new') {
|
||||
this.channel = channelNameArg;
|
||||
if (this.slackmeRef) {
|
||||
const response = await this.slackmeRef.sendMessage({
|
||||
if (this.slackAccountRef) {
|
||||
const response = await this.slackAccountRef.sendMessage({
|
||||
channelArg: this.channel,
|
||||
messageOptionsArg: this.messageOptions,
|
||||
messageOptions: this.messageOptions,
|
||||
mode: modeArg,
|
||||
ts: this.ts
|
||||
});
|
||||
this.ts = response.body.message.ts;
|
||||
this.channel = response.body.channel;
|
||||
if (modeArg === 'new') {
|
||||
this.ts = response.body.message.ts;
|
||||
this.channel = response.body.channel;
|
||||
}
|
||||
} else {
|
||||
throw new Error('you need to set a slackRef before sending the message!');
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
// pushrocks scope
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
|
||||
export { smartrequest };
|
||||
export { smartpromise, smartrequest };
|
||||
|
Loading…
x
Reference in New Issue
Block a user