From 93a5d6a79233f0ad257989596d5428ebf34e300c Mon Sep 17 00:00:00 2001 From: lizheming Date: Sun, 16 Dec 2018 23:55:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E8=A7=A3=E8=80=A6=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 153 +++++++++++++++++++----------------------------------- plugin.js | 74 ++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 99 deletions(-) create mode 100644 plugin.js diff --git a/index.js b/index.js index 7482976..e3f86a7 100644 --- a/index.js +++ b/index.js @@ -1,99 +1,54 @@ -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_TITLE, - PLUGIN_MESSAGE, - - DRONE_BUILD_LINK -} = process.env; - -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 || DRONE_BUILD_LINK; - 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); - }); -} - -sendMsgFromWechat(); -module.exports = { - getAccessToken, - sendMsgFromWork, - sendMsgFromWechat -}; +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' + } +})(exec); diff --git a/plugin.js b/plugin.js new file mode 100644 index 0000000..f095e8d --- /dev/null +++ b/plugin.js @@ -0,0 +1,74 @@ +const process = require('process'); +const render = require('drone-render'); +const request = require('request-promise-native'); + +function configParser(configs) { + const ret = {}; + for (const configName in configs) { + const { env, def } = configs[configName]; + env.split(/\s*,\s*/).some(envar => { + if (process.env.hasOwnProperty(envar)) { + ret[configName] = process.env[envar]; + return true; + } + return false; + }); + if (!ret.hasOwnProperty(configName)) { + ret[configName] = typeof def === 'function' ? def() : def; + } + } + + 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: tag, + msg_type: msgtype, + agent_id: agentid, + msg_url: url, + btn_text: btntext, + message, + safe, + 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, tag, msgtype, agentid, safe, textcard }, + json: true + }); +} + +function exec({ corpid, corp_secret, ...config }) { + return getAccessToken(corpid, corp_secret) + .then(access_token => sendMsgFromWork({ ...config, access_token })) + .catch(err => { + console.error(err); + }); +} + +module.exports = { + configParser, + getAccessToken, + sendMsgFromWork, + exec +};