簡體   English   中英

git服務器側鈎

[英]git server side hooks

在服務器上運行follow 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()

該代碼的預期目標是檢查本地所做的提交,並通過它們解析預期的模式,並檢查in PRira是否存在。 它是服務器端掛鈎,可在推送到存儲庫時激活。

在編寫python鈎子的任何提示將不勝感激。 謝謝,麻煩您了。

我建議您看看gitorious( http://gitorious.org/gitorious )。 他們使用ssh處理身份驗證和權限管理(獲取ssh給定的用戶名)。 他們在git倉庫上也有一些迷。 我想這可能有助於了解他們如何使用ruby處理git鈎子。

在更新掛鈎啟動時,服務器具有新的提交:問題是您的掛鈎是否將允許所涉及的引用移動。 您想要來自本地(發送)存儲庫的哪些信息?

對於憑據問題,請通過一個用戶將所有人歸納。 例如,GitHub使用git用戶執行此操作,這就是為什么其SSH URL以git@github.com:...開頭的原因。 然后在~git/.ssh/authorized_keys ,將用戶名與每個密鑰相關聯。 請注意,以下內容應放在一行上,但出於演示目的而進行了包裝。

no-agent-forwarding,no-port-forwarding,no-pty,no-X11-forwarding,
command="env myuser=gbgcoll /usr/bin/git-shell -c \"${SSH_ORIGINAL_COMMAND:-}\""
ssh-rsa AAAAB...

現在,看看誰在嘗試執行更新,您的鈎子將檢查$myuser環境變量。

這不會為您提供每個用戶的Jira憑據。 要解決該問題,請創建一個虛擬的Jira帳戶,該帳戶具有對所有內容的只讀訪問權,並將 Jira帳戶的憑據硬編碼您的鈎子中。 這使您可以驗證給定的PR是否存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM