Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a pull request on GitHub has conflicts using a script?

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.

like image 954
Eyal Gerber Avatar asked Mar 30 '26 14:03

Eyal Gerber


1 Answers

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 -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
'

Altenatively using a while read -r loop instead of xargs which seems problematic in some Windows environment:

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
like image 103
Léa Gris Avatar answered Apr 02 '26 03:04

Léa Gris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!