基于git web hook对push做检查

67 阅读3分钟

在git服务器上运行python脚本时遇到问题,该脚本用于检查push的提交信息,确保其遵循特定语法,但无法从用户那里获取输入,因此用户名和密码是硬编码的。现在也无法获取此次push之前发生的所有提交信息列表。

2、解决方案

  • 使用gitorious库,这是一个使用ssh来处理认证和权限管理的库。
  • 通过一个单独的用户来处理所有提交,这样可以简化凭证问题。
  • 在服务器上创建一个具有只读权限的虚拟Jira帐户,并将此账户的凭证硬编码到钩子中,以便验证给定的公关是否存在。

代码例子:

import SOAPpy
import getpass
import datetime
import sys
import re
import logging
import os

def login(x,y):
    try:
        auth = soap.login(x, y)
        return auth
    except:
          sys.exit( "Invalid username or password")

def getIssue(auth,issue):
    try:
        issue = soap.getIssue(auth, issue)
    except:
        sys.exit("No issue of that type found : Make sure all PRs are vaild jira PRs")

def git_get_commit_msg(commit_id):
    return get_shell_cmd_output("git rev-list --pretty --max-count=1 " + commit_id)

def git_get_last_commit_id():
    return get_shell_cmd_output("git log --pretty=format:%H -1")

def getCommitText():
    commit_msg_filename = sys.argv[1]
    try:
        commit_msg_text = open(commit_msg_filename).read()
        return commit_msg_text
    except:
        sys.exit("Could not read commit message")

def git_get_array_of_commit_ids(start_id, end_id):
    output = get_shell_cmd_output("git rev-list " + start_id + ".." + end_id)
    if output == "":
        return None
    commit_id_array = string.split(output, '\n')
    return commit_id_array

def get_shell_cmd_output(cmd):
    try:
        proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
        return proc.stdout.read().rstrip('\n')
    except KeyboardInterrupt:
        logging.info("... interrupted")

    except Exception, e:
        logging.error("Failed trying to execute '%s'", cmd)

def findpattern(commit_msg):
    pattern = re.compile("\w\w*-\d\d*")
    group = pattern.findall(commit_msg)
    print group
    found = len(group)
    found =0
    issues = 0
    for match in group:
            auth = soap.login(jirauser,passwd)
            getIssue(auth,match)
            issues = issues + 1
            found+=1
    if found ==0:
        sys.exit("No issue patterns found.")

    print "Retrieved issues: " + str(issues)  

def update():
    print sys.argv[2]
    print sys.argv[3]
    old_commit_id = sys.argv[2]
    new_commit_id = sys.argv[3]
    commit_id_array = git_get_array_of_commit_ids(old_commit_id, new_commit_id)
    for commit_id in commit_id_array:
        commit_text = git_get_commit_msg(commit_id)
        findpattern(commit_text)

soap = SOAPpy.WSDL.Proxy('some url')

# 使用gitorious库
import gitorious

# 创建一个gitorious库对象
g = gitorious.Gitorious()

# 使用ssh进行身份验证,并连接到远程git库
g.ssh_auth("username", "password")
g.connect("ssh://git@example.com/myrepo.git")

# 从远程git库中获取所有提交
commits = g.get_all_commits()

# 遍历每个提交
for commit in commits:
    # 检查提交消息中是否包含特定的模式
    if re.search("pattern", commit.message):
        # 如果提交消息中包含该模式,则执行某些操作
        print("Found a commit with the pattern: {}".format(commit.message))

# 使用ssh进行身份验证,并连接到远程git库
g.ssh_auth("username", "password")
g.connect("ssh://git@example.com/myrepo.git")

# 从远程git库中获取所有提交
commits = g.get_all_commits()

# 遍历每个提交
for commit in commits:
    # 检查提交消息中是否包含特定的模式
    if re.search("pattern", commit.message):
        # 如果提交消息中包含该模式,则执行某些操作
        print("Found a commit with the pattern: {}".format(commit.message))


# 使用ssh进行身份验证,并连接到远程git库
g.ssh_auth("username", "password")
g.connect("ssh://git@example.com/myrepo.git")

# 从远程git库中获取所有提交
commits = g.get_all_commits()

# 遍历每个提交
for commit in commits:
    # 检查提交消息中是否包含特定的模式
    if re.search("pattern", commit.message):
        # 如果提交消息中包含该模式,则执行某些操作
        print("Found a commit with the pattern: {}".format(commit.message))