Sub topic 14
Automating configuration and reporting with gh and the REST API
This chapter assumes you can run commands in a terminal, have the GitHub CLI (gh) installed and signed in with gh auth login, and understand the alert workflow from the earlier chapters. You will use gh api to enable protection, list alerts, and close them.
Why automate
Clicking through settings works for one repository. It does not work for fifty. The REST API lets you apply the same configuration everywhere and pull alert data into reports. gh api handles authentication for you, so you do not manage tokens in the command itself.
Enable the features
The security_and_analysis object on a repository controls these settings. Send a PATCH request that sets each feature's status to enabled.
gh api --method PATCH repos/OWNER/REPO \
-f 'security_and_analysis[secret_scanning][status]=enabled' \
-f 'security_and_analysis[secret_scanning_push_protection][status]=enabled'The -f flag sends each value as a string field. Replace OWNER/REPO with your repository. A successful call returns the updated repository as JSON. If the call fails with a 403 or 404 error, check your permissions and whether the feature is available for that repository.
List open alerts
Reading alerts is a GET request. The --jq option filters the JSON on your machine, so you see only the fields you need.
gh api 'repos/OWNER/REPO/secret-scanning/alerts?state=open' \
--jq '.[] | {number, secret_type_display_name, validity, push_protection_bypassed}'Each result shows the alert number, the secret type, the validity status, and whether someone bypassed push protection to create it. These fields support triage. An alert that is active and was created by a bypass is a good first one to look at.
Close an alert
Closing an alert is a PATCH request with a state and a resolution.
gh api --method PATCH repos/OWNER/REPO/secret-scanning/alerts/42 \
-f state=resolved \
-f resolution=revokedOther resolutions exist for cases such as false positives or test values. Pick the one that matches what happened.
The workflow in one view
The order of the last two steps matters. Revoke first, then close.
Common errors
- 404 on a repository you can see. You may lack the permission needed for the endpoint. Some endpoints return 404 instead of 403 to avoid revealing that a resource exists.
- Feature does not turn on. The feature may be unavailable for that repository. Check the response and the repository's settings page.
- Quoting problems. Wrap field names that contain square brackets in single quotes, as shown above. Otherwise your shell may try to interpret them.
- Only the first page of alerts. The API returns a limited number of results per request. Add
--paginatetogh apito fetch every page.
Apply it across repositories
You can loop over repositories to apply the same setting. This example enables both features on every non-archived repository in an organization.
gh repo list ORG --no-archived --limit 200 --json nameWithOwner --jq '.[].nameWithOwner' |
while read -r repo; do
gh api --method PATCH "repos/$repo" \
-f 'security_and_analysis[secret_scanning][status]=enabled' \
-f 'security_and_analysis[secret_scanning_push_protection][status]=enabled' \
--silent || echo "Failed: $repo"
doneThe || echo part records failures so you can review them. For a whole organization, you can also use organization-level settings, which the earlier enabling chapter covered.
Exercise
You run a script that closes alert 42 with resolution=revoked, but the token behind the alert is still valid at its provider. What is wrong, and what should you do?
Show answer
Closing an alert does not revoke the credential, so the "revoked" resolution is inaccurate. Revoke the token at its provider first (and rotate it), then close the alert with the correct resolution. If you already closed it, reopen it or update the resolution after revoking.
Exercise
Which flag makes gh api return every page of results when you list alerts, and why might you need it?
Show answer
--paginate. The API returns results in pages, so without it you may see only the first page and miss alerts.