mirror of
https://github.com/lizheming/drone-wechat.git
synced 2026-06-04 18:33:46 +08:00
Compare commits
53 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 31457c2db5 | |||
| 8f3fadac27 | |||
| 4f828a37a2 | |||
| 960b54b0da | |||
| 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 | |||
| 1ad073309e | |||
| 996d01cbcc | |||
| 2533b5f5cd | |||
| ab8de5c343 | |||
| 734dc3f63a | |||
| d60ee90f42 | |||
| a449906a10 | |||
| 738e6ffd52 | |||
| d7c4c28e27 | |||
| d615e0754d | |||
| faa6cefaea | |||
| 18edf8f861 | |||
| 55c4818d7b |
+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
|
# dotenv environment variables file
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
lcov.info
|
||||||
@@ -0,0 +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'
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
---
|
||||||
|
date: 2018-01-07T00:00:00+00:00
|
||||||
|
title: Wechat
|
||||||
|
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:
|
||||||
|
|
||||||
|
```diff
|
||||||
|
pipeline:
|
||||||
|
wechat:
|
||||||
|
image: lizheming/drone-wechat
|
||||||
|
+ 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.
|
||||||
|
{% else %}
|
||||||
|
build {{build.number}} failed. Fix me please.
|
||||||
|
{% endif %}
|
||||||
|
```
|
||||||
|
|
||||||
|
# Parameter Reference
|
||||||
|
|
||||||
|
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
|
||||||
|
: repository owner
|
||||||
|
|
||||||
|
repo.name
|
||||||
|
: repository name
|
||||||
|
|
||||||
|
build.status
|
||||||
|
: build status type enumeration, either success or failure
|
||||||
|
|
||||||
|
build.event
|
||||||
|
: build event type enumeration, one of push, pull_request, tag, deployment
|
||||||
|
|
||||||
|
build.number
|
||||||
|
: build number
|
||||||
|
|
||||||
|
build.commit
|
||||||
|
: git sha for current commit
|
||||||
|
|
||||||
|
build.branch
|
||||||
|
: git branch for current commit
|
||||||
|
|
||||||
|
build.tag
|
||||||
|
: git tag for current commit
|
||||||
|
|
||||||
|
build.ref
|
||||||
|
: git ref for current commit
|
||||||
|
|
||||||
|
build.author
|
||||||
|
: git author for current commit
|
||||||
|
|
||||||
|
build.link
|
||||||
|
: link the the build results in drone
|
||||||
|
|
||||||
|
build.started
|
||||||
|
: unix timestamp for build started
|
||||||
|
|
||||||
|
build.finished
|
||||||
|
: unix timestamp for build finished
|
||||||
|
|
||||||
|
# Template Filter
|
||||||
|
|
||||||
|
uppercasefirst
|
||||||
|
: converts the first letter of a string to uppercase
|
||||||
|
|
||||||
|
datetime
|
||||||
|
: converts a unix timestamp to a date time string. Example {{build.started | datetime}}, you can see more info in [nunjucks-date-filter](https://www.npmjs.com/package/nunjucks-date-filter)
|
||||||
|
|
||||||
|
More filter can see [builtin-filters](https://mozilla.github.io/nunjucks/templating.html#builtin-filters)
|
||||||
+4
-3
@@ -1,13 +1,14 @@
|
|||||||
FROM mhart/alpine-node:8.9.3
|
FROM mhart/alpine-node:8.9.3
|
||||||
|
|
||||||
LABEL maintainer="lizheming <i@imnerd.org>" \
|
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.vendor="lizheming" \
|
||||||
org.label-schema.schema-version="1.0"
|
org.label-schema.schema-version="1.2.0"
|
||||||
|
|
||||||
WORKDIR /wechat
|
WORKDIR /wechat
|
||||||
COPY package.json /wechat/package.json
|
COPY package.json /wechat/package.json
|
||||||
RUN npm install --registry=https://registry.npm.taobao.org
|
RUN npm install --production --registry=https://registry.npm.taobao.org
|
||||||
|
|
||||||
COPY index.js /wechat/index.js
|
COPY index.js /wechat/index.js
|
||||||
|
COPY plugin.js /wechat/plugin.js
|
||||||
ENTRYPOINT [ "node", "/wechat/index.js" ]
|
ENTRYPOINT [ "node", "/wechat/index.js" ]
|
||||||
@@ -1,17 +1,67 @@
|
|||||||
|
<img src="wechat.svg" />
|
||||||
|
|
||||||
## drone-wechat
|
## drone-wechat
|
||||||
|
[]()
|
||||||
|
[](https://coveralls.io/github/lizheming/drone-wechat?branch=master)
|
||||||
|
[]()
|
||||||
|
[](https://microbadger.com/images/lizheming/drone-wechat)
|
||||||
|
[]()
|
||||||
|
|
||||||
drone 微信消息通知插件
|
Drone plugin for sending wechat notifications.
|
||||||
|
|
||||||
## 简介
|
## Description
|
||||||
|
|
||||||
基于 http://sc.ftqq.com/ 封装的 drone 微信消息通知插件。使用前需要去 [Server酱]( http://sc.ftqq.com/) 获取密钥。Github 登录后即可在 [发送消息](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_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.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
docker run --rm \
|
docker run --rm \
|
||||||
-e PLUGIN_SCKEY=xxxx \
|
-e PLUGIN_CORPID=corpid \
|
||||||
-e PLUGIN_TITLE=xxxx \
|
-e PLUGIN_CORP_SECRET=corpsecret \
|
||||||
-e PLUGIN_MESSAGE=xxx
|
-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 \
|
||||||
|
-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
|
lizheming/drone-wechat
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
<img src="wechat.svg" />
|
||||||
|
|
||||||
|
## drone-wechat
|
||||||
|
[]()
|
||||||
|
[](https://coveralls.io/github/lizheming/drone-wechat?branch=master)
|
||||||
|
[]()
|
||||||
|
[](https://microbadger.com/images/lizheming/drone-wechat)
|
||||||
|
[]()
|
||||||
|
|
||||||
|
drone 微信消息通知插件。
|
||||||
|
|
||||||
|
其它语言版本:[English](README.md),[简体中文](README.zh-cn.md)。
|
||||||
|
|
||||||
|
## 简介
|
||||||
|
|
||||||
|
基于微信企业号封装的 drone 微信消息通知插件。
|
||||||
|
|
||||||
|
## 配置说明
|
||||||
|
|
||||||
|
- `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_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 \
|
||||||
|
-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,19 +1,57 @@
|
|||||||
const render = require('drone-render');
|
const { configParser, exec } = require('./plugin');
|
||||||
const request = require('request-promise-native');
|
configParser({
|
||||||
|
corpid: {
|
||||||
const {
|
usage: 'The corpid for authorization',
|
||||||
PLUGIN_SCKEY,
|
env: 'PLUGIN_CORPID,WECHAT_CORPID'
|
||||||
PLUGIN_TITLE,
|
},
|
||||||
PLUGIN_MESSAGE,
|
corp_secret: {
|
||||||
SERVER_CHAN_KEY
|
usage: 'The corp secret for authorization',
|
||||||
} = process.env;
|
env: 'PLUGIN_CORP_SECRET,WECHAT_CORP_SECRET'
|
||||||
|
},
|
||||||
const SCKEY = PLUGIN_SCKEY || SERVER_CHAN_KEY;
|
agent_id: {
|
||||||
|
usage: 'The agent id to send the message',
|
||||||
request({
|
env: 'PLUGIN_AGENT_ID,WECHAT_AGENT_ID'
|
||||||
url: `https://sc.ftqq.com/${SCKEY}.send`,
|
},
|
||||||
qs: {
|
to_party: {
|
||||||
text: PLUGIN_TITLE,
|
usage: 'The party ids to send message',
|
||||||
desp: render(PLUGIN_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);
|
||||||
|
|||||||
+12
-2
@@ -1,10 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "drone-wechat",
|
"name": "drone-wechat",
|
||||||
"version": "0.0.1",
|
"version": "1.1.3",
|
||||||
"description": "drone wechat notification plugin",
|
"description": "drone wechat notification plugin",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "\"\""
|
"test": "npm run eslint && npm run test-cov",
|
||||||
|
"test-cov": "nyc ava test.js && nyc report --reporter=lcov --reporter=text-lcov",
|
||||||
|
"eslint": "eslint index.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -23,8 +25,16 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/lizheming/drone-wechat#readme",
|
"homepage": "https://github.com/lizheming/drone-wechat#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"coveralls": "^3.0.2",
|
||||||
"drone-render": "^1.0.0",
|
"drone-render": "^1.0.0",
|
||||||
"request": "^2.83.0",
|
"request": "^2.83.0",
|
||||||
"request-promise-native": "^1.0.5"
|
"request-promise-native": "^1.0.5"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"ava": "^0.24.0",
|
||||||
|
"eslint": "^4.14.0",
|
||||||
|
"eslint-config-think": "^1.0.2",
|
||||||
|
"mock-require": "^2.0.2",
|
||||||
|
"nyc": "^11.4.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
const { test } = require('ava');
|
||||||
|
const mock = require('mock-require');
|
||||||
|
|
||||||
|
// 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_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 => {
|
||||||
|
mock('process', {
|
||||||
|
env: {
|
||||||
|
PLUGIN_TITLE: TITLE,
|
||||||
|
PLUGIN_MESSAGE: MESSAGE
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mock('request-promise-native', obj => {
|
||||||
|
if (obj.url.includes('gettoken')) {
|
||||||
|
return Promise.resolve({ errcode: 1, errmsg: 123 });
|
||||||
|
}
|
||||||
|
if (obj.url.includes('send')) {
|
||||||
|
t.fail();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
mock.reRequire('./index');
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 2000)).then(() => t.pass());
|
||||||
|
mock.stopAll();
|
||||||
|
});
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 300 300"><path fill="#2DC100" d="M300 255c0 24.854-20.147 45-45 45H45c-24.854 0-45-20.146-45-45V45C0 20.147 20.147 0 45 0h210c24.853 0 45 20.147 45 45v210z"/><g fill="#FFF"><path d="M200.803 111.88c-24.213 1.265-45.268 8.605-62.362 25.188-17.271 16.754-25.155 37.284-23 62.734-9.464-1.172-18.084-2.462-26.753-3.192-2.994-.252-6.547.106-9.083 1.537-8.418 4.75-16.488 10.113-26.053 16.092 1.755-7.938 2.891-14.889 4.902-21.575 1.479-4.914.794-7.649-3.733-10.849-29.066-20.521-41.318-51.232-32.149-82.85 8.483-29.25 29.315-46.989 57.621-56.236 38.635-12.62 82.054.253 105.547 30.927 8.485 11.08 13.688 23.516 15.063 38.224zm-111.437-9.852c.223-5.783-4.788-10.993-10.74-11.167-6.094-.179-11.106 4.478-11.284 10.483-.18 6.086 4.475 10.963 10.613 11.119 6.085.154 11.186-4.509 11.411-10.435zm58.141-11.171c-5.974.11-11.022 5.198-10.916 11.004.109 6.018 5.061 10.726 11.204 10.652 6.159-.074 10.83-4.832 10.772-10.977-.051-6.032-4.981-10.79-11.06-10.679z"/><path d="M255.201 262.83c-7.667-3.414-14.7-8.536-22.188-9.318-7.459-.779-15.3 3.524-23.104 4.322-23.771 2.432-45.067-4.193-62.627-20.432-33.397-30.89-28.625-78.254 10.014-103.568 34.341-22.498 84.704-14.998 108.916 16.219 21.129 27.24 18.646 63.4-7.148 86.284-7.464 6.623-10.15 12.073-5.361 20.804.884 1.612.985 3.653 1.498 5.689zm-87.274-84.499c4.881.005 8.9-3.815 9.085-8.636.195-5.104-3.91-9.385-9.021-9.406-5.06-.023-9.299 4.318-9.123 9.346.166 4.804 4.213 8.69 9.059 8.696zm56.261-18.022c-4.736-.033-8.76 3.844-8.953 8.629-.205 5.117 3.772 9.319 8.836 9.332 4.898.016 8.768-3.688 8.946-8.562.19-5.129-3.789-9.364-8.829-9.399z"/></g></svg>
|
||||||
|
After Width: | Height: | Size: 1.6 KiB |
Reference in New Issue
Block a user