From b46d958f350cbd52b8fe0254afaa3c0219fc4770 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Wed, 18 Sep 2019 15:47:51 +0200 Subject: [PATCH] fix(core): update --- test/test.ts | 42 +++++++++++++++++++++++++++- ts/index.ts | 2 +- ts/slack.classes.slackaccount.ts | 47 ++++++++++++++++++++++++++++++++ ts/slack.classes.slackme.ts | 33 ---------------------- ts/slack.classes.slackmessage.ts | 20 ++++++++++---- 5 files changed, 104 insertions(+), 40 deletions(-) create mode 100644 ts/slack.classes.slackaccount.ts delete mode 100644 ts/slack.classes.slackme.ts diff --git a/test/test.ts b/test/test.ts index d17c8ae..4d54802 100644 --- a/test/test.ts +++ b/test/test.ts @@ -31,7 +31,11 @@ tap.test('should send a message to Slack', async (tools) => { } ] }); - await testSlackme.sendMessage(testSlackMessage.messageOptions, 'random'); + await testSlackme.sendMessage({ + channelArg: 'random', + messageOptionsArg: testSlackMessage.messageOptions, + mode: 'new' + }); }); tap.test('should send a message to Slack by directly calling the message', async (tools) => { @@ -75,6 +79,42 @@ tap.test('should send a message to Slack by directly calling the message', async short: true } ] + }); + await testSlackMessage.updateAndSend({ + author_name: 'GitLab CI', + author_link: 'https://gitlab.com/', + pretext: '*Good News*: Build successfull!', + color: '#3cb371', + fields: [ + { + title: 'Branch', + value: 'Lossless Studio', + short: true + }, + { + title: 'Product ID', + value: 'onboard.me', + short: true + } + ] + }); + await testSlackMessage.startThread({ + author_name: 'Lossless Compliance', + author_link: 'https://gitlab.com/', + pretext: '*Good News*: Build successfull!', + color: '#3cb371', + fields: [ + { + title: 'Branch', + value: 'Lossless Studio', + short: true + }, + { + title: 'Product ID', + value: 'pushrocks', + short: true + } + ] }) }); diff --git a/ts/index.ts b/ts/index.ts index de7d061..88b5763 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,2 +1,2 @@ -export * from './slack.classes.slackme'; +export * from './slack.classes.slackaccount'; export * from './slack.classes.slackmessage'; diff --git a/ts/slack.classes.slackaccount.ts b/ts/slack.classes.slackaccount.ts new file mode 100644 index 0000000..09d5d1b --- /dev/null +++ b/ts/slack.classes.slackaccount.ts @@ -0,0 +1,47 @@ +import * as plugins from './slack.plugins'; +import { IMessageOptions } from './slack.classes.slackmessage'; + +export class SlackAccount { + private postUrl = 'https://slack.com/api/chat.postMessage'; + private updateUrl = 'https://slack.com/api/chat.update'; + private slackToken: string; + constructor(slackTokenArg: string) { + this.slackToken = slackTokenArg; + } + + async sendMessage(optionsArg: { + messageOptionsArg: IMessageOptions; + channelArg: string; + ts?: string; + mode: 'new' | 'threaded' | 'update'; + }) { + let requestBody: any = { + channel: optionsArg.channelArg, + attachments: [optionsArg.messageOptionsArg] + }; + let postUrl = this.postUrl; + + switch (true) { + case optionsArg.ts && optionsArg.mode === 'update': + requestBody = { + ...requestBody, + ts: optionsArg.ts + }; + postUrl = this.updateUrl; + break; + case optionsArg.ts && optionsArg.mode === 'threaded': + requestBody = { + ...requestBody, + thread_ts: optionsArg.ts + + } + } + const response = await plugins.smartrequest.postJson(postUrl, { + headers: { + Authorization: `Bearer ${this.slackToken}` + }, + requestBody + }); + return response; + } +} diff --git a/ts/slack.classes.slackme.ts b/ts/slack.classes.slackme.ts deleted file mode 100644 index 1e316fe..0000000 --- a/ts/slack.classes.slackme.ts +++ /dev/null @@ -1,33 +0,0 @@ -import * as plugins from './slack.plugins'; -import { IMessageOptions } from './slack.classes.slackmessage'; - -export class SlackAccount { - private postUrl = 'https://slack.com/api/chat.postMessage'; - private updateUrl = 'https://slack.com/api/chat.update'; - private slackToken: string; - constructor(slackTokenArg: string) { - this.slackToken = slackTokenArg; - } - - async sendMessage(messageOptionsArg: IMessageOptions, channelArg: string = 'status', tsArg?: string) { - let requestBody: any = { - channel: channelArg, - attachments: [messageOptionsArg] - } - let postUrl = this.postUrl; - if (tsArg) { - requestBody = { - ...requestBody, - ts: tsArg - } - postUrl = this.updateUrl; - } - const response = await plugins.smartrequest.postJson(postUrl, { - headers: { - 'Authorization': `Bearer ${this.slackToken}` - }, - requestBody - }); - return response; - } -} diff --git a/ts/slack.classes.slackmessage.ts b/ts/slack.classes.slackmessage.ts index 78b1424..36ab346 100644 --- a/ts/slack.classes.slackmessage.ts +++ b/ts/slack.classes.slackmessage.ts @@ -1,5 +1,5 @@ import * as plugins from './slack.plugins'; -import { SlackAccount } from './slack.classes.slackme'; +import { SlackAccount } from './slack.classes.slackaccount'; export interface IAttachmentField { title: string; @@ -66,13 +66,23 @@ export class SlackMessage { async updateAndSend(messageOptionsArg: IMessageOptions) { this.messageOptions = messageOptionsArg; - await this.sendToRoom(this.channel); + await this.sendToRoom(this.channel, 'update'); } - async sendToRoom(roomNameArg: string) { - this.channel = roomNameArg; + async startThread(messageOptionsArg: IMessageOptions) { + this.messageOptions = messageOptionsArg; + this.sendToRoom(this.channel, 'threaded') + } + + async sendToRoom(channelNameArg: string, modeArg: 'new' | 'update' | 'threaded' = 'new') { + this.channel = channelNameArg; if (this.slackmeRef) { - const response = await this.slackmeRef.sendMessage(this.messageOptions, roomNameArg, this.ts); + const response = await this.slackmeRef.sendMessage({ + channelArg: this.channel, + messageOptionsArg: this.messageOptions, + mode: modeArg, + ts: this.ts + }); this.ts = response.body.message.ts; this.channel = response.body.channel; } else {