Integrations

The GitHub bot account

GAOS reviews pull requests, comments on them and opens draft fixes with two GitHub tokens. Those tokens belong to a bot account of their own, never to a person, and the bot reaches the product’s repositories through one organisation team (D-125). This page says why, then walks through the setup step by step. The names on it are invented: the organisation acme, the bot acme-gaos-bot, the person jdoe and repositories such as acme/route-api.

Why a bot, not a person

GAOS has no GitHub identity of its own: it is whoever owns the token in its file. GAOS asks GitHub “who am I?” (GET /user) and treats that login as itself. When the token is a person’s, GAOS and that person become one login, and five things go wrong:

What happensWhy it matters
Every review, comment and draft pull request shows the person’s name and avatar.Readers cannot tell a machine review from a human one, and the person is answerable for text they never wrote.
The person’s own pull requests get only a comment, never Request changes.GAOS requests changes only when the author is someone else, because GitHub refuses a request for changes on your own pull request. With a shared login, every pull request that person opens is “their own” to GAOS, so a high finding on it blocks nothing.
The revision loop cannot tell the person’s review from GAOS’s.GAOS recognises its own past reviews by login and a hidden marker. A review the person writes by hand comes from GAOS’s login, so the loop’s rule “a person’s review is a request; GAOS’s is a finding” rests on the marker alone.
The person leaves, rotates a token or loses access, and GAOS stops.The tokens live and die with one human account: its password, its two-factor device, its employment.
The audit trail is wrong.The organisation’s log, the notifications and every “Review posted” line name the person for actions a machine took.

A bot account fixes all five with no change to GAOS: the bot is a login of its own, so the person’s pull requests are “someone else’s” again and their reviews count as a person’s.

The three ways, and the one chosen

WayShows asCode change in GAOSVerdict
A person’s token (how it started)jdoeNoneRetired: the five problems above.
A bot account (a machine user) with its own tokensacme-gaos-botNone: swap the two token filesChosen (D-125). Live the same day.
A GitHub Appacme-gaos[bot]Yes: a design and a releasePossible later. It needs code that does not exist yet.

A GitHub App is the more “official” bot: GitHub labels it bot, it takes no seat and its tokens live one hour. GAOS cannot use one today, for three reasons:

A bot account needs none of that. If the App is ever built, the team and the repository list on this page carry over unchanged: the App is installed on the same repositories.

Seats

On GitHub’s free plan for organisations, a bot account costs nothing. On a paid plan it takes one seat, like any member. GitHub’s terms allow one machine account per person or organisation that runs it; it must be run by a person who is responsible for it.

Why a team

A bot can be given access in three ways. The team wins on every point an operator cares about:

WayWhat it costs
An outside collaborator on each repositoryOne invitation per repository, each accepted by hand; no single place that lists what the bot can reach; removing it means visiting every repository.
The organisation’s base permission (every member gets Write)The bot reaches every repository of the organisation, including other products’. Too wide.
One team, gaos-bot, with Write on the listed repositoriesOne invitation. One page (Organisation → Teams → gaos-bot → Repositories) that lists exactly what the bot can reach. A new repository is one call. Offboarding is removing the bot from one team.

The team holds only the bot. GitHub makes the person who creates a team its maintainer, so the setup script removes that person again: a person added to the team would silently gain Write on every repository in it. The team is closed (visible to the organisation’s members), so anyone can see what GAOS can touch.

The team is also the boundary that makes a classic token acceptable (see the tokens): a classic token reaches everything its account reaches, and the bot’s account reaches only the team’s repositories.

Which repositories

Every sibling repository of the product, not only the ones GAOS watches today. A product is usually many repositories: the application, its frontend, shared modules, message consumers, integrations, a data layer, release notes and the QA automation repository. GAOS’s own lists name only what it acts on now:

GAOS list (per product)What being on it does
Repositories (Master.Repos)GAOS watches it: every pull request plans review cards, and the after-merge and staging flows use it.
Auto-fix target repositoriesA defect fix may open a draft pull request there.
Automation repositoriesThe QA tool opens its spec pull requests there.

