出于协作的要求, 需要在把gitlab的push event勾到rocket chat上面, 通知协作的其他人.

BUT rocket chat提供的脚本没有具体的文件diff, so, 只好修改一下, 以下是小改一下的脚本,

 /* eslint no-console:0, max-len:0 */
// see https://gitlab.com/help/web_hooks/web_hooks for full json posted by GitLab
const MENTION_ALL_ALLOWED = false; // <- check that bot permission allow has mention-all before passing this to true.
const NOTIF_COLOR = '#6498CC';
const refParser = (ref) => ref.replace(/^refs\/(?:tags|heads)\/(.+)$/, '$1');
const displayName = (name) => (name && name.toLowerCase().replace(/\s+/g, '.'));
const atName = (user) => (user && user.name ? '@' + displayName(user.name) : '');
const makeAttachment = (author, text) => {
return {
author_name: author ? displayName(author.name) : '',
author_icon: author ? author.avatar_url : '',
text,
color: NOTIF_COLOR
};
};
const pushUniq = (array, val) => ~array.indexOf(val) || array.push(val); // eslint-disable-line class Script { // eslint-disable-line
process_incoming_request({ request }) {
try {
let result = null;
const channel = request.url.query.channel;
const event = request.headers['x-gitlab-event'];
switch (event) {
case 'Push Hook':
result = this.pushEvent(request.content);
break;
case 'Merge Request Hook':
result = this.mergeRequestEvent(request.content);
break;
case 'Note Hook':
result = this.commentEvent(request.content);
break;
case 'Issue Hook':
result = this.issueEvent(request.content);
break;
case 'Tag Push Hook':
result = this.tagEvent(request.content);
break;
case 'Pipeline Hook':
result = this.pipelineEvent(request.content);
break;
case 'Build Hook':
result = this.buildEvent(request.content);
break;
default:
result = this.unknownEvent(request, event);
break;
}
if (result && result.content && channel) {
result.content.channel = '#' + channel;
}
return result;
} catch (e) {
console.log('gitlabevent error', e);
return this.createErrorChatMessage(e);
}
} createErrorChatMessage(error) {
return {
content: {
username: 'Rocket.Cat ErrorHandler',
text: 'Error occured while parsing an incoming webhook request. Details attached.',
icon_url: '',
attachments: [
{
text: `Error: '${error}', \n Message: '${error.message}', \n Stack: '${error.stack}'`,
color: NOTIF_COLOR
}
]
}
};
} unknownEvent(data, event) {
return {
content: {
username: data.user ? data.user.name : (data.user_name || 'Unknown user'),
text: `Unknown event '${event}' occured. Data attached.`,
icon_url: data.user ? data.user.avatar_url : (data.user_avatar || ''),
attachments: [
{
text: `${JSON.stringify(data, null, 4)}`,
color: NOTIF_COLOR
}
]
}
};
}
issueEvent(data) {
const state = data.object_attributes.state;
const action = data.object_attributes.action;
let user_action = state;
let assigned = ''; if (action === 'update') {
user_action = 'updated';
} if (data.assignee) {
assigned = `*Assigned to*: @${data.assignee.username}\n`;
} return {
content: {
username: 'gitlab/' + data.project.name,
icon_url: data.project.avatar_url || data.user.avatar_url || '',
text: (data.assignee && data.assignee.name !== data.user.name) ? atName(data.assignee) : '',
attachments: [
makeAttachment(
data.user, `${user_action} an issue _${data.object_attributes.title}_ on ${data.project.name}.
*Description:* ${data.object_attributes.description}.
${assigned}
See: ${data.object_attributes.url}`
)
]
}
};
} commentEvent(data) {
const comment = data.object_attributes;
const user = data.user;
const at = [];
let text;
if (data.merge_request) {
const mr = data.merge_request;
const lastCommitAuthor = mr.last_commit && mr.last_commit.author;
if (mr.assignee && mr.assignee.name !== user.name) {
at.push(atName(mr.assignee));
}
if (lastCommitAuthor && lastCommitAuthor.name !== user.name) {
pushUniq(at, atName(lastCommitAuthor));
}
text = `commented on MR [#${mr.id} ${mr.title}](${comment.url})`;
} else if (data.commit) {
const commit = data.commit;
const message = commit.message.replace(/\n[^\s\S]+/, '...').replace(/\n$/, '');
if (commit.author && commit.author.name !== user.name) {
at.push(atName(commit.author));
}
text = `commented on commit [${commit.id.slice(0, 8)} ${message}](${comment.url})`;
} else if (data.issue) {
const issue = data.issue;
text = `commented on issue [#${issue.id} ${issue.title}](${comment.url})`;
} else if (data.snippet) {
const snippet = data.snippet;
text = `commented on code snippet [#${snippet.id} ${snippet.title}](${comment.url})`;
}
return {
content: {
username: 'gitlab/' + data.project.name,
icon_url: data.project.avatar_url || user.avatar_url || '',
text: at.join(' '),
attachments: [
makeAttachment(user, `${text}\n${comment.note}`)
]
}
};
} mergeRequestEvent(data) {
const user = data.user;
const mr = data.object_attributes;
const assignee = mr.assignee;
let at = []; if (mr.action === 'open' && assignee) {
at = '\n' + atName(assignee);
} else if (mr.action === 'merge') {
const lastCommitAuthor = mr.last_commit && mr.last_commit.author;
if (assignee && assignee.name !== user.name) {
at.push(atName(assignee));
}
if (lastCommitAuthor && lastCommitAuthor.name !== user.name) {
pushUniq(at, atName(lastCommitAuthor));
}
}
return {
content: {
username: `gitlab/${mr.target.name}`,
icon_url: mr.target.avatar_url || mr.source.avatar_url || user.avatar_url || '',
text: at.join(' '),
attachments: [
makeAttachment(user, `${mr.action} MR [#${mr.iid} ${mr.title}](${mr.url})\n${mr.source_branch} into ${mr.target_branch}`)
]
}
};
} pushEvent(data) {
const project = data.project;
const user = {
name: data.user_name,
avatar_url: data.user_avatar
};
// branch removal
if (data.checkout_sha === null && !data.commits.length) {
return {
content: {
username: `gitlab/${project.name}`,
icon_url: project.avatar_url || data.user_avatar || '',
attachments: [
makeAttachment(user, `从项目 [${project.name}](${project.web_url}) 移除分支 ${refParser(data.ref)} `)
]
}
};
}
// new branch
if (data.before == 0) { // eslint-disable-line
return {
content: {
username: `gitlab/${project.name}`,
icon_url: project.avatar_url || data.user_avatar || '',
attachments: [
makeAttachment(user, `推送了新分支 [${refParser(data.ref)}](${project.web_url}/commits/${refParser(data.ref)}) 到 [${project.name}](${project.web_url}), 领先master分支 ${data.total_commits_count} 个提交`)
]
}
};
}
return {
content: {
username: `gitlab/${project.name}`,
icon_url: project.avatar_url || data.user_avatar || '',
attachments: [
makeAttachment(user, `推送了 ${data.total_commits_count} 个提交到项目 [${project.name}](${project.web_url}) 的 [${refParser(data.ref)}](${project.web_url}/commits/${refParser(data.ref)}) 分支`),
{
text: data.commits.map((commit) => `--------------------\n - ${new Date(commit.timestamp).toLocaleString()} [${commit.id.slice(0, 8)}](${commit.url}) 由 ${commit.author.name} 提交, 内容: \n ${commit.message.replace(/\s*$/, '')} \n\n增加了 ${commit.added.length} 个文件: ${commit.added.map((addfile)=>`\n[${addfile}](${project.web_url}/raw/${refParser(data.ref)}/${addfile})`)} \n\n修改了 ${commit.modified.length} 个文件: ${commit.modified.map((modifiedfile)=>`\n[${modifiedfile}](${project.web_url}/raw/${refParser(data.ref)}/${modifiedfile})`)} \n\n删除了 ${commit.removed.length} 个文件: ${commit.removed.map((removedfile)=>`\n${removedfile}`)} \n--------------------`).join('\n'),
color: NOTIF_COLOR
}
]
}
};
} tagEvent(data) {
const tag = refParser(data.ref);
const user = {
name: data.user_name,
avatar_url: data.user_avatar
};
let message;
if (data.checkout_sha === null) {
message = `deleted tag [${tag}](${data.project.web_url}/tags/)`;
} else {
message = `pushed tag [${tag} ${data.checkout_sha.slice(0, 8)}](${data.project.web_url}/tags/${tag})`;
}
return {
content: {
username: `gitlab/${data.project.name}`,
icon_url: data.project.avatar_url || data.user_avatar || '',
text: MENTION_ALL_ALLOWED ? '@all' : '',
attachments: [
makeAttachment(user, message)
]
}
};
} pipelineEvent(data) {
const commit = data.commit;
const user = {
name: data.user_name,
avatar_url: data.user_avatar
};
const pipeline = data.object_attributes; return {
content: {
username: `gitlab/${data.project.name}`,
icon_url: data.project.avatar_url || data.user_avatar || '',
attachments: [
makeAttachment(user, `pipeline returned *${pipeline.status}* for commit [${commit.id.slice(0, 8)}](${commit.url}) made by *${commit.author.name}*`)
]
}
};
} buildEvent(data) {
const user = {
name: data.user_name,
avatar_url: data.user_avatar
}; return {
content: {
username: `gitlab/${data.repository.name}`,
icon_url: '',
attachments: [
makeAttachment(user, `build named *${data.build_name}* returned *${data.build_status}* for [${data.project_name}](${data.repository.homepage})`)
]
}
};
}
} 本人还是比较懒, 是只搞了push event, 其他的有时间再搞吧!

