2017-08-11 116 views

回答

1

使用GitHub API(V3)絕對有可能。要獲得PR ID,您需要使用GitHub Search Issues API並首先找到PR號碼,然後您可以使用PR號碼查找PR ID。請注意,拉取請求將包含合併請求ID和單獨的問題ID。

例如,假設您有一個提交sha - 7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c。要從這個沙發中找到PR ID,您可以執行以下操作:

步驟1:使用commit sha找到PR編號:使用Github搜索api- https://api.github.com/search/issues?q=sha:7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c。從JSON響應,現場「數」表示PR號(在本例中16)和「ID」代表問題ID(不是PR ID)

第2步:使用PR數量和回購的細節,找到PR ID。根據步驟1中收到的JSON響應,我們可以構建以下內容 - https://api.github.com/repos/lamassu/lamassu-admin/pulls/16。在收到的JSON響應中,字段「ID」是所需的PR ID。

0

我花了很多時間調查它,這裏是結果。簡單SH行打印拉請求ID:

git ls-remote origin ‘pull/*/head’ | grep -F -f <(git rev-parse HEAD) | awk -F’/' ‘{print $3}’ 

而且在Jenkinsfile:

def gitCommitSHA = sh(returnStdout: true, script: 'git rev-parse HEAD').trim() 
def allPRs = sh(returnStdout: true, script: "origin ‘pull/*/head’") 
List result = allPRs.split('\n').findAll { it.contains(gitCommitSHA) && it.contains("refs/pull") } 
if (result.size() ==1){ 
    def str = result[0] 
    def prId = str.substring(str.indexOf("pull")+5,str.lastIndexOf("head")-1) 
    echo "Pull request id: ${prId}" 
} 
+0

謝謝。這對我非常有幫助。真的感謝 –