I was wondering how to check if a pull request has conflicts on GitHub using a script from my PC? There is a nice solution mentioned here to do it via GitHub actions: https://stackoverflow.com/a/71692270/4441211
However, taking the same script from https://olivernybroe/action-conflict-finder and running it on my PC won't work unless I do a local merge. After identifying the conflicts I would have to discard the local merge. This process seems inefficient and I was looking for a "cleaner" and faster solution.
CodePudding user response:
Here is how to pipe it without a shell loop, and parse JSON natively, either with gh api -q
built-in jq
query, or jq
itself.
#!/usr/bin/env sh
REPOSITORY_NAME=
OWNER=
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 == false)
) | .number
' |
xargs -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
'
CodePudding user response:
So I wanted to share my solution which is much "cleaner" than what I wrote in the question. I simply used GitHub API to retrieve the "mergeable" state information of the pull request. This does not require me to do any local merges and discards to search for conflict-identifying strings such as ">>>>>>>" etc.
#!/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 branch --show-current) #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