gitlab勾住rocket chat的更多相关文章

  1. Slack 开源替代品 Rocket.Chat(聊天,文件上传等等)

    Rocket.Chat 是特性最丰富的 Slack 开源替代品之一. 主要功能:群组聊天,直接通信,私聊群,桌面通知,媒体嵌入,链接预览,文件上传,语音/视频 聊天,截图等等. Rocket.Chat ...

  2. Mac 下安装运行Rocket.chat

    最近花了一周的时间,复习了HTML.CSS.原生JS,并学习了Node.js.CoffeeScript.js.MongoDB,入了下门. 因为准备在Rocket.chat 上做二次开发,所以先下载和安 ...

  3. Rocket.Chat 开源IM系统部署

    Rocket.Chat 官方给出的文档也个人觉得太麻烦了,并且对ubuntu的支持程度远高于CentOS,自己就折腾写了个安装的笔记,如果是在公司内部或者是部门内部还是很有用处的,比较看中的功能有和g ...

  4. [svc]rocket.chat内网聊天服务器搭建(类似slack)

    rocket.chat内网聊天服务 服务端有linux windows 树莓派等 支持客户端登陆- 官网 支持网页登陆 多人聊天图 还有手机客户端 部署rocket.chat 为了方便我使用docke ...

  5. 向Rocket.Chat推送消息

    Rocket.Chat推送消息 Rocket.Chat是一个开源实时通讯平台, 支持Windows, Mac OS, Linux. 支持聊天, 文件上传, 视频通话, 语音通话功能. 向Rocket. ...

  6. Centos7部署开源聊天软件rocket.chat

    一.部署rocket.chat 1.看官方文档部署,很简单,一步一步跟着部署即可 注意:需要部署节点需要联网主要是yum方式 https://rocket.chat/docs/installation ...

  7. Centos7 使用Docker安装rocket.chat聊天工具

    镜像下载.域名解析.时间同步请点击阿里云开源镜像站 下载安装 Rocket.Chat 目前最新的版本为 4.0.1,可以通过手动或者容器的方式安装.这里我推荐使用容器,部署过程会方便不少. 如果要用容 ...

  8. Gitlab勾选Remove Source Branch后本地仍然能够看到该分支

    现象: Gitlab合并Merge Request时,勾选了Remove Source Branch,但是本地仍然能够看到该分支(git branch -a),而远程仓库中该分支已经不存在. 解决: ...

  9. ocket.chat 使用 Meteor 开发的实时协作工具,类似 丁丁。

    ocket.chat  使用 Meteor 开发的实时协作工具,类似 丁丁. https://rocket.chat/

