fix(core): update

This commit is contained in:
Philipp Kunz 2019-09-18 16:47:31 +02:00
parent 84e1624e64
commit 6a8946f2b1
8 changed files with 102 additions and 26 deletions

6
package-lock.json generated
View File

@ -233,9 +233,9 @@
"dev": true "dev": true
}, },
"@pushrocks/smartpromise": { "@pushrocks/smartpromise": {
"version": "3.0.2", "version": "3.0.5",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.2.tgz", "resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.5.tgz",
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ==" "integrity": "sha512-9kHBWyDFjQ6cV1rseOfge02EH6huh/mrtqxlFoJoxnMaGWf5F8H3UEsskBBUGI6QKE1Bl8evr74AIKWwJ0r/bA=="
}, },
"@pushrocks/smartrequest": { "@pushrocks/smartrequest": {
"version": "1.1.23", "version": "1.1.23",

View File

@ -20,6 +20,7 @@
"@types/node": "^12.7.4" "@types/node": "^12.7.4"
}, },
"dependencies": { "dependencies": {
"@pushrocks/smartpromise": "^3.0.5",
"@pushrocks/smartrequest": "^1.1.23" "@pushrocks/smartrequest": "^1.1.23"
}, },
"files": [ "files": [

View File

@ -5,15 +5,15 @@ let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
import * as slackme from '../ts/index'; import * as slackme from '../ts/index';
let testSlackme: slackme.SlackAccount; let testSlackAccount: slackme.SlackAccount;
let testSlackMessage: slackme.SlackMessage; let testSlackMessage: slackme.SlackMessage;
tap.test('should create a valid slackme instance', async (tools) => { 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) => { tap.test('should send a message to Slack', async (tools) => {
testSlackMessage = new slackme.SlackMessage({ const messageOptions = {
author_name: 'GitLab CI', author_name: 'GitLab CI',
author_link: 'https://gitlab.com/', author_link: 'https://gitlab.com/',
pretext: '*Good News*: Build successfull!', pretext: '*Good News*: Build successfull!',
@ -30,16 +30,17 @@ tap.test('should send a message to Slack', async (tools) => {
short: true short: true
} }
] ]
}); };
await testSlackme.sendMessage({ await testSlackAccount.sendMessage({
channelArg: 'random', channelArg: 'random',
messageOptionsArg: testSlackMessage.messageOptions, messageOptions: messageOptions,
mode: 'new' mode: 'new'
}); });
}); });
tap.test('should send a message to Slack by directly calling the message', async (tools) => { tap.test('should send a message to Slack by directly calling the message', async (tools) => {
testSlackMessage = new slackme.SlackMessage( testSlackMessage = new slackme.SlackMessage(
testSlackAccount,
{ {
author_name: 'GitLab CI', author_name: 'GitLab CI',
author_link: 'https://gitlab.com/', 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 short: true
} }
] ]
}, }
testSlackme
); );
await testSlackMessage.sendToRoom('random'); await testSlackMessage.sendToRoom('random');
await tools.delayFor(1000); 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(); tap.start();

View File

@ -1,2 +1,3 @@
export * from './slack.classes.slackaccount'; export * from './slack.classes.slackaccount';
export * from './slack.classes.slackmessage'; export * from './slack.classes.slackmessage';
export * from './slack.classes.slacklog';

View File

@ -10,15 +10,32 @@ export class SlackAccount {
} }
async sendMessage(optionsArg: { async sendMessage(optionsArg: {
messageOptionsArg: IMessageOptions; messageOptions: IMessageOptions;
channelArg: string; channelArg: string;
ts?: string; ts?: string;
mode: 'new' | 'threaded' | 'update'; mode: 'new' | 'threaded' | 'update';
}) { }) {
let requestBody: any = { let requestBody: any = {
channel: optionsArg.channelArg, 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; let postUrl = this.postUrl;
switch (true) { switch (true) {
@ -35,7 +52,11 @@ export class SlackAccount {
thread_ts: optionsArg.ts thread_ts: optionsArg.ts
} }
break;
} }
console.log(requestBody);
const response = await plugins.smartrequest.postJson(postUrl, { const response = await plugins.smartrequest.postJson(postUrl, {
headers: { headers: {
Authorization: `Bearer ${this.slackToken}` Authorization: `Bearer ${this.slackToken}`

View 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```'
})
}
};

View File

@ -53,15 +53,19 @@ export interface IMessageOptions {
} }
export class SlackMessage { export class SlackMessage {
slackmeRef: SlackAccount; public slackAccountRef: SlackAccount;
messageOptions: IMessageOptions; public messageOptions: IMessageOptions;
channel: string; public channel: string;
ts: string; public ts: string;
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: SlackAccount) {
if (slackmeArg) { public requestRunning = plugins.smartpromise.defer();
this.slackmeRef = slackmeArg;
constructor(slackAccountArg: SlackAccount, messageOptionsArg: IMessageOptions) {
if (slackAccountArg) {
this.slackAccountRef = slackAccountArg;
} }
this.messageOptions = messageOptionsArg; this.messageOptions = messageOptionsArg;
this.requestRunning.resolve();
} }
async updateAndSend(messageOptionsArg: IMessageOptions) { async updateAndSend(messageOptionsArg: IMessageOptions) {
@ -76,15 +80,17 @@ export class SlackMessage {
async sendToRoom(channelNameArg: string, modeArg: 'new' | 'update' | 'threaded' = 'new') { async sendToRoom(channelNameArg: string, modeArg: 'new' | 'update' | 'threaded' = 'new') {
this.channel = channelNameArg; this.channel = channelNameArg;
if (this.slackmeRef) { if (this.slackAccountRef) {
const response = await this.slackmeRef.sendMessage({ const response = await this.slackAccountRef.sendMessage({
channelArg: this.channel, channelArg: this.channel,
messageOptionsArg: this.messageOptions, messageOptions: this.messageOptions,
mode: modeArg, mode: modeArg,
ts: this.ts ts: this.ts
}); });
this.ts = response.body.message.ts; if (modeArg === 'new') {
this.channel = response.body.channel; this.ts = response.body.message.ts;
this.channel = response.body.channel;
}
} else { } else {
throw new Error('you need to set a slackRef before sending the message!'); throw new Error('you need to set a slackRef before sending the message!');
} }

View File

@ -1,4 +1,5 @@
// pushrocks scope // pushrocks scope
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest'; import * as smartrequest from '@pushrocks/smartrequest';
export { smartrequest }; export { smartpromise, smartrequest };