我想知道如何使用我的 PC 上的腳本檢查拉取請求是否在 GitHub 上存在沖突?這里提到了一個很好的解決方案,可以通過 GitHub 操作來實作:https ://stackoverflow.com/a/71692270/4441211
但是,除非我進行本地合并,否則從 https://olivernybroe/action-conflict-finder 獲取相同的腳本并在我的 PC 上運行它是行不通的。在確定沖突后,我將不得不放棄本地合并。這個程序似乎效率低下,我一直在尋找“更清潔”和更快的解決方案。
uj5u.com熱心網友回復:
gh api -q以下是如何在沒有 shell 回圈的情況下對其進行管道傳輸,并使用內置jq查詢或jq自身決議 JSON 。
#!/usr/bin/env sh
REPOSITORY_NAME=
OWNER=
gh api -H "Accept: application/vnd.github json" \
"repos/${OWNER}/${REPOSITORY_NAME}/pulls" --cache 1h |
jq -j --arg curbranch "$(git rev-parse --abbrev-ref HEAD)" \
'
.[] | select(
(.base.ref == $curbranch) and
(.state == "open") and
(.draft == false)
) | .number | tostring "\u0000"
' |
xargs -0 -I{} \
gh api -H "Accept: application/vnd.github json" \
"repos/${OWNER}/${REPOSITORY_NAME}/pulls/{}" --cache 1h \
-q '
"PR #" (.number | tostring) ": "
.title " is "
if .mergeable != false then "mergeable" else "not mergeable" end
'
在某些 Windows 環境中,使用while read -r回圈而不是回圈xargs似乎有問題:
gh api -H "Accept: application/vnd.github json" \
"repos/${OWNER}/${REPOSITORY_NAME}/pulls" --cache 1h |
jq -r --arg curbranch "$(git rev-parse --abbrev-ref HEAD)" \
'
.[] | select(
(.base.ref == $curbranch) and
(.state == "open") and
(.draft != true)
) | .number
' | while read -r pr; do
gh api -H "Accept: application/vnd.github json" \
"repos/${OWNER}/${REPOSITORY_NAME}/pulls/${pr}" --cache 1h \
-q '
"PR #" (.number | tostring) ": "
.title " is "
if .mergeable != false then "mergeable" else "not mergeable" end
'
done
uj5u.com熱心網友回復:
所以我想分享我的解決方案,它比我在問題中寫的更“干凈”。我只是使用 GitHub API 來檢索拉取請求的“可合并”狀態資訊。這不需要我進行任何本地合并和丟棄來搜索沖突識別字串,例如 >>>>>>>>" 等。
#!/bin/bash
OWNER="GitHub_UserName" #your github user name or organization name - whoever owns the repository
REPOSITORY_NAME="Your_Repo_Name"
PR_List=$(gh pr list) #Get list of pull requests of the repository
Current_Branch=$(git rev-parse --abbrev-ref HEAD) #get the current branch name
if [[ $(grep "$Current_Branch" <<< "$PR_List") ]]; then #If true then there exists a PR for this branch in the repository
#Get the line of the pull request from the list:
PR_Line=$(grep "$Current_Branch" <<< "$PR_List")
#Get the specific numerical ID of the pull request
PR_ID=$(awk '{print $1}' <<< "$PR_Line") #taken from https://stackoverflow.com/a/36736249/4441211
#now get all the PR information from GitHub:
pr_data=$(gh api -H "Accept: application/vnd.github json" repos/$OWNER/$REPOSITORY_NAME/pulls/$PR_ID)
#Now extract the specific "mergeable" state of the PR and then check it:
mergeable_row=$(grep "mergeable" <<< "$pr_data")
mergeable_string=$(echo "$mergeable_row" | awk -F 'mergeable":' '{printf $NF}')
mergeable_state=$(echo "$mergeable_string" | cut -d',' -f 1)
echo "Mergeable = $mergeable_state" #for debug
if [ "$mergeable_state" == "false" ]; then
MergeIssuesFound="yes"
echo "Pull Request in repository $REPOSITORY_NAME is NOT mergeable"
else
MergeIssuesFound="no"
echo "Pull Request in repository $REPOSITORY_NAME is mergeable"
fi
fi
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/507630.html
標籤:重击 混帐 github github-api