Each of those lists switches something on, so an operator adds a repository to it only when GAOS should start acting there. The bot’s access is wider on purpose: when the operator adds a repository to a list, the bot can already read and write it, and nothing fails at the moment the feature is switched on. An auto-fix admin links a repository from the product page’s Repositories block, which checks GAOS’s access first (Linked repositories): linking there is what turns GAOS on for it.

Building the list

Start from the product’s own record of its repositories, for example an index file in its workspace, and check every name against the organisation before you grant anything:

bash
# every repository of the organisation, with its last push and whether it is archived
gh repo list acme --limit 500 --json name,isArchived,pushedAt \
  --jq '.[] | [.name, .isArchived, .pushedAt[:10]] | @tsv' | LC_ALL=C sort > org.tsv
# names in your list that the organisation does not have
cut -f1 org.tsv > org-names.txt
LC_ALL=C sort -u repos.txt | LC_ALL=C comm -23 - org-names.txt

When this check was first run, it found four kinds of mistakes, which is why it is worth running:

Leave out an archived repository, and a repository no one has pushed to for a year or more unless you know it is still deployed. Keep the final list in a private place beside the instance’s other private material, never in this repository: product and repository names are the operator’s.

Which permission

RoleEnough?Why
ReadReviews onlyA reviewer may read and review. It cannot push a fix branch, add a label or open the auto-fix’s draft pull request.
TriageNoAdds labels, but still cannot push a branch.
Write (push)YesPush gaos/fix-* and gaos/catalog-* branches, open draft pull requests, add labels, comment, review.
Maintain, AdminNeverSettings, branch protection, deletion: nothing GAOS does needs them.

Branch protection still applies to the bot: it cannot push to a protected branch or merge past a required review. One thing to know: a Request changes review from an account with Write access blocks the merge when the branch requires approving reviews, until the bot’s review is dismissed or a later GAOS review replaces it. That is the point of the review, and it is new only for pull requests the person who owned the old token opened (see why).

The two tokens

GAOS reads two token files, and the bot gets two separate tokens for them, so each can be revoked without the other:

File on the host (mode 600)SettingWhat GAOS does with it
deploy/stacks/prod.pr-github-tokenGAOS_GITHUB_TOKEN_FILEReads pull requests, their files and checks; posts the review; posts the “draft fix” comment. Traced as github.
deploy/stacks/prod.pr-github-write-tokenGAOS_GITHUB_WRITE_TOKEN_FILEThe auto-fix: blobs, trees, commits, branches, draft pull requests, labels, comments, closing a replaced pull request. Traced as github-write.

Classic or fine-grained

Give every token an expiry (90 days is a sensible default) and put the date in a calendar: an expired token turns that part of GAOS off with a log line, not an alarm.

Step by step

Steps 1–6 are on GitHub and the workstation; steps 7–9 are on the GAOS host. Nothing in GAOS is released or redeployed: only two files change and the dashboard restarts.

1. Create the account

2. Write the repository list

Put one repository name per line in repos.txt (names only, no owner), checked as in Building the list.

3. Create the team and grant it Write

Run this as an organisation owner, with gh logged in. It is a dry run unless you pass --apply, and running it twice changes nothing.

bash
#!/usr/bin/env bash
# ./gaos-bot-team.sh <org> <bot-login> <repos.txt> [--apply]
set -euo pipefail
ORG=${1:?org}; BOT=${2:?bot login}; LIST=${3:?repos file}; APPLY=${4:-}
TEAM=gaos-bot
run() { if [ "$APPLY" = --apply ]; then "$@"; else printf 'would run:'; printf ' %q' "$@"; echo; fi; }

me=$(gh api user --jq .login)
gh api "users/$BOT" --jq .login >/dev/null || { echo "no GitHub user $BOT"; exit 1; }

