Skip to content

Instantly share code, notes, and snippets.

@nitrocode
Last active February 27, 2024 19:21
Show Gist options
  • Save nitrocode/c95d915f7589f4c2ba4522834035aff0 to your computer and use it in GitHub Desktop.
Save nitrocode/c95d915f7589f4c2ba4522834035aff0 to your computer and use it in GitHub Desktop.
Common github cli gh commands

Common github cli gh commands

Prereqs

Install

brew install gh

Set env vars

Change these to be applicable to the commands below

org="myorg"
repo="infrastructure"
pr_number="42"
ref="73550b798b714feea4698f884cf094c3"

Login

gh auth login

Show status

gh auth status

Refresh token

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

Logout

gh auth logout

Export API token

export GITHUB_TOKEN=$(gh config get -h github.com oauth_token)

Remove paging

gh config set pager "less -FX"

Create autolink

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.

Create PR

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}"

Get list of all remote branches

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'

Delete all merged branches

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 output of a github actions run

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

Set all statuses to success state

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}"

Get a list of all repos

gh repo list ${org} --json 'name' --limit 1000 | jq -r '.[].name' > all-repos.txt

References

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