python 编写 git 服务端钩子hook

官方文档 https://git-scm.com/book/zh/v2/%E8%87%AA%E5%AE%9A%E4%B9%89-Git-Git-%E9%92%A9%E5%AD%90

创建仓库

[root@instance-g478od6q ysb]# git init --bare test.git
Initialized empty Git repository in /work/ysb/test.git/
[root@instance-g478od6q ysb]# cd test.git/
[root@instance-g478od6q test.git]# ls
branches  config  description  HEAD  hooks  info  objects  refs
[root@instance-g478od6q test.git]# cd hooks/
[root@instance-g478od6q hooks]# ls
applypatch-msg.sample  post-update.sample     pre-commit.sample          pre-push.sample    update.sample
commit-msg.sample      pre-applypatch.sample  prepare-commit-msg.sample  pre-rebase.sample
[root@instance-g478od6q hooks]# 

  • 钩子都被存储在 Git 目录下的 hooks 子目录中。
  • 当用 git init 初始化一个新版本库时,Git默认会在这个目录中放置一些示例脚本。
  • 这些脚本除了本身可以被调用外,它们还透露了被触发时所传入的参数。
  • 所有的示例都是 shell脚本,其中一些还混杂了 Perl 代码,
  • 不过,任何正确命名的可执行脚本都可以正常使用 —— 可以用 Ruby 或 Python,或任何你熟悉的语言编写它们。
  • 这些示例的名字都是以 .sample 结尾,如果你想启用它们,得先移除这个后缀。
  • 服务端的钩子:
    • pre-receive —— 处理来自客户端的推送操作时,最先被调用的脚本是 pre-receive。 它从标准输入获取一系列被推送的引用。如果它以非零值退出,所有的推送内容都不会被接受。 你可以用这个钩子阻止对引用进行非快进(non-fast-forward)的更新,或者对该推送所修改的所有引用和文件进行访问控制。
    • post-receive —— 挂钩在整个过程完结以后运行,可以用来更新其他系统服务或者通知用户。 它接受与 pre-receive 相同的标准输入数据。 它的用途包括给某个邮件列表发信,通知持续集成(continous integration)的服务器, 或者更新问题追踪系统(ticket-tracking system) —— 甚至可以通过分析提交信息来决定某个问题(ticket)是否应该被开启,修改或者关闭。 该脚本无法终止推送进程,不过客户端在它结束运行之前将保持连接状态, 所以如果你想做其他操作需谨慎使用它,因为它将耗费你很长的一段时间。
    • update —— update 脚本和 pre-receive 脚本十分类似,不同之处在于它会为每一个准备更新的分支各运行一次。 假如推送者同时向多个分支推送内容,pre-receive 只运行一次,相比之下 update 则会为每一个被推送的分支各运行一次。 它不会从标准输入读取内容,而是接受三个参数:引用的名字(分支),推送前的引用指向的内容的 SHA-1 值,以及用户准备推送的内容的 SHA-1 值。 如果 update 脚本以非零值退出,只有相应的那一个引用会被拒绝;其余的依然会被更新。

实现需求

注意: 创建pre-receive、post-receive文件时, 要赋予执行权限

[root@instance-g478od6q hooks]# ll
-rw-r--r-- 1 root root    0 Mar 25 20:13 pre-receive
[root@instance-g478od6q hooks]# chmod a+x pre-receive 
[root@instance-g478od6q hooks]# ll
-rwxr-xr-x 1 root root    0 Mar 25 20:13 pre-receive
保护master分支,设置push权限
# 文件:pre-receive
#!/usr/bin/python3

import sys
from subprocess import check_output

protect_branch = ['master']
protect_committer = ['username']

line = sys.stdin.readline()

argv = line.split(' ')

oldrev = argv[0].strip()
newrev = argv[1].strip()
refname = argv[2].split('/')[-1].strip()

if len(set(list(oldrev))) == 1:
    print('新分支')
    exit(0)

missed_revs = check_output(['git', 'rev-list', f'{oldrev}..{newrev}']).decode().strip().split('\n')

for missed in missed_revs:
    message = check_output(['git', 'cat-file', 'commit', missed]).decode().strip().split('\n')
    committer = message[3].split(' ')[1].strip()
    commit_message = message[5]
    # 分支保护
    if refname in protect_branch:
        # 提交用户
        if committer in protect_committer:
            exit(0)
        else:
            print(f'您的账号(committer)没有权限push到{refname}分支')
            exit(1)
    else:
        exit(0)
push之后企业微信机器人通知
# post-receive
#!/usr/bin/python3

import sys
import requests
import json
from subprocess import check_output

line = sys.stdin.readline()

argv = line.split(' ')

oldrev = argv[0].strip()
newrev = argv[1].strip()
refname = argv[2].split('/')[-1].strip()

uri = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='
key = '企业微信机器人key'


def wx_robot(content, key):
    """
    微信机器人
    :param content:
    :param key:
    :return:
    """
    url = uri + key
    data = {
        "msgtype": "text",
        "text": {
            "content": content,
            "mentioned_list": [],
            "mentioned_mobile_list": []
        }
    }
    requests.post(url, data=json.dumps(data))


if len(set(list(oldrev))) == 1:
    wx_robot('创建了新分支', key=key)
else:
    missed_revs = check_output(['git', 'rev-list', f'{oldrev}..{newrev}']).decode().strip().split('\n')
    content = ''
    for missed in missed_revs:
        message = check_output(['git', 'cat-file', 'commit', missed]).decode()
        content += f'{message}\n'
    wx_robot(content, key=key)

如果在客户端push时报错 No such file or directory,可能时windows文件在linux上不能执行的原因
解决方法:remote: error: cannot run hooks/post-receive: No such file or directory

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值