brew install gh
Change these to be applicable to the commands below
org="myorg"
repo="infrastructure"
pr_number="42"
ref="73550b798b714feea4698f884cf094c3"
gh auth login
gh auth status
It would be nice if the token granted in gh auth login
would be temporary so neither refresh
nor logout
would be needed.
gh auth refresh
gh auth logout
export GITHUB_TOKEN=$(gh config get -h github.com oauth_token)
gh config set pager "less -FX"
As of this writing, there is no way to do this without hitting the API directly
PREFIX="IR"
JIRA_ORG="example"
gh api --method POST -H "Accept: application/vnd.github+json" \
"/repos/${org}/${repo}/cookbooks/autolinks" \
-f key_prefix="${PREFIX}-" -f url_template="https://${JIRA_ORG}.atlassian.net/browse/${PREFIX}-<num>"
This is only useful if there is no infrastructure-as-code for GitHub repos i.e. no terraform, no github-settings bot, no probot-settings.
TICKET_NAME=`echo $(git symbolic-ref --short HEAD) | grep -oP '[/]?[A-Z]{1,}-[a-z0-9]+'`
gh pr create --draft --base main --title "${TICKET_NAME}"
repo=$(gh repo view --json url | jq -r .url | rev | cut -d'/' -f1-2 | rev)
gh api --method GET -H "Accept: application/vnd.github+json" "/repos/${org}/${repo}/branches" | jq -r '.[].name'
TODO: Check if merged branch is in the list of remote branches before doing a DELETE
. Use comms
.
TODO: If the above TODO can be accomplished, the || true
can also be removed
repo=$(gh repo view --json url | jq -r .url | rev | cut -d'/' -f1-2 | rev)
gh pr list --state merged --json headRefName --limit 1000 | jq -r '.[].headRefName' | \
while read branch; do \
gh api --method DELETE -H "Accept: application/vnd.github+json" "/repos/${repo}/git/refs/heads/${branch}" || true; \
done
Get most recent commit id of pr
gh pr view ${pr_number} --json 'commits' --jq '.commits | last | .oid'
Get run ids that contain the string build
in their name
gh api \
-H "Accept: application/vnd.github+json" \
/repos/${org}/${repo}/commits/${ref}/check-runs \
--jq '[ .check_runs[] | select(.name | contains("build")) | {name: .name, run_id: .details_url | capture("(?<id>[0-9]+)") | .id } ]' | jq .
See failed log
gh run view ${run_id} --log-failed
Unfortunately, there is no way to delete a bad status check using the v3 api. Bad checks can only be set to success
.
List all pr checks
gh pr checks
Get all target urls and contexts
gh api -H "Accept: application/vnd.github+json" \
/repos/${org}/${repo}/commits/${ref}/statuses | \
jq '.[] | select(.state=="failure") | {target_url: .target_url, context: .context}'
Update each one using the same target_url
and context
gh api --method POST -H "Accept: application/vnd.github+json" \
/repos/${org}/${repo}/commits/${ref}/statuses \
-f state='success' \
-f description="skipped" \
-f target_url="${target_url}" \
-f context="${context}"
gh repo list ${org} --json 'name' --limit 1000 | jq -r '.[].name' > all-repos.txt