12 Commits

Author SHA1 Message Date
7937d9aeff 3.0.4 2019-09-18 19:37:45 +02:00
07ea092dea fix(core): update 2019-09-18 19:37:45 +02:00
ccc2fc689f 3.0.3 2019-09-18 16:51:51 +02:00
9e3808bfee fix(core): update 2019-09-18 16:51:50 +02:00
717dcab4ad 3.0.2 2019-09-18 16:47:32 +02:00
6a8946f2b1 fix(core): update 2019-09-18 16:47:31 +02:00
84e1624e64 3.0.1 2019-09-18 15:47:51 +02:00
b46d958f35 fix(core): update 2019-09-18 15:47:51 +02:00
d044085739 3.0.0 2019-09-18 15:11:55 +02:00
3510767a72 BREAKING CHANGE(changed main class to SlackAccount): update 2019-09-18 15:11:55 +02:00
ec17d7835e 2.0.12 2019-09-18 12:28:42 +02:00
cb072d0f6a fix(core): update 2019-09-18 12:28:41 +02:00
10 changed files with 329 additions and 90 deletions

View File

@ -54,7 +54,7 @@ testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
@ -65,7 +65,7 @@ testBuild:
release:
stage: release
script:
- npmci node install lts
- npmci node install stable
- npmci npm publish
only:
- tags

118
package-lock.json generated
View File

