20 Commits

Author SHA1 Message Date
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
1240505227 2.0.11 2019-09-18 12:27:52 +02:00
09c0bc4e6f fix(core): update 2019-09-18 12:27:52 +02:00
41f98b6869 2.0.10 2019-09-18 12:26:32 +02:00
e854a62eae fix(core): update 2019-09-18 12:26:31 +02:00
ec86352b58 2.0.9 2019-09-08 19:37:13 +02:00
47f1e97386 fix(core): update 2019-09-08 19:37:13 +02:00
f7e1df2e64 2.0.8 2019-09-05 13:09:13 +02:00
552d3dc211 fix(core): update 2019-09-05 13:09:13 +02:00
f0d7e5c998 2.0.7 2019-09-05 13:08:43 +02:00
eeb1a99884 fix(core): update 2019-09-05 13:08:42 +02:00
55e75766b7 2.0.6 2018-10-07 22:29:15 +02:00
7012782480 fix(dependencies): update 2018-10-07 22:29:14 +02:00
4cec262ed8 2.0.5 2018-09-15 22:36:39 +02:00
7d1f7c28d6 fix(core): update 2018-09-15 22:36:39 +02:00
15 changed files with 1255 additions and 465 deletions

19
.gitignore vendored
View File

@ -1,5 +1,22 @@
.nogit/ .nogit/
node_modules/
# artifacts
coverage/ coverage/
public/ public/
pages/ pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
# custom

View File

@ -1,5 +1,5 @@
# gitzone standard # gitzone ci_default
image: hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache: cache:
paths: paths:
@ -26,6 +26,7 @@ mirror:
snyk: snyk:
stage: security stage: security
script: script:
- npmci npm prepare
- npmci command npm install -g snyk - npmci command npm install -g snyk
- npmci command npm install --ignore-scripts - npmci command npm install --ignore-scripts
- npmci command snyk test - npmci command snyk test
@ -36,44 +37,35 @@ snyk:
# ==================== # ====================
# test stage # test stage
# ==================== # ====================
testLEGACY:
stage: test
script:
- npmci node install legacy
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
allow_failure: true
testLTS: testStable:
stage: test
script:
- npmci node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testSTABLE:
stage: test stage: test
script: script:
- npmci npm prepare
- npmci node install stable - npmci node install stable
- npmci npm install - npmci npm install
- npmci npm test - npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
- priv
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv - notpriv
release: release:
stage: release stage: release
script: script:
- npmci node install stable - npmci node install lts
- npmci npm publish - npmci npm publish
only: only:
- tags - tags
@ -86,19 +78,11 @@ release:
# ==================== # ====================
codequality: codequality:
stage: metadata stage: metadata
image: docker:stable
allow_failure: true allow_failure: true
services:
- docker:stable-dind
script: script:
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/') - npmci command npm install -g tslint typescript
- docker run - npmci npm install
--env SOURCE_CODE="$PWD" - npmci command "tslint -c tslint.json ./ts/**/*.ts"
--volume "$PWD":/code
--volume /var/run/docker.sock:/var/run/docker.sock
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
artifacts:
paths: [codeclimate.json]
tags: tags:
- docker - docker
- priv - priv
@ -114,12 +98,15 @@ 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 typedoc typescript - npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install - npmci npm install
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/ - npmci command tsdoc
tags: tags:
- docker - docker
- notpriv - notpriv
@ -130,13 +117,3 @@ pages:
paths: paths:
- public - public
allow_failure: true allow_failure: true
windowsCompatibility:
image: stefanscherer/node-windows:10-build-tools
stage: metadata
script:
- npm install & npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- windows
allow_failure: true

29
.vscode/launch.json vendored Normal file
View 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"
}
]
}

View File

@ -2,5 +2,15 @@
"npmci": { "npmci": {
"npmGlobalTools": [], "npmGlobalTools": [],
"npmAccessLevel": "public" "npmAccessLevel": "public"
},
"gitzone": {
"module": {
"githost": "gitlab.com",
"gitscope": "mojoio",
"gitrepo": "slack",
"shortDescription": "slack api abstraction for the mojo.io ecosystem",
"npmPackagename": "@mojoio/slack",
"license": "MIT"
}
} }
} }

1287
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
{ {
"name": "@pushrocks/slack", "name": "@mojoio/slack",
"version": "2.0.4", "version": "3.0.1",
"private": false, "private": false,
"description": "API abstraction for mojoio", "description": "slack api abstraction for the mojo.io ecosystem",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
"author": "Lossless GmbH", "author": "Lossless GmbH",
@ -12,14 +12,25 @@
"build": "tsbuild" "build": "tsbuild"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.0.22", "@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsrun": "^1.1.12", "@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.15", "@gitzone/tstest": "^1.0.24",
"@pushrocks/qenv": "^2.0.2", "@pushrocks/qenv": "^4.0.4",
"@pushrocks/tapbundle": "^3.0.7", "@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^10.9.4" "@types/node": "^12.7.4"
}, },
"dependencies": { "dependencies": {
"smartrequest": "^1.0.6" "@pushrocks/smartrequest": "^1.1.23"
} },
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"cli.js",
"npmextra.json",
"readme.md"
]
} }

