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