【问题标题】:git server side hooksgit 服务器端钩子
【发布时间】:2010-08-06 18:17:07
【问题描述】:

我在服务器上运行以下 python 脚本时遇到问题硬编码。我现在也无法获取在此特定推送之前发生的提交消息列表。

#!/usr/bin/python

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')
# this line if for repointing the input from dev/null
#sys.stdin = open('/dev/tty', 'r') # this fails horribly.
#ask user for input
#jirauser = raw_inp
#("Username for jira: ")
jirauser = "username"
passwd = "987654321"
#passwd = getpass.getpass("Password for %s: " % jirauser)
login(jirauser,passwd)
#commit_msg = getCommitText()
#findpattern(commit_msg)
update()

此代码的预期目标是检查本地提交,并通过它们解析预期的模式,以及检查 jira(如果该 PR 存在)。它是一个服务器端钩子,在推送到存储库时被激活。

任何关于编写 python 钩子的提示将不胜感激。请和谢谢。

【问题讨论】:

  • 您的具体问题是什么?
  • 我无法从本地 git repo 获取信息。或者在我调用 git push origin master 命令后从用户那里获得。
  • 您将此脚本连接到哪个钩子?
  • @robert 它在更新钩子上

标签: python git ssh jira githooks


【解决方案1】:

我建议你看看 gitorious (http://gitorious.org/gitorious)。 他们使用 ssh 来处理身份验证和权限管理(获取 ssh 给出的用户名)。 他们在 git 存储库上也有一些钩子。我想看看他们如何使用 ruby​​ 处理 git 钩子可能会有所帮助。

【讨论】:

    【解决方案2】:

    当你的更新钩子触发时,服务器已经有了新的提交:问题是你的钩子是否允许有问题的引用移动。您想要本地(发送)存储库中的哪些信息?

    对于凭据问题,通过单个用户将所有人集中起来。例如,GitHub 使用 git 用户执行此操作,这就是他们的 SSH URL 以 git@github.com:... 开头的原因。然后在~git/.ssh/authorized_keys 中,将用户名与每个键相关联。请注意,以下内容应占一行,但出于演示目的而进行了换行。

    无代理转发,无端口转发,无 pty,无 X11 转发,
    command="env myuser=gbgcoll /usr/bin/git-shell -c \"${SSH_ORIGINAL_COMMAND:-}\""
    ssh-rsa AAAAB...

    现在要查看谁在尝试进行更新,您的钩子会检查 $myuser 环境变量。

    这不会为您提供每个用户的 Jira 凭据。要解决该问题,请创建一个对所有内容具有只读访问权限的虚拟 Jira 帐户,并将 Jira 帐户的凭据硬编码到您的挂钩中。这使您可以验证给定 PR 是否存在。

    【讨论】:

      猜你喜欢
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 2017-05-27
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多