View File

@ -1,2 +1,2 @@
vars: required:
- SLACK_TOKEN - SLACK_TOKEN

View File

@ -1,26 +1,20 @@
# slackme # @mojoio/slack
slack api abstraction for the mojo.io ecosystem
API abstraction for mojoio ## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@mojoio/slack)
## Availabililty * [gitlab.com (source)](https://gitlab.com/mojoio/slack)
* [github.com (source mirror)](https://github.com/mojoio/slack)
[![npm](https://mojoio.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/slackme) * [docs (typedoc)](https://mojoio.gitlab.io/slack/)
[![git](https://mojoio.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/mojoio/slackme)
[![git](https://mojoio.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/mojoio/slackme)
[![docs](https://mojoio.gitlab.io/assets/repo-button-docs.svg)](https://mojoio.gitlab.io/slackme/)
## Status for master ## Status for master
[![build status](https://gitlab.com/mojoio/slack/badges/master/build.svg)](https://gitlab.com/mojoio/slack/commits/master)
[![build status](https://GitLab.com/mojoio/slackme/badges/master/build.svg)](https://GitLab.com/mojoio/slackme/commits/master) [![coverage report](https://gitlab.com/mojoio/slack/badges/master/coverage.svg)](https://gitlab.com/mojoio/slack/commits/master)
[![coverage report](https://GitLab.com/mojoio/slackme/badges/master/coverage.svg)](https://GitLab.com/mojoio/slackme/commits/master) [![npm downloads per month](https://img.shields.io/npm/dm/@mojoio/slack.svg)](https://www.npmjs.com/package/@mojoio/slack)
[![npm downloads per month](https://img.shields.io/npm/dm/slackme.svg)](https://www.npmjs.com/package/slackme) [![Known Vulnerabilities](https://snyk.io/test/npm/@mojoio/slack/badge.svg)](https://snyk.io/test/npm/@mojoio/slack)
[![Dependency Status](https://david-dm.org/mojoio/slackme.svg)](https://david-dm.org/mojoio/slackme) [![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![bitHound Dependencies](https://www.bithound.io/github/mojoio/slackme/badges/dependencies.svg)](https://www.bithound.io/github/mojoio/slackme/master/dependencies/npm) [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![bitHound Code](https://www.bithound.io/github/mojoio/slackme/badges/code.svg)](https://www.bithound.io/github/mojoio/slackme) [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
[![Known Vulnerabilities](https://snyk.io/test/npm/slackme/badge.svg)](https://snyk.io/test/npm/slackme)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://standardjs.com/)
## Usage ## Usage
@ -104,9 +98,9 @@ mySlackMessage.title = 'another Title'
mySlackMessage.sendToRoom('anotherroom') 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)
[![repo-footer](https://mojoio.gitlab.io/assets/repo-footer.svg)](https://mojo.io) [![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

View File

@ -5,14 +5,40 @@ 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(process.env.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({
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 testSlackme.sendMessage({
channelArg: 'random',
messageOptionsArg: testSlackMessage.messageOptions,
mode: 'new'
});
});
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_name: 'GitLab CI',
@ -30,13 +56,66 @@ tap.test('should send a message to Slack', async () => {
value: 'pushrocks', value: 'pushrocks',
short: true short: true
} }
], ]
ts: new Date().getTime() / 1000
}, },
testSlackme testSlackme
); );
testSlackme.sendMessage(testSlackMessage.messageOptions, 'random'); await testSlackMessage.sendToRoom('random');
testSlackMessage.sendToRoom('random'); await tools.delayFor(1000);
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
}
]
});
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.start(); tap.start();

View File

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

View File

@ -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;
}
}

View File

@ -1,22 +0,0 @@
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;
}
sendMessage(messageOptionsArg: IMessageOptions, channelArg: string = 'general') {
plugins.smartrequest.post(`${this.baseUrl}${this.postRoute}`, {
headers: {
'Content-Type': 'application/json'
},
requestBody: {
channel: channelArg,
attachments: [messageOptionsArg]
}
});
}
}

View File

@ -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.slackaccount';
export interface IAttachmentField { export interface IAttachmentField {
title: string; title: string;
@ -49,24 +49,44 @@ 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;
} }
sendToRoom(roomNameArg: string) {
async updateAndSend(messageOptionsArg: IMessageOptions) {
this.messageOptions = messageOptionsArg;
await this.sendToRoom(this.channel, 'update');
}
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) { if (this.slackmeRef) {
this.slackmeRef.sendMessage(this.messageOptions, roomNameArg); 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 {
throw new Error('you need to set a slackRef before sending the message!');
} }
} }
} }

View File

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

View File

@ -1,3 +1,17 @@
{ {
"extends": "tslint-config-standard" "extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
} }