又是好久没写公众号了,7月份换了工作远程居家上班了,今天想起来还有个公众号,哈哈。
前不久前端开发有了这个一个需求,希望分支有Merge行为的时候通知到Lark群里,当时用的Gitlab的CI做的,但发现上线后有个问题,当比如A分支已提交Merge请求至B分支,这个时候发现有代码还没提交完,这个时候研发又Push了新的代码至A分支,这个时候通知又会发出来,导致有很多无效的通知,折腾了半天也没搞定,于是放弃了,在Github上找了一圈,发现了一个好东西https://github.com/XUJINKAI/webhook-bridge
,看了下代码,没有Merge动作的通知,于是加了点代码实现了自己的需求,改造代码如下,主要是增加了一个判断是否是merge_request请求,其中主要有以下几个动作,当是update请求时,就直接return None
,不发送任何通知。
- open
- update
- close
- merge
- approved
msg_parse_gitlab.py
elif action == "merge_request":
project_name = safeget(o,'project','name')
merge_action = safeget(o, 'object_attributes', 'action')
merge_title = safeget(o, 'object_attributes', 'title')
source_branch = safeget(o,'object_attributes','source_branch')
dest_branch = safeget(o,'object_attributes','target_branch')
merge_url = safeget(o, 'object_attributes', 'url')
merge_assignees = safeget(o,'object_attributes','assignee_id')
if merge_action == "open":
#r += f"{username(o)} **{merge_action}**了一个Merge请求\nMerge is **{merge_url}**\ntitle is **{merge_title}**\nsource_branch is **{source_branch}**\ndest_branch is **{dest_branch}**\nassigeners is **{merge_name}**"
r += f"{username(o)} **{merge_action}**了一个Merge请求\nMerge is **{merge_url}**\ntitle is **{merge_title}**\nsource_branch is **{source_branch}**\ndest_branch is **{dest_branch}**"
elif merge_action == "close":
r += f"{username(o)} close **{action}** {in_repo_link(o)}\n"
elif merge_action == "update":
#r += f"{username(o)} update **{action}** {in_repo_link(o)}\n"
return None
elif merge_action == "merge":
r += f"{username(o)} **{merge_action}**了一个Merge请求\nMerge is **{merge_url}**\ntitle is **{merge_title}**\nsource_branch is **{source_branch}**\ndest_branch is **{dest_branch}**"
elif merge_action == "approved":
r += f"{username(o)} approved **{action}** {in_repo_link(o)}\n"
else:
r += f"{username(o)} do **{action}** {in_repo_link(o)}\n"
else:
r += f"{username(o)} do **{action}** {in_repo_link(o)}\n"
return r
标签:username,safeget,merge,通知,Gitlab,Merge,branch,action
From: https://www.cnblogs.com/uglyliu/p/16807261.html