# 1. the team, closed (visible to members); the creator is removed again
if gh api "orgs/$ORG/teams/$TEAM" >/dev/null 2>&1; then echo "ok   team $TEAM exists"
else
  run gh api -X POST "orgs/$ORG/teams" -f name="$TEAM" -f privacy=closed \
    -f description="GAOS bot: write access to the product's repositories"
  run gh api -X DELETE "orgs/$ORG/teams/$TEAM/memberships/$me"
fi

# 2. Write ("push") on every listed repository; a missing one is reported
while read -r r; do
  [ -z "$r" ] && continue
  gh api "repos/$ORG/$r" --jq .name >/dev/null 2>&1 || { echo "SKIP $r: not found"; continue; }
  run gh api -X PUT "orgs/$ORG/teams/$TEAM/repos/$ORG/$r" -f permission=push >/dev/null
done < "$LIST"

# 3. the bot joins; a non-member gets an organisation invitation by email
run gh api -X PUT "orgs/$ORG/teams/$TEAM/memberships/$BOT" -f role=member --jq .state
bash
./gaos-bot-team.sh acme acme-gaos-bot repos.txt            # read the plan
./gaos-bot-team.sh acme acme-gaos-bot repos.txt --apply    # do it; the last line prints "pending"

4. Accept the invitation as the bot

Sign in as the bot and accept the invitation at https://github.com/orgs/acme/invitation (or from the email). Until then the membership stays pending and the bot reaches nothing. Then check, as the owner:

bash
gh api orgs/acme/teams/gaos-bot/memberships/acme-gaos-bot --jq .state     # active
gh api orgs/acme/teams/gaos-bot/members --jq '.[].login'                  # the bot, and no one else
gh api orgs/acme/teams/gaos-bot/repos --paginate --jq '.[].name' | wc -l   # the length of repos.txt

5. Create the two tokens

Still signed in as the bot: Settings → Developer settings → Personal access tokens. Create two, as the tokens describes, named so the next operator knows them: gaos review <instance> and gaos write <instance>. Copy each once; GitHub never shows it again. Never paste a token into a chat, an issue or a terminal history.

6. Check each token before it goes near GAOS

Save each token to a file first (mode 600), then ask GitHub who it is and what it may do. The commands print the login and the permissions, never the token:

bash
(umask 077; read -rs -p 'review token: ' t; echo; printf '%s\n' "$t" > review.token)
(umask 077; read -rs -p 'write token: '  t; echo; printf '%s\n' "$t" > write.token)
for f in review.token write.token; do
  curl -fsS -H "Authorization: Bearer $(cat $f)" https://api.github.com/user \
    | python3 -c 'import json,sys; print(json.load(sys.stdin)["login"])'
  curl -fsS -H "Authorization: Bearer $(cat $f)" https://api.github.com/repos/acme/route-api \
    | python3 -c 'import json,sys; print(json.load(sys.stdin)["permissions"])'
done
# expected: acme-gaos-bot, then {'admin': False, 'maintain': False, 'push': True, ...} twice

7. Pick a quiet moment

Read The switchover first. On the Pull requests page, make sure no review card is Running: a review card that is in the middle of posting when the login changes may post its review a second time.

8. Install the tokens on the host

On the GAOS host, in the deploy checkout. Keep the old files for a rollback, and write the new token into the existing file with cat >: the dashboard container has the file mounted, and an editor that saves by replacing the file leaves the container reading the old one.

bash
cd /path/to/gaos
for f in prod.pr-github-token prod.pr-github-write-token; do
  cp -p deploy/stacks/$f deploy/stacks/$f.bak-$(date +%Y%m%d)
done
read -rs -p 'review token: ' t; echo; printf '%s\n' "$t" > deploy/stacks/prod.pr-github-token
read -rs -p 'write token: '  t; echo; printf '%s\n' "$t" > deploy/stacks/prod.pr-github-write-token
unset t
stat -c '%a %U %n' deploy/stacks/prod.pr-github*-token     # 600, the deploy user
docker restart gaos-prod-dashboard-1                        # GAOS reads the tokens once, at start

