Skip to content

Instantly share code, notes, and snippets.

@kuceb
Last active May 9, 2020 01:33
Show Gist options
  • Save kuceb/3ebffb89350b76771b03a3952dbbbc39 to your computer and use it in GitHub Desktop.
Save kuceb/3ebffb89350b76771b03a3952dbbbc39 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'
@brian-mann
Copy link

brian-mann commented May 7, 2020

could be nice to make a legit @cypress module and publish to npm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment