Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinold/da26270701e4684dd97a447d1cd34c8a to your computer and use it in GitHub Desktop.
Save kevinold/da26270701e4684dd97a447d1cd34c8a to your computer and use it in GitHub Desktop.
#!/bin/bash
branchname=${1:-$(git rev-parse --abbrev-ref HEAD)}
if ! [ -x "$(command -v jq)" ]; then
echo 'Error: "jq" is not installed.' >&2
exit 1
fi
if [ ! $SECRET_GH_TOKEN ]; then
printf 'Error: $SECRET_GH_TOKEN is not set. \n - set $SECRET_GH_TOKEN to the token from: \n https://github.com/settings/tokens/new?scopes=repo&description=gh-cli-status-checks \n (e.g. append to ~/.bashrc: export SECRET_GH_TOKEN=token) ' >&2
exit 1
fi
result=$(
curl --request POST \
--url https://api.github.com/graphql \
--header 'authorization: Bearer '$SECRET_GH_TOKEN'' \
--header 'content-type: application/json' \
-s \
--data '{"query":"query ($foo: String) {\n viewer {\n login\n pullRequests(orderBy: {field: UPDATED_AT, direction: ASC}, last: 1, headRefName: $foo) {\n edges {\n node {\n id\n title\n state\n commits(last: 1) {\n edges {\n node {\n id\n commit {\n status {\n contexts {\n id\n state\n targetUrl\n description\n context\n }\n }\n message\n committedDate\n author {\n name\n }\n }\n }\n }\n }\n baseRefName\n headRefName\n permalink\n }\n }\n }\n }\n}\n","variables":{"foo":"'$branchname'"}}'
)
foundPR=$(
echo $result | jq -r \
'.data.viewer.pullRequests.edges[]' 2>/dev/null
)
if [[ -z $foundPR ]]; then
echo "no PRs found with branch name $branchname from $(echo $result | jq -r '.data.viewer.login')"
exit 1
fi
echo $result | jq -r \
'.data.viewer.pullRequests.edges | .[0]
| "PR" + "(" + if .node.state == "MERGED" then "" elif .node.state=="CLOSED" then "" else "" end +.node.state + "): " +""+.node.title+"" ,
"——— " + .node.permalink + " ———",
(
.node.commits.edges[] | "" + .node.commit.author.name + " - " + (.node.commit.committedDate | fromdateiso8601 | strftime("%a %x %r")) + "\n " + (.node.commit.message | (.[0:79] | rtrimstr(" ")) + (if length > 79 then "..." else "" end) ) + "",
(
.node.commit.status.contexts | map({title:.context, state:.state})
|
(
map(select(.state != "SUCCESS"))
| [ (map(select(.state == "FAILURE")) | map({title:.title, state: "✘"})), (map(select(.state == "PENDING")) | map({title:.title, state: "⬤"})) ] | flatten
| .[] | .state + " " + .title
),
(
"✓ "
+ (map(select(.state == "SUCCESS"))| length | tostring)
+ " passed"
)
)
)
'
## print jq to columns
# | [.[]| with_entries( .key |= ascii_downcase ) ]
# | (.[0] |keys_unsorted | @tsv), (.[]|.|map(.) |@tsv)
# | column -t -s $'\t'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment