From e854a62eae04c443df3f026c923ad554de95eaf5 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Wed, 18 Sep 2019 12:26:31 +0200 Subject: [PATCH] fix(core): update --- .vscode/launch.json | 29 ++++++++++++++++++++++++++++ test/test.ts | 32 +++++++++++++++++++++++-------- ts/slack.classes.slackme.ts | 33 +++++++++++++++++++++++--------- ts/slack.classes.slackmessage.ts | 16 ++++++++++++---- 4 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..112db52 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "current file", + "type": "node", + "request": "launch", + "args": [ + "${relativeFile}" + ], + "runtimeArgs": ["-r", "@gitzone/tsrun"], + "cwd": "${workspaceRoot}", + "protocol": "inspector", + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": "test.ts", + "type": "node", + "request": "launch", + "args": [ + "test/test.ts" + ], + "runtimeArgs": ["-r", "@gitzone/tsrun"], + "cwd": "${workspaceRoot}", + "protocol": "inspector", + "internalConsoleOptions": "openOnSessionStart" + } + ] +} diff --git a/test/test.ts b/test/test.ts index 24bff15..197a1da 100644 --- a/test/test.ts +++ b/test/test.ts @@ -8,11 +8,11 @@ import * as slackme from '../ts/index'; let testSlackme: slackme.Slackme; let testSlackMessage: slackme.SlackMessage; -tap.test('should create a valid slackme instance', async () => { +tap.test('should create a valid slackme instance', async (tools) => { testSlackme = new slackme.Slackme(testQenv.getEnvVarOnDemand('SLACK_TOKEN')); }); -tap.test('should send a message to Slack', async () => { +tap.test('should send a message to Slack', async (tools) => { testSlackMessage = new slackme.SlackMessage({ author_name: 'GitLab CI', author_link: 'https://gitlab.com/', @@ -29,14 +29,12 @@ tap.test('should send a message to Slack', async () => { value: 'pushrocks', short: true } - ], - ts: new Date().getTime() / 1000 + ] }); await testSlackme.sendMessage(testSlackMessage.messageOptions, 'random'); - await expect(testSlackMessage.sendToRoom('random')).to.eventually.be.rejected; }); -tap.test('should send a message to Slack by directly calling the message', async () => { +tap.test('should send a message to Slack by directly calling the message', async (tools) => { testSlackMessage = new slackme.SlackMessage( { author_name: 'GitLab CI', @@ -54,12 +52,30 @@ tap.test('should send a message to Slack by directly calling the message', async value: 'pushrocks', short: true } - ], - ts: new Date().getTime() / 1000 + ] }, testSlackme ); await testSlackMessage.sendToRoom('random'); + await tools.delayFor(5000); + 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: 'pushrocks', + short: true + } + ] + }) }); tap.start(); diff --git a/ts/slack.classes.slackme.ts b/ts/slack.classes.slackme.ts index 129b8b9..0803e7d 100644 --- a/ts/slack.classes.slackme.ts +++ b/ts/slack.classes.slackme.ts @@ -2,19 +2,34 @@ import * as plugins from './slack.plugins'; import { IMessageOptions } from './slack.classes.slackmessage'; export class Slackme { - private baseUrl = 'https://hooks.slack.com/services/'; - private postRoute: string; - constructor(postRouteArg: string) { - this.postRoute = postRouteArg; + 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 = 'general') { - const response = await plugins.smartrequest.postJson(`${this.baseUrl}${this.postRoute}`, { - requestBody: { - channel: channelArg, - attachments: [messageOptionsArg] + 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; + } + console.log(requestBody); + const response = await plugins.smartrequest.postJson(postUrl, { + headers: { + 'Authorization': `Bearer ${this.slackToken}` + }, + requestBody }); + console.log(response.body); return response; } } diff --git a/ts/slack.classes.slackmessage.ts b/ts/slack.classes.slackmessage.ts index 68557bd..29260a7 100644 --- a/ts/slack.classes.slackmessage.ts +++ b/ts/slack.classes.slackmessage.ts @@ -49,24 +49,32 @@ export interface IMessageOptions { thumb_url?: string; footer?: string; footer_icon?: string; - /** - * timestamp as epoch time - */ ts?: number; } export class SlackMessage { slackmeRef: Slackme; messageOptions: IMessageOptions; + channel: string; + ts: string; constructor(messageOptionsArg: IMessageOptions, slackmeArg?: Slackme) { if (slackmeArg) { this.slackmeRef = slackmeArg; } this.messageOptions = messageOptionsArg; } + + async updateAndSend(messageOptionsArg: IMessageOptions) { + this.messageOptions = messageOptionsArg; + await this.sendToRoom(this.channel); + } + async sendToRoom(roomNameArg: string) { + this.channel = roomNameArg; if (this.slackmeRef) { - await this.slackmeRef.sendMessage(this.messageOptions, roomNameArg); + const response = await this.slackmeRef.sendMessage(this.messageOptions, roomNameArg, this.ts); + 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!'); }