每当有新的代码提交时调用(这包括新的更改)。
每当合并代码时调用。
每当增加 reviewer 时调用。
创建飞书群组 >> 群组设置 >> 群机器人 >> 添加机器人 >> 选择添加【自定义机器人】
复制 webhook 地址,之后有用。
安全设置设置自定义关键词,只有包含该自定义关键词的消息,才会被接收。
以 patchset-created 举例,传参格式如下:
patchset-created
–change Ia21d96fc3491f79b9beeb9e0dd0aeb9fad9f3fed
–is-draft false
–kind REWORK
–change-url http://gerrit.com/gerrit/5431
–change-owner Anonymous Coward (xxxx@qq.com)
–project rk3568_android11
–branch master
–uploader Anonymous Coward (xxxx@qq.com)
–commit c70a40eeef997ae6dc16abb72bccadc991de6d0e
–patchset 1
————————————————
每当有新的代码提交时调用(这包括新的更改)。代码如下:
#!/usr/bin/python3
#coding:utf-8
import sys
import os
import requests
import json
import time
port = '29418'
user = 'xxx'
ip = 'xxx.xxx.xxx.xxx'
url = 'https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxx' #飞书 webhook 地址
commit_sha1 = sys.argv[2]
print(commit_sha1)
query_cmd = 'ssh -p %s %s@%s gerrit query %s --format=JSON --patch-sets --current-patch-set' %(port, user, ip, commit_sha1)
print(query_cmd)
p = os.popen(query_cmd)
query_ret_json = p.read().splitlines()
e = p.close()
if e is not None or len(query_ret_json) != 2:
sys.exit("fatal: Can't find %s" %commit_sha1)
query_ret = json.loads(query_ret_json[0])
print(len(query_ret['patchSets']))
if len(query_ret['patchSets']) != 1:
sys.exit()
commit_user = query_ret['owner']['username']
commit_email = query_ret['owner']['email']
commit_project = query_ret['project']
commit_branch = query_ret['branch']
commit_url = query_ret['url']
commit_message = query_ret['commitMessage'].splitlines()[0]
commit_time = query_ret['createdOn']
patchset_msg = '**Subject :** ' + commit_message + '\n' +\
'**Owner :** ' + commit_user + '\n' +\
'**Project :** ' + commit_project + '\n' +\
'**Branch :** ' + commit_branch
print(patchset_msg)
method = 'post'
headers = {
'Content-Type': 'application/json'
}
json = {
"msg_type": "interactive",
"card": {
"config": {
"wide_screen_mode": True
},
"elements": [
{
"tag": "div",
"text": {
"content": patchset_msg,
"tag": "lark_md"
}
},
{
"tag": "hr"
},
{
"actions": [
{
"tag": "button",
"text": {
"content": "Gerrit Review",
"tag": "plain_text"
},
"type": "primary",
"url": commit_url
}
],
"tag": "action"
}
],
"header": {
"template": "blue",
"title": {
"content": "Gerrit " + patchset-created,
"tag": "plain_text"
}
}
}
}
requests.request(method=method, url=url, headers=headers, json=json)