-
-
Save iainlane/feebff304f956889e8d630136acd7c2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
usage() { | |
echo "$0 [owner] [repo] [sha]" >&2 | |
} | |
if [ "${GITHUB_TOKEN:-}" = "" ]; then | |
usage | |
echo "Error: \$GITHUB_TOKEN must be set to a valid GitHub Personal Access Token" >&2 | |
exit 1 | |
fi | |
if [ $# -ne 3 ]; then | |
usage | |
exit 1 | |
fi | |
OUT=$(jq -cn " | |
{ | |
query: \$query, | |
variables: { | |
owner: \"$1\", | |
repo: \"$2\", | |
sha: \"$3\", | |
} | |
}" \ | |
--arg query ' | |
query associatedPRs($sha: String, $repo: String!, $owner: String!) { | |
repository(name: $repo, owner: $owner) { | |
object(expression: $sha) { | |
... on Commit { | |
associatedPullRequests(first: 5) { | |
edges { | |
node { | |
number | |
} | |
} | |
} | |
} | |
} | |
} | |
}' \ | |
| curl \ | |
--silent \ | |
--header "Authorization: token $GITHUB_TOKEN" \ | |
--header "Content-Type: application/json" \ | |
--data @- \ | |
https://api.gijthub.com/graphql) | |
ERRORS=$(echo "${OUT}" | jq -r '(select (.data.repository.object == null))') | |
# there was an error | |
if [ -n "${ERRORS}" ]; then | |
echo -n "Error getting pull requests." >&2 | |
MESSAGES=$(echo "${OUT}" | jq -r 'select(.errors) | .errors | .[].message') | |
if [ -n "${MESSAGES}" ]; then | |
echo " Error messages:" >&2 | |
while IFS= read -r message; do | |
echo " ${message}" >&2 | |
done <<< "${MESSAGES}" | |
else | |
echo " Check the commit SHA and try again." >&2 | |
fi | |
exit 1 | |
fi | |
echo "${OUT}" | jq -r '.data.repository.object.associatedPullRequests.edges | .[].node.number' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment