60 Commits

Author SHA1 Message Date
Austin Lee 6288e4afac Update .drone.yml 2021-06-13 18:36:39 +08:00
Austin Lee 3e0dec29ee Update .drone.yml 2021-06-13 18:34:52 +08:00
lizheming 46b144516a feat: update dep drone-render version 2020-09-04 20:52:48 +08:00
lizheming f6d38f75a8 1.1.7 2019-12-10 13:27:57 +08:00
Austin Lee 58ca98d95f Merge pull request #5 from zgayjjf/patch-1
feat: add support for Nunjuncks with btntext
2019-12-10 13:27:22 +08:00
jeffjing b1482817f1 feat: add support for Nunjuncks with btntext 2019-12-10 13:26:03 +08:00
lizheming 7c09b00e73 1.1.6 2019-02-01 19:14:41 +08:00
lizheming 27431b7e9d fix #4 2019-02-01 19:14:37 +08:00
lizheming a3b45bf3d7 1.1.5 2019-01-31 15:07:07 +08:00
lizheming 547d96caea update drone-render version 2019-01-31 15:07:04 +08:00
lizheming ebb4fa4536 add to_user default value 2019-01-30 17:18:05 +08:00
lizheming 5b410441e6 1.1.4 2019-01-30 17:14:09 +08:00
lizheming a10b8c2982 fix test bug 2019-01-30 17:13:59 +08:00
lizheming 31457c2db5 1.1.3 2019-01-30 17:10:44 +08:00
lizheming 8f3fadac27 fix #3 2019-01-30 17:10:40 +08:00
lizheming 4f828a37a2 1.1.2 2019-01-23 11:55:49 +08:00
lizheming 960b54b0da fix test with totag 2019-01-23 11:45:48 +08:00
lizheming 13e1a9672a 1.1.1 2019-01-23 11:43:39 +08:00
lizheming 7c508b94a1 fix #2 2019-01-23 11:43:30 +08:00
lizheming 4c689927d8 fix version 2019-01-13 21:33:41 +08:00
lizheming 3810c42e90 add eslint disable 2019-01-13 21:31:16 +08:00
lizheming ae91f54be3 add log msg 2019-01-13 21:30:50 +08:00
lizheming a0ef9d4b26 fix def is 0 or false error 2019-01-13 21:11:25 +08:00
lizheming 1fc4bd1275 add debug mode 2019-01-13 20:52:36 +08:00
lizheming e53de0b4e4 update drone wechat notifiy title 2018-12-20 22:03:18 +08:00
lizheming f8afb41b72 add render support for title 2018-12-20 21:58:58 +08:00
lizheming 76a3278435 update sonar sources 2018-12-19 22:20:55 +08:00
lizheming 40c3af1759 update sonar_host 2018-12-19 22:17:35 +08:00
lizheming 74ef2be5a1 add file to docker in Dockerfile 2018-12-19 21:57:17 +08:00
lizheming 759632c133 anyway run step 2018-12-18 09:36:45 +08:00
lizheming 6c83fe7b28 set build step without registry 2018-12-17 00:02:57 +08:00
lizheming deba525faa rm cache for drone 2018-12-16 23:57:26 +08:00
lizheming 749bc72903 update drone 2018-12-16 23:56:06 +08:00
lizheming 9c55502ccb update npm script 2018-12-16 23:56:02 +08:00
lizheming 5f358b0cf9 修改单元测试 2018-12-16 23:55:46 +08:00
lizheming 93a5d6a792 拆分解耦逻辑 2018-12-16 23:55:39 +08:00
lizheming 4dd376b565 add drone_build_link for msg_url default 2018-12-10 11:19:08 +08:00
lizheming ceeb1a1d14 disable sonar 2018-12-10 10:16:41 +08:00
lizheming 05dde2a990 add lcov ignore 2018-12-10 10:09:51 +08:00
lizheming 42f12803c1 update drone 2018-12-09 22:37:00 +08:00
lizheming 387eff6ec0 增加 coveralls 以及 sonarqube 2018-12-09 22:28:04 +08:00
lizheming 85cb2c566d disable travis 2018-12-03 12:15:07 +08:00
lizheming 9db93837f7 add notifier for drone 2018-12-03 12:12:21 +08:00
lizheming 651b21897a update drone config 2018-12-03 11:59:23 +08:00
lizheming be4e4a0a7a rm unused test case 2018-12-03 11:59:18 +08:00
lizheming da1cf0d3f3 disable drone cache 2018-11-25 10:38:44 +08:00
lizheming 1ee57a206a drone init 2018-11-25 10:25:25 +08:00
lizheming 009655fcfe update drone docs 2018-03-23 09:55:56 +08:00
lizheming b511b81acc 2.0.0 2018-03-23 09:53:35 +08:00
lizheming e38559fd09 remove server chan service 2018-03-23 09:53:12 +08:00
lizheming 3d2f8941f1 rm unnecessary word 2018-01-09 10:26:26 +08:00
lizheming 4dbc0654a7 update dockerfile version 2018-01-09 09:18:28 +08:00
lizheming 7d12e3185c fix readme mistake 2018-01-09 09:08:49 +08:00
lizheming 1ad073309e 1.1.0 2018-01-08 22:42:23 +08:00
lizheming 996d01cbcc update docs.md 2018-01-08 22:42:13 +08:00
lizheming 2533b5f5cd update docs.md 2018-01-08 22:40:16 +08:00
lizheming ab8de5c343 update test 2018-01-08 22:27:15 +08:00
lizheming 734dc3f63a rm camelcase lint 2018-01-08 21:55:51 +08:00
lizheming d60ee90f42 add wechat corp id support raw 2018-01-08 21:51:53 +08:00
lizheming a449906a10 update plugin info 2018-01-07 18:09:30 +08:00
13 changed files with 483 additions and 88 deletions
+81
View File
@@ -0,0 +1,81 @@
kind: pipeline
name: default
steps:
# - name: restore-cache
# image: drillster/drone-volume-cache
# volumes:
# - name: cache
# path: /cache
# settings:
# restore: true
# mount:
# - ./node_modules
- name: build
image: node:lts-alpine
commands:
- npm set config package-lock false
- npm install
- npm test
- name: coveralls
pull: true
image: lizheming/drone-coveralls
environment:
COVERALLS_REPO_TOKEN:
from_secret: coveralls_token
settings:
debug: true
files:
- coverage/lcov.info
# - name: code analysis
# image: jgentry/drone-sonar
# environment:
# sonar_host: "https://sonarcloud.io"
# sonar_login:
# from_secret: sonar_token
# - name: rebuild-cache
# image: drillster/drone-volume-cache
# volumes:
# - name: cache
# path: /cache
# settings:
# rebuild: true
# mount:
# - ./node_modules
# - name: wechat
# image: lizheming/drone-wechat
# pull: always
# settings:
# title: >
# {% if success %}
# ✅ {{repo.owner}}/{{repo.name}} 第 {{build.number}} 次构建成功!
# {% else %}
# ❌ {{repo.owner}}/{{repo.name}} 第 {{build.number}} 次构建失败了,快来修理下吧。
# {% endif %}
# corpid:
# from_secret: corpid
# corp_secret:
# from_secret: corp_secret
# agent_id:
# from_secret: agent_id
# to_user: "@all"
# message: >
# {% if success %}
# 😊主人,{{repo.owner}}/{{repo.name}}第{{build.number}}次构建成功!
# {% else %}
# 😭主人,{{repo.owner}}/{{repo.name}}第{{build.number}}次构建失败了,快来修理下吧。
# {% endif %}
# when:
# status:
# - success
# - failure
# volumes:
# - name: cache
# host:
# path: /tmp/cache
+4 -1
View File
@@ -1,3 +1,6 @@
{
"extends": "think"
"extends": "think",
"rules": {
"camelcase": "off"
}
}
+1
View File
@@ -57,3 +57,4 @@ typings/
# dotenv environment variables file
.env
lcov.info
+8 -8
View File
@@ -1,8 +1,8 @@
language: node_js
node_js:
- '8'
sudo: false
script:
- "npm test"
after_success:
- 'npm install coveralls && ./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
# language: node_js
# node_js:
# - '8'
# sudo: false
# script:
# - "npm test"
# after_success:
# - 'npm install coveralls && ./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
+61 -7
View File
@@ -5,16 +5,25 @@ author: lizheming
tags: [ notifications, chat ]
repo: lizheming/drone-wechat
logo: wechat.svg
image: lizheming/drone-wechat
---
The Wechat plugin posts build status messages to your account. The below pipeline configuration demonstrates simple usage:
```yaml
pipline:
```diff
pipeline:
wechat:
image: lizheming/drone-wechat
sckey: xxxxxx
title: "plugin notification"
+ corpid: corpid
+ corp_secret: secret
+ agent_id: 1234567
+ to_user: 111
+ to_party: 112
+ to_tag: ${DRONE_REPO_NAME}
+ msg_url: ${DRONE_BUILD_LINK}
+ safe: 1
+ btn_txt: more
title: ${DRONE_REPO_NAME}
message: >
{%if success %}
build {{build.number}} succeeded. Good job.
@@ -23,16 +32,61 @@ pipline:
{% endif %}
```
# Praameter Reference
# Parameter Reference
sckey
: SCKEY get from [ServerChan](http://sc.ftqq.com)
corpid
: The corpid for authorization
corp_secret
: The corp secret for authorization
agent_id:
: The agent id to send the message
to_party
: The party ids to send message
to_user
: The user ids to send the message to
to_tag
: The tag ids to send the message to
safe
: encrypt message, default is false
msg_url
: The link for the text card click
btn_text
: The text for the button on the card
title
: Notification title
message
: Notification body message, support markdown
# Secret Reference
wechat_corpid
: The corpid for authorization
wechat\_corp\_secret
: The corp secret for authorization
wechat\_agent\_id
: The agent id to send the message
wechat\_to\_party
: The party ids to send message
wechat\_to\_user
: The user ids to send the message to
wechat\_to\_tag
: The tag ids to send the message to
# Template Reference
repo.owner
+3 -2
View File
@@ -1,13 +1,14 @@
FROM mhart/alpine-node:8.9.3
LABEL maintainer="lizheming <i@imnerd.org>" \
org.label-schema.name="Drone Telegram Node" \
org.label-schema.name="Drone Wechat Notification" \
org.label-schema.vendor="lizheming" \
org.label-schema.schema-version="1.0"
org.label-schema.schema-version="1.2.0"
WORKDIR /wechat
COPY package.json /wechat/package.json
RUN npm install --production --registry=https://registry.npm.taobao.org
COPY index.js /wechat/index.js
COPY plugin.js /wechat/plugin.js
ENTRYPOINT [ "node", "/wechat/index.js" ]
+29 -7
View File
@@ -7,17 +7,31 @@
[![](https://images.microbadger.com/badges/image/lizheming/drone-wechat.svg)](https://microbadger.com/images/lizheming/drone-wechat)
[![GitHub release](https://img.shields.io/github/release/lizheming/drone-wechat.svg)]()
Drone plugin for sending telegram notifications.
Drone plugin for sending wechat notifications.
## Description
This drone wechat notification plugin builds base on [ServerChan](http://sc.ftqq.com). Before using you should login to [ServerChan](http://sc.ftqq.com) to get `SCKEY`. After login with Github account you can get `SCKEY` at http://sc.ftqq.com/?c=code.
This drone wechat notification plugin builds base on Wechat for Work.
Read this in other languages: [English](README.md), [简体中文](README.zh-cn.md).
## Environment
- `PLUGIN_SCKEY`: SCKEY get from [ServerChan](http://sc.ftqq.com)
- `PLUGIN_CORPID`: The corpid for authorization
- `WECHAT_CORPID`: alias for `PLUGIN_CORPID`
- `PLUGIN_CORP_SECRET`: The corp secret for authorization
- `WECHAT_CORP_SECRET`: alias for `PLUGIN_CORP_SECRET`
- `PLUGIN_AGENT_ID`: The agent id to send the message
- `WECHAT_AGENT_ID`: alias for `PLUGIN_AGENT_ID`
- `PLUGIN_TO_PARTY`: The party ids to send the message
- `WECHAT_TO_PARTY`: alias for `PLUGIN_TO_PARTY`
- `PLUGIN_TO_USER`: The user ids to send the message to
- `WECHAT_TO_USER`: alias for `PLUGIN_TO_USER`
- `PLUGIN_TO_TAG`: The tag ids to send the message to
- `WECHAT_TO_TAG`: alias for `PLUGIN_TO_TAG`
- `PLUGIN_SAFE`: encrypt message, default is false
- `PLUGIN_MSG_URL`: The link for the text card click
- `PLUGIN_BTN_TEXT`: The text for the button on the card
- `PLUGIN_TITLE`: Notification title
- `PLUGIN_MESSAGE`: Notification body message, support markdown.
@@ -25,9 +39,17 @@ Read this in other languages: [English](README.md), [简体中文](README.zh-cn.
```
docker run --rm \
-e PLUGIN_SCKEY=xxxx \
-e PLUGIN_TITLE=xxxx \
-e PLUGIN_MESSAGE=xxx \
-e PLUGIN_CORPID=corpid \
-e PLUGIN_CORP_SECRET=corpsecret \
-e PLUGIN_AGENT_ID=agentid \
-e PLUGIN_TO_USER=userId \
-e PLUGIN_TO_TAG=tagId \
-e PLUGIN_TO_PARTY=toParty \
-e PLUGIN_SAFE=1 \
-e PLUGIN_MSG_URL=url \
-e PLUGIN_BTN_TXT=true \
-e PLUGIN_TITLE=title \
-e PLUGIN_MESSAGE=description \
-e DRONE_REPO_OWNER=lizheming \
-e DRONE_REPO_NAME=drone-wechat \
-e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \
@@ -42,4 +64,4 @@ docker run --rm \
-e DRONE_JOB_STARTED=1477550550 \
-e DRONE_JOB_FINISHED=1477550750 \
lizheming/drone-wechat
```
```
+29 -7
View File
@@ -13,21 +13,43 @@ drone 微信消息通知插件。
## 简介
基于 http://sc.ftqq.com/ 封装的 drone 微信消息通知插件。使用前需要去 [Server酱]( http://sc.ftqq.com/) 获取密钥。Github 登录后即可在 [发送消息](http://sc.ftqq.com/?c=code) 页面查看
基于微信企业号封装的 drone 微信消息通知插件
## 配置说明
- `PLUGIN_SCKEY`: SCKEY get from [ServerChan](http://sc.ftqq.com)
- `PLUGIN_TITLE`: Notification title
- `PLUGIN_MESSAGE`: Notification body message, support markdown.
- `PLUGIN_CORPID`: 企业Id
- `WECHAT_CORPID`: `PLUGIN_CORPID` 别名
- `PLUGIN_CORP_SECRET`: 管理组的凭证密钥
- `WECHAT_CORP_SECRET`: `PLUGIN_CORP_SECRET` 别名
- `PLUGIN_AGENT_ID`: 企业应用的id,整型。可在应用的设置页面查看
- `WECHAT_AGENT_ID`: `PLUGIN_AGENT_ID` 别名
- `PLUGIN_TO_PARTY`: 部门ID列表,多个接收者用‘|’分隔,最多支持100个。
- `WECHAT_TO_PARTY`: `PLUGIN_TO_PARTY` 别名
- `PLUGIN_TO_USER`: 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向该企业应用的全部成员发送
- `WECHAT_TO_USER`: `PLUGIN_TO_USER` 别名
- `PLUGIN_TO_TAG`: 标签ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
- `WECHAT_TO_TAG`: `PLUGIN_TO_TAG` 别名
- `PLUGIN_SAFE`: 表示是否是保密消息,0表示否,1表示是,默认0
- `PLUGIN_MSG_URL`: 点击后跳转的链接。
- `PLUGIN_BTN_TEXT`: 按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断。
- `PLUGIN_TITLE`: 消息卡片标题
- `PLUGIN_MESSAGE`: 消息卡片正文,支持 Markdown。
## 如何使用
```
docker run --rm \
-e PLUGIN_SCKEY=xxxx \
-e PLUGIN_TITLE=xxxx \
-e PLUGIN_MESSAGE=xxx \
-e PLUGIN_CORPID=corpid \
-e PLUGIN_CORP_SECRET=corpsecret \
-e PLUGIN_AGENT_ID=agentid \
-e PLUGIN_TO_USER=userId \
-e PLUGIN_TO_TAG=tagId \
-e PLUGIN_TO_PARTY=toParty \
-e PLUGIN_SAFE=1 \
-e PLUGIN_MSG_URL=url \
-e PLUGIN_BTN_TXT=true \
-e PLUGIN_TITLE=title \
-e PLUGIN_MESSAGE=description \
-e DRONE_REPO_OWNER=lizheming \
-e DRONE_REPO_NAME=drone-wechat \
-e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \
+56 -19
View File
@@ -1,20 +1,57 @@
const process = require('process');
const render = require('drone-render');
const request = require('request-promise-native');
const {
PLUGIN_SCKEY,
PLUGIN_TITLE,
PLUGIN_MESSAGE,
SERVER_CHAN_KEY
} = process.env;
const SCKEY = PLUGIN_SCKEY || SERVER_CHAN_KEY;
request({
url: `https://sc.ftqq.com/${SCKEY}.send`,
qs: {
text: PLUGIN_TITLE,
desp: render(PLUGIN_MESSAGE)
const { configParser, exec } = require('./plugin');
configParser({
corpid: {
usage: 'The corpid for authorization',
env: 'PLUGIN_CORPID,WECHAT_CORPID'
},
corp_secret: {
usage: 'The corp secret for authorization',
env: 'PLUGIN_CORP_SECRET,WECHAT_CORP_SECRET'
},
agent_id: {
usage: 'The agent id to send the message',
env: 'PLUGIN_AGENT_ID,WECHAT_AGENT_ID'
},
to_party: {
usage: 'The party ids to send message',
env: 'PLUGIN_TO_PARTY,WECHAT_TO_PARTY'
},
to_user: {
usage: 'The user ids to send the message to',
env: 'PLUGIN_TO_USER,WECHAT_TO_USER'
},
to_tag: {
usage: 'The tag ids to send the message to',
env: 'PLUGIN_TO_TAG,WECHAT_TO_TAG'
},
msg_type: {
usage: 'The message send type',
def: 'textcard',
env: 'PLUGIN_MSG_TYPE'
},
safe: {
usage: 'encrypt message, default is false',
def: 0,
env: 'PLUGIN_SAFE'
},
msg_url: {
usage: 'The link for the text card click',
env: 'PLUGIN_MSG_URL,DRONE_BUILD_LINK'
},
btn_text: {
usage: 'The text for the button on the card',
env: 'PLUGIN_BTN_TEXT'
},
title: {
usage: 'Notification title',
env: 'PLUGIN_TITLE'
},
message: {
usage: 'Notification body message, support markdown',
env: 'PLUGIN_MESSAGE'
},
debug: {
usage: 'debug mode',
env: 'PLUGIN_DEBUG'
}
});
})(exec);
+4 -3
View File
@@ -1,11 +1,11 @@
{
"name": "drone-wechat",
"version": "1.0.0",
"version": "1.1.7",
"description": "drone wechat notification plugin",
"main": "index.js",
"scripts": {
"test": "npm run eslint && npm run test-cov",
"test-cov": "nyc ava test.js && nyc report --reporter=html",
"test-cov": "nyc ava test.js && nyc report --reporter=lcov --reporter=text-lcov",
"eslint": "eslint index.js"
},
"repository": {
@@ -25,7 +25,8 @@
},
"homepage": "https://github.com/lizheming/drone-wechat#readme",
"dependencies": {
"drone-render": "^1.0.0",
"coveralls": "^3.0.2",
"drone-render": "^1.0.4",
"request": "^2.83.0",
"request-promise-native": "^1.0.5"
},
+95
View File
@@ -0,0 +1,95 @@
const process = require('process');
const render = require('drone-render');
const request = require('request-promise-native');
function log(...args) {
const debug = process.env.PLUGIN_DEBUG;
if (!debug) {
return;
}
// eslint-disable-next-line
console.log(...args);
}
function configParser(configs) {
const ret = {};
for (const configName in configs) {
const { env, def } = configs[configName];
if (def !== undefined) {
ret[configName] = typeof def === 'function' ? def() : def;
}
env.split(/\s*,\s*/).some(envar => {
if (process.env.hasOwnProperty(envar)) {
ret[configName] = process.env[envar];
return true;
}
return false;
});
}
return fn => fn(ret);
}
function getAccessToken(corpid, corpsecret) {
return request({
url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken',
qs: { corpid, corpsecret },
json: true
}).then(resp => {
if (!resp.access_token) {
throw new Error(resp);
}
return resp.access_token;
});
}
function sendMsgFromWork({
access_token,
to_user: touser,
to_party: toparty,
to_tag: totag,
msg_type: msgtype,
agent_id: agentid,
msg_url: url,
btn_text: btntext,
message,
safe,
title
}) {
title = title && render(title);
totag = totag && render(totag);
btntext = btntext && render(btntext);
const description = message && render(message);
const textcard = { title, url, btntext, description };
return request({
method: 'POST',
url: 'https://qyapi.weixin.qq.com/cgi-bin/message/send',
qs: { access_token },
body: { touser, toparty, totag, msgtype, agentid, safe, textcard },
json: true
});
}
async function exec({ corpid, corp_secret, ...config }) {
try {
log('config parse end, config except corpid and corp_secret:', config);
const access_token = await getAccessToken(corpid, corp_secret);
log('access_token request success!');
log('http request data:', config);
const resp = await sendMsgFromWork({ ...config, access_token });
log('send msg success, and http response content is:');
log(resp);
// eslint-disable-next-line
console.log('==Send notification to Wechat success!==');
} catch (e) {
console.error(e);
}
}
module.exports = {
configParser,
getAccessToken,
sendMsgFromWork,
exec
};
+8
View File
@@ -0,0 +1,8 @@
sonar.projectKey=drone-wechat
sonar.organization=lizheming-github
sonar.language=js
sonar.sources=index.js,plugin.js
sonar.tests=test.js
sonar.javascript.lcov.reportPaths=coverage/lcov.info
sonar.host.url=https://sonarcloud.io
sonar.exclusions=test.js
+104 -34
View File
@@ -1,51 +1,121 @@
const {test} = require('ava');
const { test } = require('ava');
const mock = require('mock-require');
const SCKEY = 'testsckey';
const SCKEY2 = 'testsckey2';
// const SCKEY = 'testsckey';
// const SCKEY2 = 'testsckey2';
const TITLE = 'test for title';
const MESSAGE = 'test for message';
// test('send wechat with serverchan', async t => {
// t.plan(2);
// mock('process', {
// env: {
// PLUGIN_SCKEY: SCKEY,
// PLUGIN_TITLE: TITLE,
// PLUGIN_MESSAGE: MESSAGE
// }
// });
// mock('drone-render', text => {
// t.is(text, MESSAGE);
// return text + '1';
// });
// mock('request-promise-native', obj => {
// t.deepEqual(obj, {
// url: `https://sc.ftqq.com/${SCKEY}.send`,
// qs: {
// text: TITLE,
// desp: MESSAGE + '1'
// }
// });
// });
// mock.reRequire('./index.js');
// mock.stopAll();
// });
// test('get wechat corp access token', async t => {
// t.plan(2);
// mock('request-promise-native', obj => {
// t.deepEqual(obj, {
// url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken',
// qs: {
// corpid: 111,
// corpsecret: 222
// },
// json: true
// });
// return Promise.resolve({ access_token: 1234 });
// });
// const plugin = mock.reRequire('./plugin');
// t.is(1234, await plugin.getAccessToken(111, 222));
// mock.stopAll();
// });
test('send wechat with corp id', async t => {
mock('process', {
env: {
PLUGIN_TITLE: TITLE,
PLUGIN_MESSAGE: MESSAGE,
PLUGIN_TO_PARTY: 'party',
PLUGIN_TO_TAG: 'tag',
PLUGIN_TO_USER: '@all',
PLUGIN_MSG_URL: '',
PLUGIN_BTN_TEXT: 'more',
PLUGIN_AGENT_ID: 1122
}
});
mock('request-promise-native', function(obj) {
if (obj.url.includes('gettoken')) {
return Promise.resolve({ access_token: 1234 });
}
if (obj.url.includes('send')) {
t.deepEqual(obj, {
method: 'POST',
url: 'https://qyapi.weixin.qq.com/cgi-bin/message/send',
qs: {
access_token: 1234
},
body: {
touser: '@all',
toparty: 'party',
totag: 'tag',
msgtype: 'textcard',
agentid: 1122,
safe: 0,
textcard: {
title: TITLE,
description: MESSAGE,
url: '',
btntext: 'more'
}
},
json: true
});
}
});
await mock.reRequire('./index');
mock.stopAll();
});
test('send wechat', async t => {
t.plan(2);
mock('process', {
env: {
PLUGIN_SCKEY: SCKEY,
PLUGIN_TITLE: TITLE,
PLUGIN_MESSAGE: MESSAGE
}
});
mock('drone-render', text => {
t.is(text, MESSAGE);
return text + '1';
});
mock('request-promise-native', obj => {
t.deepEqual(obj, {
url: `https://sc.ftqq.com/${SCKEY}.send`,
qs: {
text: TITLE,
desp: MESSAGE + '1'
}
});
});
mock.reRequire('./index.js');
mock.stopAll();
});
test('send wechat with SERVER_CHAN_KEY', async t => {
mock('process', {
env: {
SERVER_CHAN_KEY: SCKEY2,
PLUGIN_TITLE: TITLE,
PLUGIN_MESSAGE: MESSAGE
}
});
mock('request-promise-native', obj => {
t.is(obj.url, `https://sc.ftqq.com/${SCKEY2}.send`);
if (obj.url.includes('gettoken')) {
return Promise.resolve({ errcode: 1, errmsg: 123 });
}
if (obj.url.includes('send')) {
t.fail();
}
});
mock.reRequire('./index.js');
mock.reRequire('./index');
await new Promise(resolve => setTimeout(resolve, 2000)).then(() => t.pass());
mock.stopAll();
});