Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
d044085739 | |||
3510767a72 | |||
ec17d7835e | |||
cb072d0f6a | |||
1240505227 | |||
09c0bc4e6f | |||
41f98b6869 | |||
e854a62eae | |||
ec86352b58 | |||
47f1e97386 | |||
f7e1df2e64 | |||
552d3dc211 |
@ -38,13 +38,25 @@ snyk:
|
|||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install lts
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
@ -86,7 +98,9 @@ trigger:
|
|||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-dbase:npmci
|
||||||
|
services:
|
||||||
|
- docker:stable-dind
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/slack",
|
"name": "@mojoio/slack",
|
||||||
"version": "2.0.7",
|
"version": "3.0.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/slack",
|
"name": "@mojoio/slack",
|
||||||
"version": "2.0.7",
|
"version": "3.0.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "slack api abstraction for the mojo.io ecosystem",
|
"description": "slack api abstraction for the mojo.io ecosystem",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -27,6 +27,7 @@
|
|||||||
"ts_web/*",
|
"ts_web/*",
|
||||||
"dist/*",
|
"dist/*",
|
||||||
"dist_web/*",
|
"dist_web/*",
|
||||||
|
"dist_ts_web/*",
|
||||||
"assets/*",
|
"assets/*",
|
||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
|
@ -101,6 +101,6 @@ mySlackMessage.sendToRoom('anotherroom')
|
|||||||
For further information read the linked docs at the top of this readme.
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
[](https://maintainedby.lossless.com)
|
||||||
|
47
test/test.ts
47
test/test.ts
@ -5,14 +5,14 @@ let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
|
|||||||
|
|
||||||
import * as slackme from '../ts/index';
|
import * as slackme from '../ts/index';
|
||||||
|
|
||||||
let testSlackme: slackme.Slackme;
|
let testSlackme: slackme.SlackAccount;
|
||||||
let testSlackMessage: slackme.SlackMessage;
|
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'));
|
testSlackme = new slackme.SlackAccount(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({
|
testSlackMessage = new slackme.SlackMessage({
|
||||||
author_name: 'GitLab CI',
|
author_name: 'GitLab CI',
|
||||||
author_link: 'https://gitlab.com/',
|
author_link: 'https://gitlab.com/',
|
||||||
@ -29,15 +29,36 @@ tap.test('should send a message to Slack', async () => {
|
|||||||
value: 'pushrocks',
|
value: 'pushrocks',
|
||||||
short: true
|
short: true
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
ts: new Date().getTime() / 1000
|
|
||||||
});
|
});
|
||||||
await testSlackme.sendMessage(testSlackMessage.messageOptions, 'random');
|
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({
|
testSlackMessage = new slackme.SlackMessage(
|
||||||
|
{
|
||||||
|
author_name: 'GitLab CI',
|
||||||
|
author_link: 'https://gitlab.com/',
|
||||||
|
pretext: '*Good News*: Build successfull!',
|
||||||
|
color: '#3cb371',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
title: 'Branch',
|
||||||
|
value: 'Lossless Cloud',
|
||||||
|
short: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Product ID',
|
||||||
|
value: 'pushrocks',
|
||||||
|
short: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
testSlackme
|
||||||
|
);
|
||||||
|
await testSlackMessage.sendToRoom('random');
|
||||||
|
await tools.delayFor(1000);
|
||||||
|
await testSlackMessage.updateAndSend({
|
||||||
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!',
|
||||||
@ -45,7 +66,7 @@ tap.test('should send a message to Slack by directly calling the message', async
|
|||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
title: 'Branch',
|
title: 'Branch',
|
||||||
value: 'Lossless Cloud',
|
value: 'Lossless Studio',
|
||||||
short: true
|
short: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -53,10 +74,8 @@ tap.test('should send a message to Slack by directly calling the message', async
|
|||||||
value: 'pushrocks',
|
value: 'pushrocks',
|
||||||
short: true
|
short: true
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
ts: new Date().getTime() / 1000
|
})
|
||||||
}, testSlackme);
|
|
||||||
await testSlackMessage.sendToRoom('random');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,19 +1,33 @@
|
|||||||
import * as plugins from './slack.plugins';
|
import * as plugins from './slack.plugins';
|
||||||
import { IMessageOptions } from './slack.classes.slackmessage';
|
import { IMessageOptions } from './slack.classes.slackmessage';
|
||||||
|
|
||||||
export class Slackme {
|
export class SlackAccount {
|
||||||
private baseUrl = 'https://hooks.slack.com/services/';
|
private postUrl = 'https://slack.com/api/chat.postMessage';
|
||||||
private postRoute: string;
|
private updateUrl = 'https://slack.com/api/chat.update';
|
||||||
constructor(postRouteArg: string) {
|
private slackToken: string;
|
||||||
this.postRoute = postRouteArg;
|
constructor(slackTokenArg: string) {
|
||||||
|
this.slackToken = slackTokenArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendMessage(messageOptionsArg: IMessageOptions, channelArg: string = 'general') {
|
async sendMessage(messageOptionsArg: IMessageOptions, channelArg: string = 'status', tsArg?: string) {
|
||||||
await plugins.smartrequest.postJson(`${this.baseUrl}${this.postRoute}`, {
|
let requestBody: any = {
|
||||||
requestBody: {
|
channel: channelArg,
|
||||||
channel: channelArg,
|
attachments: [messageOptionsArg]
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as plugins from './slack.plugins';
|
import * as plugins from './slack.plugins';
|
||||||
import { Slackme } from './slack.classes.slackme';
|
import { SlackAccount } from './slack.classes.slackme';
|
||||||
|
|
||||||
export interface IAttachmentField {
|
export interface IAttachmentField {
|
||||||
title: string;
|
title: string;
|
||||||
@ -49,24 +49,32 @@ export interface IMessageOptions {
|
|||||||
thumb_url?: string;
|
thumb_url?: string;
|
||||||
footer?: string;
|
footer?: string;
|
||||||
footer_icon?: string;
|
footer_icon?: string;
|
||||||
/**
|
|
||||||
* timestamp as epoch time
|
|
||||||
*/
|
|
||||||
ts?: number;
|
ts?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SlackMessage {
|
export class SlackMessage {
|
||||||
slackmeRef: Slackme;
|
slackmeRef: SlackAccount;
|
||||||
messageOptions: IMessageOptions;
|
messageOptions: IMessageOptions;
|
||||||
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: Slackme) {
|
channel: string;
|
||||||
|
ts: string;
|
||||||
|
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: SlackAccount) {
|
||||||
if (slackmeArg) {
|
if (slackmeArg) {
|
||||||
this.slackmeRef = slackmeArg;
|
this.slackmeRef = slackmeArg;
|
||||||
}
|
}
|
||||||
this.messageOptions = messageOptionsArg;
|
this.messageOptions = messageOptionsArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async updateAndSend(messageOptionsArg: IMessageOptions) {
|
||||||
|
this.messageOptions = messageOptionsArg;
|
||||||
|
await this.sendToRoom(this.channel);
|
||||||
|
}
|
||||||
|
|
||||||
async sendToRoom(roomNameArg: string) {
|
async sendToRoom(roomNameArg: string) {
|
||||||
|
this.channel = roomNameArg;
|
||||||
if (this.slackmeRef) {
|
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 {
|
} 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!');
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user