Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
84e1624e64 | |||
b46d958f35 | |||
d044085739 | |||
3510767a72 | |||
ec17d7835e | |||
cb072d0f6a | |||
1240505227 | |||
09c0bc4e6f | |||
41f98b6869 | |||
e854a62eae | |||
ec86352b58 | |||
47f1e97386 | |||
f7e1df2e64 | |||
552d3dc211 | |||
f0d7e5c998 | |||
eeb1a99884 | |||
55e75766b7 | |||
7012782480 | |||
4cec262ed8 | |||
7d1f7c28d6 |
19
.gitignore
vendored
19
.gitignore
vendored
@ -1,5 +1,22 @@
|
||||
.nogit/
|
||||
node_modules/
|
||||
|
||||
# artifacts
|
||||
coverage/
|
||||
public/
|
||||
pages/
|
||||
|
||||
# installs
|
||||
node_modules/
|
||||
|
||||
# caches
|
||||
.yarn/
|
||||
.cache/
|
||||
.rpt2_cache
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
|
||||
# custom
|
@ -1,5 +1,5 @@
|
||||
# gitzone standard
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
# gitzone ci_default
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
|
||||
cache:
|
||||
paths:
|
||||
@ -26,6 +26,7 @@ mirror:
|
||||
snyk:
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
@ -36,44 +37,35 @@ snyk:
|
||||
# ====================
|
||||
# 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:
|
||||
stage: test
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testSTABLE:
|
||||
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
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install stable
|
||||
- npmci node install lts
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
@ -86,19 +78,11 @@ release:
|
||||
# ====================
|
||||
codequality:
|
||||
stage: metadata
|
||||
image: docker:stable
|
||||
allow_failure: true
|
||||
services:
|
||||
- docker:stable-dind
|
||||
script:
|
||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
||||
- docker run
|
||||
--env SOURCE_CODE="$PWD"
|
||||
--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]
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- docker
|
||||
- priv
|
||||
@ -114,12 +98,15 @@ trigger:
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
image: hosttoday/ht-docker-dbase:npmci
|
||||
services:
|
||||
- docker:stable-dind
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci command npm install -g typedoc typescript
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
@ -130,13 +117,3 @@ pages:
|
||||
paths:
|
||||
- public
|
||||
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
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,5 +2,15 @@
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"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
1287
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
33
package.json
33
package.json
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "@pushrocks/slack",
|
||||
"version": "2.0.4",
|
||||
"name": "@mojoio/slack",
|
||||
"version": "3.0.1",
|
||||
"private": false,
|
||||
"description": "API abstraction for mojoio",
|
||||
"description": "slack api abstraction for the mojo.io ecosystem",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"author": "Lossless GmbH",
|
||||
@ -12,14 +12,25 @@
|
||||
"build": "tsbuild"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tsrun": "^1.1.12",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/qenv": "^2.0.2",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.9.4"
|
||||
"@gitzone/tsbuild": "^2.1.17",
|
||||
"@gitzone/tsrun": "^1.2.8",
|
||||
"@gitzone/tstest": "^1.0.24",
|
||||
"@pushrocks/qenv": "^4.0.4",
|
||||
"@pushrocks/tapbundle": "^3.0.13",
|
||||
"@types/node": "^12.7.4"
|
||||
},
|
||||
"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"
|
||||
]
|
||||
}
|
||||
|
40
readme.md
40
readme.md
@ -1,26 +1,20 @@
|
||||
# slackme
|
||||
# @mojoio/slack
|
||||
slack api abstraction for the mojo.io ecosystem
|
||||
|
||||
API abstraction for mojoio
|
||||
|
||||
## Availabililty
|
||||
|
||||
[](https://www.npmjs.com/package/slackme)
|
||||
[](https://GitLab.com/mojoio/slackme)
|
||||
[](https://github.com/mojoio/slackme)
|
||||
[](https://mojoio.gitlab.io/slackme/)
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@mojoio/slack)
|
||||
* [gitlab.com (source)](https://gitlab.com/mojoio/slack)
|
||||
* [github.com (source mirror)](https://github.com/mojoio/slack)
|
||||
* [docs (typedoc)](https://mojoio.gitlab.io/slack/)
|
||||
|
||||
## Status for master
|
||||
|
||||
[](https://GitLab.com/mojoio/slackme/commits/master)
|
||||
[](https://GitLab.com/mojoio/slackme/commits/master)
|
||||
[](https://www.npmjs.com/package/slackme)
|
||||
[](https://david-dm.org/mojoio/slackme)
|
||||
[](https://www.bithound.io/github/mojoio/slackme/master/dependencies/npm)
|
||||
[](https://www.bithound.io/github/mojoio/slackme)
|
||||
[](https://snyk.io/test/npm/slackme)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
||||
[](https://standardjs.com/)
|
||||
[](https://gitlab.com/mojoio/slack/commits/master)
|
||||
[](https://gitlab.com/mojoio/slack/commits/master)
|
||||
[](https://www.npmjs.com/package/@mojoio/slack)
|
||||
[](https://snyk.io/test/npm/@mojoio/slack)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://prettier.io/)
|
||||
|
||||
## Usage
|
||||
|
||||
@ -104,9 +98,9 @@ mySlackMessage.title = 'another Title'
|
||||
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)
|
||||
> | 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://mojo.io)
|
||||
[](https://maintainedby.lossless.com)
|
||||
|
95
test/test.ts
95
test/test.ts
@ -5,14 +5,40 @@ let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
|
||||
|
||||
import * as slackme from '../ts/index';
|
||||
|
||||
let testSlackme: slackme.Slackme;
|
||||
let testSlackme: slackme.SlackAccount;
|
||||
let testSlackMessage: slackme.SlackMessage;
|
||||
|
||||
tap.test('should create a valid slackme instance', async () => {
|
||||
testSlackme = new slackme.Slackme(process.env.SLACK_TOKEN);
|
||||
tap.test('should create a valid slackme instance', async (tools) => {
|
||||
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(
|
||||
{
|
||||
author_name: 'GitLab CI',
|
||||
@ -30,13 +56,66 @@ tap.test('should send a message to Slack', async () => {
|
||||
value: 'pushrocks',
|
||||
short: true
|
||||
}
|
||||
],
|
||||
ts: new Date().getTime() / 1000
|
||||
]
|
||||
},
|
||||
testSlackme
|
||||
);
|
||||
testSlackme.sendMessage(testSlackMessage.messageOptions, 'random');
|
||||
testSlackMessage.sendToRoom('random');
|
||||
await 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();
|
||||
|
@ -1,2 +1,2 @@
|
||||
export * from './slack.classes.slackme';
|
||||
export * from './slack.classes.slackaccount';
|
||||
export * from './slack.classes.slackmessage';
|
||||
|
47
ts/slack.classes.slackaccount.ts
Normal file
47
ts/slack.classes.slackaccount.ts
Normal 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;
|
||||
}
|
||||
}
|
@ -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]
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -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;
|
||||
@ -49,24 +49,44 @@ export interface IMessageOptions {
|
||||
thumb_url?: string;
|
||||
footer?: string;
|
||||
footer_icon?: string;
|
||||
/**
|
||||
* timestamp as epoch time
|
||||
*/
|
||||
ts?: number;
|
||||
}
|
||||
|
||||
export class SlackMessage {
|
||||
slackmeRef: Slackme;
|
||||
slackmeRef: SlackAccount;
|
||||
messageOptions: IMessageOptions;
|
||||
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: Slackme) {
|
||||
channel: string;
|
||||
ts: string;
|
||||
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: SlackAccount) {
|
||||
if (slackmeArg) {
|
||||
this.slackmeRef = slackmeArg;
|
||||
}
|
||||
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) {
|
||||
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!');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
import * as smartrequest from 'smartrequest';
|
||||
// pushrocks scope
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
|
||||
export { smartrequest };
|
||||
|
16
tslint.json
16
tslint.json
@ -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"
|
||||
}
|
||||
|
Reference in New Issue
Block a user