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 happens | Why 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
| Way | Shows as | Code change in GAOS | Verdict |
|---|---|---|---|
| A person’s token (how it started) | jdoe | None | Retired: the five problems above. |
| A bot account (a machine user) with its own tokens | acme-gaos-bot | None: swap the two token files | Chosen (D-125). Live the same day. |
| A GitHub App | acme-gaos[bot] | Yes: a design and a release | Possible 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:
- The token check accepts only
ghp_andgithub_pat_tokens. An App’s token starts withghs_and is refused. - GAOS learns its own login from
GET /user, which an App’s token cannot call. The App’s name has to come fromGET /appinstead. - An App’s token expires after an hour, so GAOS would have to sign a JWT with the App’s private key and renew the token before it lapses. Today it reads one token at start and keeps it.
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:
| Way | What it costs |
|---|---|
| An outside collaborator on each repository | One 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 repositories | One 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 repositories | A defect fix may open a draft pull request there. |
| Automation repositories | The 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:
# 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:
- A name misspelled in the index (
websockerforwebsocket). Fix the spelling. - A “service” that is only a folder inside another repository. Leave it out.
- An external open-source project the product uses but does not own. Leave it out.
- Real sibling repositories that the index had forgotten (the database migrations, the manual, a query library). Add them.
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
| Role | Enough? | Why |
|---|---|---|
| Read | Reviews only | A reviewer may read and review. It cannot push a fix branch, add a label or open the auto-fix’s draft pull request. |
| Triage | No | Adds labels, but still cannot push a branch. |
Write (push) | Yes | Push gaos/fix-* and gaos/catalog-* branches, open draft pull requests, add labels, comment, review. |
| Maintain, Admin | Never | Settings, 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) | Setting | What GAOS does with it |
|---|---|---|
deploy/stacks/prod.pr-github-token | GAOS_GITHUB_TOKEN_FILE | Reads pull requests, their files and checks; posts the review; posts the “draft fix” comment. Traced as github. |
deploy/stacks/prod.pr-github-write-token | GAOS_GITHUB_WRITE_TOKEN_FILE | The auto-fix: blobs, trees, commits, branches, draft pull requests, labels, comments, closing a replaced pull request. Traced as github-write. |
Classic or fine-grained
- Classic, scope
repoonly. The simplest, and safe here because the bot’s account is the boundary: the token reaches only the team’s repositories. The write token needsGAOS_AUTOFIX_ALLOW_CLASSIC=1in the instance’s settings, or GAOS refuses it. Never tickworkflow,admin:org,delete_repoor anywrite:/admin:scope outsiderepo. - Fine-grained, if the organisation allows them: resource owner
acme, All repositories (the account’s reach is still the team), permissions Contents, Pull requests and Issues read and write; Actions, Checks, Commit statuses and Metadata read. An organisation may require an owner to approve a fine-grained token before it works.
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
- Sign up a new GitHub account, for example
acme-gaos-bot, with a shared team mailbox, not a person’s address, so that more than one person can recover it. - Turn on two-factor authentication. Store the password, the two-factor secret and the recovery codes in the team’s password vault.
- Set the profile’s name to something a reader understands, such as GAOS (automated reviews), and add a short bio that says who runs it.
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.
#!/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
./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:
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:
(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.
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
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)
- The Integrations view of the back office shows the GitHub client on.
- The next pull request in a watched repository gets its review from
acme-gaos-bot. - After a day of normal traffic, revoke the person’s two old tokens on GitHub and delete the
.bak-files.
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 review card that was posting at the moment of the change does not find the review it already posted, and may post it again. Step 7 avoids this.
- An open pull request whose newest commit already has a GAOS review from
jdoe: when the revision loop next runs on it, it reads that review as a person’s. A high finding no longer holds the pull request for a person, and a low or medium one is treated as a person’s request. A new push moves the pull request to a new commit, and the old review stops counting. If such a pull request matters, re-run its review from the card after the switch. - Everything new is posted and recognised as the bot’s.
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:
| Check | How | If it is so |
|---|---|---|
| Do people open pull requests with it? | gh api "search/issues?q=org:acme+author:<login>" --jq .total_count | Their 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 admin | A 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 never | Give 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
| Task | How |
|---|---|
| The product gains a repository | Add 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 product | gh api -X DELETE orgs/acme/teams/gaos-bot/repos/acme/<repo>. |
| A token is about to expire | Create the new token, then steps 6, 8 and 9 for that one file. Revoke the old one after the restart. |
| A token may have leaked | Revoke it on GitHub first, then install a new one. GAOS turns that part off until then, with a log line. |
| A second GAOS instance | Its 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 bot | Remove it from the team (it then reaches nothing), revoke its tokens, empty the two files and restart: GAOS turns GitHub off. |
Troubleshooting
| You see | Why, 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 person | The 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-token | GitHub 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 everywhere | The organisation requires an owner to approve fine-grained tokens. Approve it under Organisation → Settings → Personal access tokens → Pending requests. |