mirror of
https://github.com/lizheming/drone-wechat.git
synced 2026-06-04 18:33:46 +08:00
Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 13e1a9672a | |||
| 7c508b94a1 | |||
| 4c689927d8 | |||
| 3810c42e90 | |||
| ae91f54be3 | |||
| a0ef9d4b26 | |||
| 1fc4bd1275 | |||
| e53de0b4e4 | |||
| f8afb41b72 | |||
| 76a3278435 | |||
| 40c3af1759 | |||
| 74ef2be5a1 | |||
| 759632c133 | |||
| 6c83fe7b28 | |||
| deba525faa | |||
| 749bc72903 | |||
| 9c55502ccb | |||
| 5f358b0cf9 | |||
| 93a5d6a792 | |||
| 4dd376b565 | |||
| ceeb1a1d14 | |||
| 05dde2a990 | |||
| 42f12803c1 | |||
| 387eff6ec0 | |||
| 85cb2c566d | |||
| 9db93837f7 | |||
| 651b21897a | |||
| be4e4a0a7a | |||
| da1cf0d3f3 | |||
| 1ee57a206a | |||
| 009655fcfe | |||
| b511b81acc | |||
| e38559fd09 | |||
| 3d2f8941f1 | |||
| 4dbc0654a7 | |||
| 7d12e3185c |
+79
@@ -0,0 +1,79 @@
|
||||
kind: pipline
|
||||
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
|
||||
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
|
||||
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
|
||||
@@ -57,3 +57,4 @@ typings/
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
|
||||
lcov.info
|
||||
+8
-8
@@ -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'
|
||||
|
||||
@@ -10,22 +10,6 @@ image: lizheming/drone-wechat
|
||||
|
||||
The Wechat plugin posts build status messages to your account. The below pipeline configuration demonstrates simple usage:
|
||||
|
||||
```diff
|
||||
pipline:
|
||||
wechat:
|
||||
image: lizheming/drone-wechat
|
||||
+ sckey: xxxxxx
|
||||
title: ${DRONE_REPO_NAME}
|
||||
message: >
|
||||
{%if success %}
|
||||
build {{build.number}} succeeded. Good job.
|
||||
{% else %}
|
||||
build {{build.number}} failed. Fix me please.
|
||||
{% endif %}
|
||||
```
|
||||
|
||||
If you want push notification with your own wechat corp id, you can config like this:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
wechat:
|
||||
@@ -77,9 +61,6 @@ msg_url
|
||||
btn_text
|
||||
: The text for the button on the card
|
||||
|
||||
sckey
|
||||
: SCKEY get from [ServerChan](http://sc.ftqq.com)
|
||||
|
||||
title
|
||||
: Notification title
|
||||
|
||||
@@ -88,10 +69,10 @@ message
|
||||
|
||||
# Secret Reference
|
||||
|
||||
wechat\_corpid
|
||||
wechat_corpid
|
||||
: The corpid for authorization
|
||||
|
||||
wechat\_corp_secret
|
||||
wechat\_corp\_secret
|
||||
: The corp secret for authorization
|
||||
|
||||
wechat\_agent\_id
|
||||
@@ -100,16 +81,12 @@ wechat\_agent\_id
|
||||
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
|
||||
|
||||
wechat\_sckey
|
||||
: SCKEY get from [ServerChan](http://sc.ftqq.com)
|
||||
|
||||
# Template Reference
|
||||
|
||||
repo.owner
|
||||
|
||||
+2
-1
@@ -3,11 +3,12 @@ FROM mhart/alpine-node:8.9.3
|
||||
LABEL maintainer="lizheming <i@imnerd.org>" \
|
||||
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" ]
|
||||
@@ -7,11 +7,11 @@
|
||||
[](https://microbadger.com/images/lizheming/drone-wechat)
|
||||
[]()
|
||||
|
||||
Drone plugin for sending telegram notifications.
|
||||
Drone plugin for sending wechat notifications.
|
||||
|
||||
## Description
|
||||
|
||||
This drone wechat notification plugin builds base on Wechat for Work. You can config your own wechat corp id, or you can get wechat notification quickly with key from [ServerChan](http://sc.ftqq.com). If you use [ServerChan](http://sc.ftqq.com) you need get `SCKEY` before using. 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).
|
||||
|
||||
@@ -32,15 +32,11 @@ Read this in other languages: [English](README.md), [简体中文](README.zh-cn.
|
||||
- `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_SCKEY`: SCKEY get from [ServerChan](http://sc.ftqq.com)
|
||||
- `WECHAT_SCKEY`: alias for `PLUGIN_SCKEY`
|
||||
- `PLUGIN_TITLE`: Notification title
|
||||
- `PLUGIN_MESSAGE`: Notification body message, support markdown.
|
||||
|
||||
## Usage
|
||||
|
||||
### With wechat corp id
|
||||
|
||||
```
|
||||
docker run --rm \
|
||||
-e PLUGIN_CORPID=corpid \
|
||||
@@ -69,26 +65,3 @@ docker run --rm \
|
||||
-e DRONE_JOB_FINISHED=1477550750 \
|
||||
lizheming/drone-wechat
|
||||
```
|
||||
|
||||
### With server chan key
|
||||
|
||||
```
|
||||
docker run --rm \
|
||||
-e PLUGIN_SCKEY=xxxx \
|
||||
-e PLUGIN_TITLE=xxxx \
|
||||
-e PLUGIN_MESSAGE=xxx \
|
||||
-e DRONE_REPO_OWNER=lizheming \
|
||||
-e DRONE_REPO_NAME=drone-wechat \
|
||||
-e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \
|
||||
-e DRONE_COMMIT_BRANCH=master \
|
||||
-e DRONE_COMMIT_LINK=https://github.com/lizheming/drone-wechat/compare/master... \
|
||||
-e DRONE_COMMIT_AUTHOR=lizheming \
|
||||
-e DRONE_COMMIT_AUTHOR_EMAIL=secretlzm007@gmail.com \
|
||||
-e DRONE_BUILD_NUMBER=1 \
|
||||
-e DRONE_BUILD_STATUS=success \
|
||||
-e DRONE_BUILD_LINK=http://github.com/lizheming/drone-wechat \
|
||||
-e DRONE_TAG=1.0.0 \
|
||||
-e DRONE_JOB_STARTED=1477550550 \
|
||||
-e DRONE_JOB_FINISHED=1477550750 \
|
||||
lizheming/drone-wechat
|
||||
```
|
||||
+1
-28
@@ -13,7 +13,7 @@ drone 微信消息通知插件。
|
||||
|
||||
## 简介
|
||||
|
||||
基于微信企业号封装的 drone 微信消息通知插件。你可以设置自己的企业号,也可以使用 [Server酱]( http://sc.ftqq.com/) 来快速配置微信消息通知功能。如果使用 [Server酱]( http://sc.ftqq.com/) 需要在使用前获取密钥。Github 登录后即可在 [发送消息](http://sc.ftqq.com/?c=code) 页面查看。
|
||||
基于微信企业号封装的 drone 微信消息通知插件。
|
||||
|
||||
## 配置说明
|
||||
|
||||
@@ -32,15 +32,11 @@ drone 微信消息通知插件。
|
||||
- `PLUGIN_SAFE`: 表示是否是保密消息,0表示否,1表示是,默认0
|
||||
- `PLUGIN_MSG_URL`: 点击后跳转的链接。
|
||||
- `PLUGIN_BTN_TEXT`: 按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断。
|
||||
- `PLUGIN_SCKEY`: [Server酱](http://sc.ftqq.com) 申请的密钥
|
||||
- `WECHAT_SCKEY`: `PLUGIN_SCKEY` 别名
|
||||
- `PLUGIN_TITLE`: 消息卡片标题
|
||||
- `PLUGIN_MESSAGE`: 消息卡片正文,支持 Markdown。
|
||||
|
||||
## 如何使用
|
||||
|
||||
### 自行配置微信企业号
|
||||
|
||||
```
|
||||
docker run --rm \
|
||||
-e PLUGIN_CORPID=corpid \
|
||||
@@ -68,27 +64,4 @@ docker run --rm \
|
||||
-e DRONE_JOB_STARTED=1477550550 \
|
||||
-e DRONE_JOB_FINISHED=1477550750 \
|
||||
lizheming/drone-wechat
|
||||
```
|
||||
|
||||
### 使用Server酱快速集成
|
||||
|
||||
```
|
||||
docker run --rm \
|
||||
-e PLUGIN_SCKEY=xxxx \
|
||||
-e PLUGIN_TITLE=xxxx \
|
||||
-e PLUGIN_MESSAGE=xxx \
|
||||
-e DRONE_REPO_OWNER=lizheming \
|
||||
-e DRONE_REPO_NAME=drone-wechat \
|
||||
-e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \
|
||||
-e DRONE_COMMIT_BRANCH=master \
|
||||
-e DRONE_COMMIT_LINK=https://github.com/lizheming/drone-wechat/compare/master... \
|
||||
-e DRONE_COMMIT_AUTHOR=lizheming \
|
||||
-e DRONE_COMMIT_AUTHOR_EMAIL=secretlzm007@gmail.com \
|
||||
-e DRONE_BUILD_NUMBER=1 \
|
||||
-e DRONE_BUILD_STATUS=success \
|
||||
-e DRONE_BUILD_LINK=http://github.com/lizheming/drone-wechat \
|
||||
-e DRONE_TAG=1.0.0 \
|
||||
-e DRONE_JOB_STARTED=1477550550 \
|
||||
-e DRONE_JOB_FINISHED=1477550750 \
|
||||
lizheming/drone-wechat
|
||||
```
|
||||
@@ -1,118 +1,58 @@
|
||||
const process = require('process');
|
||||
const render = require('drone-render');
|
||||
const request = require('request-promise-native');
|
||||
|
||||
const {
|
||||
PLUGIN_CORPID,
|
||||
WECHAT_CORPID,
|
||||
PLUGIN_CORP_SECRET,
|
||||
WECHAT_CORP_SECRET,
|
||||
PLUGIN_AGENT_ID,
|
||||
WECHAT_AGENT_ID,
|
||||
PLUGIN_TO_PARTY,
|
||||
WECHAT_TO_PARTY,
|
||||
PLUGIN_TO_USER,
|
||||
WECHAT_TO_USER,
|
||||
PLUGIN_TO_TAG,
|
||||
WECHAT_TO_TAG,
|
||||
|
||||
PLUGIN_MSG_TYPE,
|
||||
PLUGIN_SAFE,
|
||||
PLUGIN_MSG_URL,
|
||||
PLUGIN_BTN_TEXT,
|
||||
|
||||
PLUGIN_SCKEY,
|
||||
WECHAT_SERVER_CHAN_KEY,
|
||||
|
||||
PLUGIN_TITLE,
|
||||
PLUGIN_MESSAGE
|
||||
} = process.env;
|
||||
|
||||
function sendMsgFromServerChan() {
|
||||
const SCKEY = PLUGIN_SCKEY || WECHAT_SERVER_CHAN_KEY;
|
||||
if (!SCKEY) {
|
||||
return false;
|
||||
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',
|
||||
def: '@all',
|
||||
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'
|
||||
}
|
||||
|
||||
request({
|
||||
url: `https://sc.ftqq.com/${SCKEY}.send`,
|
||||
qs: {
|
||||
text: PLUGIN_TITLE,
|
||||
desp: render(PLUGIN_MESSAGE)
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getAccessToken() {
|
||||
const CORPID = PLUGIN_CORPID || WECHAT_CORPID;
|
||||
const CORP_SECRET = PLUGIN_CORP_SECRET || WECHAT_CORP_SECRET;
|
||||
|
||||
return request({
|
||||
url: 'https://qyapi.weixin.qq.com/cgi-bin/gettoken',
|
||||
qs: {
|
||||
corpid: CORPID,
|
||||
corpsecret: CORP_SECRET
|
||||
},
|
||||
json: true
|
||||
}).then(resp => {
|
||||
if (!resp.access_token) {
|
||||
throw new Error(resp);
|
||||
}
|
||||
return resp.access_token;
|
||||
});
|
||||
}
|
||||
|
||||
function sendMsgFromWork(access_token) {
|
||||
const TO_USER = PLUGIN_TO_USER || WECHAT_TO_USER || '@all';
|
||||
const TO_PARTY = PLUGIN_TO_PARTY || WECHAT_TO_PARTY;
|
||||
const TO_TAG = PLUGIN_TO_TAG || WECHAT_TO_TAG;
|
||||
const AGENT_ID = PLUGIN_AGENT_ID || WECHAT_AGENT_ID;
|
||||
|
||||
const MSG_TYPE = PLUGIN_MSG_TYPE || 'textcard';
|
||||
const SAFE = PLUGIN_SAFE || 0;
|
||||
const TITLE = PLUGIN_TITLE;
|
||||
const DESCRIPTION = render(PLUGIN_MESSAGE);
|
||||
const MSG_URL = PLUGIN_MSG_URL;
|
||||
const BTN_TEXT = PLUGIN_BTN_TEXT;
|
||||
|
||||
return request({
|
||||
method: 'POST',
|
||||
url: 'https://qyapi.weixin.qq.com/cgi-bin/message/send',
|
||||
qs: {
|
||||
access_token
|
||||
},
|
||||
body: {
|
||||
touser: TO_USER,
|
||||
toparty: TO_PARTY,
|
||||
tag: TO_TAG,
|
||||
msgtype: MSG_TYPE,
|
||||
agentid: AGENT_ID,
|
||||
safe: SAFE,
|
||||
textcard: {
|
||||
title: TITLE,
|
||||
description: DESCRIPTION,
|
||||
url: MSG_URL,
|
||||
btntext: BTN_TEXT
|
||||
}
|
||||
},
|
||||
json: true
|
||||
});
|
||||
}
|
||||
|
||||
function sendMsgFromWechat() {
|
||||
return getAccessToken()
|
||||
.then(sendMsgFromWork)
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
|
||||
sendMsgFromServerChan() || sendMsgFromWechat();
|
||||
module.exports = {
|
||||
sendMsgFromServerChan,
|
||||
getAccessToken,
|
||||
sendMsgFromWork,
|
||||
sendMsgFromWechat
|
||||
};
|
||||
})(exec);
|
||||
|
||||
+3
-2
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "drone-wechat",
|
||||
"version": "1.1.0",
|
||||
"version": "1.1.1",
|
||||
"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,6 +25,7 @@
|
||||
},
|
||||
"homepage": "https://github.com/lizheming/drone-wechat#readme",
|
||||
"dependencies": {
|
||||
"coveralls": "^3.0.2",
|
||||
"drone-render": "^1.0.0",
|
||||
"request": "^2.83.0",
|
||||
"request-promise-native": "^1.0.5"
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
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 = render(title);
|
||||
const description = 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
|
||||
};
|
||||
@@ -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
|
||||
@@ -1,68 +1,57 @@
|
||||
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);
|
||||
// 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('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();
|
||||
});
|
||||
// mock.reRequire('./index.js');
|
||||
// mock.stopAll();
|
||||
// });
|
||||
|
||||
test('get wechat corp access token', async t => {
|
||||
t.plan(3);
|
||||
// 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
|
||||
// });
|
||||
|
||||
mock('process', {
|
||||
env: {
|
||||
PLUGIN_CORPID: 111,
|
||||
PLUGIN_CORP_SECRET: 222,
|
||||
PLUGIN_TITLE: TITLE,
|
||||
PLUGIN_MESSAGE: MESSAGE
|
||||
}
|
||||
});
|
||||
mock('request-promise-native', obj => {
|
||||
if (obj.url.includes('gettoken')) {
|
||||
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 wechat = mock.reRequire('./index');
|
||||
t.is(1234, await wechat.getAccessToken());
|
||||
mock.stopAll();
|
||||
});
|
||||
// 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', {
|
||||
@@ -76,9 +65,9 @@ test('send wechat with corp id', async t => {
|
||||
PLUGIN_AGENT_ID: 1122
|
||||
}
|
||||
});
|
||||
mock('request-promise-native', obj => {
|
||||
mock('request-promise-native', function(obj) {
|
||||
if (obj.url.includes('gettoken')) {
|
||||
return Promise.resolve({access_token: 1234});
|
||||
return Promise.resolve({ access_token: 1234 });
|
||||
}
|
||||
if (obj.url.includes('send')) {
|
||||
t.deepEqual(obj, {
|
||||
@@ -105,7 +94,7 @@ test('send wechat with corp id', async t => {
|
||||
});
|
||||
}
|
||||
});
|
||||
mock.reRequire('./index');
|
||||
await mock.reRequire('./index');
|
||||
mock.stopAll();
|
||||
});
|
||||
|
||||
@@ -118,7 +107,7 @@ test('send wechat', async t => {
|
||||
});
|
||||
mock('request-promise-native', obj => {
|
||||
if (obj.url.includes('gettoken')) {
|
||||
return Promise.resolve({errcode: 1, errmsg: 123});
|
||||
return Promise.resolve({ errcode: 1, errmsg: 123 });
|
||||
}
|
||||
if (obj.url.includes('send')) {
|
||||
t.fail();
|
||||
|
||||
Reference in New Issue
Block a user