随机推荐

  1. 迭代加深搜索POJ 3134 Power Calculus

    题意:输入正整数n(1<=n<=1000),问最少需要几次乘除法可以从x得到x的n次方,计算过程中x的指数要求是正的. 题解:这道题,他的结果是由1经过n次加减得到的,所以最先想到的就是暴 ...

  2. 二叉树的递归遍历 天平UVa839

    题意:输入一个树状的天平,利用杠杆原理,根据力矩是否相等(W1D1==W1D2)判断天平是否平衡 解题思路:1.由于判断天平是否平衡,当W1和W2都为0的时候,会先输入左子树,再输入右子树 2.此时的 ...

  3. PHP去除unicode续:json_encode之后,仅仅有文字,数字不见了的解决方法

    接前文.http://blog.csdn.net/yanzi1225627/article/details/44985487 这么处理了一段时间.确实没发现问题.但近期发现了一个bug.比方输入&qu ...

  4. LDA主题模型学习笔记5:C源代码理解

    1.说明 本文对LDA原始论文的作者所提供的C代码中LDA的主要逻辑部分做凝视,原代码可在这里下载到:https://github.com/Blei-Lab/lda-c 这份代码实现论文<Lat ...

  5. Android binder学习一:主要概念

    要看得懂android代码,首先要了解binder机制.binder机制也是android里面比較难以理解的一块,这里记录一下binder的重要概念以及实现.作为备忘. 部分内容来源于网上,如有侵权. ...

  6. zookeeper web ui--&gt;node-zk-browser安装

    眼下公司正在使用zookeeper做配置管理和其它工作,在网上找几个zookeeper管理工具,都不尽人意,要么功能不够强大,要么不能友好的浏览zk树形结构.我的想法是zk管理工具,应该有一个树形结构 ...

  7. Java集合源代码剖析(二)【HashMap、Hashtable】

    HashMap源代码剖析 ; // 最大容量(必须是2的幂且小于2的30次方.传入容量过大将被这个值替换) static final int MAXIMUM_CAPACITY = 1 << ...

  8. 01_GIT基础、安装

     1 为什么选择GIT 分布式,强调个体 公共server压力和数据量都不会太大 速度快.灵活 随意两个开发人员之间能够非常easy的解决冲突 离线工作 每日工作备份 能够吃懊悔药 2  GIT基 ...

  9. Lua代码提示和方法跳转

    前言 当在一个大型工程中编写大量的lua脚本时,代码提示和方法跳转等功能很实用,据我所了解的目前除LuaStudio之外,似乎还没有一个很好的编辑器.但今天讲述的是Idea +EmmyLua插件 达到 ...

  10. [UWP]了解模板化控件(7):支持Command

    以我的经验来说,要让TemplatedControl支持Command的需求不会很多,大部分情况用附加属性解决这个需求会更便利些,譬如UWPCommunityToolkit的HyperlinkExte ...