Repository navigation
Phase 2: Worker auth layer for GitHub App tokens (#437) - #442
Merged
neuromechanist merged 2 commits intoMay 11, 2026
Merged
neuromechanist merged 2 commits into
neuromechanist merged 2 commits into
Conversation
New module backend/src/services/github-auth.ts:
- signAppJwt: behavioral mirror of the Phase 1 script's helper. Same
PKCS#1 / encrypted-PKCS#8 rejection and 30s iat skew margin. Two
copies (script + Worker) are intentional; tests in both files pin the
same invariants.
- fetchInstallationToken: POST /app/installations/:id/access_tokens,
parses token + expires_at, throws with the raw body on non-2xx or
malformed responses. Takes a fetch override + baseUrl for the test
Bun.serve fake.
- getInstallationToken: module-scoped cache keyed by installation ID.
Refresh at 5 min before expiry. Concurrent callers attach to a single
in-flight `refreshing` promise so we never mint N tokens for N
parallel orchestrator steps. Failed mints drop the entry so retries
start clean.
- GitHubAuth discriminated source ({kind: "pat"} | {kind: "app", ...}).
- getDefaultGitHubAuth returns app auth when all App fields and the
requested installationId are set, falls back to PAT otherwise. The
fallback keeps dev environments without App secrets working until
ops sets them.
- resolveInstallationId maps an org login to its configured installation
id, decoupling Phase 3 callsites from the exact env var names.
- Test seams (__resetInstallationTokenCacheForTests,
__seedInstallationTokenCacheForTests) mirror the same pattern in
backend/src/services/github.ts.
backend/src/types/bindings.ts:
- Add four optional fields (GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY,
GITHUB_APP_INSTALLATION_ID_NEMAR_DATASETS,
GITHUB_APP_INSTALLATION_ID_NEMAR_ORG). Optional in this phase; Phase 5
drops the PAT and makes these required.
test/github-auth.test.ts (20 tests, all green):
- JWT contract parallels test/verify-github-app.test.ts.
- fetchInstallationToken against a Bun.serve fake that captures the
JWT in the Authorization header and returns a canned response.
- Cache hit / expiring-soon refresh / two-installation isolation /
concurrent-caller coalescing / failed-mint cleanup.
- getDefaultGitHubAuth across each missing-field permutation.
- resolveInstallationId for known + unknown + non-numeric values.
No callsites changed; the 48 reads of GITHUB_ADMIN_PAT keep working
unmodified. Phase 3 (#438) does the cutover.
📦 Package PreviewYour package is ready to test! bun install -g nemar-cli@PR442Version: 🤖 Updated on each push. |
Reviewer themes: silent fallbacks (test-analyzer + silent-failure-hunter agreed), type design polish, comment rot. Code (backend/src/services/github-auth.ts): - fetchInstallationToken now rejects empty-string `token` and already-expired `expires_at` (silent-failure / test-analyzer #3). A born-expired token would have looped the cache on every call. - New getGitHubAppConfig(env) is the single source of truth for "is the App configured". Consolidates the duplicated GITHUB_APP_ID / KEY reads, parses the installation IDs once, and emits a console.warn when a configured installation ID is malformed. Without the warn, a typo'd ID silently degrades to the PAT path with zero indication (silent-failure #2). - getDefaultGitHubAuth now throws when neither App nor a non-empty PAT is configured (silent-failure #1). Before, callers got { kind: "pat", token: "" } and the eventual GitHub call surfaced an opaque "Bad credentials" 401. - resolveInstallationId routes through getGitHubAppConfig so the App config check and install-ID lookup share one entry point. - New `EpochMs` branded type for `expiresAt`. Prevents seconds/ms mixing across the module (type-design #2). - `FetchInstallationTokenOptions` renamed to internal-only and not exported (type-design #3). - Trimmed phase/issue narrative from doc comments (comment-analyzer). Tests (test/github-auth.test.ts): - New: appId=0 boundary (matches Phase 1's coverage of the same case in the script copy). - New: empty-token rejection. - New: past-`expires_at` rejection. - New: signAppJwt failure during refresh clears the cache; recovery on next call succeeds. - New: concurrent failures don't poison the cache for a fresh caller. - New: case-sensitive org match pinned with a regression test. - New: getGitHubAppConfig (3 tests covering null, happy path, warn-on- malformed-install-id). - New: getDefaultGitHubAuth throws when neither App nor PAT configured. bindings.ts didn't need to change for this round; the four GITHUB_APP_* fields stay optional, and the consolidated validation happens at runtime via getGitHubAppConfig. 31/31 tests pass. tsc --noEmit clean at root and backend/.
Contributor
Author
Review findings addressed (commit 8deb686)Five reviews ran in parallel. Aggregated and applied; full details in commit body.
Tests: 31/31 pass. `tsc --noEmit` clean at root and `backend/`. |
neuromechanist
merged commit May 11, 2026
719b61b
into
feature/issue-432-epic-github-app
1 check passed
This was referenced May 11, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds the Worker-side GitHub App auth helper. No callsite changes — all 48 existing reads of `GITHUB_ADMIN_PAT` keep working untouched. Phase 3 (#438) will route `githubFetchWithRetry` through `GitHubAuth` and flip every site over.
New module: `backend/src/services/github-auth.ts`
`backend/src/types/bindings.ts`
Four new optional fields in the existing optional-secrets block:
Optional in this phase. Phase 5 drops PAT and makes them required.
Tests (`test/github-auth.test.ts`)
20 tests, all green:
Out of scope (deferred)
Closes #437. Part of epic #432.
Test plan