@ -1,9 +1,29 @@
{
"name": "@mojoio/slack",
"version": "2.0.11",
"version": "3.0.4",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@babel/code-frame": {
"version": "7.5.5",
"resolved": "https://verdaccio.lossless.one/@babel%2fcode-frame/-/code-frame-7.5.5.tgz",
"integrity": "sha512-27d4lZoomVyo51VegxI20xZPuSHusqbQag/ztrBC7wegWoQ1nLREPVSKSW8byhTlzTKyNE4ifaTA6lCp7JjpFw==",
"dev": true,
"requires": {
"@babel/highlight": "^7.0.0"
}
},
"@babel/highlight": {
"version": "7.5.0",
"resolved": "https://verdaccio.lossless.one/@babel%2fhighlight/-/highlight-7.5.0.tgz",
"integrity": "sha512-7dV4eu9gBxoM0dAnj/BCFDW9LFU0zvTrkq0ugM7pnHEgguOEeOz1so2ZghEdzviYzQEED0r4EAgpsBChKy1TRQ==",
"dev": true,
"requires": {
"chalk": "^2.0.0",
"esutils": "^2.0.2",
"js-tokens": "^4.0.0"
}
},
"@gitzone/tsbuild": {
"version": "2.1.17",
"resolved": "https://verdaccio.lossless.one/@gitzone%2ftsbuild/-/tsbuild-2.1.17.tgz",
@ -233,9 +253,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",
@ -473,6 +493,12 @@
"integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
"dev": true
},
"builtin-modules": {
"version": "1.1.1",
"resolved": "https://verdaccio.lossless.one/builtin-modules/-/builtin-modules-1.1.1.tgz",
"integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=",
"dev": true
},
"camelcase": {
"version": "5.3.1",
"resolved": "https://verdaccio.lossless.one/camelcase/-/camelcase-5.3.1.tgz",
@ -609,6 +635,12 @@
"delayed-stream": "~1.0.0"
}
},
"commander": {
"version": "2.20.0",
"resolved": "https://verdaccio.lossless.one/commander/-/commander-2.20.0.tgz",
"integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==",
"dev": true
},
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
@ -707,6 +739,12 @@
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
"dev": true
},
"esutils": {
"version": "2.0.3",
"resolved": "https://verdaccio.lossless.one/esutils/-/esutils-2.0.3.tgz",
"integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==",
"dev": true
},
"execa": {
"version": "1.0.0",
"resolved": "https://verdaccio.lossless.one/execa/-/execa-1.0.0.tgz",
@ -881,6 +919,12 @@
"integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=",
"dev": true
},
"js-tokens": {
"version": "4.0.0",
"resolved": "https://verdaccio.lossless.one/js-tokens/-/js-tokens-4.0.0.tgz",
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
"dev": true
},
"js-yaml": {
"version": "3.13.1",
"resolved": "https://verdaccio.lossless.one/js-yaml/-/js-yaml-3.13.1.tgz",
@ -988,6 +1032,21 @@
"brace-expansion": "^1.1.7"
}
},
"minimist": {
"version": "0.0.8",
"resolved": "https://verdaccio.lossless.one/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
"dev": true
},
"mkdirp": {
"version": "0.5.1",
"resolved": "https://verdaccio.lossless.one/mkdirp/-/mkdirp-0.5.1.tgz",
"integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
"dev": true,
"requires": {
"minimist": "0.0.8"
}
},
"moment": {
"version": "2.24.0",
"resolved": "https://verdaccio.lossless.one/moment/-/moment-2.24.0.tgz",
@ -1152,6 +1211,12 @@
"integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=",
"dev": true
},
"path-parse": {
"version": "1.0.6",
"resolved": "https://verdaccio.lossless.one/path-parse/-/path-parse-1.0.6.tgz",
"integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==",
"dev": true
},
"pathval": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.0.tgz",
@ -1219,6 +1284,15 @@
"integrity": "sha1-l/cXtp1IeE9fUmpsWqj/3aBVpNE=",
"dev": true
},
"resolve": {
"version": "1.12.0",
"resolved": "https://verdaccio.lossless.one/resolve/-/resolve-1.12.0.tgz",
"integrity": "sha512-B/dOmuoAik5bKcD6s6nXDCjzUKnaDvdkRyAk6rsmsKLipWj4797iothd7jmmUhWTfinVMU+wc56rYKsit2Qy4w==",
"dev": true,
"requires": {
"path-parse": "^1.0.6"
}
},
"restore-cursor": {
"version": "2.0.0",
"resolved": "https://verdaccio.lossless.one/restore-cursor/-/restore-cursor-2.0.0.tgz",
@ -1409,6 +1483,42 @@
"integrity": "sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==",
"dev": true
},
"tslint": {
"version": "5.20.0",
"resolved": "https://verdaccio.lossless.one/tslint/-/tslint-5.20.0.tgz",
"integrity": "sha512-2vqIvkMHbnx8acMogAERQ/IuINOq6DFqgF8/VDvhEkBqQh/x6SP0Y+OHnKth9/ZcHQSroOZwUQSN18v8KKF0/g==",
"dev": true,
"requires": {
"@babel/code-frame": "^7.0.0",
"builtin-modules": "^1.1.1",
"chalk": "^2.3.0",
"commander": "^2.12.1",
"diff": "^4.0.1",
"glob": "^7.1.1",
"js-yaml": "^3.13.1",
"minimatch": "^3.0.4",
"mkdirp": "^0.5.1",
"resolve": "^1.3.2",
"semver": "^5.3.0",
"tslib": "^1.8.0",
"tsutils": "^2.29.0"
}
},
"tslint-config-prettier": {
"version": "1.18.0",
"resolved": "https://verdaccio.lossless.one/tslint-config-prettier/-/tslint-config-prettier-1.18.0.tgz",
"integrity": "sha512-xPw9PgNPLG3iKRxmK7DWr+Ea/SzrvfHtjFt5LBl61gk2UBG/DB9kCXRjv+xyIU1rUtnayLeMUVJBcMX8Z17nDg==",
"dev": true
},
"tsutils": {
"version": "2.29.0",
"resolved": "https://verdaccio.lossless.one/tsutils/-/tsutils-2.29.0.tgz",
"integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==",
"dev": true,
"requires": {
"tslib": "^1.8.1"
}
},
"type-detect": {
"version": "4.0.8",
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "@mojoio/slack",
"version": "2.0.11",
"version": "3.0.4",
"private": false,
"description": "slack api abstraction for the mojo.io ecosystem",
"main": "dist/index.js",
@ -17,9 +17,12 @@
"@gitzone/tstest": "^1.0.24",
"@pushrocks/qenv": "^4.0.4",
"@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^12.7.4"
"@types/node": "^12.7.4",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0"
},
"dependencies": {
"@pushrocks/smartpromise": "^3.0.5",
"@pushrocks/smartrequest": "^1.1.23"
},
"files": [

View File

@ -5,15 +5,41 @@ let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
import * as slackme from '../ts/index';
let testSlackme: slackme.Slackme;
let testSlackAccount: slackme.SlackAccount;
let testSlackMessage: slackme.SlackMessage;
tap.test('should create a valid slackme instance', async (tools) => {
testSlackme = new slackme.Slackme(testQenv.getEnvVarOnDemand('SLACK_TOKEN'));
tap.test('should create a valid slackme instance', async tools => {
testSlackAccount = new slackme.SlackAccount(testQenv.getEnvVarOnDemand('SLACK_TOKEN'));
});
tap.test('should send a message to Slack', async (tools) => {
testSlackMessage = new slackme.SlackMessage({
tap.test('should send a message to Slack', async tools => {
const messageOptions = {
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
}
]
};
await testSlackAccount.sendMessage({
channelArg: 'random',
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/',
pretext: '*Good News*: Build successfull!',
@ -31,33 +57,8 @@ tap.test('should send a message to Slack', async (tools) => {
}
]
});
await testSlackme.sendMessage(testSlackMessage.messageOptions, 'random');
});
tap.test('should send a message to Slack by directly calling the message', async (tools) => {
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(5000);
await tools.delayFor(1000);
await testSlackMessage.updateAndSend({
author_name: 'GitLab CI',
author_link: 'https://gitlab.com/',
@ -75,7 +76,55 @@ 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
}
]
});
});
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();

View File

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

View File

@ -0,0 +1,63 @@
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: {
messageOptions: IMessageOptions;
channelArg: string;
ts?: string;
mode: 'new' | 'threaded' | 'update';
}) {
let requestBody: any = {
channel: optionsArg.channelArg,
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) {
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
};
break;
}
const response = await plugins.smartrequest.postJson(postUrl, {
headers: {
Authorization: `Bearer ${this.slackToken}`
},
requestBody
});
return response;
}
}

View File

@ -0,0 +1,29 @@
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

@ -1,33 +0,0 @@
import * as plugins from './slack.plugins';
import { IMessageOptions } from './slack.classes.slackmessage';
export class Slackme {
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;
}
}

View File

@ -1,5 +1,5 @@
import * as plugins from './slack.plugins';
import { Slackme } from './slack.classes.slackme';
import { SlackAccount } from './slack.classes.slackaccount';
export interface IAttachmentField {
title: string;
@ -53,28 +53,44 @@ export interface IMessageOptions {
}
export class SlackMessage {
slackmeRef: Slackme;
messageOptions: IMessageOptions;
channel: string;
ts: string;
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: Slackme) {
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) {
this.messageOptions = messageOptionsArg;
await this.sendToRoom(this.channel);
await this.sendToRoom(this.channel, 'update');
}
async sendToRoom(roomNameArg: string) {
this.channel = roomNameArg;
if (this.slackmeRef) {
const response = await this.slackmeRef.sendMessage(this.messageOptions, roomNameArg, this.ts);
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.slackAccountRef) {
const response = await this.slackAccountRef.sendMessage({
channelArg: this.channel,
messageOptions: this.messageOptions,
mode: modeArg,
ts: this.ts
});
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!');
}

View File

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