本文以Github上的gitlab2tele
项目为基础扩展实现了所有的通知类型, Giblab钩子的JSON格式参考 Gitlab Webhook
配置Giblab
Project
> Settings
> Web Hooks
设置Web Hook
的URL地址(例如http://192.168.8.1:8888/gitlab/project
), 选择要发送的事件, 并保存
安装依赖库
pip install -r requirements.txt
启动脚本
$PROJECT_ROOT/gitlab2tele/runserver.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from flask import Flask, request
import gitlab2tele
import logging
app = Flask(__name__)
app.logger.setLevel(level=0)
app.logger.addHandler(logging.StreamHandler())
# TOKEN = os.environ['TOKEN']
TOKEN = '这里填写机器人的TOKEN'
# CHAT_ID = int(os.environ['CHAT_ID'])
# 群ID是负数
# 这里填写群chat_id
# 群的chat_id可以通过 https://api.telegram.org/bot<TOKEN>/getUpdates 获取, 把<TOKEN>替换为实际的值
CHAT_ID = -123456789
app.logger.info('TOKEN:' + TOKEN)
app.logger.info('CHAT_ID: ' + str(CHAT_ID))
@app.route("/gitlab/project", methods=["PUT", "POST"])
def gitlab_project():
json_obj = request.get_json()
app.logger.info(json_obj)
ts = gitlab2tele.TeleSender(TOKEN, CHAT_ID)
ts.post_project_event(json_obj)
return 'OK'
if __name__ == '__main__':
port = int(os.environ.get("PORT", 8888))
app.run('0.0.0.0', port=port, debug=True)
通知接收代码
$PROJECT_ROOT/gitlab2tele/__init__.py
# -*- coding: utf-8 -*-
import telebot
import json
class TeleSender():
def __init__(self, token, chat_id):
self.bot = telebot.TeleBot(token)
self.chat_id = chat_id
def post_project_event(self, json_obj):
obj = json_obj
event_type = obj['object_kind']
if event_type == 'push':
self.__revice_push(obj)
if event_type == 'merge_request':
self.__revice_merge_request(obj)
if event_type == 'note':
self.__revice_note(obj)
if event_type == 'tag_push':
self.__revice_tag_push(obj)
if event_type == 'issue':
self.__revice_issue(obj)
"""
PUSH通知
"""
def __revice_push(self, json_obj):
msg = self.__parse_push_event(json_obj)
self.bot.send_message(self.chat_id, msg)
def __parse_push_event(self, json_obj):
push_user = json_obj['user_name']
commit_count = json_obj['total_commits_count']
commits = [
'%s\n%s %s\n' % (x['url'], x['timestamp'], x['message']) for x in json_obj['commits']
]
repo_url = "\n".join([x for x in commits])
repo_name = json_obj['repository']['name']
ret = u'%s push %d commits to %s. %s' % (push_user, commit_count, repo_name, repo_url)
return ret
"""
评论通知
"""
def __revice_note(self, json_obj):
msg = self.__parse_note_event(json_obj)
self.bot.send_message(self.chat_id, msg)
def __parse_note_event(self, json_obj):
if json_obj['object_attributes']['noteable_type'] == 'Commit':
url = json_obj['object_attributes']['url']
note = json_obj['object_attributes']['note']
ret = u'评论(Commit): \n%s\n%s' % (url, note)
return ret
if json_obj['object_attributes']['noteable_type'] == 'MergeRequest':
url = json_obj['object_attributes']['url']
note = json_obj['object_attributes']['note']
ret = u'评论(MergeRequest):\n%s\n%s' % (url, note)
return ret
if json_obj['object_attributes']['noteable_type'] == 'Issue':
url = json_obj['object_attributes']['url']
note = json_obj['object_attributes']['note']
ret = u'评论(Issue):\n%s\n%s' % (url, note)
return ret
if json_obj['object_attributes']['noteable_type'] == 'Snippet':
url = json_obj['object_attributes']['url']
note = json_obj['object_attributes']['note']
ret = u'评论(Snippet):\n%s\n%s' % (url, note)
return ret
"""
合并通知
"""
def __revice_merge_request(self, json_obj):
msg = self.__parse_merge_request(json_obj)
self.bot.send_message(self.chat_id, msg)
def __parse_merge_request(self, json_obj):
request_user = json_obj['user']['name']
mr_title = json_obj['object_attributes']['title']
url = json_obj['object_attributes']['url']
state = json_obj['object_attributes']['state']
ret = u'%s %s a merge request : %s. %s' % (request_user, state, mr_title, url)
return ret
"""
问题通知
"""
def __revice_issue(self, json_obj):
msg = self.__parse_issue_event(json_obj)
self.bot.send_message(self.chat_id, msg)
def __parse_issue_event(self, json_obj):
issue_title = json_obj['object_attributes']['title']
issue_url = json_obj['object_attributes']['url']
issue_description = json_obj['object_attributes']['description']
ret = '%s\n%s\n%s' % (issue_title, issue_url, issue_description)
return ret
"""
标记PUSH
"""
def __revice_tag_push(self, json_obj):
msg = self.__parse_tag_push_event(json_obj)
self.bot.send_message(self.chat_id, msg)
def __parse_tag_push_event(self, json_obj):
push_user = json_obj['user_name']
repo_name = json_obj['repository']['name']
ret = '%s create or (delete) tags to repo: \n%s' % (push_user, repo_name)
return ret
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。