The write token is used only when GAOS_AUTOFIX_ALLOW_CLASSIC=1 is in the instance’s settings (for a classic token). A restart keeps every setting the last deploy gave the container; there is no need to redeploy.

9. Verify

bash
docker logs --since 5m gaos-prod-dashboard-1 2>&1 | grep -E 'GitHub client|write token'
# pull requests: GitHub client on
# auto-fix: write token on (classic (broad))      -- or (fine-grained)

To roll back, copy the .bak- files over the token files with cat old > new and restart the dashboard again.

The switchover

GAOS knows its own past reviews and comments by two things together: the login that posted them and a hidden marker in their text. Neither is enough alone, because anyone can paste a marker. When the token changes from jdoe to acme-gaos-bot, the reviews GAOS posted earlier still carry the marker but now come from “someone else”:

A later release may let GAOS treat a list of earlier logins as its own, which closes both gaps.

An existing shared account, for the interim

Signing up a dedicated account can wait on a mailbox or an approval. In the meantime an operator may point GAOS at an account the organisation already has, and move to the dedicated bot later with one more token swap (D-126). It works the same day and needs no code change and no team script, but check the account first, because a shared account brings back what a bot was meant to fix:

CheckHowIf it is so
Do people open pull requests with it?gh api "search/issues?q=org:acme+author:<login>" --jq .total_countTheir pull requests get only a comment from GAOS, and their reviews read as GAOS’s to the revision loop.
What does it reach?GET /user/repos with the token: count push and adminA leaked token reaches all of it, other products included. Prefer an account whose reach is the product.
Do its tokens expire?The github-authentication-token-expiration header of any API answer; absent means neverGive the next token an expiry.

Install its tokens exactly as in steps 6–9, keep the old files as backups, and write down which account is in use. When the dedicated account exists: run the team script for it, accept the invitation, create its two tokens and repeat steps 6–9. The switchover applies again, from the shared login to the bot’s.

Later: a new repository, rotation, offboarding

TaskHow
The product gains a repositoryAdd its name to repos.txt and re-run the script with --apply, or one call: gh api -X PUT orgs/acme/teams/gaos-bot/repos/acme/<repo> -f permission=push. Then add it to a GAOS list if GAOS should act there.
A repository leaves the productgh api -X DELETE orgs/acme/teams/gaos-bot/repos/acme/<repo>.
A token is about to expireCreate the new token, then steps 6, 8 and 9 for that one file. Revoke the old one after the restart.
A token may have leakedRevoke it on GitHub first, then install a new one. GAOS turns that part off until then, with a log line.
A second GAOS instanceIts own tokens, named for that instance. Instances share nothing (Instances); they may share the bot account if both serve the same organisation, never a token.
Retire the botRemove it from the team (it then reaches nothing), revoke its tokens, empty the two files and restart: GAOS turns GitHub off.

Troubleshooting

You seeWhy, and what to do
GitHub client off (the token is not a GitHub token)The file holds something other than a ghp_ or github_pat_ token, such as a GitHub App’s ghs_ token. See the three ways.
GitHub client off (... must be mode 0600)chmod 600 the file and restart.
auto-fix: write token off (a classic ghp_ token is refused ...)Set GAOS_AUTOFIX_ALLOW_CLASSIC=1 in the instance’s settings and redeploy, or use a fine-grained token.
The log says on, but reviews still show the personThe container reads the old file: an editor replaced it instead of writing into it. Write it again with cat > and recreate the container through a normal deploy.
A card fails with github-tokenGitHub answered 401, 403 or 404. On every repository: the token expired or was revoked, or the invitation is still pending. On one repository only: it is not in the team; add it (a new repository).
A fine-grained token answers 403 everywhereThe organisation requires an owner to approve fine-grained tokens. Approve it under Organisation → Settings → Personal access tokens → Pending requests.