Skip to content

Multi-org fine-grained tokens + dual-auth cross-org gh repo sync - #1

Draft
rastaman wants to merge 3 commits into
trunkfrom
feat/multi-org-tokens-12888
Draft

rastaman wants to merge 3 commits into
trunkfrom
feat/multi-org-tokens-12888

Conversation

@rastaman

@rastaman rastaman commented Jul 23, 2026 •

Copy link
Copy Markdown
Owner

Why this exists

Upstream context: cli/cli#12888

Summary

This fork implements:

  1. Multiple fine-grained PATs under one gh install (per-org account keys).
  2. Cross-org gh repo sync using two stored tokens when classic PATs are forbidden.

Problem

  1. A fine-grained PAT is scoped to a single resource owner (one user or one org). Orgs that disable classic PATs force one token per owner.
  2. Stock gh resolves one active token per host, which is awkward for multi-org workflows.
  3. gh repo sync uses POST /repos/.../merge-upstream, which authenticates with one credential that must see both fork and upstream. Cross-org sync with FG PATs fails with HTTP 403: Resource not accessible by personal access token, even when each org has a valid FG token with Contents write.

Real-world case that triggered this:

  • Destination fork: ludopro/clients-sync
  • Upstream: myorg/clients-sync
  • Two FG tokens under account keys ludo and myorg

What was implemented

1. Multiple fine-grained tokens per host

Commits:

Behavior:

  • gh auth login --with-token --org ACME stores the token under account key ACME.
  • Optional metadata under users.<accountKey>: user (real login) and orgs.
  • Per-invocation override via GH_AUTH_ACCOUNT=<accountKey>.

Docs: docs/multi-org-tokens.md

2. Dual-token cross-org gh repo sync

Commit:

  • 6bf4c9754 — feat(repo-sync): dual-token cross-org sync for fine-grained PATs

New flags:

--dest-auth string     Stored account key for destination repository auth
--source-auth string   Stored account key for source repository auth

When either flag is set:

  1. --source is required (parent discovery needs one token that can see both orgs; FG tokens usually cannot).
  2. merge-upstream is skipped (single-credential API cannot span two orgs).
  3. Prefer dual-token git refs path: read tip with source token, update dest ref with dest token.
  4. If refs update fails, fall back to a temporary bare git fetch (source token) + push (dest token).

Example:

gh repo sync ludopro/clients-sync \
  --source myorg/clients-sync \
  --dest-auth ludo \
  --source-auth myorg

3. Reproducible cross-build script

script/build-cross.sh builds darwin/arm64, linux/arm64, and linux/amd64 with CGO_ENABLED=0, -trimpath, and SOURCE_DATE_EPOCH from git HEAD. Outputs under dist/cross/<version>/ plus checksums. Darwin arm64 was smoke-tested successfully.

Explicitly not done (yet)

  • Phase 2 automatic org→token selection from URL / -R
  • Making merge-upstream itself accept two tokens (not possible: one Authorization header per request)
  • Upstream PR against cli/cli

Status

Area Status
Multi-account FG token storage OK
GH_AUTH_ACCOUNT / account metadata OK
Dual-auth repo sync (refs + git fallback) OK
Cross-build script OK
Darwin arm64 manual test OK
Upstream PR to cli/cli PENDING

rastaman and others added 3 commits July 23, 2026 12:27
A fine-grained token is scoped to a single organization, but the CLI
resolved exactly one token per host, so a single gh install could not
drive several orgs (or run concurrent processes against different orgs).

This adds Phase 1 of cli#12888:

- `gh auth login --with-token --org <label>` stores a token under an org
  label instead of the GitHub login, so multiple tokens owned by the same
  login can coexist on one host without overwriting each other. The real
  login and org are recorded as account metadata.
- `GH_AUTH_ACCOUNT` selects which stored account to use for a single
  invocation without changing the active account, enabling concurrent gh
  processes to target different orgs in parallel. It is gated by a
  config-only membership check (no keyring probe on unknown values) and is
  ignored when an explicit env token is set.
- `gh auth status` surfaces the account label and organizations for
  org-scoped accounts; classic single-account output and JSON are
  unchanged. `gh auth switch` selects these accounts by their org label.
- Document GH_AUTH_ACCOUNT in the environment help topic.

Config storage is backward compatible: sibling accounts and the new
`user`/`orgs` keys live under `hosts.<host>.users.<account>`, which go-gh
ignores, so no hosts.yaml migration is required.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Show the before/after hosts.yml for cli#12888 so the additive schema is
concrete: org-scoped accounts are keyed by the org label and add optional
`user` (real login) and `orgs` fields, while classic accounts and their
files are untouched.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Add --dest-auth/--source-auth so gh repo sync can read upstream and write
the fork with separate stored account tokens, falling back to a temporary
git fetch/push when merge-upstream cannot span two orgs. Include a
reproducible cross-build script for local darwin/linux testing.

Co-authored-by: Cursor <[email protected]>
@rastaman
rastaman force-pushed the feat/multi-org-tokens-12888 branch 2 times, most recently from 328d165 to 6bf4c97 Compare July 23, 2026 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant