Skip to content

Commit

Permalink
Dev to master, finish feishu (#13)
Browse files Browse the repository at this point in the history
* separate local config and default config

* finish feishu (#12)

* finish feishu, readme optimize feishu template
  • Loading branch information
goodideal authored Sep 6, 2023
1 parent c7dc267 commit c3e1a3a
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 81 deletions.
1 change: 0 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
"semi": true,
"singleQuote": true,
"arrowParens": "avoid",
"bracketSpacing": true
}
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Gitlab通知机器人

- [Gitlab通知机器人](#gitlab通知机器人)
- [功能展示](#功能展示)
- [系统对接](#系统对接)
- [环境变量](#环境变量)
- [与企业微信对接](#与企业微信对接)
- [与飞书对接](#与飞书对接)
- [应用部署运行](#应用部署运行)
- [使用Docker部署](#使用docker部署)
- [直接运行](#直接运行)
- [与Gitlab集成](#与gitlab集成)


`Gitlab``push``tag push``merge request``pipeline`推送到第三方IM平台的机器人,如企业微信、飞书等;

`1.0.0`: 采用内置代码,且仅支持企业微信;
Expand All @@ -11,8 +23,8 @@ todo:
- [X] 使用mustache模板
- [X] 增加note通知
- [X] 增加system hook通知,参考 [gitlab system hook](https://docs.gitlab.com/ee/administration/system_hooks.html)
- [X] 支持飞书机器人
- [ ] 增加消息模板配置文件
- [ ] 支持飞书机器人
- [ ] 支持按天统计数据


Expand Down Expand Up @@ -46,9 +58,24 @@ Gitlab pipeline 流水线

![alt gitlab-pipeline-msg](./docs/gitlab-pipeline-msg-1.png)

## 与企业微信对接
## 系统对接

### 环境变量

- `PLATFORM``qywx`, `feishu`,即:企业微信、飞书;
- `WEBHOOK_URL_XXX`:机器人webhook地址,具体配置参考[应用部署运行](#应用部署运行)

### 与企业微信对接

如何添加群机器人可自行百度,[企业微信群机器人配置说明](https://work.weixin.qq.com/api/doc/90000/90136/91770)

### 与飞书对接

飞书群里添加机器人。[飞书群机器人配置说明](https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot)

![alt gitlab-feishu](./docs/gitlab-feishu.png)

如何添加群机器人可自行百度。企业微[信群机器人配置说明](https://work.weixin.qq.com/api/doc/90000/90136/91770)
webhook地址示例:https://open.feishu.cn/open-apis/bot/v2/hook/UUID


## 应用部署运行
Expand Down
85 changes: 60 additions & 25 deletions app/service/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,29 @@ Mustache.escape = text =>
// all customized variables start with GB_
class WebhookService extends Service {
async translateMsg(data = {}, platform, gitlabEvent) {
this.platform = platform;
const { template, response, color } = this.config;

// set templage, response, color code
if (!template[platform]) {
this.logger.error(`can't find ${platform} in template`);
this.template = template.default;
} else {
this.template = template[platform];
}

if (!response[platform]) {
this.logger.error(`can't find ${platform} in response`);
this.response = response.default;
} else {
this.response = response[platform];
}

if (!color[platform]) {
this.logger.error(`can't find ${platform} in color`);
this.color = color.default;
} else {
this.color = color[platform];
}

const { object_kind } = data;

Expand All @@ -36,10 +58,9 @@ class WebhookService extends Service {
break;
}

return {
msgtype: 'markdown',
markdown: { content: content.join(' \n ') },
};
const { path, body } = this.response;
this.setJsonValue(body, path, content.join());
return body;
}

async pushHookHandler(content, data) {
Expand Down Expand Up @@ -80,7 +101,7 @@ class WebhookService extends Service {
}

async systemHookHandler(content, data) {
const template = this.getTemplateByPlatform(this.platform);
const template = this.template;

const { event_name } = data;
if (!EVENT_TYPE[event_name]) {
Expand Down Expand Up @@ -123,7 +144,7 @@ class WebhookService extends Service {
GB_op = '将代码推至';
}

const template = this.getTemplateByPlatform(this.platform);
const template = this.template;

this.logger.debug('template: ', template.push);
this.logger.debug('content: ', content);
Expand Down Expand Up @@ -200,7 +221,7 @@ class WebhookService extends Service {
// gitlab 11.3 未支持source参数
GB_sourceString = `${name}`;
}
const template = this.getTemplateByPlatform(this.platform);
const template = this.template;
const pipeline = Mustache.render(template.pipeline, {
...data,
GB_pipelineId,
Expand Down Expand Up @@ -242,7 +263,7 @@ class WebhookService extends Service {
default:
}

const template = this.getTemplateByPlatform(this.platform);
const template = this.template;
const merge_request = Mustache.render(template.merge_request, {
...data,
GB_stateAction,
Expand All @@ -267,7 +288,7 @@ class WebhookService extends Service {
GB_op = '删除';
}

const template = this.getTemplateByPlatform(this.platform);
const template = this.template;
const tag_push = Mustache.render(template.tag_push, {
...data,
GB_tag,
Expand All @@ -282,7 +303,7 @@ class WebhookService extends Service {
const { object_attributes = {} } = data;
const { state } = object_attributes;

const template = this.getTemplateByPlatform(this.platform);
const template = this.template;
const issue = Mustache.render(template.issue, {
...data,
GB_state: this.formatStatus(state),
Expand All @@ -294,7 +315,7 @@ class WebhookService extends Service {
const { object_attributes = {} } = data;
const { action } = object_attributes;

const template = this.getTemplateByPlatform(this.platform);
const template = this.template;
const issue = Mustache.render(template.wiki, {
...data,
GB_action: this.formatAction(action),
Expand All @@ -306,7 +327,7 @@ class WebhookService extends Service {
const { object_attributes = {} } = data;
const { action } = object_attributes;

const template = this.getTemplateByPlatform(this.platform);
const template = this.template;
const note = Mustache.render(template.note, {
...data,
GB_action: this.formatAction(action),
Expand All @@ -315,23 +336,23 @@ class WebhookService extends Service {
}

formatStatus(status) {
let color = 'comment',
let color = this.color.grey,
str,
isNotify = true;
switch (status) {
case 'failed':
color = 'warning';
color = this.color.red;
str = '执行失败';
break;
case 'success':
color = 'info';
color = this.color.green;
str = '执行成功';
break;
case 'running':
str = '运行中';
break;
case 'pending':
color = 'warning';
color = this.color.red;
str = '准备中';
isNotify = false;
break;
Expand All @@ -345,11 +366,10 @@ class WebhookService extends Service {
str = '需手动触发';
break;
case 'opened':
color = 'info';
color = this.color.green;
str = '开启';
break;
case 'closed':
color = 'info';
str = '关闭';
break;
default:
Expand Down Expand Up @@ -429,15 +449,30 @@ class WebhookService extends Service {
return changes;
}

getTemplateByPlatform(platform) {
const { template } = this.config;
setJsonValue(obj, path, value) {
const keys = path.split('.');
let currentObj = obj;

if (!template[platform]) {
this.logger.error(`can't find ${platform} in template`);
return template.default;
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
if (!currentObj[key] || typeof currentObj[key] !== 'object') {
// 如果当前键不存在或不是一个对象,则创建一个新的空对象
currentObj[key] = {};
}
currentObj = currentObj[key];
}

return template[platform];
const lastKey = keys[keys.length - 1];
if (Array.isArray(currentObj[lastKey])) {
// 如果路径的最后一部分是数组,根据指定的索引设置新值
const index = parseInt(lastKey, 10); // 将字符串索引转换为整数
if (!isNaN(index) && index >= 0) {
currentObj[lastKey][index] = value;
}
} else {
// 否则,设置新值
currentObj[lastKey] = value;
}
}
}

Expand Down
Loading

0 comments on commit c3e1a3a

Please sign in to comment.