diff --git a/hub/client/application/hub/view-models/repository-show-data/index.test.ts b/hub/client/application/hub/view-models/repository-show-data/index.test.ts new file mode 100644 index 000000000..da6b82140 --- /dev/null +++ b/hub/client/application/hub/view-models/repository-show-data/index.test.ts @@ -0,0 +1,71 @@ +import { describe, expect, it } from "vitest" + +import { repositoryResponseSchema, workspaceResponseSchema } from "@application/hub/contracts" + +import { repositoryShowData } from "." + +describe("repositoryShowData()", () => { + it("uses a GitHub snapshot without joining an unrelated user by login in real data", () => { + const repository = { id: "1", nameWithOwner: "example/repository", defaultBranch: "main" } + const workspace = workspaceResponseSchema.parse({ + viewer: { id: "viewer", login: "viewer", name: null, avatarUrl: null }, + repositories: [repository], + }) + const data = repositoryResponseSchema.parse({ + repository, + prototypes: [ + { + id: "2", + repositoryId: repository.id, + name: "GitHub snapshot", + branchName: "proto/github", + description: null, + authorId: null, + authorLogin: "sam", + authorProvider: "github", + canEdit: false, + builds: [], + }, + { + id: "3", + repositoryId: repository.id, + name: "Explicit owner", + branchName: "proto/owner", + description: null, + authorId: "entra-user", + authorLogin: "sam", + authorProvider: "entra", + canEdit: false, + builds: [], + }, + ], + pagination: { page: 1, pageSize: 20, totalCount: 2, pageCount: 1 }, + authors: [ + { + id: "entra-user", + login: "sam", + name: "Entra user", + provider: "entra", + avatarUrl: "https://example.test/entra.png", + }, + ], + }) + + const { prototypes } = repositoryShowData(workspace, data, new URLSearchParams(), { + real: true, + }) + + expect.soft(prototypes.find(({ id }) => id === "2")?.author).toEqual({ + avatarUrl: null, + label: "By @sam", + ownerLabel: "@sam", + url: "https://github.com/sam", + }) + expect(prototypes.find(({ id }) => id === "3")?.author).toEqual({ + avatarUrl: "https://example.test/entra.png", + label: "By Entra user", + ownerLabel: "Entra user", + url: null, + }) + }) +}) diff --git a/hub/client/application/hub/view-models/repository-show-data/index.ts b/hub/client/application/hub/view-models/repository-show-data/index.ts index a2480a3ab..b2d95665c 100644 --- a/hub/client/application/hub/view-models/repository-show-data/index.ts +++ b/hub/client/application/hub/view-models/repository-show-data/index.ts @@ -57,9 +57,11 @@ export function repositoryShowData( const builds = [...prototype.builds].sort((first, second) => second.number - first.number) const latestBuild = prototype.latestBuild === undefined ? builds[0] : prototype.latestBuild const reviewBuild = builds.find((build) => build.previewUrl && build.desktopScreenshotUrl) - const prototypeAuthor = data.authors.find((item) => - prototype.authorId ? item.id === prototype.authorId : item.login === prototype.authorLogin, - ) + const prototypeAuthor = prototype.authorId + ? data.authors.find((item) => item.id === prototype.authorId) + : options.real + ? undefined + : data.authors.find((item) => item.login === prototype.authorLogin) const authorName = prototypeAuthor?.provider && prototypeAuthor.provider !== "github" ? (prototypeAuthor.name ?? prototypeAuthor.login) diff --git a/hub/docs/deployment.md b/hub/docs/deployment.md index a31e74433..c2617cad6 100644 --- a/hub/docs/deployment.md +++ b/hub/docs/deployment.md @@ -56,6 +56,8 @@ Runtime and setup require `DATABASE_URL`; setup accepts no additional references Versioned references make rotation explicit. An unversioned runtime/setup URL is resolved once at startup; there is no background refresh or automatic revision restart. Use the installation's serialized release procedure to load a rotated value. Source development, local image runs with directly supplied protected environment values, and the existing setup/provisioning scripts retain their original entrypoints and behavior. +The Azure entrypoint also supports the scoped [GitHub author repair and webhook checks](github-actions-builds.md#commit-authors). These maintenance targets load the same runtime secret references into their own process before importing the command. + ## Setup before release Run `npm run setup` from `hub/`, or invoke `node dist/server/scripts/setup/index.js` inside the image. Normal API and worker startup never runs setup. diff --git a/hub/docs/github-actions-builds.md b/hub/docs/github-actions-builds.md index 0a925eb2e..b6c435c11 100644 --- a/hub/docs/github-actions-builds.md +++ b/hub/docs/github-actions-builds.md @@ -27,7 +27,7 @@ Before adding `builds`, upgrade every selected workflow's Replica publish, scree Hub reads the default branch from GitHub, resolves its commit, and checks its Git tree before reading configuration at that exact revision. Neither `.github` nor `replica.json` may be a symbolic link. Incomplete directory listings stop selection rather than count as missing configuration. Hub never uses the prototype branch's configuration, so existing prototype branches need no configuration update. Webhooks only queue run IDs; import workers and recovery enforce the same validated selection. Each import job or repository poll reads a fresh selection; changing the file affects subsequent work without replacing or deleting successful Builds. -Removing a workflow stops artifact imports from it. Its next import pass marks any queued or running Build still owned by that run as failed with a configuration explanation, so it leaves the awaiting-import sweep. It does not create Builds, transfer ownership, or change successful Builds. To retry a stopped Build, select the workflow again and rerun it as a new run or attempt. +Removing a workflow stops artifact imports from it. Its next import pass marks any queued or running Build still owned by that run as failed with a configuration explanation, so it leaves the awaiting-import sweep. For a completed run, it records only the attempt's completion on an owned successful or failed Build, which ends automatic author and screenshot recovery for that attempt. It does not create Builds, transfer ownership, change artifacts or owners, or rewrite failure details. To retry a stopped Build, select the workflow again and rerun it as a new run or attempt; use the [author repair command](#commit-authors) for missing authors. An omitted file, `builds`, or `builds.workflows` keeps the legacy default. An explicit list replaces that default; include `.github/workflows/replica.yml` explicitly to select it too. Use one to ten unique paths directly under `.github/workflows/`, ending in `.yml` or `.yaml`. Filenames must start with an ASCII letter or digit and contain only letters, digits, dots, underscores, or hyphens; each complete path is limited to 255 characters. Configuration must be a regular JSON file of at most 16 KiB. Invalid configuration stops imports and discovery with an error naming the configuration file; provider failures do not select a fallback workflow. @@ -125,10 +125,30 @@ One Build exists per prototype identity and commit. Each pass is idempotent: GitHub lists named artifacts across rerun attempts. Hub prefers packages created at or after the current attempt's `run_started_at`. If that attempt has no dist package, Hub reuses an unexpired dist from an earlier attempt of the same run only when the Build already has that digest or the current attempt's screenshots name it. This supports **Re-run failed jobs** when only screenshots failed and GitHub keeps the successful publish job's artifact. During a full rerun, the current attempt has no screenshots yet, so Hub waits for that attempt's own dist instead of fixing the Build to the earlier one. If the run completes without a matching dist, the Build fails without storing the earlier package. The first successful dist import fixes the Build's digest; subsequent attempts never replace it, and screenshots attach only when their digest matches that Build. -Hub records each contributing run's completed attempt only after registration and screenshot processing finish. A transport or storage error leaves that run pending for recovery even if another run already imported the dist preview. A newer attempt still needs its own pass; delayed completion from an older attempt cannot undo a newer receipt. Owning-run completion and immutable dist ownership remain unchanged. +Hub records each contributing run's completed attempt only after author resolution, registration, and screenshot processing finish. A provider, transport, or storage error leaves that run pending for recovery even if the dist preview is already available. The awaiting-import retry and workflow discovery both retry failed Builds without an author or completed watermark, without reopening the Build or replacing its failure details. A deselected completed run settles that attempt, so it leaves both scans; reselection needs a new run or attempt, or the repair command. The existing deselected-workflow failure summary distinguishes a stopped unfinished Build, so keep its stored text stable. A newer attempt still needs its own pass; delayed completion from an older attempt cannot undo a newer receipt. Owning-run completion and immutable dist ownership remain unchanged. Storage keys derive from the Build, file path, and content. Retries reuse identical objects, a key with different bytes stops the import, and keys are never deleted. Unreferenced keys left by an interrupted import are harmless. +### Commit authors + +Hub resolves the linked GitHub account from the imported commit SHA, not the workflow actor or free-text commit name/email. Builds retain that provider identity and login. Imports and repairs do not create Hub users or assign or replace Prototype owners: commit attribution is not authenticated ownership. Without an owner, the index and detail header use the current Build's GitHub author snapshot (or the latest Build when none is current), and index search uses that same visible author. Bots remain bot accounts, and a commit response without a linked account remains unknown with a warning. Failed commit lookups, including HTTP 404 or 422, are reported and retried instead of being treated as an unlinked author. Available artifacts still import; recovery can requeue eligible Builds while their completed import pass remains unrecorded. Later passes reuse an existing GitHub identity without another commit lookup. + +Builds whose completed import pass was already recorded are not automatically revisited solely for missing author metadata. From `hub/`, use the existing configured App and database connection to inspect one retained Build: + +```sh +npm run github:repair-author -- --repository-id GITHUB_NUMERIC_REPOSITORY_ID --build-id BUILD_UUID +``` + +The default only resolves the saved commit. An `available` result shows the proposed author, whether the Build metadata would update, remain unchanged, or be preserved, and that the Prototype owner stays unchanged. It does not write. After approving the target, add `--apply` to fill missing Build identity metadata. Repeated application returns `unchanged`; an existing different or incomplete author snapshot returns `preserved`. The write rechecks the connected repository, Build, commit, and run after the provider lookup, so concurrent changes may alter the result. No Prototype owner, artifacts, Build state, workflow ownership, or current selection are replaced. This command can repair older retained Builds outside the polling window. Applying it to production requires separate approval; do not paste credentials into arguments. + +For a built image with runtime secrets already supplied, use `npm run start:github:repair-author --` with the same arguments. For an Azure installation that loads Key Vault secrets at startup, use: + +```sh +node dist/server/scripts/azure-entrypoint/index.js repair-github-build-author --repository-id GITHUB_NUMERIC_REPOSITORY_ID --build-id BUILD_UUID +``` + +The Azure command loads the existing runtime secret references in the maintenance process before running the repair. It does not change the running Hub process or its configuration. + ## Automatic prototype registration A successful imported Build on a branch starting with `proto/` automatically registers that branch's Prototype. The rule is case-sensitive and includes nested names such as `proto/settings/mobile`. Both webhook imports and recovery use the same registration path. Queued or failed Builds do not register a new Prototype. @@ -147,7 +167,7 @@ Replacement histories reject workflow runs created at or before the recorded del Deletion events are still recorded when received, rather than confirmed synchronously against GitHub. A delayed delete can temporarily retire a live branch; the next poll checks continuity before resuming the latest identity. A delete received after restoration causes another such check. When only a SHA-less `delete` event arrives, continuity is lost, but every retained relationship stays on the retired identity. Deletion events missed beyond the recovery window cannot establish a lifecycle boundary. A replacement's intermediate workflow runs that predate a delayed deletion receipt are conservatively skipped unless their commit is its confirmed current head; re-running the full workflow on the current head remains safe. -Gallery and prototype-list queries are unchanged. `registeredAt !== null` remains the registration contract, alongside their existing repository, branch, and visibility checks. +`registeredAt !== null` remains the registration contract, alongside existing repository, branch, and visibility checks. Gallery search also includes the displayed Build author when the Prototype has no owner. ## Artifact contracts @@ -169,7 +189,7 @@ GitHub does not retry failed webhook deliveries, so scheduled jobs recover misse | ---------------- | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Every minute | `recover-github-webhook-deliveries` | Scans the App's recent webhook deliveries from a saved checkpoint and asks GitHub to redeliver workflow, installation, and access events that Hub never recorded. | | Every 5 minutes | `discover-github-workflow-runs` | Polls branch heads and recent Replica workflow runs for connected repositories and enqueues unsettled runs, so Builds arrive even without webhooks. | -| Every 5 minutes | `retry-github-builds-awaiting-import` | Re-enqueues recent GitHub Builds whose dist import or final screenshot pass is still owed. | +| Every 5 minutes | `retry-github-builds-awaiting-import` | Re-enqueues queued or running Builds, successful Builds whose final author or screenshot pass is still owed, and failed Builds whose author lookup is still owed. | | Every 15 minutes | `reconcile-github-installation` | Refreshes the installation's repository list. | Webhook redelivery covers deliveries from the past three days, the limit of GitHub's delivery history. It retries unrecorded deliveries with status 0, 401, 403, 404, 429, or 5xx, including access-removal events rejected during an authentication or routing outage. A lease lets one worker run at a time. Each missed delivery is retried at most 12 times, with backoff from one minute to one hour, and settled records are removed after 30 days. Redelivery cannot help while Hub is unreachable; discovery and retry then import the Build once Hub is back, as long as its artifact has not expired. @@ -178,8 +198,32 @@ Discovery and retry look back three days, matching the artifacts' retention. Run Branch polling selects a matching existing Build when a branch returns to an earlier commit; it does not invent a Build without a workflow package or delete branches. Poll observations do not replace workflow creation times or rewrite an unchanged prototype selection. Reselection retains the latest workflow timestamp and run ID so a known run's redelivery cannot undo it, while a new run can still become current. When an older installation has a selection timestamp but no saved run ID, runs with the same timestamp do not replace the selection because their order is unknown; a later timestamp can still select a Build. -Each poll bounds branch listing to ten pages of 100. Workflow discovery queries each selected workflow and removes duplicate run IDs. It pages through recent runs within the recovery window, up to GitHub's 1,000-result filtered-search limit per workflow; reaching that limit reports an error instead of silently accepting a truncated list. A provider failure for any selected workflow rejects that repository's discovery pass rather than returning a partial list. Saved unfinished Builds are retried separately. A very busy repository can therefore outrun discovery of previously unseen runs; recovery is a safety net, not an unlimited delivery guarantee. Runs that reuse an immutable Build or have lost ownership to a newer run do not keep queuing redundant imports. Webhook imports and polling share immutable Build ownership, so whichever valid dist import commits first wins. +Each poll bounds branch listing to ten pages of 100. Workflow discovery queries each selected workflow and removes duplicate run IDs. It pages through recent runs within the recovery window, up to GitHub's 1,000-result filtered-search limit per workflow; reaching that limit reports an error instead of silently accepting a truncated list. A provider failure for any selected workflow rejects that repository's discovery pass rather than returning a partial list. Saved queued, running, incomplete successful, and author-pending failed Builds are retried separately, so they do not depend on discovery's limits. A very busy repository can therefore outrun discovery of previously unseen runs; recovery is a safety net, not an unlimited delivery guarantee. Runs that reuse an immutable Build or have lost ownership to a newer run do not keep queuing redundant imports. Webhook imports and polling share immutable Build ownership, so whichever valid dist import commits first wins. + +### Verify webhook communication + +From `hub/`, using an existing authorized runtime environment: + +```sh +npm run github:check-webhooks -- --repository-id GITHUB_NUMERIC_REPOSITORY_ID --run-id WORKFLOW_RUN_ID +``` + +For a built image with runtime secrets already supplied, use `npm run start:github:check-webhooks --` with the same arguments. For an Azure installation using runtime Key Vault references, use: + +```sh +node dist/server/scripts/azure-entrypoint/index.js check-github-webhooks --repository-id GITHUB_NUMERIC_REPOSITORY_ID --run-id WORKFLOW_RUN_ID +``` + +This command checks App JWT signing/identity, installation access, repository-scoped Actions/Contents token access, the exact webhook URL (`HUB_APPLICATION_ORIGIN/api/github/webhooks`), JSON/TLS settings, required event subscriptions, and App delivery-history reads. It issues a short-lived installation token but does not send an event, request redelivery, change settings, or write database records. It never outputs tokens, the webhook secret, response payloads, or configured URLs. + +App delivery-history endpoints use an **App JWT**, not an installation token. A successful artifact download therefore does not prove delivery-recovery access. Recovery errors distinguish HTTP rejection (including the status), transport failure, and an invalid successful response; none is silently treated as success. HTTP 401 requires checking App ID/key validity and clock, while 403 requires checking App authority and provider policy. Those statuses are evidence to investigate, not proof of one particular configuration fault. + +Delivery IDs are positive int64 values and can exceed JavaScript's safe integer range. Hub preserves those JSON integer tokens as decimal strings through database storage and redelivery URLs, rather than rounding them. A schema rejection on a successful delivery-history response is not an authentication failure. + +To inspect a real delivery, obtain its GUID from this repository's GitHub App delivery history and add `--delivery-guid DELIVERY_UUID`. The command only reads receipts after finding that GUID for the selected installation and repository in the latest 100 App deliveries. It uses a read-only database transaction with a five-second statement timeout. Missing or older provider history is `unknown`, not a pass. A receipt proves Hub accepted the signature and handled/enqueued the event, not that import finished. The separate repository/run Build check requires completion evidence for the attempt currently reported by GitHub, including runs that contributed to another run's Build; polling can also have processed that run. Neither check associates the delivery with the supplied run. A `succeeded` recovery record may be settled from an existing receipt or provider delivery, so it does not prove a redelivery occurred. + +For end-to-end testing, use a dedicated test installation/repository and a normal signed `workflow_run` or `workflow_job` event from its prototype workflow. Verify the GUID receipt, Build commit/run/artifact linkage, and any recovery state. GitHub's **Redeliver** action sends a real event and can enqueue imports; it needs separate production approval. A webhook ping alone does not establish workflow import or recovery. Local route and recovery tests cover signature rejection, queue failure, receipts, and redelivery without production traffic. ## Not yet implemented -Workflow-installation UI, Build start pages and authorship display, and the templates API are later increments. +Workflow-installation UI, Build start pages, and the templates API are later increments. diff --git a/hub/docs/share-links.md b/hub/docs/share-links.md index d69b0c55c..d385a6acd 100644 --- a/hub/docs/share-links.md +++ b/hub/docs/share-links.md @@ -15,9 +15,9 @@ Hosted public sharing is off by default. It requires configured Build storage an The prototype's **Share links** action requires a GitHub session and one of these permissions: - GitHub currently reports repository administrator permission. -- At least one successful Build for this prototype records the signed-in GitHub identity as its commit author, and GitHub currently reports at least read access. +- The signed-in user is the prototype's stored owner, at least one successful Build records that GitHub identity as its commit author, and GitHub currently reports at least read access. -Hub matches the recorded provider, issuer, and immutable GitHub user ID, not the login. Repository write access, a Hub administrator role, or an Entra session does not substitute for either rule. The API rechecks permission for each list, create, and revoke request. The browser lists only active hosted links. +Hub matches the recorded provider, issuer, and immutable GitHub user ID, not the login. Commit attribution alone does not assign ownership or authorize sharing. Ownerless imported prototypes therefore require repository administrator permission. Existing owners are retained; the schema does not independently record or re-attest the source of historical owner assignments. Repository write access, a Hub administrator role, or an Entra session does not substitute for either rule. The API rechecks permission for each list, create, and revoke request. The browser lists only active hosted links. An explicit `none` result for the expected GitHub identity permanently revokes only that creator's links in that repository. Confirmed GitHub App disconnection revokes all hosted links for the affected repositories. Unavailable or ambiguous evidence denies management without revoking links. Public visits do not call GitHub, and some webhook events narrow signed-in access without proving a creator lost all access. Sharing does not promise immediate detection of creator access loss. diff --git a/hub/package.json b/hub/package.json index 9c60c1027..50a0b1ca3 100644 --- a/hub/package.json +++ b/hub/package.json @@ -20,6 +20,8 @@ "local:builds": "node server/scripts/run-local-builds/index.ts", "local:builds:enqueue": "node server/scripts/enqueue-local-builds/index.ts", "local:worker": "node server/scripts/start-local-worker/index.ts", + "github:repair-author": "node server/scripts/repair-github-build-author/index.ts", + "github:check-webhooks": "node server/scripts/check-github-webhooks/index.ts", "db:studio": "prisma studio", "jobs:setup": "node server/scripts/setup-jobs/index.ts", "setup": "node server/scripts/setup/index.ts", @@ -30,6 +32,8 @@ "start:worker:scheduled": "node dist/server/workers/index.js scheduled", "start:jobs:setup": "node dist/server/scripts/setup-jobs/index.js", "start:setup": "node dist/server/scripts/setup/index.js", + "start:github:repair-author": "node dist/server/scripts/repair-github-build-author/index.js", + "start:github:check-webhooks": "node dist/server/scripts/check-github-webhooks/index.js", "controls:build": "node script/build-controls-bridge.mjs", "controls:assets:check": "node script/build-controls-bridge.mjs --check", "controls:test": "node --test script/tests/build-controls.test.mjs", diff --git a/hub/server/domains/github-builds/discover-github-workflow-runs/index.ts b/hub/server/domains/github-builds/discover-github-workflow-runs/index.ts index e7a0aea7a..59e1f4bc4 100644 --- a/hub/server/domains/github-builds/discover-github-workflow-runs/index.ts +++ b/hub/server/domains/github-builds/discover-github-workflow-runs/index.ts @@ -5,6 +5,7 @@ import type { GitHubWorkflowRun } from "../../../integrations/github/create-gith import { listGitHubBuildRuns } from "../../../repositories/github-builds/list-github-build-runs/index.ts" import { listGitHubBranchIdentities } from "../../../repositories/prototypes/list-github-branch-identities/index.ts" import { listConnectedGitHubRepositories } from "../../../repositories/repository-access/list-connected-github-repositories/index.ts" +import { githubDeselectedWorkflowFailure } from "../../../utils/github-deselected-workflow-failure/index.ts" import type { GitHubWorkflowRunJob } from "../get-github-workflow-run-job-definition/index.ts" import { refreshGitHubBranchHeads } from "../refresh-github-branch-heads/index.ts" @@ -22,8 +23,9 @@ export interface DiscoverGitHubWorkflowRunsDependencies { /** * A run is settled once its Build can no longer change: a successful Build whose - * completed-run pass finished, a superseded Build, or a failure that this attempt - * cannot reopen. Everything else still needs an import pass. + * completed-run pass finished, a failed Build without unresolved attribution, a + * deselected or superseded Build, or an older attempt that cannot reopen its Build. + * Everything else still needs an import pass. */ function settled(run: GitHubWorkflowRun, saved: SavedRun[]) { return saved.some((build) => { @@ -37,10 +39,17 @@ function settled(run: GitHubWorkflowRun, saved: SavedRun[]) { } return build.runId !== null && build.runId > run.id } + if ( + build.state === "failed" && + (build.authorProvider !== null || build.failureSummary === githubDeselectedWorkflowFailure) + ) { + return build.runAttempt >= run.runAttempt + } return ( - build.runAttempt >= run.runAttempt && - ((build.state === "successful" && build.workflowCompletedAt !== null) || - build.state === "failed") + build.runAttempt > run.runAttempt || + (build.runAttempt === run.runAttempt && + (build.state === "successful" || build.state === "failed") && + build.workflowCompletedAt !== null) ) }) } diff --git a/hub/server/domains/github-builds/errors.ts b/hub/server/domains/github-builds/errors.ts new file mode 100644 index 000000000..8935a951e --- /dev/null +++ b/hub/server/domains/github-builds/errors.ts @@ -0,0 +1,6 @@ +export class GitHubBuildAuthorTargetError extends Error { + constructor() { + super("No GitHub Build matches that repository and Build ID.") + this.name = "GitHubBuildAuthorTargetError" + } +} diff --git a/hub/server/domains/github-builds/import-github-workflow-run/index.db.test.ts b/hub/server/domains/github-builds/import-github-workflow-run/index.db.test.ts index 6aa4a692e..70842b505 100644 --- a/hub/server/domains/github-builds/import-github-workflow-run/index.db.test.ts +++ b/hub/server/domains/github-builds/import-github-workflow-run/index.db.test.ts @@ -2,23 +2,24 @@ import { createHash, randomInt, randomUUID } from "node:crypto" import { describe, expect, it, vi } from "vitest" -import { prisma } from "../../../integrations/prisma/index.ts" +import { prisma } from "../../../integrations/prisma" +import { GitHubCommitLookupError } from "../../../integrations/github/errors" import type { GitHubWorkflowArtifact, GitHubWorkflowRun, -} from "../../../integrations/github/create-github-actions-client/types.ts" -import { findBuildArtifact } from "../../../repositories/build-artifacts/find-build-artifact/index.ts" +} from "../../../integrations/github/create-github-actions-client/types" +import { findBuildArtifact } from "../../../repositories/build-artifacts/find-build-artifact" import { listGitHubBuildsAwaitingImport } from "../../../repositories/github-builds/list-github-builds-awaiting-import" import { createGitHubWebhookRouteDependencies } from "../../../routes/api/github/webhooks/create-github-webhook-route-dependencies" -import { createZipArchive } from "../../../test/create-zip-archive/index.ts" -import type { ArtifactStorage } from "../../artifacts/create-artifact-storage/types.ts" -import { extractDistPackage } from "../../build-artifacts/extract-dist-package/index.ts" -import { readBuildPreviewFile } from "../../build-preview/read-build-preview-file/index.ts" -import { distArtifactName, screenshotArtifactName } from "../constants.ts" +import { createZipArchive } from "../../../test/create-zip-archive" +import type { ArtifactStorage } from "../../artifacts/create-artifact-storage/types" +import { extractDistPackage } from "../../build-artifacts/extract-dist-package" +import { readBuildPreviewFile } from "../../build-preview/read-build-preview-file" +import { distArtifactName, screenshotArtifactName } from "../constants" import { discoverGitHubWorkflowRuns } from "../discover-github-workflow-runs" import type { GitHubWorkflowRunJob } from "../get-github-workflow-run-job-definition" import { refreshGitHubBranchHeads } from "../refresh-github-branch-heads" -import { importGitHubWorkflowRun } from "./index.ts" +import { importGitHubWorkflowRun } from "." const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 1]) const logger = { info() {}, warn() {} } @@ -64,6 +65,7 @@ async function createFixture() { defaultBranch: "main", }, }) + const storage = createImmutableStorage() const run: GitHubWorkflowRun = { @@ -87,6 +89,13 @@ async function createFixture() { const client = { getBuildWorkflowPaths: () => Promise.resolve([...workflowPaths]), getWorkflowRun: (): Promise => Promise.resolve({ ...run }), + getCommitAuthor: vi + .fn< + ( + commitSha: string, + ) => Promise<{ providerUserId: string; login: string; avatarUrl: string | null } | null> + >() + .mockResolvedValue(null), listRunArtifacts: (_runId: number, name: string) => Promise.resolve( packages.has(name) @@ -112,9 +121,9 @@ async function createFixture() { function importRun(job = { githubRepositoryId, runId: run.id }) { return importGitHubWorkflowRun({ database: prisma, storage, client, logger }, job) } - function findBuild() { + function findBuild(buildId?: string) { return prisma.build.findFirstOrThrow({ - where: { prototype: { repository: { githubRepositoryId } } }, + where: { id: buildId, prototype: { repository: { githubRepositoryId } } }, include: { artifacts: { orderBy: { path: "asc" } }, prototype: true }, }) } @@ -379,7 +388,511 @@ describe("importGitHubWorkflowRun", () => { authorLogin: null, authorName: null, authorAvatarUrl: null, + prototype: { authorId: null }, + }) + expect(await prisma.user.count()).toBe(0) + expect(fixture.client.getCommitAuthor).toHaveBeenCalledWith(fixture.run.headSha) + }) + + it("records the GitHub commit author without assigning the mapped user as prototype owner", async () => { + const fixture = await createFixture() + const author = await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "101" }, + }, + }, + include: { profile: true }, + }) + await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "9" }, + }, + }, + }) + fixture.client.getCommitAuthor.mockResolvedValue({ + providerUserId: "101", + login: "commit-author", + avatarUrl: "https://avatars.githubusercontent.com/u/101", + }) + + await fixture.importRun() + + expect(author.profile).toBeNull() + expect(fixture.client.getCommitAuthor).toHaveBeenCalledWith(fixture.run.headSha) + expect(await fixture.findBuild()).toMatchObject({ + commitSha: fixture.run.headSha, + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "101", + authorLogin: "commit-author", + authorName: null, + authorAvatarUrl: "https://avatars.githubusercontent.com/u/101", + prototype: { authorId: null }, + }) + expect(await prisma.user.count()).toBe(2) + expect(await prisma.externalIdentity.count()).toBe(2) + }) + + it("keeps an unmapped author's provider snapshot without creating a Hub identity", async () => { + const fixture = await createFixture() + const unrelated = await prisma.user.create({ + data: { + externalIdentities: { + create: [ + { provider: "github", issuer: "https://github.example.test", subject: "101" }, + { provider: "entra", issuer: "https://github.com", subject: "101" }, + ], + }, + }, + }) + fixture.client.getCommitAuthor.mockResolvedValue({ + providerUserId: "101", + login: "unmapped-author", + avatarUrl: null, + }) + + await fixture.importRun() + + expect(await fixture.findBuild()).toMatchObject({ + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "101", + authorLogin: "unmapped-author", + authorAvatarUrl: null, + prototype: { authorId: null }, + }) + expect(await prisma.user.findMany({ select: { id: true } })).toEqual([{ id: unrelated.id }]) + expect(await prisma.externalIdentity.count()).toBe(2) + }) + + it("preserves a legacy author snapshot instead of mixing in a different identity on import", async () => { + const fixture = await createFixture() + await fixture.importRun() + const build = await fixture.findBuild() + const snapshot = { + authorLogin: "legacy-author", + authorName: "Legacy author", + authorAvatarUrl: "https://example.test/legacy.png", + } + await prisma.build.update({ where: { id: build.id }, data: snapshot }) + await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "101" }, + }, + }, + }) + fixture.client.getCommitAuthor.mockClear().mockResolvedValue({ + providerUserId: "101", + login: "different-author", + avatarUrl: "https://example.test/different.png", + }) + + await fixture.importRun() + + expect(fixture.client.getCommitAuthor).toHaveBeenCalledWith(fixture.run.headSha) + expect(await fixture.findBuild()).toMatchObject({ + authorProvider: null, + authorIssuer: null, + authorProviderUserId: null, + ...snapshot, + prototype: { authorId: null }, + }) + expect(await prisma.user.count()).toBe(1) + }) + + it("preserves an explicit prototype owner while recording a different commit author", async () => { + const fixture = await createFixture() + await fixture.importRun() + const original = await fixture.findBuild() + const owner = await prisma.user.create({ data: {} }) + await prisma.prototype.update({ + where: { id: original.prototypeId }, + data: { authorId: owner.id }, + }) + await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "101" }, + }, + }, + }) + fixture.run.headSha = "b".repeat(40) + fixture.run.id += 1 + fixture.run.createdAt = new Date("2026-01-01T00:02:00Z") + fixture.client.getCommitAuthor.mockResolvedValue({ + providerUserId: "101", + login: "different-author", + avatarUrl: null, + }) + + await fixture.importRun() + + const build = await prisma.build.findUniqueOrThrow({ + where: { + prototypeId_commitSha: { + prototypeId: original.prototypeId, + commitSha: fixture.run.headSha, + }, + }, + include: { prototype: true }, + }) + expect(build).toMatchObject({ + authorProviderUserId: "101", + authorLogin: "different-author", + prototype: { authorId: owner.id }, + }) + }) + + it("retries a failed author lookup instead of completing the import with unknown attribution", async () => { + const fixture = await createFixture() + fixture.packages.set(distArtifactName, distPackage()) + fixture.client.getCommitAuthor + .mockRejectedValueOnce(new Error("Commit lookup unavailable")) + .mockResolvedValue({ + providerUserId: "101", + login: "commit-author", + avatarUrl: null, + }) + + await expect(fixture.importRun()).rejects.toThrow("Commit lookup unavailable") + await expect(fixture.importRun()).resolves.toMatchObject({ dist: "imported" }) + + expect(await fixture.findBuild()).toMatchObject({ + state: "successful", + authorProviderUserId: "101", + authorLogin: "commit-author", + }) + expect(await prisma.build.count()).toBe(1) + }) + + it("imports available artifacts while a failed author lookup remains observable and retryable", async () => { + const fixture = await createFixture() + fixture.run.headBranch = "proto/welcome" + fixture.run.status = "completed" + fixture.run.conclusion = "success" + const warn = vi.spyOn(logger, "warn") + const dist = distPackage() + fixture.packages.set(distArtifactName, dist) + fixture.packages.set( + screenshotArtifactName, + screenshotPackage(extractDistPackage(dist).distDigest), + ) + fixture.client.getCommitAuthor + .mockRejectedValueOnce(new GitHubCommitLookupError(422)) + .mockResolvedValue({ providerUserId: "101", login: "commit-author", avatarUrl: null }) + + await expect(fixture.importRun()).rejects.toThrow("GitHub commit lookup failed (HTTP 422).") + const imported = await fixture.findBuild() + expect(imported).toMatchObject({ + state: "successful", + authorProviderUserId: null, + workflowCompletedAt: null, + prototype: { authorId: null }, + }) + expect(imported.prototype.registeredAt).toBeInstanceOf(Date) + expect(imported.artifacts.map((artifact) => artifact.path)).toEqual([ + "dist/assets/app.js", + "dist/index.html", + "screenshots/desktop.png", + ]) + expect(warn).toHaveBeenCalledWith( + { + buildId: imported.id, + runId: fixture.run.id, + errorType: "GitHubCommitLookupError", + statusCode: 422, + }, + "GitHub commit author lookup failed; artifact import will continue and retry", + ) + const retry = { githubRepositoryId: fixture.run.repositoryId, runId: fixture.run.id } + expect( + await listGitHubBuildsAwaitingImport(prisma, { createdAfter: new Date(0) }), + ).toContainEqual(retry) + + await expect(fixture.importRun()).resolves.toEqual({ + dist: "unchanged", + screenshots: "attached", + }) + const repaired = await fixture.findBuild() + expect(repaired).toMatchObject({ + id: imported.id, + authorProviderUserId: "101", + workflowCompletedAt: fixture.run.updatedAt, + }) + expect(repaired.artifacts).toEqual(imported.artifacts) + expect( + await listGitHubBuildsAwaitingImport(prisma, { createdAfter: new Date(0) }), + ).not.toContainEqual(retry) + }) + + it.each([ + { + dist: "missing", + failureSummary: `GitHub Actions did not provide the ${distArtifactName} artifact.`, + }, + { dist: "invalid", failureSummary: "The package does not contain dist/index.html." }, + ])( + "recovers author resolution for a failed Build with $dist dist without reopening it", + async ({ dist, failureSummary }) => { + const fixture = await createFixture() + fixture.run.headBranch = "proto/recover-author" + fixture.run.status = "completed" + fixture.run.conclusion = "success" + if (dist === "invalid") { + fixture.packages.set(distArtifactName, createZipArchive([{ name: "dist/app.js" }])) + } + const owner = await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "101" }, + }, + }, + }) + const author = { + providerUserId: "101", + login: "commit-author", + avatarUrl: "https://example.test/author.png", + } + fixture.client.getCommitAuthor + .mockRejectedValueOnce(new GitHubCommitLookupError(503)) + .mockRejectedValueOnce(new GitHubCommitLookupError(503)) + .mockResolvedValue(author) + const job = { githubRepositoryId: fixture.run.repositoryId, runId: fixture.run.id } + const createdAfter = new Date(0) + async function recovery() { + return { + awaiting: await listGitHubBuildsAwaitingImport(prisma, { createdAfter }), + discovered: await discoverGitHubWorkflowRuns( + { + database: prisma, + listWorkflowRuns: (repositoryId, since) => + Promise.resolve( + repositoryId === fixture.run.repositoryId && + fixture.run.createdAt >= since && + fixture.workflowPaths.includes(fixture.run.path) + ? [fixture.run] + : [], + ), + listBranchHeads: () => Promise.resolve([]), + isAncestor: () => Promise.resolve(false), + logger, + }, + { createdAfter }, + ), + } + } + + await expect(fixture.importRun()).rejects.toThrow("GitHub commit lookup failed (HTTP 503).") + const imported = await fixture.findBuild() + await prisma.prototype.update({ + where: { id: imported.prototypeId }, + data: { authorId: owner.id }, + }) + const failed = await fixture.findBuild() + expect(failed).toMatchObject({ + state: "failed", + failureSummary, + authorProviderUserId: null, + githubWorkflowRunId: BigInt(fixture.run.id), + githubWorkflowRunAttempt: fixture.run.runAttempt, + workflowCompletedAt: null, + distDigest: null, + artifacts: [], + prototype: { authorId: owner.id, registeredAt: null }, + }) + expect(failed.startedAt).toBeInstanceOf(Date) + expect(failed.finishedAt).toBeInstanceOf(Date) + expect(await recovery()).toEqual({ awaiting: [job], discovered: [job] }) + + fixture.packages.set(distArtifactName, distPackage("

Late dist

")) + await expect(fixture.importRun()).rejects.toThrow("GitHub commit lookup failed (HTTP 503).") + expect(await fixture.findBuild()).toMatchObject({ + id: failed.id, + state: "failed", + failureSummary, + startedAt: failed.startedAt, + finishedAt: failed.finishedAt, + githubWorkflowRunId: failed.githubWorkflowRunId, + githubWorkflowRunAttempt: failed.githubWorkflowRunAttempt, + distDigest: null, + artifacts: [], + }) + expect(await recovery()).toEqual({ awaiting: [job], discovered: [job] }) + + await expect(fixture.importRun()).resolves.toMatchObject({ dist: "unchanged" }) + expect(await fixture.findBuild()).toMatchObject({ + id: failed.id, + state: "failed", + failureSummary, + startedAt: failed.startedAt, + finishedAt: failed.finishedAt, + githubWorkflowRunId: failed.githubWorkflowRunId, + githubWorkflowRunAttempt: failed.githubWorkflowRunAttempt, + workflowCompletedAt: fixture.run.updatedAt, + distDigest: null, + artifacts: [], + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: author.providerUserId, + authorLogin: author.login, + authorAvatarUrl: author.avatarUrl, + prototype: { authorId: owner.id, registeredAt: null, currentBuildId: failed.id }, + }) + expect(fixture.client.getCommitAuthor).toHaveBeenCalledTimes(3) + expect(await fixture.findAnalyticsEvents()).toEqual([]) + expect(await recovery()).toEqual({ awaiting: [], discovered: [] }) + }, + ) + + it("does not let a delayed failed-run author lookup overwrite a newer successful attempt", async () => { + const fixture = await createFixture() + fixture.run.status = "completed" + fixture.run.conclusion = "success" + const author = { providerUserId: "101", login: "commit-author", avatarUrl: null } + fixture.client.getCommitAuthor.mockRejectedValueOnce(new GitHubCommitLookupError(503)) + await expect(fixture.importRun()).rejects.toThrow("GitHub commit lookup failed (HTTP 503).") + const failed = await fixture.findBuild() + + let newer: Awaited> | undefined + fixture.client.getCommitAuthor + .mockImplementationOnce(async () => { + fixture.run.runAttempt = 2 + fixture.run.updatedAt = new Date("2026-01-01T00:05:00Z") + fixture.packages.set(distArtifactName, distPackage("

New attempt

")) + await fixture.importRun() + newer = await fixture.findBuild() + return author + }) + .mockResolvedValue(author) + + await fixture.importRun() + + expect(newer).toMatchObject({ + id: failed.id, + state: "successful", + githubWorkflowRunAttempt: 2, + workflowCompletedAt: fixture.run.updatedAt, + authorProviderUserId: author.providerUserId, }) + expect(newer?.artifacts.length).toBeGreaterThan(0) + expect(await fixture.findBuild()).toEqual(newer) + }) + + it("finishes late screenshots without looking up an already saved GitHub identity", async () => { + const fixture = await createFixture() + fixture.packages.set(distArtifactName, distPackage()) + fixture.client.getCommitAuthor.mockResolvedValue({ + providerUserId: "101", + login: "commit-author", + avatarUrl: "https://example.test/author.png", + }) + await fixture.importRun() + const before = await fixture.findBuild() + expect(before).toMatchObject({ + state: "successful", + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "101", + authorLogin: "commit-author", + prototype: { authorId: null }, + }) + await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "101" }, + }, + }, + }) + + fixture.run.status = "completed" + fixture.run.conclusion = "success" + fixture.run.updatedAt = new Date("2026-01-01T00:05:00Z") + fixture.packages.set(screenshotArtifactName, screenshotPackage(before.distDigest!)) + fixture.client.getCommitAuthor + .mockClear() + .mockRejectedValue(new Error("Commit lookup unavailable")) + + await expect(fixture.importRun()).resolves.toEqual({ + dist: "unchanged", + screenshots: "attached", + }) + + expect(fixture.client.getCommitAuthor).not.toHaveBeenCalled() + const after = await fixture.findBuild() + expect(after).toMatchObject({ + id: before.id, + authorProvider: before.authorProvider, + authorIssuer: before.authorIssuer, + authorProviderUserId: before.authorProviderUserId, + authorLogin: before.authorLogin, + authorAvatarUrl: before.authorAvatarUrl, + workflowCompletedAt: fixture.run.updatedAt, + prototype: { authorId: null }, + }) + expect(after.artifacts.map((artifact) => artifact.path)).toContain("screenshots/desktop.png") + expect(await prisma.user.count()).toBe(1) + }) + + it("retries missing author identity without assigning the mapped user as prototype owner", async () => { + const fixture = await createFixture() + fixture.packages.set(distArtifactName, distPackage()) + await fixture.importRun() + const before = await fixture.findBuild() + expect(before).toMatchObject({ + state: "successful", + authorProviderUserId: null, + prototype: { authorId: null }, + }) + expect(await prisma.user.count()).toBe(0) + + fixture.run.status = "completed" + fixture.run.conclusion = "success" + fixture.run.updatedAt = new Date("2026-01-01T00:05:00Z") + fixture.packages.set(screenshotArtifactName, screenshotPackage(before.distDigest!)) + fixture.client.getCommitAuthor + .mockClear() + .mockRejectedValueOnce(new Error("Commit lookup unavailable")) + + await expect(fixture.importRun()).rejects.toThrow("Commit lookup unavailable") + expect(fixture.client.getCommitAuthor).toHaveBeenCalledWith(fixture.run.headSha) + expect(await fixture.findBuild()).toMatchObject({ + authorProviderUserId: null, + workflowCompletedAt: null, + prototype: { authorId: null }, + }) + expect(await prisma.user.count()).toBe(0) + + await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "101" }, + }, + }, + }) + fixture.client.getCommitAuthor.mockClear().mockResolvedValue({ + providerUserId: "101", + login: "commit-author", + avatarUrl: null, + }) + + await expect(fixture.importRun()).resolves.toEqual({ + dist: "unchanged", + screenshots: "attached", + }) + expect(fixture.client.getCommitAuthor).toHaveBeenCalledWith(fixture.run.headSha) + expect(await fixture.findBuild()).toMatchObject({ + id: before.id, + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "101", + authorLogin: "commit-author", + workflowCompletedAt: fixture.run.updatedAt, + prototype: { authorId: null }, + }) + expect(await prisma.user.count()).toBe(1) }) it("attaches late screenshots only when they depict the imported dist", async () => { @@ -1060,6 +1573,7 @@ describe("importGitHubWorkflowRun", () => { fixture.run.runAttempt = observedAttempt fixture.run.updatedAt = new Date("2026-01-01T00:05:00Z") fixture.packages.set(distArtifactName, distPackage()) + fixture.client.getCommitAuthor.mockClear() const listArtifacts = vi.spyOn(fixture.client, "listRunArtifacts") const download = vi.spyOn(fixture.client, "downloadArtifact") const write = vi.spyOn(fixture.storage, "write") @@ -1085,12 +1599,24 @@ describe("importGitHubWorkflowRun", () => { expect(stopped.failureSummary).toMatch(/default branch/i) expect(stopped.failureSummary).toContain("builds.workflows") expect(await prisma.branchHeadObservation.findMany()).toEqual(observations) + expect(fixture.client.getCommitAuthor).not.toHaveBeenCalled() expect(listArtifacts).not.toHaveBeenCalled() expect(download).not.toHaveBeenCalled() expect(write).not.toHaveBeenCalled() - await expect(listGitHubBuildsAwaitingImport(prisma, { createdAfter })).resolves.toEqual([]) + const awaiting = await listGitHubBuildsAwaitingImport(prisma, { createdAfter }) fixture.workflowPaths.push(".github/workflows/replica.yml") + const discovered = await discoverGitHubWorkflowRuns( + { + database: prisma, + listWorkflowRuns: () => Promise.resolve([fixture.run]), + listBranchHeads: () => Promise.resolve([]), + isAncestor: () => Promise.resolve(false), + logger, + }, + { createdAfter }, + ) + expect({ awaiting, discovered }).toEqual({ awaiting: [], discovered: [] }) await fixture.importRun() expect(await fixture.findBuild()).toMatchObject({ state: "failed", @@ -1194,6 +1720,29 @@ describe("importGitHubWorkflowRun", () => { expect(listArtifacts).not.toHaveBeenCalled() }) + it("settles a deselected Build awaiting its author without changing its import or owner", async () => { + const fixture = await createFixture() + fixture.run.status = "completed" + fixture.run.conclusion = "success" + fixture.packages.set(distArtifactName, distPackage()) + fixture.client.getCommitAuthor.mockRejectedValue(new GitHubCommitLookupError(503)) + await expect(fixture.importRun()).rejects.toThrow("GitHub commit lookup failed (HTTP 503).") + const before = await fixture.findBuild() + const createdAfter = new Date(before.createdAt.getTime() - 1) + expect(await listGitHubBuildsAwaitingImport(prisma, { createdAfter })).toHaveLength(1) + fixture.workflowPaths.splice(0) + + await expect(fixture.importRun()).resolves.toEqual({ dist: "ignored", screenshots: "absent" }) + + const settled = await fixture.findBuild() + expect(settled).toEqual({ + ...before, + updatedAt: settled.updatedAt, + workflowCompletedAt: fixture.run.updatedAt, + }) + expect(await listGitHubBuildsAwaitingImport(prisma, { createdAfter })).toEqual([]) + }) + it("does not move the current Build back for a late older run", async () => { const fixture = await createFixture() fixture.packages.set(distArtifactName, distPackage()) diff --git a/hub/server/domains/github-builds/import-github-workflow-run/index.ts b/hub/server/domains/github-builds/import-github-workflow-run/index.ts index 08dde8180..e4618103c 100644 --- a/hub/server/domains/github-builds/import-github-workflow-run/index.ts +++ b/hub/server/domains/github-builds/import-github-workflow-run/index.ts @@ -10,6 +10,7 @@ import type { GitHubWorkflowArtifact, GitHubWorkflowRun, } from "../../../integrations/github/create-github-actions-client/types.ts" +import { GitHubCommitLookupError } from "../../../integrations/github/errors.ts" import { distArtifactName, maximumArtifactDownloadBytes, @@ -59,7 +60,7 @@ export async function importGitHubWorkflowRun( run, }) if (settled) { - logger.info({ runId: run.id }, "Stopped unfinished Build from a deselected workflow") + logger.info({ runId: run.id }, "Settled the Build from a deselected workflow") } return { dist: "ignored", screenshots: "absent" } } @@ -68,9 +69,32 @@ export async function importGitHubWorkflowRun( job.githubRepositoryId, run, workflowPaths, + async (commitSha) => { + const author = await client.getCommitAuthor(commitSha) + if (!author) { + logger.warn({ runId: run.id, commitSha }, "GitHub commit has no linked author") + } + return author + }, ) if (prepared.status !== "ready") return { dist: "ignored", screenshots: "absent" } - const { buildId, buildRun, distDigest, ownsBuild, state } = prepared + const { buildId, buildRun, distDigest, ownsBuild, state, authorError } = prepared + if (prepared.authorPreserved) { + logger.warn({ buildId, runId: run.id }, "Existing Build author metadata was preserved") + } + if (prepared.authorError) { + logger.warn( + { + buildId, + runId: run.id, + errorType: prepared.authorError.name, + ...(prepared.authorError instanceof GitHubCommitLookupError + ? { statusCode: prepared.authorError.statusCode } + : {}), + }, + "GitHub commit author lookup failed; artifact import will continue and retry", + ) + } const completed = run.status === "completed" const completedAt = completed ? run.updatedAt : null @@ -78,7 +102,7 @@ export async function importGitHubWorkflowRun( await completeGitHubBuildFailure(database, { buildId, run: buildRun, - completedAt, + completedAt: authorError ? null : completedAt, failureSummary, }) logger.warn({ buildId, runId: run.id, failureSummary }, "GitHub Build import failed") @@ -150,7 +174,12 @@ export async function importGitHubWorkflowRun( } const result = await importGitHubDistPackage( dependencies, - { buildId, run: buildRun, completedAt, expectedDistDigest }, + { + buildId, + run: buildRun, + completedAt: authorError ? null : completedAt, + expectedDistDigest, + }, downloaded.bytes, ) if (result.status === "digest-mismatch") { @@ -175,7 +204,8 @@ export async function importGitHubWorkflowRun( await registerImportedPrototype(database, buildId) } const screenshots = await importScreenshots() - // Screenshot errors throw before this, so recovery keeps retrying until they attach. + if (prepared.authorError) throw prepared.authorError + // Author and screenshot errors throw before this, so recovery retries the pass. if (completedAt) { await recordGitHubBuildRunCompletion(database, { buildId, run: buildRun, completedAt }) } diff --git a/hub/server/domains/github-builds/import-github-workflow-run/types.ts b/hub/server/domains/github-builds/import-github-workflow-run/types.ts index b1fb3c76a..0bafc4e68 100644 --- a/hub/server/domains/github-builds/import-github-workflow-run/types.ts +++ b/hub/server/domains/github-builds/import-github-workflow-run/types.ts @@ -4,7 +4,11 @@ import type { GitHubBuildImportDependencies } from "../types.ts" export interface ImportGitHubWorkflowRunDependencies extends GitHubBuildImportDependencies { client: Pick< ReturnType, - "getBuildWorkflowPaths" | "getWorkflowRun" | "listRunArtifacts" | "downloadArtifact" + | "getBuildWorkflowPaths" + | "getWorkflowRun" + | "getCommitAuthor" + | "listRunArtifacts" + | "downloadArtifact" > } diff --git a/hub/server/domains/github-builds/prepare-github-workflow-run-build/index.ts b/hub/server/domains/github-builds/prepare-github-workflow-run-build/index.ts index 3d8f932e0..41eefdbee 100644 --- a/hub/server/domains/github-builds/prepare-github-workflow-run-build/index.ts +++ b/hub/server/domains/github-builds/prepare-github-workflow-run-build/index.ts @@ -1,6 +1,10 @@ import type { PrismaClient } from "../../../generated/prisma/client.ts" import type { GitHubWorkflowRun } from "../../../integrations/github/create-github-actions-client/types.ts" +import { GitHubProviderRequestError } from "../../../integrations/github/errors.ts" +import { findGitHubBuildAuthor } from "../../../repositories/github-builds/find-github-build-author/index.ts" import { prepareGitHubBuild } from "../../../repositories/github-builds/prepare-github-build/index.ts" +import { recordGitHubBuildAuthor } from "../../../repositories/github-builds/record-github-build-author/index.ts" +import type { createGitHubActionsClient } from "../../../integrations/github/create-github-actions-client/index.ts" import { isGitHubWorkflowBranchPush } from "../is-github-workflow-branch-push/index.ts" /** @@ -12,6 +16,7 @@ export async function prepareGitHubWorkflowRunBuild( githubRepositoryId: number, run: GitHubWorkflowRun, workflowPaths: string[], + getCommitAuthor: ReturnType["getCommitAuthor"], ) { if ( !isGitHubWorkflowBranchPush(githubRepositoryId, run) || @@ -33,5 +38,47 @@ export async function prepareGitHubWorkflowRunBuild( author: null, }) if (prepared.status !== "ready") return prepared - return { ...prepared, buildRun } + const saved = await findGitHubBuildAuthor(database, { + buildId: prepared.buildId, + commitSha: run.headSha, + githubRepositoryId, + }) + if (!saved) throw new Error("The Build or repository no longer matches the GitHub run.") + const noIdentity = + saved.authorProvider === null && + saved.authorIssuer === null && + saved.authorProviderUserId === null + const savedGitHubIdentity = + saved.authorProvider === "github" && + saved.authorIssuer === "https://github.com" && + saved.authorProviderUserId + let authorPreserved = !noIdentity && !savedGitHubIdentity + let author: { providerUserId: string; login: string | null; avatarUrl: string | null } | null = + null + let authorError: GitHubProviderRequestError | undefined + if (savedGitHubIdentity) { + author = { + providerUserId: savedGitHubIdentity, + login: saved.authorLogin, + avatarUrl: saved.authorAvatarUrl, + } + } else if (noIdentity) { + try { + author = await getCommitAuthor(run.headSha) + } catch (error) { + if (!(error instanceof GitHubProviderRequestError)) throw error + authorError = error + } + } + if (author) { + const recorded = await recordGitHubBuildAuthor(database, { + buildId: prepared.buildId, + commitSha: run.headSha, + githubRepositoryId, + githubWorkflowRunId: saved.githubWorkflowRunId, + author, + }) + authorPreserved = recorded.status === "preserved" + } + return { ...prepared, buildRun, authorPreserved: Boolean(authorPreserved), authorError } } diff --git a/hub/server/domains/github-builds/repair-github-build-author/index.db.test.ts b/hub/server/domains/github-builds/repair-github-build-author/index.db.test.ts new file mode 100644 index 000000000..b82341c85 --- /dev/null +++ b/hub/server/domains/github-builds/repair-github-build-author/index.db.test.ts @@ -0,0 +1,341 @@ +import { describe, expect, it, vi } from "vitest" + +import { prisma } from "../../../integrations/prisma" +import { repairGitHubBuildAuthor } from "." + +describe("repairGitHubBuildAuthor()", () => { + async function fixture() { + const repository = await prisma.repository.create({ + data: { nameWithOwner: "octo/demo", defaultBranch: "main", githubRepositoryId: 56 }, + }) + const prototype = await prisma.prototype.create({ + data: { repositoryId: repository.id, branchName: "proto/demo" }, + }) + const build = await prisma.build.create({ + data: { + prototypeId: prototype.id, + number: 1, + state: "successful", + commitSha: "a".repeat(40), + githubWorkflowRunId: 7, + distDigest: `sha256:${"b".repeat(64)}`, + }, + }) + await prisma.prototype.update({ + where: { id: prototype.id }, + data: { currentBuildId: build.id }, + }) + const author = { + providerUserId: "9", + login: "commit-author", + avatarUrl: "https://example.test/author.png", + } + const getCommitAuthor = vi.fn().mockResolvedValue(author) + return { + author, + build, + prototype, + repository, + getCommitAuthor, + dependencies: { database: prisma, client: { getCommitAuthor }, logger: { warn: vi.fn() } }, + input: { buildId: build.id, githubRepositoryId: 56, apply: false }, + } + } + + it("previews and applies only the target Build attribution without assigning a mapped owner", async () => { + const { author, dependencies, input, build, prototype, getCommitAuthor } = await fixture() + const mapped = await prisma.user.create({ + data: { + externalIdentities: { + create: { + provider: "github", + issuer: "https://github.com", + subject: author.providerUserId, + }, + }, + }, + include: { profile: true }, + }) + const otherBuild = await prisma.build.create({ + data: { + prototypeId: prototype.id, + number: 2, + state: "successful", + commitSha: "c".repeat(40), + githubWorkflowRunId: 8, + }, + }) + const beforePrototype = await prisma.prototype.findUniqueOrThrow({ + where: { id: prototype.id }, + }) + const beforeIdentity = await prisma.externalIdentity.findFirstOrThrow({ + where: { userId: mapped.id }, + }) + + expect(mapped.profile).toBeNull() + expect(beforePrototype.authorId).toBeNull() + expect(await repairGitHubBuildAuthor(dependencies, input)).toEqual({ + status: "available", + author, + build: "would-update", + prototypeOwner: "unchanged", + }) + expect(getCommitAuthor).toHaveBeenCalledWith(build.commitSha) + expect(await prisma.build.findUnique({ where: { id: build.id } })).toEqual(build) + expect(await prisma.build.findUnique({ where: { id: otherBuild.id } })).toEqual(otherBuild) + expect(await prisma.prototype.findUnique({ where: { id: prototype.id } })).toEqual( + beforePrototype, + ) + expect( + await prisma.user.findUnique({ where: { id: mapped.id }, include: { profile: true } }), + ).toEqual(mapped) + expect(await prisma.externalIdentity.findUnique({ where: { id: beforeIdentity.id } })).toEqual( + beforeIdentity, + ) + + expect(await repairGitHubBuildAuthor(dependencies, { ...input, apply: true })).toEqual({ + status: "updated", + }) + const after = await prisma.build.findUniqueOrThrow({ where: { id: build.id } }) + expect(after).toMatchObject({ + state: "successful", + distDigest: build.distDigest, + githubWorkflowRunId: build.githubWorkflowRunId, + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "9", + authorLogin: "commit-author", + authorAvatarUrl: author.avatarUrl, + }) + expect(await prisma.prototype.findUnique({ where: { id: prototype.id } })).toEqual( + beforePrototype, + ) + expect( + await prisma.user.findUnique({ where: { id: mapped.id }, include: { profile: true } }), + ).toEqual(mapped) + expect(await prisma.externalIdentity.findUnique({ where: { id: beforeIdentity.id } })).toEqual( + beforeIdentity, + ) + expect(await repairGitHubBuildAuthor(dependencies, { ...input, apply: true })).toEqual({ + status: "unchanged", + }) + const renamedAuthor = { ...author, login: "renamed-author" } + getCommitAuthor.mockResolvedValueOnce(renamedAuthor) + expect(await repairGitHubBuildAuthor(dependencies, input)).toEqual({ + status: "available", + author: renamedAuthor, + build: "unchanged", + prototypeOwner: "unchanged", + }) + expect(await prisma.build.findUnique({ where: { id: build.id } })).toEqual(after) + expect(await prisma.build.findUnique({ where: { id: otherBuild.id } })).toEqual(otherBuild) + expect(await prisma.prototype.findUnique({ where: { id: prototype.id } })).toEqual( + beforePrototype, + ) + }) + + it.each([ + { field: "legacy login", snapshot: { authorLogin: "legacy-author" } }, + { field: "legacy name", snapshot: { authorName: "Legacy author" } }, + { field: "legacy avatar", snapshot: { authorAvatarUrl: "https://example.test/legacy.png" } }, + { + field: "different GitHub identity", + snapshot: { + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "10", + authorLogin: "other-author", + }, + }, + ])( + "previews and preserves a $field author snapshot rather than mixing identities", + async ({ snapshot }) => { + const { author, dependencies, input, build, prototype } = await fixture() + await prisma.build.update({ where: { id: build.id }, data: snapshot }) + await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "9" }, + }, + }, + }) + const before = await prisma.build.findUniqueOrThrow({ where: { id: build.id } }) + const beforePrototype = await prisma.prototype.findUniqueOrThrow({ + where: { id: prototype.id }, + }) + + expect(await repairGitHubBuildAuthor(dependencies, input)).toEqual({ + status: "available", + author, + build: "preserved", + prototypeOwner: "unchanged", + }) + expect(await prisma.build.findUnique({ where: { id: build.id } })).toEqual(before) + expect(await prisma.prototype.findUnique({ where: { id: prototype.id } })).toEqual( + beforePrototype, + ) + + await expect( + repairGitHubBuildAuthor(dependencies, { ...input, apply: true }), + ).resolves.toEqual({ status: "preserved" }) + + expect(await prisma.build.findUnique({ where: { id: build.id } })).toEqual(before) + expect( + (await prisma.prototype.findUniqueOrThrow({ where: { id: prototype.id } })).authorId, + ).toBe(null) + expect(await prisma.user.count()).toBe(1) + }, + ) + + it("keeps an explicit prototype owner while applying a different commit author", async () => { + const { author, dependencies, input, build, prototype } = await fixture() + const owner = await prisma.user.create({ data: {} }) + await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "9" }, + }, + }, + }) + await prisma.prototype.update({ + where: { id: prototype.id }, + data: { authorId: owner.id }, + }) + + expect(await repairGitHubBuildAuthor(dependencies, input)).toEqual({ + status: "available", + author, + build: "would-update", + prototypeOwner: "unchanged", + }) + expect( + (await prisma.prototype.findUniqueOrThrow({ where: { id: prototype.id } })).authorId, + ).toBe(owner.id) + expect(await repairGitHubBuildAuthor(dependencies, { ...input, apply: true })).toEqual({ + status: "updated", + }) + expect(await prisma.build.findUniqueOrThrow({ where: { id: build.id } })).toMatchObject({ + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "9", + authorLogin: "commit-author", + }) + expect( + (await prisma.prototype.findUniqueOrThrow({ where: { id: prototype.id } })).authorId, + ).toBe(owner.id) + }) + + it("rejects repair if the repository is disconnected during the author lookup", async () => { + const { dependencies, input, build, prototype, repository, getCommitAuthor } = await fixture() + await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "9" }, + }, + }, + }) + const beforePrototype = await prisma.prototype.findUniqueOrThrow({ + where: { id: prototype.id }, + }) + getCommitAuthor.mockImplementationOnce(async () => { + await prisma.repository.update({ + where: { id: repository.id }, + data: { removedAt: new Date("2026-01-02T00:00:00Z") }, + }) + return { providerUserId: "9", login: "commit-author", avatarUrl: null } + }) + + await expect(repairGitHubBuildAuthor(dependencies, { ...input, apply: true })).rejects.toThrow( + "The GitHub repository is no longer connected.", + ) + + expect(getCommitAuthor).toHaveBeenCalledWith(build.commitSha) + expect(await prisma.build.findUnique({ where: { id: build.id } })).toEqual(build) + expect(await prisma.prototype.findUnique({ where: { id: prototype.id } })).toEqual( + beforePrototype, + ) + }) + + it("rejects repair if the Build commit changes during the author lookup", async () => { + const { dependencies, input, build, prototype, getCommitAuthor } = await fixture() + await prisma.user.create({ + data: { + externalIdentities: { + create: { provider: "github", issuer: "https://github.com", subject: "9" }, + }, + }, + }) + const beforePrototype = await prisma.prototype.findUniqueOrThrow({ + where: { id: prototype.id }, + }) + let changedBuild: typeof build | undefined + getCommitAuthor.mockImplementationOnce(async () => { + changedBuild = await prisma.build.update({ + where: { id: build.id }, + data: { commitSha: "b".repeat(40) }, + }) + return { providerUserId: "9", login: "commit-author", avatarUrl: null } + }) + + await expect(repairGitHubBuildAuthor(dependencies, { ...input, apply: true })).rejects.toThrow( + "The Build no longer matches the repository, commit, and run.", + ) + + expect(getCommitAuthor).toHaveBeenCalledWith(build.commitSha) + expect(changedBuild).toBeDefined() + expect(await prisma.build.findUnique({ where: { id: build.id } })).toEqual(changedBuild) + expect(await prisma.prototype.findUnique({ where: { id: prototype.id } })).toEqual( + beforePrototype, + ) + }) + + it("rejects repair if the Build run changes during the author lookup", async () => { + const { dependencies, input, build, prototype, getCommitAuthor } = await fixture() + const beforePrototype = await prisma.prototype.findUniqueOrThrow({ + where: { id: prototype.id }, + }) + let changedBuild: typeof build | undefined + getCommitAuthor.mockImplementationOnce(async () => { + changedBuild = await prisma.build.update({ + where: { id: build.id }, + data: { githubWorkflowRunId: 8 }, + }) + return { providerUserId: "9", login: "commit-author", avatarUrl: null } + }) + + await expect(repairGitHubBuildAuthor(dependencies, { ...input, apply: true })).rejects.toThrow( + "The Build no longer matches the repository, commit, and run.", + ) + + expect(changedBuild).toBeDefined() + expect(await prisma.build.findUnique({ where: { id: build.id } })).toEqual(changedBuild) + expect(await prisma.prototype.findUnique({ where: { id: prototype.id } })).toEqual( + beforePrototype, + ) + }) + + it.each([false, true])( + "rejects a repository mismatch before resolving a private commit with apply=%s", + async (apply) => { + const { dependencies, input, getCommitAuthor } = await fixture() + await expect( + repairGitHubBuildAuthor(dependencies, { ...input, githubRepositoryId: 99, apply }), + ).rejects.toThrow("No GitHub Build matches") + expect(getCommitAuthor).not.toHaveBeenCalled() + }, + ) + + it("leaves an unmapped commit unknown and observable, and propagates provider failures", async () => { + const { dependencies, input, build, getCommitAuthor } = await fixture() + getCommitAuthor.mockResolvedValueOnce(null) + expect(await repairGitHubBuildAuthor(dependencies, { ...input, apply: true })).toEqual({ + status: "unavailable", + }) + expect(dependencies.logger.warn).toHaveBeenCalled() + getCommitAuthor.mockRejectedValueOnce(new Error("provider unavailable")) + await expect(repairGitHubBuildAuthor(dependencies, { ...input, apply: true })).rejects.toThrow( + "provider unavailable", + ) + expect(await prisma.build.findUnique({ where: { id: build.id } })).toEqual(build) + }) +}) diff --git a/hub/server/domains/github-builds/repair-github-build-author/index.ts b/hub/server/domains/github-builds/repair-github-build-author/index.ts new file mode 100644 index 000000000..7f691a795 --- /dev/null +++ b/hub/server/domains/github-builds/repair-github-build-author/index.ts @@ -0,0 +1,46 @@ +import type { Logger } from "pino" + +import type { PrismaClient } from "../../../generated/prisma/client.ts" +import type { createGitHubActionsClient } from "../../../integrations/github/create-github-actions-client/index.ts" +import { findGitHubBuildAuthorTarget } from "../../../repositories/github-builds/find-github-build-author-target/index.ts" +import { recordGitHubBuildAuthor } from "../../../repositories/github-builds/record-github-build-author/index.ts" +import { getGitHubBuildAuthorAction } from "../../../utils/get-github-build-author-action/index.ts" +import { GitHubBuildAuthorTargetError } from "../errors.ts" + +/** Explicitly repairs one retained Build, including those outside the recovery window. */ +export async function repairGitHubBuildAuthor( + dependencies: { + database: PrismaClient + client: Pick, "getCommitAuthor"> + logger: Pick + }, + input: { buildId: string; githubRepositoryId: number; apply: boolean }, +) { + const target = await findGitHubBuildAuthorTarget(dependencies.database, input) + if (!target?.commitSha || target.githubWorkflowRunId === null) { + throw new GitHubBuildAuthorTargetError() + } + const author = await dependencies.client.getCommitAuthor(target.commitSha) + if (!author) { + dependencies.logger.warn( + { buildId: target.id, commitSha: target.commitSha }, + "GitHub commit has no linked author", + ) + return { status: "unavailable" as const } + } + if (!input.apply) { + return { + status: "available" as const, + author, + build: getGitHubBuildAuthorAction(target, author), + prototypeOwner: "unchanged" as const, + } + } + return recordGitHubBuildAuthor(dependencies.database, { + buildId: target.id, + commitSha: target.commitSha, + githubRepositoryId: input.githubRepositoryId, + githubWorkflowRunId: target.githubWorkflowRunId, + author, + }) +} diff --git a/hub/server/domains/github-webhooks/check-github-webhook-communication/index.test.ts b/hub/server/domains/github-webhooks/check-github-webhook-communication/index.test.ts new file mode 100644 index 000000000..61618a94f --- /dev/null +++ b/hub/server/domains/github-webhooks/check-github-webhook-communication/index.test.ts @@ -0,0 +1,341 @@ +import { generateKeyPairSync } from "node:crypto" +import { describe, expect, it, vi } from "vitest" + +import { githubAppRequirements } from "../../../../client/utils/github-app-requirements" +import type { PrismaClient } from "../../../generated/prisma/client.ts" +import { findGitHubWebhookEvidence } from "../../../repositories/github-webhook-deliveries/find-github-webhook-evidence" +import { checkGitHubWebhookCommunication } from "." + +vi.mock( + "../../../repositories/github-webhook-deliveries/find-github-webhook-evidence/index.ts", + () => ({ + findGitHubWebhookEvidence: vi.fn(), + }), +) + +const { privateKey } = generateKeyPairSync("rsa", { modulusLength: 2048 }) +const input = { + credentials: { appId: "12", installationId: "34", privateKey }, + applicationOrigin: "https://hub.example", + githubRepositoryId: 56, + runId: 7, +} + +function delivery( + overrides: Partial<{ + id: number + guid: string + installation_id: number + repository_id: number + }> = {}, +) { + return { + id: 1, + guid: "delivery-guid", + delivered_at: "2026-01-01T00:02:00Z", + status_code: 200, + event: "workflow_run", + installation_id: 34, + repository_id: 56, + ...overrides, + } +} + +function providerFetch( + options: { + commitStatus?: number + deliveries?: ReturnType[] + deliveryStatus?: number + events?: string[] + nextPage?: boolean + runAttempt?: number + webhookUrl?: string + } = {}, +) { + function respond(url: Parameters[0], init?: RequestInit): Response { + const requestUrl = new URL(url instanceof Request ? url.url : url.toString()) + const path = requestUrl.pathname + if (init?.method === "POST") { + expect(path).toBe("/app/installations/34/access_tokens") + if (typeof init.body !== "string") throw new Error("Expected a JSON request body") + const body: unknown = JSON.parse(init.body) + expect(body).toEqual({ + repository_ids: [56], + permissions: { actions: "read", contents: "read" }, + }) + return Response.json({ token: "installation-secret" }) + } + if (path === "/app") + return Response.json({ + id: 12, + events: options.events ?? githubAppRequirements.events.map(({ key }) => key), + }) + if (path === "/app/installations/34") return Response.json({ app_id: 12 }) + if (path === "/app/hook/config") + return Response.json({ + url: options.webhookUrl ?? "https://hub.example/api/github/webhooks", + content_type: "json", + insecure_ssl: "0", + secret: "webhook-secret", + }) + if (path === "/app/hook/deliveries") { + if (options.deliveryStatus) + return new Response('{"private":"provider body"}', { status: options.deliveryStatus }) + return Response.json( + options.deliveries ?? [], + options.nextPage + ? { + headers: { + link: '; rel="next"', + }, + } + : undefined, + ) + } + if (path.endsWith("/actions/runs/7")) + return Response.json({ + id: 7, + run_attempt: options.runAttempt ?? 1, + path: ".github/workflows/replica.yml", + event: "push", + head_branch: "proto/demo", + head_sha: "a".repeat(40), + status: "completed", + conclusion: "success", + html_url: "https://github.com/o/r/actions/runs/7", + created_at: "2026-01-01T00:00:00Z", + updated_at: "2026-01-01T00:01:00Z", + repository: { id: 56 }, + }) + if (path.endsWith(`/commits/${"a".repeat(40)}`)) + return options.commitStatus + ? new Response("{}", { status: options.commitStatus }) + : Response.json({ sha: "a".repeat(40), author: null }) + throw new Error("Unexpected request") + } + return vi.fn((...args) => Promise.resolve(respond(...args))) +} + +describe("checkGitHubWebhookCommunication()", () => { + it("reports unobserved evidence separately without exposing provider secrets", async () => { + const fetch = providerFetch({ + webhookUrl: "https://wrong.example/private?secret=hidden", + deliveryStatus: 403, + }) + const checks = await checkGitHubWebhookCommunication(input, { fetch }) + + expect(JSON.stringify(checks)).not.toMatch( + /hidden|installation-secret|webhook-secret|provider body/, + ) + expect(checks).toEqual( + expect.arrayContaining([ + expect.objectContaining({ name: "private_key", result: "pass" }), + expect.objectContaining({ name: "installation", result: "pass" }), + expect.objectContaining({ name: "webhook_configuration", result: "fail" }), + expect.objectContaining({ name: "webhook_events", result: "pass" }), + expect.objectContaining({ name: "repository_workflow_access", result: "pass" }), + expect.objectContaining({ name: "delivery_history", result: "fail" }), + expect.objectContaining({ name: "signed_receipt", result: "unknown" }), + expect.objectContaining({ name: "run_build_import", result: "unknown" }), + expect.objectContaining({ name: "delivery_to_run", result: "unknown" }), + expect.objectContaining({ name: "webhook_recovery", result: "unknown" }), + ]), + ) + expect(checks.find(({ name }) => name === "delivery_history")?.detail).toContain("HTTP 403") + }) + + it.each([404, 422])( + "does not report Contents access as verified after HTTP %s", + async (status) => { + const fetch = providerFetch({ commitStatus: status }) + + const checks = await checkGitHubWebhookCommunication(input, { fetch }) + + expect(checks).toContainEqual( + expect.objectContaining({ name: "repository_workflow_access", result: "fail" }), + ) + }, + ) + + it.each(githubAppRequirements.events.map(({ key }) => key))( + "rejects webhook subscriptions missing the required %s event", + async (missing) => { + const events = githubAppRequirements.events + .map(({ key }) => key) + .filter((key) => key !== missing) + const checks = await checkGitHubWebhookCommunication(input, { + fetch: providerFetch({ events }), + }) + + expect(checks).toContainEqual( + expect.objectContaining({ name: "webhook_events", result: "fail" }), + ) + }, + ) + + it("reports a signed receipt and run import without inferring their association or recovery", async () => { + const database = {} as PrismaClient + const fetch = providerFetch({ deliveries: [delivery()] }) + vi.mocked(findGitHubWebhookEvidence).mockResolvedValue({ + receipt: { event: "workflow_run", createdAt: new Date("2026-01-01T00:02:00Z") }, + build: { + state: "successful", + owningAttempt: 1, + completedAttempt: 1, + completedAt: new Date("2026-01-01T00:03:00Z"), + }, + redelivery: { state: "succeeded", attempts: 1, failureCode: null }, + }) + + const checks = await checkGitHubWebhookCommunication( + { ...input, deliveryGuid: "delivery-guid", database }, + { fetch }, + ) + + expect(findGitHubWebhookEvidence).toHaveBeenCalledWith( + database, + expect.objectContaining({ deliveryGuid: "delivery-guid", githubRepositoryId: 56, runId: 7 }), + ) + expect(checks).toEqual( + expect.arrayContaining([ + expect.objectContaining({ name: "signed_receipt", result: "pass" }), + expect.objectContaining({ name: "run_build_import", result: "pass" }), + expect.objectContaining({ name: "delivery_to_run", result: "unknown" }), + expect.objectContaining({ name: "webhook_recovery", result: "unknown" }), + ]), + ) + }) + + it("does not claim a later provider attempt was imported using an earlier attempt's evidence", async () => { + const database = {} as PrismaClient + const fetch = providerFetch({ deliveries: [delivery()], runAttempt: 2 }) + vi.mocked(findGitHubWebhookEvidence).mockResolvedValue({ + receipt: { event: "workflow_run", createdAt: new Date("2026-01-01T00:02:00Z") }, + build: { + state: "successful", + owningAttempt: 1, + completedAttempt: 1, + completedAt: new Date("2026-01-01T00:03:00Z"), + }, + redelivery: null, + }) + + const checks = await checkGitHubWebhookCommunication( + { ...input, deliveryGuid: "delivery-guid", database }, + { fetch }, + ) + const importCheck = checks.find((check) => check.name === "run_build_import") + expect(importCheck?.result).toBe("unknown") + expect(importCheck?.detail).toContain("observed run attempt: 2; completed import attempt: 1") + }) + + it.each([{ installation_id: 99 }, { repository_id: 99 }])( + "does not claim a signed receipt from a different provider scope (%j)", + async (scope) => { + const database = {} as PrismaClient + const fetch = providerFetch({ deliveries: [delivery(scope)] }) + vi.mocked(findGitHubWebhookEvidence).mockResolvedValue({ + receipt: { event: "workflow_run", createdAt: new Date("2026-01-01T00:02:00Z") }, + build: { + state: "successful", + owningAttempt: 1, + completedAttempt: 1, + completedAt: new Date("2026-01-01T00:03:00Z"), + }, + redelivery: { state: "succeeded", attempts: 1, failureCode: null }, + }) + + const checks = await checkGitHubWebhookCommunication( + { ...input, deliveryGuid: "delivery-guid", database }, + { fetch }, + ) + + expect(checks).toContainEqual( + expect.objectContaining({ name: "signed_receipt", result: "unknown" }), + ) + expect(checks).toContainEqual( + expect.objectContaining({ name: "delivery_to_run", result: "unknown" }), + ) + expect(findGitHubWebhookEvidence).not.toHaveBeenCalled() + }, + ) + + it("does not claim a signed receipt when the recorded event differs from the provider delivery", async () => { + const database = {} as PrismaClient + const fetch = providerFetch({ deliveries: [delivery()] }) + vi.mocked(findGitHubWebhookEvidence).mockResolvedValue({ + receipt: { event: "push", createdAt: new Date("2026-01-01T00:02:00Z") }, + build: null, + redelivery: null, + }) + + const checks = await checkGitHubWebhookCommunication( + { ...input, deliveryGuid: "delivery-guid", database }, + { fetch }, + ) + + expect(checks).toContainEqual( + expect.objectContaining({ name: "signed_receipt", result: "fail" }), + ) + }) + + it("distinguishes an unreadable delivery history from an absent GUID", async () => { + const database = {} as PrismaClient + const checks = await checkGitHubWebhookCommunication( + { ...input, deliveryGuid: "delivery-guid", database }, + { fetch: providerFetch({ deliveryStatus: 403 }) }, + ) + + expect(checks).toContainEqual( + expect.objectContaining({ + name: "signed_receipt", + result: "unknown", + detail: "Delivery history could not be read. A signed receipt was not checked.", + }), + ) + expect(findGitHubWebhookEvidence).not.toHaveBeenCalled() + }) + + it("distinguishes no GUID from a GUID outside the latest 100 deliveries", async () => { + const database = {} as PrismaClient + const fetch = providerFetch({ + deliveries: Array.from({ length: 100 }, (_, index) => + delivery({ id: index + 1, guid: `other-${index}` }), + ), + nextPage: true, + }) + vi.mocked(findGitHubWebhookEvidence).mockResolvedValue({ + receipt: null, + build: null, + redelivery: null, + }) + + const withoutGuid = await checkGitHubWebhookCommunication({ ...input, database }, { fetch }) + const missingGuid = await checkGitHubWebhookCommunication( + { ...input, deliveryGuid: "unseen-guid", database }, + { fetch }, + ) + + const withoutReceipt = withoutGuid.find(({ name }) => name === "signed_receipt") + const missingReceipt = missingGuid.find(({ name }) => name === "signed_receipt") + expect(withoutReceipt).toMatchObject({ result: "unknown" }) + expect(missingReceipt).toMatchObject({ result: "unknown" }) + expect(withoutReceipt?.detail).toMatch(/guid/i) + expect(withoutReceipt?.detail).toMatch(/not|no|supply|provide|missing|without/i) + expect(missingReceipt?.detail).toMatch( + /(?:latest|recent)[^\d]{0,20}100|100.{0,20}latest|100.{0,20}recent/i, + ) + expect(missingReceipt?.detail).toMatch(/not|n't|outside|absent|no matching/i) + expect(missingReceipt?.detail).not.toBe(withoutReceipt?.detail) + const historyCalls = fetch.mock.calls.filter(([url]) => { + const requestUrl = new URL(url instanceof Request ? url.url : url.toString()) + return requestUrl.pathname === "/app/hook/deliveries" + }) + expect(historyCalls).toHaveLength(2) + for (const [url] of historyCalls) { + const requestUrl = new URL(url instanceof Request ? url.url : url.toString()) + expect(requestUrl.searchParams.get("per_page")).toBe("100") + expect(requestUrl.searchParams.has("cursor")).toBe(false) + } + }) +}) diff --git a/hub/server/domains/github-webhooks/check-github-webhook-communication/index.ts b/hub/server/domains/github-webhooks/check-github-webhook-communication/index.ts new file mode 100644 index 000000000..ba4b69248 --- /dev/null +++ b/hub/server/domains/github-webhooks/check-github-webhook-communication/index.ts @@ -0,0 +1,171 @@ +import type { PrismaClient } from "../../../generated/prisma/client.ts" +import { createGitHubActionsClient } from "../../../integrations/github/create-github-actions-client/index.ts" +import { createGitHubWebhookDeliveriesClient } from "../../../integrations/github/create-github-webhook-deliveries-client/index.ts" +import type { + GitHubWebhookDeliveriesClientInput, + GitHubWebhookDeliveriesClientOptions, +} from "../../../integrations/github/create-github-webhook-deliveries-client/types.ts" +import { GitHubWebhookRequestError } from "../../../integrations/github/create-github-webhook-deliveries-client/errors.ts" +import { GitHubCommitLookupError } from "../../../integrations/github/errors.ts" +import { testGitHubAppConnection } from "../../../integrations/github/test-github-app-connection/index.ts" +import { findGitHubWebhookEvidence } from "../../../repositories/github-webhook-deliveries/find-github-webhook-evidence/index.ts" + +/** Observes provider settings and receipts without sending or redelivering an event. */ +export async function checkGitHubWebhookCommunication( + input: { + credentials: GitHubWebhookDeliveriesClientInput & { installationId: string } + applicationOrigin: string + githubRepositoryId: number + runId: number + deliveryGuid?: string + database?: PrismaClient + }, + options: GitHubWebhookDeliveriesClientOptions = {}, +) { + const { credentials } = input + const client = createGitHubWebhookDeliveriesClient(credentials, options) + const checks: { name: string; result: "pass" | "fail" | "unknown"; detail: string }[] = + await testGitHubAppConnection(credentials, options) + async function check(name: string, operation: () => Promise) { + try { + const passed = await operation() + checks.push({ + name, + result: passed ? "pass" : "fail", + detail: passed ? "Verified." : "The observed value does not match Hub's requirements.", + }) + } catch (error) { + checks.push({ + name, + result: "fail", + detail: + error instanceof GitHubWebhookRequestError || error instanceof GitHubCommitLookupError + ? error.message + : "The provider check failed. Check App permissions, installation access, and connectivity.", + }) + } + } + await check("webhook_configuration", async () => { + const configuration = await client.getConfiguration() + return ( + configuration.url === new URL("/api/github/webhooks", input.applicationOrigin).href && + configuration.contentType === "json" && + configuration.insecureSsl === "0" + ) + }) + await check("webhook_events", async () => { + const events = await client.getSubscribedEvents() + return [ + "push", + "delete", + "workflow_run", + "workflow_job", + "installation_target", + "repository", + "member", + "membership", + "organization", + "team", + ].every((event) => events.includes(event)) + }) + let observedRun: { runAttempt: number; status: string } | undefined + await check("repository_workflow_access", async () => { + const actions = createGitHubActionsClient( + { ...credentials, githubRepositoryId: input.githubRepositoryId }, + options, + ) + const run = await actions.getWorkflowRun(input.runId) + if (!run || run.repositoryId !== input.githubRepositoryId) return false + observedRun = { runAttempt: run.runAttempt, status: run.status } + await actions.getCommitAuthor(run.headSha) + return true + }) + let historyRead = false + let observed: { event: string } | undefined + await check("delivery_history", async () => { + const page = await client.listDeliveries(null) + historyRead = true + observed = page.deliveries.find( + (delivery) => + delivery.guid === input.deliveryGuid && + String(delivery.installationId) === credentials.installationId && + delivery.repositoryId === input.githubRepositoryId, + ) + return true + }) + if (!input.deliveryGuid || !observed || !input.database) { + const reason = !input.deliveryGuid + ? "No delivery GUID was supplied." + : !historyRead + ? "Delivery history could not be read." + : !observed + ? "The GUID was not found for this installation and repository in the latest 100 deliveries." + : "No existing database connection was supplied." + checks.push({ + name: "signed_receipt", + result: "unknown", + detail: `${reason} A signed receipt was not checked.`, + }) + checks.push({ + name: "run_build_import", + result: "unknown", + detail: `${reason} The repository and run Build was not checked.`, + }) + checks.push({ + name: "delivery_to_run", + result: "unknown", + detail: "No evidence associates the delivery with the supplied workflow run.", + }) + checks.push({ + name: "webhook_recovery", + result: "unknown", + detail: `${reason} Recovery evidence was not checked.`, + }) + } else { + const evidence = await findGitHubWebhookEvidence(input.database, { + deliveryGuid: input.deliveryGuid, + githubRepositoryId: input.githubRepositoryId, + runId: input.runId, + }) + checks.push({ + name: "signed_receipt", + result: evidence.receipt?.event === observed.event ? "pass" : "fail", + detail: evidence.receipt + ? evidence.receipt.event === observed.event + ? "Hub recorded this delivery after signature verification and event handling." + : "Hub's recorded event does not match this provider delivery." + : "Hub has no recorded receipt for this delivery.", + }) + checks.push({ + name: "run_build_import", + result: + observedRun?.status === "completed" && + evidence.build?.state === "failed" && + evidence.build.owningAttempt === observedRun.runAttempt + ? "fail" + : observedRun?.status === "completed" && + evidence.build?.state === "successful" && + evidence.build.completedAttempt === observedRun.runAttempt && + evidence.build.completedAt + ? "pass" + : "unknown", + detail: evidence.build + ? `Independent run Build state: ${evidence.build.state}; observed run attempt: ${observedRun?.runAttempt ?? "unknown"}; completed import attempt: ${evidence.build.completedAttempt ?? "none"}. Polling may also have processed this run.` + : "No Build was observed for this repository and run.", + }) + checks.push({ + name: "delivery_to_run", + result: "unknown", + detail: + "The receipt and Build observations do not establish that this delivery caused this run's import.", + }) + checks.push({ + name: "webhook_recovery", + result: evidence.redelivery?.state === "failed" ? "fail" : "unknown", + detail: evidence.redelivery + ? `Recorded recovery state: ${evidence.redelivery.state}; attempts: ${evidence.redelivery.attempts}. A succeeded state may be settled from an existing receipt or provider delivery without redelivery.` + : "No recovery attempt is recorded for this delivery; recovery was not exercised.", + }) + } + return checks +} diff --git a/hub/server/domains/github-webhooks/recover-github-webhook-deliveries/index.db.test.ts b/hub/server/domains/github-webhooks/recover-github-webhook-deliveries/index.db.test.ts index 3e38c3df2..f26b924ba 100644 --- a/hub/server/domains/github-webhooks/recover-github-webhook-deliveries/index.db.test.ts +++ b/hub/server/domains/github-webhooks/recover-github-webhook-deliveries/index.db.test.ts @@ -54,6 +54,18 @@ async function redelivery(id: string) { } describe("recoverGitHubWebhookDeliveries", () => { + it("retains a large delivery ID through database storage, claiming, and redelivery", async () => { + const missed = delivery({ id: "9007199254740993" }) + const recovery = dependencies([[missed]]) + await recoverGitHubWebhookDeliveries(recovery) + expect(recovery.redeliver).toHaveBeenCalledWith("9007199254740993") + expect(await redelivery(missed.guid)).toMatchObject({ + githubDeliveryId: 9007199254740993n, + state: "waiting", + attempts: 1, + }) + }) + it("redelivers only missed deliveries the Hub can still handle", async () => { const missed = delivery() const timedOut = delivery({ statusCode: 0, event: "installation" }) @@ -139,7 +151,7 @@ describe("recoverGitHubWebhookDeliveries", () => { await prisma.gitHubWebhookRedelivery.create({ data: { id: exhausted.guid, - githubDeliveryId: exhausted.id, + githubDeliveryId: BigInt(exhausted.id), event: "workflow_job", state: "waiting", attempts: 12, @@ -189,7 +201,7 @@ describe("recoverGitHubWebhookDeliveries", () => { await prisma.gitHubWebhookRedelivery.create({ data: { id: attempt.guid, - githubDeliveryId: attempt.id, + githubDeliveryId: BigInt(attempt.id), event: attempt.event, state: "requesting", attempts, @@ -275,7 +287,7 @@ describe("recoverGitHubWebhookDeliveries", () => { const attempt = delivery() return { id: attempt.guid, - githubDeliveryId: attempt.id, + githubDeliveryId: BigInt(attempt.id), event: "workflow_job", state, retryAt: new Date(now.getTime() + day), diff --git a/hub/server/domains/github-webhooks/types.ts b/hub/server/domains/github-webhooks/types.ts index 723410424..21b4c3c04 100644 --- a/hub/server/domains/github-webhooks/types.ts +++ b/hub/server/domains/github-webhooks/types.ts @@ -12,7 +12,7 @@ export interface GitHubWebhookRecoveryDependencies { ) => Promise<{ deliveries: GitHubWebhookDeliveryAttempt[]; nextCursor: string | null }> logger: Pick now: () => Date - redeliver: (githubDeliveryId: number) => Promise<"requested" | "unavailable"> + redeliver: (githubDeliveryId: number | string) => Promise<"requested" | "unavailable"> } export interface GitHubWebhookRecoveryLease { diff --git a/hub/server/domains/public-sharing/lock-hosted-sharing-approval/index.ts b/hub/server/domains/public-sharing/lock-hosted-sharing-approval/index.ts index 4f756a0bd..e12c71012 100644 --- a/hub/server/domains/public-sharing/lock-hosted-sharing-approval/index.ts +++ b/hub/server/domains/public-sharing/lock-hosted-sharing-approval/index.ts @@ -41,7 +41,7 @@ export async function lockHostedSharingApproval( const prototype = await findHostedSharingPrototype( transaction, approval, - approval.permission === "admin" ? undefined : approval.identity, + approval.permission === "admin" ? undefined : { ...approval.identity, userId: approval.userId }, ) if (!prototype || prototype.branchName === prototype.repository.defaultBranch) { throw new PublicSharingError("forbidden", "You cannot manage share links for this prototype.") diff --git a/hub/server/integrations/github/create-github-actions-client/index.test.ts b/hub/server/integrations/github/create-github-actions-client/index.test.ts index fb3f2f285..41e97a50c 100644 --- a/hub/server/integrations/github/create-github-actions-client/index.test.ts +++ b/hub/server/integrations/github/create-github-actions-client/index.test.ts @@ -484,11 +484,16 @@ describe("createGitHubActionsClient", () => { ).rejects.toThrow(/16384|16 KiB|large|size|bytes/i) }) - it("requests one repository-scoped token for Actions and branch reads", async () => { - const fetch = respond(() => new Response(JSON.stringify({ total_count: 0, artifacts: [] }))) + it("reuses one repository-scoped token for Actions and commit reads", async () => { + const fetch = respond((url) => + url.pathname.includes("/commits/") + ? Response.json({ sha: "a".repeat(40), author: null }) + : Response.json({ total_count: 0, artifacts: [] }), + ) const client = createGitHubActionsClient(input, { fetch }) await client.listRunArtifacts(7, "replica-build-v1") + await client.getCommitAuthor("a".repeat(40)) await client.listRunArtifacts(7, "replica-screenshots-v1") const tokenCalls = fetch.mock.calls.filter(([url]) => @@ -501,6 +506,86 @@ describe("createGitHubActionsClient", () => { }) }) + it.each([ + { id: 101, login: "commit-author", avatar_url: "https://avatars.githubusercontent.com/u/101" }, + { id: 102, login: "replica[bot]", avatar_url: null, type: "Bot" }, + ])("reads the canonical account for an immutable commit by $login", async (author) => { + const sha = "a".repeat(40) + const fetch = respond((url, init) => { + expect(url.pathname).toBe(`/repositories/56/commits/${sha}`) + expect(init.headers).toMatchObject({ Authorization: "Bearer t" }) + return Response.json({ + sha, + author, + committer: { id: 999, login: "different-committer" }, + commit: { author: { name: "Unverified display name", email: "author@example.test" } }, + }) + }) + const client = createGitHubActionsClient(input, { fetch }) + + await expect(client.getCommitAuthor(sha)).resolves.toEqual({ + providerUserId: String(author.id), + login: author.login, + avatarUrl: author.avatar_url, + }) + }) + + it.each([null, {}])( + "does not turn Git author text into a provider account (%j)", + async (author) => { + const sha = "a".repeat(40) + const fetch = respond(() => + Response.json({ + sha, + author, + committer: { id: 999, login: "committer" }, + commit: { author: { name: "Octocat", email: "octocat@example.test" } }, + }), + ) + const client = createGitHubActionsClient(input, { fetch }) + + await expect(client.getCommitAuthor(sha)).resolves.toBeNull() + }, + ) + + it.each([ + { sha: "b".repeat(40), author: { id: 101, login: "commit-author" } }, + { sha: "b".repeat(40), author: null }, + { sha: "b".repeat(40), author: {} }, + { author: { id: 101, login: "commit-author" } }, + { sha: "a".repeat(40) }, + { sha: "a".repeat(40), author: { id: "101", login: "commit-author" } }, + { sha: "a".repeat(40), author: { id: 101 } }, + { sha: "a".repeat(40), author: { login: "commit-author" } }, + { sha: "a".repeat(40), author: { type: "User" } }, + ])("rejects malformed or mismatched commit evidence %#", async (commit) => { + const fetch = respond(() => Response.json(commit)) + const client = createGitHubActionsClient(input, { fetch }) + + await expect(client.getCommitAuthor("a".repeat(40))).rejects.toThrow(GitHubProviderRequestError) + }) + + it.each([403, 404, 422, 503])( + "does not hide HTTP %s commit lookup failures as unknown", + async (status) => { + const fetch = respond(() => new Response("{}", { status })) + const client = createGitHubActionsClient(input, { fetch }) + + await expect(client.getCommitAuthor("a".repeat(40))).rejects.toThrow( + GitHubProviderRequestError, + ) + }, + ) + + it("throws when the commit lookup transport fails", async () => { + const fetch = respond(() => { + throw new Error("Connection reset") + }) + const client = createGitHubActionsClient(input, { fetch }) + + await expect(client.getCommitAuthor("a".repeat(40))).rejects.toThrow(GitHubProviderRequestError) + }) + it("downloads from the redirect without forwarding the token", async () => { const fetch = respond((url) => url.hostname === "api.github.com" diff --git a/hub/server/integrations/github/create-github-actions-client/index.ts b/hub/server/integrations/github/create-github-actions-client/index.ts index 339dbd870..fce6b986e 100644 --- a/hub/server/integrations/github/create-github-actions-client/index.ts +++ b/hub/server/integrations/github/create-github-actions-client/index.ts @@ -2,7 +2,7 @@ import { z } from "zod" import { readBuildWorkflowPaths } from "../../../utils/read-build-workflow-paths/index.ts" import { createGitHubAppJwt } from "../create-github-app-jwt/index.ts" -import { GitHubProviderRequestError } from "../errors.ts" +import { GitHubCommitLookupError, GitHubProviderRequestError } from "../errors.ts" import type { GitHubActionsClientInput, GitHubActionsClientOptions, @@ -60,6 +60,10 @@ const artifactsSchema = z.looseObject({ ), }) const maximumArtifactPages = 10 +const commitSchema = z.looseObject({ + sha: z.string().regex(/^[0-9a-f]{40}$/), + author: z.union([actorSchema.nonoptional(), z.strictObject({})]), +}) const maximumConfigurationBytes = 16_384 const repositorySchema = z.looseObject({ id: z.number().int().positive(), @@ -285,6 +289,32 @@ export function createGitHubActionsClient( return { getBuildWorkflowPaths, + + /** Resolves the account GitHub associates with this immutable commit, not its pusher. */ + async getCommitAuthor(commitSha: string) { + if (!/^[0-9a-f]{40}$/.test(commitSha)) { + throw new GitHubProviderRequestError({ cause: new Error("Invalid commit SHA.") }) + } + const response = await send( + `${repositoryPath}/commits/${commitSha}`, + await installationToken(), + ) + if (!response.ok) throw new GitHubCommitLookupError(response.status) + const commit = await parse(commitSchema, response) + if (commit.sha !== commitSha) { + throw new GitHubProviderRequestError({ + cause: new Error("GitHub returned a different commit."), + }) + } + return commit.author && "id" in commit.author + ? { + providerUserId: String(commit.author.id), + login: commit.author.login, + avatarUrl: commit.author.avatar_url ?? null, + } + : null + }, + /** Lists branch heads without assuming a truncated response is a complete snapshot. */ async listBranchHeads(): Promise<{ branchName: string; commitSha: string }[]> { const heads: { branchName: string; commitSha: string }[] = [] diff --git a/hub/server/integrations/github/create-github-webhook-deliveries-client/errors.ts b/hub/server/integrations/github/create-github-webhook-deliveries-client/errors.ts new file mode 100644 index 000000000..3e1207e3b --- /dev/null +++ b/hub/server/integrations/github/create-github-webhook-deliveries-client/errors.ts @@ -0,0 +1,16 @@ +import { GitHubProviderRequestError } from "../errors.ts" + +export class GitHubWebhookRequestError extends GitHubProviderRequestError { + readonly operation: string + readonly failure: "http" | "transport" | "response" + readonly statusCode: number | undefined + + constructor(operation: string, failure: "http" | "transport" | "response", statusCode?: number) { + super() + this.operation = operation + this.failure = failure + this.statusCode = statusCode + this.name = "GitHubWebhookRequestError" + this.message = `GitHub webhook ${operation} failed (${failure}${statusCode ? `, HTTP ${statusCode}` : ""}).` + } +} diff --git a/hub/server/integrations/github/create-github-webhook-deliveries-client/index.test.ts b/hub/server/integrations/github/create-github-webhook-deliveries-client/index.test.ts index c55ed6c27..a1a78e785 100644 --- a/hub/server/integrations/github/create-github-webhook-deliveries-client/index.test.ts +++ b/hub/server/integrations/github/create-github-webhook-deliveries-client/index.test.ts @@ -20,6 +20,35 @@ function respondWith(response: Response) { } describe("createGitHubWebhookDeliveriesClient", () => { + it("preserves int64 delivery IDs from raw JSON through a redelivery request", async () => { + const id = "9007199254740993" + const fetch = vi + .fn() + .mockResolvedValueOnce( + new Response(JSON.stringify([delivery]).replace('"id":9', `"id":${id}`)), + ) + .mockResolvedValueOnce(new Response(null, { status: 202 })) + const client = createGitHubWebhookDeliveriesClient(input, { fetch }) + const page = await client.listDeliveries(null) + expect(page.deliveries[0]?.id).toBe(id) + await client.redeliver(page.deliveries[0]!.id) + expect(fetch.mock.calls[1]?.[0]).toEqual( + new URL(`https://api.github.com/app/hook/deliveries/${id}/attempts`), + ) + }) + + it.each(["9223372036854775808", "-9007199254740993", '"../config"'])( + "rejects a delivery ID outside the positive int64 contract (%s)", + async (id) => { + const client = createGitHubWebhookDeliveriesClient(input, { + fetch: respondWith( + new Response(JSON.stringify([delivery]).replace('"id":9', `"id":${id}`)), + ), + }) + await expect(client.listDeliveries(null)).rejects.toMatchObject({ failure: "response" }) + }, + ) + it("lists deliveries and follows GitHub's next cursor", async () => { const fetch = respondWith( new Response(JSON.stringify([delivery]), { @@ -82,4 +111,43 @@ describe("createGitHubWebhookDeliveriesClient", () => { await expect(client.redeliver(9)).rejects.toBeInstanceOf(GitHubProviderRequestError) }) + + it("distinguishes rejected credentials from an unreadable successful response without leaking bodies", async () => { + const fetch = vi + .fn() + .mockResolvedValueOnce(new Response('{"message":"private response"}', { status: 401 })) + .mockResolvedValueOnce(new Response('{"private":"response"}')) + const client = createGitHubWebhookDeliveriesClient(input, { fetch }) + + await expect(client.listDeliveries(null)).rejects.toMatchObject({ + operation: "list-deliveries", + failure: "http", + statusCode: 401, + }) + const error = await client.listDeliveries(null).catch((error: unknown) => error) + expect(error).toMatchObject({ + operation: "list-deliveries", + failure: "response", + statusCode: 200, + }) + expect(JSON.stringify(error)).not.toContain("private") + }) + + it("returns only public webhook configuration fields", async () => { + const client = createGitHubWebhookDeliveriesClient(input, { + fetch: respondWith( + Response.json({ + url: "https://hub.example/api/github/webhooks", + content_type: "json", + insecure_ssl: "0", + secret: "must-not-escape", + }), + ), + }) + expect(await client.getConfiguration()).toEqual({ + url: "https://hub.example/api/github/webhooks", + contentType: "json", + insecureSsl: "0", + }) + }) }) diff --git a/hub/server/integrations/github/create-github-webhook-deliveries-client/index.ts b/hub/server/integrations/github/create-github-webhook-deliveries-client/index.ts index df28866be..1747478fe 100644 --- a/hub/server/integrations/github/create-github-webhook-deliveries-client/index.ts +++ b/hub/server/integrations/github/create-github-webhook-deliveries-client/index.ts @@ -1,7 +1,7 @@ import { z } from "zod" import { createGitHubAppJwt } from "../create-github-app-jwt/index.ts" -import { GitHubProviderRequestError } from "../errors.ts" +import { GitHubWebhookRequestError } from "./errors.ts" import type { GitHubWebhookDeliveriesClientInput, GitHubWebhookDeliveriesClientOptions, @@ -10,14 +10,21 @@ import type { const deliveriesPath = "/app/hook/deliveries" const pageSize = "100" +const deliveryIdSchema = z.union([ + z.number().int().positive(), + z + .string() + .refine((value) => /^[1-9][0-9]{0,18}$/.test(value) && BigInt(value) <= 9223372036854775807n), +]) const deliveriesSchema = z.array( z.looseObject({ - id: z.number().int().positive(), + id: deliveryIdSchema, guid: z.string().max(100), delivered_at: z.iso.datetime({ offset: true }), status_code: z.number().int().nullable(), event: z.string().max(100), installation_id: z.number().int().positive().nullish(), + repository_id: z.number().int().positive().nullish(), }), ) @@ -31,7 +38,12 @@ function readNextCursor(link: string | null, apiUrl: string) { .map((part) => /^\s*<([^>]+)>\s*;\s*rel="next"\s*$/.exec(part)?.[1]) .find(Boolean) if (!next) return null - const url = new URL(next) + let url: URL + try { + url = new URL(next) + } catch { + throw new GitHubWebhookRequestError("pagination", "response") + } const base = new URL(deliveriesPath, apiUrl) const keys = [...url.searchParams.keys()].sort() const cursor = url.searchParams.get("cursor") @@ -43,7 +55,7 @@ function readNextCursor(link: string | null, apiUrl: string) { !cursor || cursor.length > 2048 ) { - throw new GitHubProviderRequestError({ cause: new Error("Unexpected deliveries link.") }) + throw new GitHubWebhookRequestError("pagination", "response") } return cursor } @@ -58,7 +70,7 @@ export function createGitHubWebhookDeliveriesClient( timeoutMs = 10_000, }: GitHubWebhookDeliveriesClientOptions = {}, ) { - async function send(path: string, init: RequestInit = {}) { + async function send(path: string, operation: string, init: RequestInit = {}) { try { return await request(new URL(path, apiUrl), { ...init, @@ -71,41 +83,91 @@ export function createGitHubWebhookDeliveriesClient( redirect: "error", signal: AbortSignal.timeout(timeoutMs), }) - } catch (error) { - throw new GitHubProviderRequestError({ cause: error }) + } catch { + throw new GitHubWebhookRequestError(operation, "transport") } } + async function read(path: string, operation: string, schema: Schema) { + const response = await send(path, operation) + if (!response.ok) throw new GitHubWebhookRequestError(operation, "http", response.status) + let body: unknown + try { + // GitHub's int64 IDs can exceed Number's precision; retain the JSON token. + body = JSON.parse( + await response.text(), + (key: string, value: unknown, context?: { source?: string }) => + key === "id" && typeof value === "number" && !Number.isSafeInteger(value) + ? context?.source + : value, + ) + } catch { + throw new GitHubWebhookRequestError(operation, "response", response.status) + } + const parsed = schema.safeParse(body) + if (!parsed.success) throw new GitHubWebhookRequestError(operation, "response", response.status) + return { data: parsed.data, response } + } + return { + /** Reads settings only; never returns the webhook secret or makes a test delivery. */ + async getConfiguration() { + const { data } = await read( + "/app/hook/config", + "configuration", + z.object({ + url: z.string(), + content_type: z.string(), + insecure_ssl: z.union([z.string(), z.number()]), + }), + ) + return { + url: data.url, + contentType: data.content_type, + insecureSsl: String(data.insecure_ssl), + } + }, + + async getSubscribedEvents() { + return (await read("/app", "events", z.object({ events: z.array(z.string()) }))).data.events + }, + /** Lists one page of deliveries, newest first. */ async listDeliveries( cursor: string | null, ): Promise<{ deliveries: GitHubWebhookDeliveryAttempt[]; nextCursor: string | null }> { const query = new URLSearchParams({ per_page: pageSize }) if (cursor) query.set("cursor", cursor) - const response = await send(`${deliveriesPath}?${query}`) - if (!response.ok) throw new GitHubProviderRequestError() - const body = deliveriesSchema.safeParse(await response.json().catch(() => undefined)) - if (!body.success) throw new GitHubProviderRequestError({ cause: body.error }) + const { data, response } = await read( + `${deliveriesPath}?${query}`, + "list-deliveries", + deliveriesSchema, + ) return { - deliveries: body.data.map((delivery) => ({ + deliveries: data.map((delivery) => ({ id: delivery.id, guid: delivery.guid, deliveredAt: new Date(delivery.delivered_at), statusCode: delivery.status_code ?? 0, event: delivery.event, installationId: delivery.installation_id ?? null, + ...(delivery.repository_id ? { repositoryId: delivery.repository_id } : {}), })), nextCursor: readNextCursor(response.headers.get("link"), apiUrl), } }, /** Asks GitHub to send a delivery again. Unavailable means GitHub no longer has it. */ - async redeliver(deliveryId: number): Promise<"requested" | "unavailable"> { - const response = await send(`${deliveriesPath}/${deliveryId}/attempts`, { method: "POST" }) + async redeliver(deliveryId: number | string): Promise<"requested" | "unavailable"> { + if (!deliveryIdSchema.safeParse(deliveryId).success) { + throw new TypeError("Invalid GitHub webhook delivery ID.") + } + const response = await send(`${deliveriesPath}/${deliveryId}/attempts`, "redeliver", { + method: "POST", + }) if (response.status === 202) return "requested" if (response.status === 404 || response.status === 410) return "unavailable" - throw new GitHubProviderRequestError() + throw new GitHubWebhookRequestError("redeliver", "http", response.status) }, } } diff --git a/hub/server/integrations/github/create-github-webhook-deliveries-client/types.ts b/hub/server/integrations/github/create-github-webhook-deliveries-client/types.ts index 5b6feec57..3c78c1ad5 100644 --- a/hub/server/integrations/github/create-github-webhook-deliveries-client/types.ts +++ b/hub/server/integrations/github/create-github-webhook-deliveries-client/types.ts @@ -14,11 +14,12 @@ export interface GitHubWebhookDeliveriesClientOptions { /** One attempt to deliver a webhook. Redelivered attempts share the original guid. */ export interface GitHubWebhookDeliveryAttempt { - id: number + id: number | string guid: string deliveredAt: Date /** 0 when GitHub could not reach the Hub at all. */ statusCode: number event: string installationId: number | null + repositoryId?: number } diff --git a/hub/server/integrations/github/errors.ts b/hub/server/integrations/github/errors.ts index d1086fbcd..40ff07481 100644 --- a/hub/server/integrations/github/errors.ts +++ b/hub/server/integrations/github/errors.ts @@ -4,3 +4,14 @@ export class GitHubProviderRequestError extends Error { this.name = "GitHubProviderRequestError" } } + +export class GitHubCommitLookupError extends GitHubProviderRequestError { + readonly statusCode: number + + constructor(status: number) { + super() + this.name = "GitHubCommitLookupError" + this.message = `GitHub commit lookup failed (HTTP ${status}).` + this.statusCode = status + } +} diff --git a/hub/server/repositories/github-builds/find-github-build-author-target/index.ts b/hub/server/repositories/github-builds/find-github-build-author-target/index.ts new file mode 100644 index 000000000..a655bb06e --- /dev/null +++ b/hub/server/repositories/github-builds/find-github-build-author-target/index.ts @@ -0,0 +1,31 @@ +import type { PrismaClient } from "../../../generated/prisma/client.ts" + +export async function findGitHubBuildAuthorTarget( + database: PrismaClient, + input: { buildId: string; githubRepositoryId: number }, +) { + return database.build.findFirst({ + where: { + id: input.buildId, + githubWorkflowRunId: { not: null }, + commitSha: { not: null }, + prototype: { + repository: { + githubRepositoryId: BigInt(input.githubRepositoryId), + removedAt: null, + }, + }, + }, + select: { + id: true, + commitSha: true, + githubWorkflowRunId: true, + authorProvider: true, + authorIssuer: true, + authorProviderUserId: true, + authorLogin: true, + authorName: true, + authorAvatarUrl: true, + }, + }) +} diff --git a/hub/server/repositories/github-builds/find-github-build-author/index.ts b/hub/server/repositories/github-builds/find-github-build-author/index.ts new file mode 100644 index 000000000..e57df395f --- /dev/null +++ b/hub/server/repositories/github-builds/find-github-build-author/index.ts @@ -0,0 +1,24 @@ +import type { PrismaClient } from "../../../generated/prisma/client.ts" + +export function findGitHubBuildAuthor( + database: PrismaClient, + input: { buildId: string; commitSha: string; githubRepositoryId: number }, +) { + return database.build.findFirst({ + where: { + id: input.buildId, + commitSha: input.commitSha, + prototype: { + repository: { githubRepositoryId: BigInt(input.githubRepositoryId), removedAt: null }, + }, + }, + select: { + githubWorkflowRunId: true, + authorProvider: true, + authorIssuer: true, + authorProviderUserId: true, + authorLogin: true, + authorAvatarUrl: true, + }, + }) +} diff --git a/hub/server/repositories/github-builds/list-github-build-runs/index.ts b/hub/server/repositories/github-builds/list-github-build-runs/index.ts index 2de4f98c9..240617cba 100644 --- a/hub/server/repositories/github-builds/list-github-build-runs/index.ts +++ b/hub/server/repositories/github-builds/list-github-build-runs/index.ts @@ -40,6 +40,8 @@ export async function listGitHubBuildRuns( githubWorkflowRunAttempt: true, state: true, workflowCompletedAt: true, + authorProvider: true, + failureSummary: true, githubRunCompletions: { where: { githubWorkflowRunId: { in: runs.map((run) => BigInt(run.id)) } }, select: { githubWorkflowRunId: true, runAttempt: true }, @@ -59,6 +61,8 @@ export async function listGitHubBuildRuns( runAttempt: build.githubWorkflowRunAttempt ?? 0, state: build.state, workflowCompletedAt: build.workflowCompletedAt, + authorProvider: build.authorProvider, + failureSummary: build.failureSummary, runCompletions: build.githubRunCompletions.map((run) => ({ runId: Number(run.githubWorkflowRunId), runAttempt: run.runAttempt, diff --git a/hub/server/repositories/github-builds/list-github-builds-awaiting-import/index.db.test.ts b/hub/server/repositories/github-builds/list-github-builds-awaiting-import/index.db.test.ts index 3c89c651a..aa27152f5 100644 --- a/hub/server/repositories/github-builds/list-github-builds-awaiting-import/index.db.test.ts +++ b/hub/server/repositories/github-builds/list-github-builds-awaiting-import/index.db.test.ts @@ -2,9 +2,10 @@ import { randomInt, randomUUID } from "node:crypto" import { describe, expect, it } from "vitest" -import type { Prisma } from "../../../generated/prisma/client.ts" -import { prisma } from "../../../integrations/prisma/index.ts" -import { listGitHubBuildsAwaitingImport } from "./index.ts" +import type { Prisma } from "../../../generated/prisma/client" +import { prisma } from "../../../integrations/prisma" +import { githubDeselectedWorkflowFailure } from "../../../utils/github-deselected-workflow-failure" +import { listGitHubBuildsAwaitingImport } from "." const createdAfter = new Date(Date.now() - 60 * 60 * 1_000) @@ -76,4 +77,35 @@ describe("listGitHubBuildsAwaitingImport", () => { expect(listed).not.toContainEqual(build) }) + + it("retries failed Builds only while their author lookup is pending", async () => { + const repository = await createRepository() + const authorPending = await repository.createBuild({ + state: "failed", + failureSummary: "The package is invalid.", + }) + const completed = await repository.createBuild({ + state: "failed", + failureSummary: "The package is invalid.", + workflowCompletedAt: new Date(), + }) + const deselected = await repository.createBuild({ + state: "failed", + failureSummary: githubDeselectedWorkflowFailure, + }) + const attributed = await repository.createBuild({ + state: "failed", + failureSummary: "The package is invalid.", + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "9", + }) + + const listed = await listGitHubBuildsAwaitingImport(prisma, { createdAfter, limit: 10_000 }) + + expect(listed).toContainEqual(authorPending) + for (const settled of [completed, deselected, attributed]) { + expect(listed).not.toContainEqual(settled) + } + }) }) diff --git a/hub/server/repositories/github-builds/list-github-builds-awaiting-import/index.ts b/hub/server/repositories/github-builds/list-github-builds-awaiting-import/index.ts index cb409c1f3..cbe678725 100644 --- a/hub/server/repositories/github-builds/list-github-builds-awaiting-import/index.ts +++ b/hub/server/repositories/github-builds/list-github-builds-awaiting-import/index.ts @@ -1,8 +1,10 @@ import type { PrismaClient } from "../../../generated/prisma/client.ts" +import { githubDeselectedWorkflowFailure } from "../../../utils/github-deselected-workflow-failure/index.ts" /** - * Lists recent GitHub Builds whose import has not finished: the dist is still pending, - * or the run has not been seen complete, so late screenshots may still be attachable. + * Lists recent GitHub Builds that still owe an import or completed-run pass, + * including failed Builds whose author lookup is pending. A deselected run's pass + * settles its Build, so it leaves this list. */ export async function listGitHubBuildsAwaitingImport( database: PrismaClient, @@ -15,6 +17,12 @@ export async function listGitHubBuildsAwaitingImport( OR: [ { state: { in: ["queued", "running"] } }, { state: "successful", workflowCompletedAt: null }, + { + state: "failed", + workflowCompletedAt: null, + authorProvider: null, + failureSummary: { not: githubDeselectedWorkflowFailure }, + }, ], prototype: { branchDeletedAt: null, diff --git a/hub/server/repositories/github-builds/record-github-build-author/index.db.test.ts b/hub/server/repositories/github-builds/record-github-build-author/index.db.test.ts new file mode 100644 index 000000000..e5ac1f199 --- /dev/null +++ b/hub/server/repositories/github-builds/record-github-build-author/index.db.test.ts @@ -0,0 +1,117 @@ +import { randomInt, randomUUID } from "node:crypto" + +import { describe, expect, it } from "vitest" + +import { prisma } from "../../../integrations/prisma" +import { recordGitHubBuildAuthor } from "." + +async function createBuild() { + const githubRepositoryId = randomInt(1, 2 ** 31) + const repository = await prisma.repository.create({ + data: { githubRepositoryId, nameWithOwner: `octo/${randomUUID()}`, defaultBranch: "main" }, + }) + const prototype = await prisma.prototype.create({ + data: { + repositoryId: repository.id, + branchName: "proto/welcome", + name: "Welcome", + registeredAt: new Date(), + }, + }) + const build = await prisma.build.create({ + data: { + prototypeId: prototype.id, + number: 1, + state: "successful", + commitSha: "a".repeat(40), + githubWorkflowRunId: 9_007_199_254_740_993n, + githubWorkflowRunAttempt: 2, + distDigest: "sha256:" + "b".repeat(64), + startedAt: new Date("2026-01-01T00:00:00Z"), + finishedAt: new Date("2026-01-01T00:01:00Z"), + workflowCompletedAt: new Date("2026-01-01T00:01:00Z"), + artifacts: { + create: { + kind: "dist", + path: "dist/index.html", + storageKey: randomUUID(), + byteLength: 10, + }, + }, + }, + }) + await prisma.prototype.update({ + where: { id: prototype.id }, + data: { currentBuildId: build.id }, + }) + const input = { + buildId: build.id, + commitSha: build.commitSha!, + githubRepositoryId, + githubWorkflowRunId: build.githubWorkflowRunId, + author: { providerUserId: "101", login: "commit-author", avatarUrl: null }, + } + return { input, find: () => findBuild(build.id) } +} + +function findBuild(id: string) { + return prisma.build.findUniqueOrThrow({ + where: { id }, + include: { artifacts: true, prototype: true }, + }) +} + +describe("recordGitHubBuildAuthor()", () => { + it("records a legacy Build's author once without changing its import, owner, or selection", async () => { + const { input, find } = await createBuild() + const legacy = await find() + const users = await prisma.user.count() + + await expect(recordGitHubBuildAuthor(prisma, input)).resolves.toEqual({ status: "updated" }) + + const repaired = await find() + expect(repaired).toEqual({ + ...legacy, + updatedAt: repaired.updatedAt, + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "101", + authorLogin: "commit-author", + }) + await expect(recordGitHubBuildAuthor(prisma, input)).resolves.toEqual({ + status: "unchanged", + }) + expect(await find()).toEqual(repaired) + expect(await prisma.user.count()).toBe(users) + }) + + it("does not replace an existing Build author snapshot or prototype owner", async () => { + const { input, find } = await createBuild() + const owner = await prisma.user.create({ data: {} }) + const saved = await find() + await prisma.prototype.update({ + where: { id: saved.prototypeId }, + data: { authorId: owner.id }, + }) + await prisma.build.update({ + where: { id: saved.id }, + data: { + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "101", + authorLogin: "saved-author", + authorName: "Saved author name", + }, + }) + const before = await find() + + await expect( + recordGitHubBuildAuthor(prisma, { + ...input, + author: { providerUserId: "202", login: "different-author", avatarUrl: null }, + }), + ).resolves.toEqual({ status: "preserved" }) + + expect(await find()).toEqual(before) + }) +}) diff --git a/hub/server/repositories/github-builds/record-github-build-author/index.ts b/hub/server/repositories/github-builds/record-github-build-author/index.ts new file mode 100644 index 000000000..57333703f --- /dev/null +++ b/hub/server/repositories/github-builds/record-github-build-author/index.ts @@ -0,0 +1,63 @@ +import type { PrismaClient } from "../../../generated/prisma/client.ts" +import { getGitHubBuildAuthorAction } from "../../../utils/get-github-build-author-action/index.ts" + +/** Repairs author metadata without changing Build ownership, artifacts, or selection. */ +export async function recordGitHubBuildAuthor( + database: PrismaClient, + input: { + buildId: string + commitSha: string + githubRepositoryId: number + githubWorkflowRunId: bigint | null + author: { providerUserId: string; login: string | null; avatarUrl: string | null } + }, +) { + return database.$transaction(async (transaction) => { + const [repository] = await transaction.$queryRaw>` + SELECT "id" FROM "repositories" + WHERE "github_repository_id" = ${BigInt(input.githubRepositoryId)} + AND "removed_at" IS NULL + FOR SHARE + ` + if (!repository) throw new Error("The GitHub repository is no longer connected.") + const [build] = await transaction.$queryRaw< + Array<{ + authorProvider: string | null + authorIssuer: string | null + authorProviderUserId: string | null + authorLogin: string | null + authorName: string | null + authorAvatarUrl: string | null + }> + >` + SELECT builds."author_provider" AS "authorProvider", + builds."author_issuer" AS "authorIssuer", + builds."author_provider_user_id" AS "authorProviderUserId", + builds."author_login" AS "authorLogin", + builds."author_name" AS "authorName", + builds."author_avatar_url" AS "authorAvatarUrl" + FROM "builds" + INNER JOIN "prototypes" ON prototypes."id" = builds."prototype_id" + WHERE builds."id" = ${input.buildId}::uuid + AND builds."commit_sha" = ${input.commitSha} + AND builds."github_workflow_run_id" IS NOT DISTINCT FROM ${input.githubWorkflowRunId}::bigint + AND prototypes."repository_id" = ${repository.id}::uuid + FOR UPDATE OF builds + ` + if (!build) throw new Error("The Build no longer matches the repository, commit, and run.") + const action = getGitHubBuildAuthorAction(build, input.author) + if (action === "would-update") { + await transaction.build.update({ + where: { id: input.buildId }, + data: { + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: input.author.providerUserId, + authorLogin: input.author.login, + authorAvatarUrl: input.author.avatarUrl, + }, + }) + } + return { status: action === "would-update" ? ("updated" as const) : action } + }) +} diff --git a/hub/server/repositories/github-builds/record-github-build-run-completion/index.db.test.ts b/hub/server/repositories/github-builds/record-github-build-run-completion/index.db.test.ts index 6a5f17d5f..891a9e83b 100644 --- a/hub/server/repositories/github-builds/record-github-build-run-completion/index.db.test.ts +++ b/hub/server/repositories/github-builds/record-github-build-run-completion/index.db.test.ts @@ -127,26 +127,86 @@ describe("recordGitHubBuildRunCompletion()", () => { ).toEqual(build) }) - it.each(["running", "failed"] as const)( - "does not record a completed pass for a %s Build", - async (state) => { - const build = await createBuild(state) + it("does not record a completed pass for a running Build", async () => { + const build = await createBuild("running") + await expect( + recordGitHubBuildRunCompletion(prisma, { + buildId: build.id, + run: { runId: 100n, runAttempt: 2 }, + completedAt: new Date(), + }), + ).resolves.toBe(false) + + expect(await prisma.gitHubBuildRunCompletion.count()).toBe(0) + expect( + await prisma.build.findUniqueOrThrow({ + where: { id: build.id }, + include: { artifacts: true }, + }), + ).toEqual(build) + }) + + it("marks only the exact failed owning run complete and preserves its failure and artifacts", async () => { + const build = await createBuild("failed") + const owner = await prisma.user.create({ data: {} }) + await prisma.prototype.update({ + where: { id: build.prototypeId }, + data: { authorId: owner.id }, + }) + await prisma.build.update({ + where: { id: build.id }, + data: { + failureSummary: "The package is invalid.", + startedAt: new Date("2026-11-18T11:00:00Z"), + finishedAt: new Date("2026-11-18T11:10:00Z"), + artifacts: { + create: { + kind: "dist", + path: "dist/retained.html", + storageKey: "retained-failure-artifact", + byteLength: 10, + }, + }, + }, + }) + const before = await prisma.build.findUniqueOrThrow({ + where: { id: build.id }, + include: { artifacts: true, prototype: true }, + }) + const completedAt = new Date("2026-11-18T12:00:00Z") + for (const run of [ + { runId: 100n, runAttempt: 1 }, + { runId: 100n, runAttempt: 3 }, + { runId: 101n, runAttempt: 2 }, + ]) { await expect( - recordGitHubBuildRunCompletion(prisma, { - buildId: build.id, - run: { runId: 100n, runAttempt: 2 }, - completedAt: new Date(), - }), + recordGitHubBuildRunCompletion(prisma, { buildId: build.id, run, completedAt }), ).resolves.toBe(false) + } + expect( + await prisma.build.findUniqueOrThrow({ + where: { id: build.id }, + include: { artifacts: true, prototype: true }, + }), + ).toEqual(before) - expect(await prisma.gitHubBuildRunCompletion.count()).toBe(0) - expect( - await prisma.build.findUniqueOrThrow({ - where: { id: build.id }, - include: { artifacts: true }, - }), - ).toEqual(build) - }, - ) + await expect( + recordGitHubBuildRunCompletion(prisma, { + buildId: build.id, + run: { runId: 100n, runAttempt: 2 }, + completedAt, + }), + ).resolves.toBe(true) + const after = await prisma.build.findUniqueOrThrow({ + where: { id: build.id }, + include: { artifacts: true, prototype: true }, + }) + expect(after).toEqual({ + ...before, + workflowCompletedAt: completedAt, + updatedAt: after.updatedAt, + }) + expect(await prisma.gitHubBuildRunCompletion.count({ where: { buildId: build.id } })).toBe(0) + }) }) diff --git a/hub/server/repositories/github-builds/record-github-build-run-completion/index.ts b/hub/server/repositories/github-builds/record-github-build-run-completion/index.ts index b288c2355..e4f5c3929 100644 --- a/hub/server/repositories/github-builds/record-github-build-run-completion/index.ts +++ b/hub/server/repositories/github-builds/record-github-build-run-completion/index.ts @@ -2,8 +2,8 @@ import type { PrismaClient } from "../../../generated/prisma/client.ts" import type { GitHubBuildRun } from "../types.ts" /** - * Records a completed artifact pass per contributing run without changing dist ownership. - * Owning-run completion also retains its existing Build watermark. + * Records completed author and artifact passes per contributing run without changing + * dist ownership. Owning-run completion also retains its existing Build watermark. */ export async function recordGitHubBuildRunCompletion( database: PrismaClient, @@ -11,10 +11,23 @@ export async function recordGitHubBuildRunCompletion( ) { return database.$transaction(async (transaction) => { const build = await transaction.build.findFirst({ - where: { id: input.buildId, state: "successful" }, - select: { id: true }, + where: { id: input.buildId, state: { in: ["successful", "failed"] } }, + select: { id: true, state: true }, }) if (!build) return false + if (build.state === "failed") { + const completed = await transaction.build.updateMany({ + where: { + id: input.buildId, + state: "failed", + githubWorkflowRunId: input.run.runId, + githubWorkflowRunAttempt: input.run.runAttempt, + workflowCompletedAt: null, + }, + data: { workflowCompletedAt: input.completedAt }, + }) + return completed.count === 1 + } const run = { buildId: build.id, githubWorkflowRunId: input.run.runId, diff --git a/hub/server/repositories/github-builds/settle-deselected-github-build/index.db.test.ts b/hub/server/repositories/github-builds/settle-deselected-github-build/index.db.test.ts new file mode 100644 index 000000000..4c7ee4caa --- /dev/null +++ b/hub/server/repositories/github-builds/settle-deselected-github-build/index.db.test.ts @@ -0,0 +1,71 @@ +import { randomInt, randomUUID } from "node:crypto" + +import { describe, expect, it } from "vitest" + +import { prisma } from "../../../integrations/prisma" +import { settleDeselectedGitHubBuild } from "." + +async function createFinishedBuild(githubWorkflowRunAttempt: number) { + const githubRepositoryId = randomInt(1, 2 ** 31) + const repository = await prisma.repository.create({ + data: { githubRepositoryId, nameWithOwner: `octo/${randomUUID()}`, defaultBranch: "main" }, + }) + const prototype = await prisma.prototype.create({ + data: { repositoryId: repository.id, branchName: "proto/welcome" }, + }) + const build = await prisma.build.create({ + data: { + prototypeId: prototype.id, + number: 1, + state: "failed", + failureSummary: "The package is invalid.", + commitSha: "a".repeat(40), + githubWorkflowRunId: 100n, + githubWorkflowRunAttempt, + }, + }) + const run = { + id: 100, + runAttempt: 1, + headBranch: "proto/welcome", + headSha: "a".repeat(40), + status: "completed", + updatedAt: new Date("2026-01-01T00:05:00Z"), + } + return { build, run, githubRepositoryId } +} + +describe("settleDeselectedGitHubBuild()", () => { + it("records only the completion of a finished Build from the completed run", async () => { + const { build, run, githubRepositoryId } = await createFinishedBuild(1) + + await expect(settleDeselectedGitHubBuild(prisma, { githubRepositoryId, run })).resolves.toBe( + true, + ) + + const settled = await prisma.build.findUniqueOrThrow({ where: { id: build.id } }) + expect(settled).toEqual({ + ...build, + updatedAt: settled.updatedAt, + workflowCompletedAt: run.updatedAt, + }) + }) + + it("leaves a finished Build alone for an in-progress run or an older attempt", async () => { + const inProgress = await createFinishedBuild(1) + const newer = await createFinishedBuild(2) + + for (const { run, githubRepositoryId } of [ + { ...inProgress, run: { ...inProgress.run, status: "in_progress" } }, + newer, + ]) { + await expect(settleDeselectedGitHubBuild(prisma, { githubRepositoryId, run })).resolves.toBe( + false, + ) + } + + for (const { build } of [inProgress, newer]) { + expect(await prisma.build.findUniqueOrThrow({ where: { id: build.id } })).toEqual(build) + } + }) +}) diff --git a/hub/server/repositories/github-builds/settle-deselected-github-build/index.ts b/hub/server/repositories/github-builds/settle-deselected-github-build/index.ts index 0b6d73f90..6099eadaf 100644 --- a/hub/server/repositories/github-builds/settle-deselected-github-build/index.ts +++ b/hub/server/repositories/github-builds/settle-deselected-github-build/index.ts @@ -1,6 +1,12 @@ import type { PrismaClient } from "../../../generated/prisma/client.ts" +import { githubDeselectedWorkflowFailure } from "../../../utils/github-deselected-workflow-failure/index.ts" -/** Stops only the unfinished Build still owned by the deselected run and attempt. */ +/** + * Stops only the unfinished Build still owned by the deselected run and attempt. + * After the run completes, a finished Build still awaiting its completed-run pass + * keeps its state, failure, dist, and author, and records only the completion so + * direct recovery stops retrying it. + */ export async function settleDeselectedGitHubBuild( database: PrismaClient, input: { @@ -16,18 +22,29 @@ export async function settleDeselectedGitHubBuild( }, ) { const { run } = input - const owned = { + const runOwned = { githubWorkflowRunId: BigInt(run.id), githubWorkflowRunAttempt: { lte: run.runAttempt }, commitSha: run.headSha, - state: { in: ["queued" as const, "running" as const] }, prototype: { branchName: run.headBranch, branchDeletedAt: null, repository: { githubRepositoryId: BigInt(input.githubRepositoryId), removedAt: null }, }, } + const owned = { ...runOwned, state: { in: ["queued" as const, "running" as const] } } return database.$transaction(async (transaction) => { + if (run.status === "completed") { + const finished = await transaction.build.updateMany({ + where: { + ...runOwned, + state: { in: ["successful", "failed"] }, + workflowCompletedAt: null, + }, + data: { workflowCompletedAt: run.updatedAt }, + }) + if (finished.count > 0) return true + } const build = await transaction.build.findFirst({ where: owned, select: { id: true, startedAt: true }, @@ -42,8 +59,7 @@ export async function settleDeselectedGitHubBuild( startedAt: build.startedAt ?? finishedAt, finishedAt, workflowCompletedAt: run.status === "completed" ? run.updatedAt : null, - failureSummary: - "The Build workflow is no longer selected by builds.workflows in .github/replica.json on the repository's default branch. Select it again and rerun the workflow to retry.", + failureSummary: githubDeselectedWorkflowFailure, }, }) return result.count === 1 diff --git a/hub/server/repositories/github-webhook-deliveries/find-github-webhook-evidence/index.db.test.ts b/hub/server/repositories/github-webhook-deliveries/find-github-webhook-evidence/index.db.test.ts new file mode 100644 index 000000000..3c7190423 --- /dev/null +++ b/hub/server/repositories/github-webhook-deliveries/find-github-webhook-evidence/index.db.test.ts @@ -0,0 +1,87 @@ +import { randomUUID } from "node:crypto" +import { describe, expect, it } from "vitest" + +import { prisma } from "../../../integrations/prisma" +import { findGitHubWebhookEvidence } from "." + +describe("findGitHubWebhookEvidence()", () => { + it("observes only the requested receipt and repository/run Build without changing them", async () => { + const repository = await prisma.repository.create({ + data: { nameWithOwner: "octo/demo", defaultBranch: "main", githubRepositoryId: 56 }, + }) + const prototype = await prisma.prototype.create({ + data: { repositoryId: repository.id, branchName: "proto/demo" }, + }) + const build = await prisma.build.create({ + data: { + prototypeId: prototype.id, + number: 1, + state: "successful", + githubWorkflowRunId: 7, + githubWorkflowRunAttempt: 1, + workflowCompletedAt: new Date(), + }, + }) + const deliveryGuid = randomUUID() + const receipt = await prisma.gitHubWebhookDelivery.create({ + data: { id: deliveryGuid, event: "workflow_run", payloadDigest: "a".repeat(64) }, + }) + const input = { deliveryGuid, githubRepositoryId: 56, runId: 7 } + expect(await findGitHubWebhookEvidence(prisma, input)).toMatchObject({ + receipt: { event: "workflow_run", createdAt: receipt.createdAt }, + redelivery: null, + build: { + state: "successful", + owningAttempt: 1, + completedAttempt: 1, + completedAt: build.workflowCompletedAt, + }, + }) + expect( + (await findGitHubWebhookEvidence(prisma, { ...input, githubRepositoryId: 99 })).build, + ).toBeNull() + expect((await findGitHubWebhookEvidence(prisma, { ...input, runId: 8 })).build).toBeNull() + expect( + (await findGitHubWebhookEvidence(prisma, { ...input, deliveryGuid: randomUUID() })).receipt, + ).toBeNull() + expect(await prisma.build.findUnique({ where: { id: build.id } })).toEqual(build) + expect(await prisma.gitHubWebhookDelivery.findUnique({ where: { id: deliveryGuid } })).toEqual( + receipt, + ) + }) + + it("finds a contributing run's own completed attempt rather than the dist owner's watermark", async () => { + const repository = await prisma.repository.create({ + data: { nameWithOwner: "octo/demo", defaultBranch: "main", githubRepositoryId: 56 }, + }) + const prototype = await prisma.prototype.create({ + data: { repositoryId: repository.id, branchName: "proto/demo" }, + }) + const completionTime = new Date("2026-01-02T00:00:00Z") + await prisma.build.create({ + data: { + prototypeId: prototype.id, + number: 1, + state: "successful", + githubWorkflowRunId: 7, + githubWorkflowRunAttempt: 1, + workflowCompletedAt: new Date("2026-01-01T00:00:00Z"), + githubRunCompletions: { + create: { githubWorkflowRunId: 8, runAttempt: 2, completedAt: completionTime }, + }, + }, + }) + + const evidence = await findGitHubWebhookEvidence(prisma, { + deliveryGuid: randomUUID(), + githubRepositoryId: 56, + runId: 8, + }) + expect(evidence.build).toEqual({ + state: "successful", + owningAttempt: null, + completedAttempt: 2, + completedAt: completionTime, + }) + }) +}) diff --git a/hub/server/repositories/github-webhook-deliveries/find-github-webhook-evidence/index.ts b/hub/server/repositories/github-webhook-deliveries/find-github-webhook-evidence/index.ts new file mode 100644 index 000000000..99597d45d --- /dev/null +++ b/hub/server/repositories/github-webhook-deliveries/find-github-webhook-evidence/index.ts @@ -0,0 +1,61 @@ +import type { PrismaClient } from "../../../generated/prisma/client.ts" + +export async function findGitHubWebhookEvidence( + database: PrismaClient, + input: { deliveryGuid: string; githubRepositoryId: number; runId: number }, +) { + return database.$transaction(async (transaction) => { + await transaction.$executeRaw`SET TRANSACTION READ ONLY` + await transaction.$executeRaw`SET LOCAL statement_timeout = '5s'` + const receipt = await transaction.gitHubWebhookDelivery.findUnique({ + where: { id: input.deliveryGuid }, + select: { event: true, createdAt: true }, + }) + const redelivery = await transaction.gitHubWebhookRedelivery.findUnique({ + where: { id: input.deliveryGuid }, + select: { state: true, attempts: true, failureCode: true }, + }) + const runId = BigInt(input.runId) + const build = await transaction.build.findFirst({ + where: { + OR: [ + { githubWorkflowRunId: runId }, + { githubRunCompletions: { some: { githubWorkflowRunId: runId } } }, + ], + prototype: { repository: { githubRepositoryId: BigInt(input.githubRepositoryId) } }, + }, + select: { + state: true, + githubWorkflowRunId: true, + githubWorkflowRunAttempt: true, + workflowCompletedAt: true, + githubRunCompletions: { + where: { githubWorkflowRunId: runId }, + select: { runAttempt: true, completedAt: true }, + }, + }, + }) + const owningAttempt = + build?.githubWorkflowRunId === runId ? build.githubWorkflowRunAttempt : null + let completion = build?.githubRunCompletions[0] ?? null + if ( + owningAttempt !== null && + build?.workflowCompletedAt && + (!completion || owningAttempt > completion.runAttempt) + ) { + completion = { runAttempt: owningAttempt, completedAt: build.workflowCompletedAt } + } + return { + receipt, + redelivery, + build: build + ? { + state: build.state, + owningAttempt, + completedAttempt: completion?.runAttempt ?? null, + completedAt: completion?.completedAt ?? null, + } + : null, + } + }) +} diff --git a/hub/server/repositories/github-webhook-redeliveries/claim-due-github-webhook-redeliveries/index.ts b/hub/server/repositories/github-webhook-redeliveries/claim-due-github-webhook-redeliveries/index.ts index 66f3c082d..f68b3e2b7 100644 --- a/hub/server/repositories/github-webhook-redeliveries/claim-due-github-webhook-redeliveries/index.ts +++ b/hub/server/repositories/github-webhook-redeliveries/claim-due-github-webhook-redeliveries/index.ts @@ -44,7 +44,10 @@ export async function claimDueGitHubWebhookRedeliveries( if (count === 1) { claimed.push({ attempts: row.attempts + 1, - githubDeliveryId: Number(row.githubDeliveryId), + githubDeliveryId: + row.githubDeliveryId <= BigInt(Number.MAX_SAFE_INTEGER) + ? Number(row.githubDeliveryId) + : row.githubDeliveryId.toString(), id: row.id, }) } diff --git a/hub/server/repositories/github-webhook-redeliveries/types.ts b/hub/server/repositories/github-webhook-redeliveries/types.ts index 3d2db2bab..b268d87c4 100644 --- a/hub/server/repositories/github-webhook-redeliveries/types.ts +++ b/hub/server/repositories/github-webhook-redeliveries/types.ts @@ -1,6 +1,6 @@ export interface NewGitHubWebhookRedelivery { id: string - githubDeliveryId: number + githubDeliveryId: number | string event: string retryAt: Date expiresAt: Date @@ -8,6 +8,6 @@ export interface NewGitHubWebhookRedelivery { export interface ClaimedGitHubWebhookRedelivery { id: string - githubDeliveryId: number + githubDeliveryId: number | string attempts: number } diff --git a/hub/server/repositories/prototype-index/find-prototype-detail/index.db.test.ts b/hub/server/repositories/prototype-index/find-prototype-detail/index.db.test.ts index 8bab52d42..cc5eff611 100644 --- a/hub/server/repositories/prototype-index/find-prototype-detail/index.db.test.ts +++ b/hub/server/repositories/prototype-index/find-prototype-detail/index.db.test.ts @@ -5,6 +5,75 @@ import { prisma } from "../../../integrations/prisma" import { findPrototypeDetail } from "." describe("findPrototypeDetail()", () => { + it("uses the current Build's author on every history page without creating an owner", async () => { + const repository = await prisma.repository.create({ + data: { nameWithOwner: "octo/demo", defaultBranch: "main" }, + }) + const prototype = await prisma.prototype.create({ + data: { repositoryId: repository.id, branchName: "proto/demo" }, + }) + const current = await prisma.build.create({ + data: { + prototypeId: prototype.id, + number: 1, + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "9", + authorLogin: "commit-author", + }, + }) + await prisma.build.create({ data: { prototypeId: prototype.id, number: 2 } }) + await prisma.prototype.update({ + where: { id: prototype.id }, + data: { currentBuildId: current.id }, + }) + + const detail = await findPrototypeDetail(prisma, repository.id, prototype.id, undefined, { + page: 1, + pageSize: 1, + }) + expect(detail?.prototype).toMatchObject({ + authorLogin: "commit-author", + authorProvider: "github", + }) + expect(await prisma.user.count()).toBe(0) + + await prisma.prototype.update({ where: { id: prototype.id }, data: { currentBuildId: null } }) + const olderPage = await findPrototypeDetail(prisma, repository.id, prototype.id, undefined, { + page: 2, + pageSize: 1, + }) + expect(olderPage?.prototype.authorLogin).toBeNull() + }) + + it("keeps the explicit Hub owner rather than substituting a contributor", async () => { + const repository = await prisma.repository.create({ + data: { nameWithOwner: "octo/demo", defaultBranch: "main" }, + }) + const owner = await prisma.user.create({ + data: { profile: { create: { provider: "github", login: "owner" } } }, + }) + const prototype = await prisma.prototype.create({ + data: { + repositoryId: repository.id, + branchName: "proto/demo", + authorId: owner.id, + builds: { + create: { + number: 1, + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "9", + authorLogin: "contributor", + }, + }, + }, + }) + expect( + (await findPrototypeDetail(prisma, repository.id, prototype.id))?.prototype.authorLogin, + ).toBe("owner") + }) + it("maps successful Build artifacts to access URLs and the selected Controls identity", async () => { const repositoryId = randomUUID() const prototypeId = randomUUID() diff --git a/hub/server/repositories/prototype-index/find-prototype-detail/index.ts b/hub/server/repositories/prototype-index/find-prototype-detail/index.ts index 7c174dc62..9e3fb4019 100644 --- a/hub/server/repositories/prototype-index/find-prototype-detail/index.ts +++ b/hub/server/repositories/prototype-index/find-prototype-detail/index.ts @@ -1,4 +1,4 @@ -import { Prisma, type BuildState } from "../../../generated/prisma/client.ts" +import { Prisma, type Build } from "../../../generated/prisma/client.ts" import { trimPrototypeMetadata } from "../../../utils/trim-prototype-metadata/index.ts" import { type RepositoryScope, visibleRepositoryWhere } from "../visible-repository-where/index.ts" @@ -117,14 +117,19 @@ export async function findPrototypeDetail( return secondCreatedAt - firstCreatedAt || second.number - first.number }) - let latestBuild: { number: number; state: BuildState } | null = + type HeaderBuild = Pick< + Build, + "number" | "state" | "authorLogin" | "authorName" | "authorProvider" + > + let latestBuild: HeaderBuild | null = builds.find(({ id }) => id === prototype.currentBuildId) ?? builds[0] ?? null if (page) { // The header describes the whole prototype, not only this page. latestBuild = ( - await transaction.$queryRaw<{ number: number; state: BuildState }[]>(Prisma.sql` - SELECT number, state FROM builds + await transaction.$queryRaw(Prisma.sql` + SELECT number, state, author_login AS "authorLogin", + author_name AS "authorName", author_provider AS "authorProvider" FROM builds WHERE prototype_id = ${prototypeId}::uuid AND ( ${prototype.currentBuildId}::uuid IS NULL @@ -193,6 +198,16 @@ export async function findPrototypeDetail( const controlsIdentity = selectedBuild?.artifacts.find( ({ path }) => path === "dist/controls.replica.json", )?.storageKey + const authorBuild = latestBuild + const author = prototype.author + ? prototype.author.profile + : authorBuild?.authorProvider === "github" + ? { + login: authorBuild.authorLogin, + name: authorBuild.authorName, + provider: authorBuild.authorProvider, + } + : null return { repository, prototype: { @@ -202,9 +217,9 @@ export async function findPrototypeDetail( branchName: prototype.branchName, description: prototype.description, startPage: (prototype.startPage && trimPrototypeMetadata(prototype.startPage)) || "/", - authorLogin: prototype.author?.profile?.login ?? null, - authorName: prototype.author?.profile?.name ?? null, - authorProvider: prototype.author?.profile?.provider ?? null, + authorLogin: author?.login ?? null, + authorName: author?.name ?? null, + authorProvider: author?.provider ?? null, canEdit: false, latestBuild: latestBuild ? { number: latestBuild.number, state: latestBuild.state } diff --git a/hub/server/repositories/prototype-index/find-repository-index/index.db.test.ts b/hub/server/repositories/prototype-index/find-repository-index/index.db.test.ts new file mode 100644 index 000000000..501d0b167 --- /dev/null +++ b/hub/server/repositories/prototype-index/find-repository-index/index.db.test.ts @@ -0,0 +1,204 @@ +import { describe, expect, it } from "vitest" + +import { prisma } from "../../../integrations/prisma" +import { findRepositoryIndex } from "." + +describe("findRepositoryIndex()", () => { + it("uses the current Build's provider snapshot without inventing a Hub owner", async () => { + const repository = await prisma.repository.create({ + data: { nameWithOwner: "octo/demo", defaultBranch: "main" }, + }) + const prototype = await prisma.prototype.create({ + data: { + repositoryId: repository.id, + branchName: "proto/demo", + name: "Demo", + registeredAt: new Date(), + }, + }) + const current = await prisma.build.create({ + data: { + prototypeId: prototype.id, + number: 1, + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "9", + authorLogin: "commit-author", + }, + }) + await prisma.build.create({ + data: { prototypeId: prototype.id, number: 2 }, + }) + await prisma.prototype.update({ + where: { id: prototype.id }, + data: { currentBuildId: current.id }, + }) + + const index = await findRepositoryIndex(prisma, repository.id) + expect(index?.prototypes[0]).toMatchObject({ authorId: null, authorLogin: "commit-author" }) + expect(index?.authors).toEqual([]) + expect(await prisma.user.count()).toBe(0) + }) + + it("keeps the explicit Hub owner rather than substituting a contributor", async () => { + const repository = await prisma.repository.create({ + data: { nameWithOwner: "octo/demo", defaultBranch: "main" }, + }) + const owner = await prisma.user.create({ + data: { profile: { create: { provider: "github", login: "owner" } } }, + }) + const prototype = await prisma.prototype.create({ + data: { + repositoryId: repository.id, + branchName: "proto/demo", + name: "Demo", + registeredAt: new Date(), + authorId: owner.id, + }, + }) + await prisma.build.create({ + data: { + prototypeId: prototype.id, + number: 1, + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "9", + authorLogin: "contributor", + }, + }) + expect((await findRepositoryIndex(prisma, repository.id))?.prototypes[0]).toMatchObject({ + authorId: owner.id, + authorLogin: "owner", + }) + }) + + it("searches only the displayed Build author within the requested repository", async () => { + const repository = await prisma.repository.create({ + data: { nameWithOwner: "octo/demo", defaultBranch: "main" }, + }) + const otherRepository = await prisma.repository.create({ + data: { nameWithOwner: "octo/other", defaultBranch: "main" }, + }) + const owner = await prisma.user.create({ + data: { profile: { create: { provider: "github", login: "explicit-owner" } } }, + }) + function createPrototype(branch: string) { + return prisma.prototype.create({ + data: { + repositoryId: branch === "other" ? otherRepository.id : repository.id, + branchName: `proto/${branch}`, + name: branch, + registeredAt: new Date(), + ...(branch === "owned" ? { authorId: owner.id } : {}), + }, + }) + } + const [current, latest, owned, other] = await Promise.all([ + createPrototype("current"), + createPrototype("latest"), + createPrototype("owned"), + createPrototype("other"), + ]) + const authorIdentity = { + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "9", + } + const currentBuild = await prisma.build.create({ + data: { + prototypeId: current.id, + number: 1, + ...authorIdentity, + authorLogin: "displayed-current", + createdAt: new Date("2026-01-01"), + }, + }) + await prisma.prototype.update({ + where: { id: current.id }, + data: { currentBuildId: currentBuild.id }, + }) + await prisma.build.createMany({ + data: [ + { + prototypeId: current.id, + number: 2, + ...authorIdentity, + authorLogin: "hidden-newer", + createdAt: new Date("2026-01-02"), + }, + { + prototypeId: latest.id, + number: 1, + ...authorIdentity, + authorLogin: "displayed-latest", + createdAt: new Date("2026-01-01"), + workflowCreatedAt: new Date("2026-01-03"), + }, + { + prototypeId: latest.id, + number: 2, + ...authorIdentity, + authorLogin: "hidden-older", + createdAt: new Date("2026-01-02"), + }, + { + prototypeId: owned.id, + number: 1, + ...authorIdentity, + authorLogin: "hidden-contributor", + }, + { + prototypeId: other.id, + number: 1, + ...authorIdentity, + authorLogin: "displayed-current", + }, + ], + }) + async function search(query: string) { + return findRepositoryIndex(prisma, repository.id, { query, authorId: null, page: 1 }) + } + + expect((await search("displayed-current"))?.prototypes.map(({ id }) => id)).toEqual([ + current.id, + ]) + expect((await search("DISPLAYED-LATEST"))?.prototypes.map(({ id }) => id)).toEqual([latest.id]) + expect((await search("hidden"))?.pagination.totalCount).toBe(0) + expect((await search("explicit-owner"))?.prototypes.map(({ id }) => id)).toEqual([owned.id]) + expect((await search("%"))?.pagination.totalCount).toBe(0) + expect((await search("_"))?.pagination.totalCount).toBe(0) + }) + + it("counts and pages fallback-author matches before selecting cards", async () => { + const repository = await prisma.repository.create({ + data: { nameWithOwner: "octo/demo", defaultBranch: "main" }, + }) + for (let index = 0; index < 25; index++) { + await prisma.prototype.create({ + data: { + repositoryId: repository.id, + branchName: `proto/demo-${index}`, + name: `Demo ${index}`, + registeredAt: new Date(Date.UTC(2026, 0, 1, index)), + builds: { + create: { + number: 1, + authorProvider: "github", + authorIssuer: "https://github.com", + authorProviderUserId: "9", + authorLogin: "matching", + }, + }, + }, + }) + } + + const page = await findRepositoryIndex(prisma, repository.id, { + query: "matching", + authorId: null, + page: 2, + }) + expect(page?.pagination).toMatchObject({ page: 2, totalCount: 25, pageCount: 2 }) + expect(page?.prototypes.map(({ branchName }) => branchName)).toEqual(["proto/demo-0"]) + }) +}) diff --git a/hub/server/repositories/prototype-index/find-repository-index/index.ts b/hub/server/repositories/prototype-index/find-repository-index/index.ts index c59d60a6a..973e6f9e3 100644 --- a/hub/server/repositories/prototype-index/find-repository-index/index.ts +++ b/hub/server/repositories/prototype-index/find-repository-index/index.ts @@ -32,17 +32,6 @@ export async function findRepositoryIndex( const where: Prisma.PrototypeWhereInput = { ...eligible, ...(filters.authorId ? { authorId: filters.authorId } : {}), - ...(search - ? { - OR: [ - { name: search }, - { description: search }, - { branchName: search }, - { author: { profile: { login: search } } }, - { author: { profile: { name: search } } }, - ], - } - : {}), } // Keep the count, page, and cards from the same database snapshot. const { @@ -53,13 +42,64 @@ export async function findRepositoryIndex( latestBuilds: buildSummaries, } = await database.$transaction( async (transaction) => { - const totalCount = await transaction.prototype.count({ where }) + // Prisma cannot filter by the same COALESCE-ordered Build used by the card. + const searchFrom = search + ? Prisma.sql` + FROM prototypes AS prototype + LEFT JOIN user_profiles AS profile ON profile.user_id = prototype.author_id + LEFT JOIN LATERAL ( + SELECT builds.author_provider, builds.author_login + FROM builds + WHERE builds.prototype_id = prototype.id + AND (prototype.current_build_id IS NULL OR builds.id = prototype.current_build_id) + ORDER BY COALESCE(builds.workflow_created_at, builds.created_at) DESC, + builds.number DESC + LIMIT 1 + ) AS author_build ON prototype.author_id IS NULL + WHERE prototype.repository_id = ${repository.id}::uuid + AND prototype.registered_at IS NOT NULL + AND prototype.branch_deleted_at IS NULL + AND NOT prototype.replica + AND prototype.branch_name NOT IN (${repository.defaultBranch}, 'replica/install-workflow') + ${filters.authorId ? Prisma.sql`AND prototype.author_id = ${filters.authorId}::uuid` : Prisma.empty} + AND ( + prototype.name ILIKE ${`%${search.contains}%`} + OR prototype.description ILIKE ${`%${search.contains}%`} + OR prototype.branch_name ILIKE ${`%${search.contains}%`} + OR profile.login ILIKE ${`%${search.contains}%`} + OR profile.name ILIKE ${`%${search.contains}%`} + OR ( + prototype.author_id IS NULL AND author_build.author_provider = 'github' + AND author_build.author_login ILIKE ${`%${search.contains}%`} + ) + ) + ` + : null + let totalCount: number + if (searchFrom) { + const [count] = await transaction.$queryRaw<{ count: number }[]>( + Prisma.sql`SELECT COUNT(*)::int AS count ${searchFrom}`, + ) + if (!count) throw new Error("The prototype search count was not returned.") + totalCount = count.count + } else { + totalCount = await transaction.prototype.count({ where }) + } // A page past the end shows the last page, as a shared link may outlive results. const page = Math.min(filters.page, Math.max(1, Math.ceil(totalCount / pageSize))) + const matchingIds = searchFrom + ? ( + await transaction.$queryRaw<{ id: string }[]>(Prisma.sql` + SELECT prototype.id ${searchFrom} + ORDER BY prototype.registered_at DESC, prototype.id DESC + LIMIT ${pageSize} OFFSET ${(page - 1) * pageSize} + `) + ).map(({ id }) => id) + : null const records = await transaction.prototype.findMany({ - where, + where: { ...where, ...(matchingIds ? { id: { in: matchingIds } } : {}) }, orderBy: [{ registeredAt: "desc" }, { id: "desc" }], - skip: (page - 1) * pageSize, + skip: matchingIds ? 0 : (page - 1) * pageSize, take: pageSize, select: { id: true, @@ -74,12 +114,20 @@ export async function findRepositoryIndex( const latestBuilds = records.length > 0 ? await transaction.$queryRaw< - { prototypeId: string; number: number; state: BuildState }[] + { + prototypeId: string + number: number + state: BuildState + authorProvider: string | null + authorLogin: string | null + }[] >(Prisma.sql` - SELECT prototypes.id AS "prototypeId", latest_build.number, latest_build.state + SELECT prototypes.id AS "prototypeId", latest_build.number, latest_build.state, + latest_build.author_provider AS "authorProvider", + latest_build.author_login AS "authorLogin" FROM prototypes LEFT JOIN LATERAL ( - SELECT builds.number, builds.state + SELECT builds.number, builds.state, builds.author_provider, builds.author_login FROM builds WHERE builds.prototype_id = prototypes.id AND ( @@ -137,6 +185,7 @@ export async function findRepositoryIndex( const prototypes = records.map((record) => { if (!record.name) throw new Error("A registered prototype must have a name.") addAuthor(record.author) + const latestBuild = latestBuilds.get(record.id) return { id: record.id, repositoryId: record.repositoryId, @@ -144,9 +193,13 @@ export async function findRepositoryIndex( description: record.description, branchName: record.branchName, authorId: record.author?.id ?? null, - authorLogin: record.author?.profile?.login ?? null, + authorLogin: record.author + ? (record.author.profile?.login ?? null) + : latestBuild?.authorProvider === "github" + ? latestBuild.authorLogin + : null, canEdit: false, - latestBuild: latestBuilds.get(record.id) ?? null, + latestBuild: latestBuild ? { number: latestBuild.number, state: latestBuild.state } : null, builds: [], } }) diff --git a/hub/server/repositories/public-sharing/find-hosted-sharing-prototype/index.ts b/hub/server/repositories/public-sharing/find-hosted-sharing-prototype/index.ts index f674d6462..2cfd06b1a 100644 --- a/hub/server/repositories/public-sharing/find-hosted-sharing-prototype/index.ts +++ b/hub/server/repositories/public-sharing/find-hosted-sharing-prototype/index.ts @@ -3,7 +3,7 @@ import type { Prisma, PrismaClient } from "../../../generated/prisma/client.ts" export function findHostedSharingPrototype( database: PrismaClient | Prisma.TransactionClient, input: { repositoryId: string; prototypeId: string }, - author?: { issuer: string; subject: string }, + author?: { userId: string; issuer: string; subject: string }, ) { return database.prototype.findFirst({ where: { @@ -15,6 +15,7 @@ export function findHostedSharingPrototype( repository: { githubRepositoryId: { not: null }, localPath: null, removedAt: null }, ...(author ? { + authorId: author.userId, builds: { some: { state: "successful", diff --git a/hub/server/routes/api/public-sharing/hosted-sharing.db.test.ts b/hub/server/routes/api/public-sharing/hosted-sharing.db.test.ts index b7e549395..0efc28043 100644 --- a/hub/server/routes/api/public-sharing/hosted-sharing.db.test.ts +++ b/hub/server/routes/api/public-sharing/hosted-sharing.db.test.ts @@ -225,14 +225,29 @@ describe("hosted public sharing", () => { } }) - it("binds browser management to repository admin or a successful immutable author on that prototype", async () => { + it("requires both prototype ownership and a successful immutable author, or repository admin", async () => { const target = await createTarget(100, "51") const other = await createTarget(200, "99") const selectedBuild = await createBuild(target.prototype.id, 2, "99") await createBuild(other.prototype.id, 2, "51", false) + const wrongIssuer = await createBuild(other.prototype.id, 3, "51") + const wrongProvider = await createBuild(other.prototype.id, 4, "51") + await prisma.build.update({ + where: { id: wrongIssuer.id }, + data: { authorIssuer: "https://github.example.test" }, + }) + await prisma.build.update({ + where: { id: wrongProvider.id }, + data: { authorProvider: "entra" }, + }) const author = await createActor("51") + const otherAuthor = await createActor("99") const hubAdmin = await createActor("52", "ADMINISTRATOR") const repoAdmin = await createActor("53") + await prisma.prototype.update({ + where: { id: other.prototype.id }, + data: { authorId: author.userId }, + }) const entra = { ...author, sessionProvider: "entra", role: "ADMINISTRATOR" as const } const lookup = vi.fn((viewer) => Promise.resolve( @@ -244,14 +259,50 @@ describe("hosted public sharing", () => { ), ) const sharing = await createPublicSharingService(prisma, enabledEnvironment, { mode: "hosted" }) - const hub = await startHub(sharing, { author, hubAdmin, repoAdmin, entra }, lookup) + const hub = await startHub(sharing, { author, otherAuthor, hubAdmin, repoAdmin, entra }, lookup) try { + const unownedPublicPath = await createLink(hub, target.links, "repoAdmin", 2) + const unownedShareId = unownedPublicPath.split("/").at(-1)! + const unowned = await hub.request(`${target.path}?view=minimized`, "author") + const unownedBody = (await unowned.json()) as { + prototype: { canManageShareLinks: boolean } + } + expect({ + capability: unownedBody.prototype.canManageShareLinks, + list: (await hub.request(target.links, "author")).status, + create: (await hub.request(target.links, "author", "POST", linkForm)).status, + revoke: (await hub.request(`${target.links}/${unownedShareId}`, "author", "DELETE")).status, + }).toEqual({ capability: false, list: 403, create: 403, revoke: 403 }) + expect((await hub.request(unownedPublicPath)).status).toBe(200) + + await prisma.prototype.update({ + where: { id: target.prototype.id }, + data: { authorId: author.userId }, + }) const prototype = await hub.request(`${target.path}?view=minimized&build=2`, "author") expect(await prototype.json()).toMatchObject({ prototype: { canManageShareLinks: true } }) + const anotherAuthor = await hub.request(`${target.path}?view=minimized`, "otherAuthor") + expect(await anotherAuthor.json()).toMatchObject({ + prototype: { canManageShareLinks: false }, + }) + expect((await hub.request(target.links, "otherAuthor")).status).toBe(403) + expect((await hub.request(target.links, "otherAuthor", "POST", linkForm)).status).toBe(403) + expect( + (await hub.request(`${target.links}/${unownedShareId}`, "otherAuthor", "DELETE")).status, + ).toBe(403) + expect((await hub.request(unownedPublicPath)).status).toBe(200) + expect( + (await prisma.prototype.findUniqueOrThrow({ where: { id: target.prototype.id } })).authorId, + ).toBe(author.userId) const failedAuthor = await hub.request(`${other.path}?view=minimized`, "author") expect(await failedAuthor.json()).toMatchObject({ prototype: { canManageShareLinks: false } }) + expect((await hub.request(other.links, "author", "POST", linkForm)).status).toBe(403) expect((await hub.request(target.links, "hubAdmin", "POST", linkForm)).status).toBe(403) expect((await hub.request(target.links, "entra", "POST", linkForm)).status).toBe(403) + expect( + (await hub.request(`${target.links}/${unownedShareId}`, "author", "DELETE")).status, + ).toBe(200) + expect((await hub.request(unownedPublicPath)).status).toBe(404) const publicPath = await createLink(hub, target.links, "author", 2) const shareId = publicPath.split("/").at(-1)! @@ -283,11 +334,54 @@ describe("hosted public sharing", () => { } }) + it("rechecks prototype ownership after the GitHub permission lookup", async () => { + const target = await createTarget(100, "51") + const author = await createActor("51") + const nextOwner = await createActor("52") + await prisma.prototype.update({ + where: { id: target.prototype.id }, + data: { authorId: author.userId }, + }) + const started = signal() + const release = signal() + const lookup = vi.fn(async () => { + started.resolve() + await release.promise + return "read" + }) + const sharing = await createPublicSharingService(prisma, enabledEnvironment, { mode: "hosted" }) + const hub = await startHub(sharing, { author }, lookup) + try { + const pending = hub.request(target.links, "author", "POST", linkForm) + expect( + await Promise.race([started.promise.then(() => "lookup"), pending.then(() => "response")]), + ).toBe("lookup") + await prisma.prototype.update({ + where: { id: target.prototype.id }, + data: { authorId: nextOwner.userId }, + }) + release.resolve() + + expect((await pending).status).toBe(403) + expect(await prisma.shareLink.count()).toBe(0) + expect( + (await prisma.prototype.findUniqueOrThrow({ where: { id: target.prototype.id } })).authorId, + ).toBe(nextOwner.userId) + } finally { + release.resolve() + await hub.close() + } + }) + it("does not mistake provider failure for loss and invalidates earlier positive proof after scoped loss", async () => { const target = await createTarget(100, "51") const other = await createTarget(200, "51") const author = await createActor("51") const repoAdmin = await createActor("52") + await prisma.prototype.updateMany({ + where: { id: { in: [target.prototype.id, other.prototype.id] } }, + data: { authorId: author.userId }, + }) const lookup = vi.fn((viewer) => Promise.resolve( viewer.externalIdentityId === repoAdmin.externalIdentityId ? "admin" : "read", @@ -333,6 +427,10 @@ describe("hosted public sharing", () => { it("serves only the pinned Build over public handoffs and blocks them permanently after policy disablement", async () => { const target = await createTarget(100, "51") const author = await createActor("51") + await prisma.prototype.update({ + where: { id: target.prototype.id }, + data: { authorId: author.userId }, + }) const lookup = vi.fn().mockResolvedValue("read") const sharing = await createPublicSharingService(prisma, enabledEnvironment, { mode: "hosted" }) const hub = await startHub(sharing, { author }, lookup) @@ -367,6 +465,10 @@ describe("hosted public sharing", () => { it("permanently revokes links on a confirmed installation disconnection, not an ambiguous access event", async () => { const target = await createTarget(100, "51") const author = await createActor("51") + await prisma.prototype.update({ + where: { id: target.prototype.id }, + data: { authorId: author.userId }, + }) const provider = { configurationDigest: "a".repeat(64), revision: 1 } const auth = await prisma.authenticationProvider.create({ data: { key: "github", enabled: false }, diff --git a/hub/server/scripts/azure-entrypoint/index.test.ts b/hub/server/scripts/azure-entrypoint/index.test.ts index 1124940be..56a5a000b 100644 --- a/hub/server/scripts/azure-entrypoint/index.test.ts +++ b/hub/server/scripts/azure-entrypoint/index.test.ts @@ -10,6 +10,29 @@ vi.mock("../../integrations/pino/create-logger", () => ({ })) describe("Azure executable entrypoint", () => { + it("reports maintenance startup failures even when logging is disabled without exposing secrets", async () => { + const originalArguments = process.argv + const originalExitCode = process.exitCode + const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true) + process.argv = ["node", "synthetic-azure-entrypoint", "check-github-webhooks"] + process.exitCode = undefined + onTestFinished(() => { + process.argv = originalArguments + process.exitCode = originalExitCode + stderr.mockRestore() + }) + mocks.run.mockRejectedValue(new Error("synthetic-private-secret")) + vi.resetModules() + + await expect(import(".")).resolves.toBeDefined() + + expect(process.exitCode).toBe(1) + expect(stderr).toHaveBeenCalledWith( + expect.stringContaining("check secret references, managed identity"), + ) + expect(JSON.stringify(stderr.mock.calls)).not.toContain("synthetic-private-secret") + }) + it.each(["Error", "non-Error"] as const)( "reports a safe fatal record and exit status for a secret-bearing %s rejection", async (kind) => { diff --git a/hub/server/scripts/azure-entrypoint/index.ts b/hub/server/scripts/azure-entrypoint/index.ts index b4d3d06ea..e8a7210a9 100644 --- a/hub/server/scripts/azure-entrypoint/index.ts +++ b/hub/server/scripts/azure-entrypoint/index.ts @@ -4,9 +4,15 @@ import { runAzureEntrypoint } from "./run/index.ts" try { await runAzureEntrypoint(process.argv[2]) } catch (error) { - createLogger().fatal( - { errorType: error instanceof Error ? error.name : "UnknownError" }, - "Azure startup failed; check secret references, managed identity, Key Vault access, and workload configuration", - ) + if (["repair-github-build-author", "check-github-webhooks"].includes(process.argv[2] ?? "")) { + process.stderr.write( + "Azure maintenance could not load its runtime environment; check secret references, managed identity, Key Vault access, and workload configuration.\n", + ) + } else { + createLogger().fatal( + { errorType: error instanceof Error ? error.name : "UnknownError" }, + "Azure startup failed; check secret references, managed identity, Key Vault access, and workload configuration", + ) + } process.exitCode = 1 } diff --git a/hub/server/scripts/azure-entrypoint/run/index.test.ts b/hub/server/scripts/azure-entrypoint/run/index.test.ts index 7a292dde2..6431fa188 100644 --- a/hub/server/scripts/azure-entrypoint/run/index.test.ts +++ b/hub/server/scripts/azure-entrypoint/run/index.test.ts @@ -11,6 +11,10 @@ const mocks = vi.hoisted(() => ({ loggerImported: vi.fn(), setup: vi.fn<() => Promise>(), provision: vi.fn(), + repairImported: vi.fn(), + checkImported: vi.fn(), + repair: vi.fn<() => Promise>(), + check: vi.fn<() => Promise>(), info: vi.fn(), })) vi.mock("../load-secret-environment", () => ({ @@ -40,8 +44,48 @@ describe("runAzureEntrypoint()", () => { mocks.loggerImported() return { logger: { info: mocks.info } } }) + vi.doMock("../../repair-github-build-author/run", () => { + mocks.repairImported(process.env.DATABASE_URL) + return { runGitHubBuildAuthorRepair: mocks.repair } + }) + vi.doMock("../../check-github-webhooks/run", () => { + mocks.checkImported(process.env.DATABASE_URL) + return { runGitHubWebhookCheck: mocks.check } + }) }) + it.each([ + ["repair-github-build-author", "repair"], + ["check-github-webhooks", "check"], + ] as const)( + "loads runtime secrets before importing the %s maintenance command and forwards its arguments", + async (target, command) => { + const originalEnvironment = process.env + const originalArguments = process.argv + process.env = { RUNTIME_MODE: "combined" } + process.argv = ["node", "azure-entrypoint", target, "--repository-id", "56"] + onTestFinished(() => { + process.env = originalEnvironment + process.argv = originalArguments + }) + mocks.loadSecrets.mockImplementation((environment) => { + expect(mocks.repairImported).not.toHaveBeenCalled() + expect(mocks.checkImported).not.toHaveBeenCalled() + environment.DATABASE_URL = "synthetic-database-secret" + return Promise.resolve() + }) + + await runAzureEntrypoint(target) + + expect(mocks.loadSecrets).toHaveBeenCalledExactlyOnceWith(process.env, "runtime") + expect(mocks[`${command}Imported`]).toHaveBeenCalledExactlyOnceWith( + "synthetic-database-secret", + ) + expect(mocks[command]).toHaveBeenCalledExactlyOnceWith(["--repository-id", "56"]) + expect(mocks.runtimeImported).not.toHaveBeenCalled() + }, + ) + it.each(["runtime", "setup", "provision"] as const)( "loads all secrets before importing only the selected %s entrypoint", async (target) => { diff --git a/hub/server/scripts/azure-entrypoint/run/index.ts b/hub/server/scripts/azure-entrypoint/run/index.ts index 787f5bdbb..2c885f536 100644 --- a/hub/server/scripts/azure-entrypoint/run/index.ts +++ b/hub/server/scripts/azure-entrypoint/run/index.ts @@ -1,12 +1,21 @@ import { loadAzureSecretEnvironment } from "../load-secret-environment/index.ts" export async function runAzureEntrypoint(target: string | undefined): Promise { - if (!["runtime", "setup", "provision"].includes(target ?? "")) { - throw new Error("Azure entrypoint must be runtime, setup, or provision.") + if ( + ![ + "runtime", + "setup", + "provision", + "repair-github-build-author", + "check-github-webhooks", + ].includes(target ?? "") + ) { + throw new Error("Azure entrypoint target is invalid.") } const environment = process.env + const maintenance = target === "repair-github-build-author" || target === "check-github-webhooks" if ( - (target === "runtime" && environment.RUNTIME_MODE === "setup") || + ((target === "runtime" || maintenance) && environment.RUNTIME_MODE === "setup") || (target === "setup" && environment.RUNTIME_MODE !== "setup") ) { throw new Error("Azure entrypoint and runtime mode disagree.") @@ -14,6 +23,16 @@ export async function runAzureEntrypoint(target: string | undefined): Promise ({ + configuration: vi.fn(), + check: vi.fn(), +})) +vi.mock("../../../domains/authentication/read-github-app-configuration", () => ({ + readGitHubAppConfiguration: mocks.configuration, +})) +vi.mock("../../../domains/github-webhooks/check-github-webhook-communication", () => ({ + checkGitHubWebhookCommunication: mocks.check, +})) + +import { runGitHubWebhookCheck } from "." + +describe("runGitHubWebhookCheck()", () => { + let originalExitCode: typeof process.exitCode + + beforeEach(() => { + originalExitCode = process.exitCode + process.exitCode = undefined + vi.stubEnv("LOG_LEVEL", "silent") + vi.stubEnv("HUB_APPLICATION_ORIGIN", "https://hub.example") + mocks.configuration.mockReturnValue({ + appCredentials: { appId: "1", installationId: "2", privateKey: "synthetic-secret" }, + }) + }) + + afterEach(() => { + process.exitCode = originalExitCode + }) + + it("prints webhook check results independently of log level and reports failures", async () => { + const stdout = vi.spyOn(process.stdout, "write").mockImplementation(() => true) + mocks.check.mockResolvedValue([{ name: "webhook_events", result: "fail", detail: "Missing." }]) + + await runGitHubWebhookCheck(["--repository-id", "56", "--run-id", "7"]) + + expect(stdout).toHaveBeenCalledWith( + `${JSON.stringify({ + checks: [{ name: "webhook_events", result: "fail", detail: "Missing." }], + })}\n`, + ) + expect(process.exitCode).toBe(1) + }) + + it("hides unexpected diagnostic errors", async () => { + const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true) + mocks.check.mockRejectedValue(new Error("synthetic-provider-secret")) + + await runGitHubWebhookCheck(["--repository-id", "56", "--run-id", "7"]) + + expect(stderr).toHaveBeenCalledWith(expect.stringContaining("GitHub maintenance failed;")) + expect(stderr).not.toHaveBeenCalledWith(expect.stringContaining("synthetic-provider-secret")) + expect(process.exitCode).toBe(1) + }) +}) diff --git a/hub/server/scripts/check-github-webhooks/run/index.ts b/hub/server/scripts/check-github-webhooks/run/index.ts new file mode 100644 index 000000000..983c92442 --- /dev/null +++ b/hub/server/scripts/check-github-webhooks/run/index.ts @@ -0,0 +1,67 @@ +import { parseArgs } from "node:util" + +import { readGitHubAppConfiguration } from "../../../domains/authentication/read-github-app-configuration/index.ts" +import { checkGitHubWebhookCommunication } from "../../../domains/github-webhooks/check-github-webhook-communication/index.ts" +import { GitHubMaintenanceInputError } from "../../github-maintenance-errors.ts" +import { presentGitHubMaintenanceFailure } from "../../present-github-maintenance-failure/index.ts" + +export async function runGitHubWebhookCheck(args: string[]) { + try { + function parseArguments() { + try { + return parseArgs({ + args, + options: { + "repository-id": { type: "string" }, + "run-id": { type: "string" }, + "delivery-guid": { type: "string" }, + }, + }) + } catch (error) { + if ( + error instanceof TypeError && + "code" in error && + typeof error.code === "string" && + error.code.startsWith("ERR_PARSE_ARGS_") + ) { + throw new GitHubMaintenanceInputError("webhook-usage") + } + throw error + } + } + const { values } = parseArguments() + const githubRepositoryId = Number(values["repository-id"]) + const runId = Number(values["run-id"]) + const deliveryGuid = values["delivery-guid"] + const applicationOrigin = process.env.HUB_APPLICATION_ORIGIN + if ( + ![githubRepositoryId, runId].every((id) => Number.isSafeInteger(id) && id > 0) || + !applicationOrigin || + (deliveryGuid && !/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(deliveryGuid)) + ) { + throw new GitHubMaintenanceInputError("webhook-usage") + } + const credentials = readGitHubAppConfiguration(process.env).appCredentials + if (!credentials) throw new GitHubMaintenanceInputError("configuration") + const database = deliveryGuid + ? (await import("../../../integrations/prisma/index.ts")).prisma + : undefined + try { + const checks = await checkGitHubWebhookCommunication({ + credentials, + applicationOrigin, + githubRepositoryId, + runId, + deliveryGuid, + database, + }) + process.stdout.write(`${JSON.stringify({ checks })}\n`) + if (checks.some((check) => check.result === "fail")) process.exitCode = 1 + } finally { + await database?.$disconnect() + } + } catch (error) { + process.stderr.write(`${presentGitHubMaintenanceFailure(error)}\n`) + process.exitCode = 1 + } +} diff --git a/hub/server/scripts/github-maintenance-errors.ts b/hub/server/scripts/github-maintenance-errors.ts new file mode 100644 index 000000000..3634d00ee --- /dev/null +++ b/hub/server/scripts/github-maintenance-errors.ts @@ -0,0 +1,13 @@ +export class GitHubMaintenanceInputError extends Error { + constructor(reason: "repair-usage" | "webhook-usage" | "configuration") { + const messages = { + "repair-usage": + "Provide --repository-id and --build-id; add --apply only to save the repair.", + "webhook-usage": + "Provide --repository-id, --run-id, and HUB_APPLICATION_ORIGIN; --delivery-guid is optional.", + configuration: "GitHub App installation settings are incomplete or invalid.", + } + super(messages[reason]) + this.name = "GitHubMaintenanceInputError" + } +} diff --git a/hub/server/scripts/present-github-maintenance-failure/index.ts b/hub/server/scripts/present-github-maintenance-failure/index.ts new file mode 100644 index 000000000..aa22953fe --- /dev/null +++ b/hub/server/scripts/present-github-maintenance-failure/index.ts @@ -0,0 +1,24 @@ +import { GitHubBuildAuthorTargetError } from "../../domains/github-builds/errors.ts" +import { GitHubWebhookRequestError } from "../../integrations/github/create-github-webhook-deliveries-client/errors.ts" +import { GitHubCommitLookupError } from "../../integrations/github/errors.ts" +import { GitHubMaintenanceInputError } from "../github-maintenance-errors.ts" + +export function presentGitHubMaintenanceFailure(error: unknown): string { + if ( + error instanceof GitHubMaintenanceInputError || + error instanceof GitHubBuildAuthorTargetError + ) { + return error.message + } + if (error instanceof GitHubCommitLookupError) { + return `GitHub commit lookup failed (HTTP ${error.statusCode}).` + } + if (error instanceof GitHubWebhookRequestError) { + const status = + error.statusCode && Number.isInteger(error.statusCode) && error.statusCode <= 599 + ? `, HTTP ${error.statusCode}` + : "" + return `GitHub webhook request failed (${error.failure}${status}).` + } + return "GitHub maintenance failed; check the scoped IDs, App configuration, and database access." +} diff --git a/hub/server/scripts/repair-github-build-author/index.ts b/hub/server/scripts/repair-github-build-author/index.ts new file mode 100644 index 000000000..20c05e1e2 --- /dev/null +++ b/hub/server/scripts/repair-github-build-author/index.ts @@ -0,0 +1,5 @@ +import "dotenv/config" + +import { runGitHubBuildAuthorRepair } from "./run/index.ts" + +await runGitHubBuildAuthorRepair(process.argv.slice(2)) diff --git a/hub/server/scripts/repair-github-build-author/run/index.test.ts b/hub/server/scripts/repair-github-build-author/run/index.test.ts new file mode 100644 index 000000000..39c33074d --- /dev/null +++ b/hub/server/scripts/repair-github-build-author/run/index.test.ts @@ -0,0 +1,105 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" + +import { GitHubBuildAuthorTargetError } from "../../../domains/github-builds/errors" +import { GitHubCommitLookupError } from "../../../integrations/github/errors" + +const mocks = vi.hoisted(() => ({ + configuration: vi.fn(), + repair: vi.fn(), + actions: vi.fn(), + disconnect: vi.fn(), +})) +vi.mock("../../../domains/authentication/read-github-app-configuration", () => ({ + readGitHubAppConfiguration: mocks.configuration, +})) +vi.mock("../../../domains/github-builds/repair-github-build-author", () => ({ + repairGitHubBuildAuthor: mocks.repair, +})) +vi.mock("../../../integrations/github/create-github-actions-client", () => ({ + createGitHubActionsClient: mocks.actions, +})) +vi.mock("../../../integrations/prisma", () => ({ + prisma: { $disconnect: mocks.disconnect }, +})) +vi.mock("../../../integrations/pino", () => ({ logger: { warn: vi.fn() } })) + +import { runGitHubBuildAuthorRepair } from "." + +const buildId = "00000000-0000-4000-8000-000000000001" + +describe("runGitHubBuildAuthorRepair()", () => { + let originalExitCode: typeof process.exitCode + + beforeEach(() => { + originalExitCode = process.exitCode + process.exitCode = undefined + vi.stubEnv("LOG_LEVEL", "silent") + mocks.configuration.mockReturnValue({ + appCredentials: { appId: "1", installationId: "2", privateKey: "synthetic-secret" }, + }) + mocks.actions.mockReturnValue({}) + mocks.disconnect.mockResolvedValue(undefined) + }) + + afterEach(() => { + process.exitCode = originalExitCode + }) + + it("prints the author repair preview even when operational logging is silent", async () => { + const stdout = vi.spyOn(process.stdout, "write").mockImplementation(() => true) + const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true) + mocks.repair.mockResolvedValue({ + status: "available", + author: { providerUserId: "7", login: "mona", avatarUrl: null }, + build: "would-update", + prototypeOwner: "unchanged", + }) + + await runGitHubBuildAuthorRepair(["--repository-id", "56", "--build-id", buildId]) + + expect(stdout).toHaveBeenCalledWith( + `${JSON.stringify({ + buildId, + status: "available", + author: { providerUserId: "7", login: "mona", avatarUrl: null }, + build: "would-update", + prototypeOwner: "unchanged", + })}\n`, + ) + expect(mocks.repair).toHaveBeenCalledWith(expect.anything(), { + githubRepositoryId: 56, + buildId, + apply: false, + }) + expect(mocks.disconnect).toHaveBeenCalledOnce() + expect(stderr).not.toHaveBeenCalled() + }) + + it("reports safe usage without echoing invalid argument values", async () => { + const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true) + + await runGitHubBuildAuthorRepair(["--synthetic-secret=private"]) + + expect(stderr).toHaveBeenCalledWith( + "Provide --repository-id and --build-id; add --apply only to save the repair.\n", + ) + expect(stderr).not.toHaveBeenCalledWith(expect.stringContaining("synthetic-secret")) + expect(process.exitCode).toBe(1) + }) + + it.each([ + [new GitHubBuildAuthorTargetError(), "No GitHub Build matches that repository and Build ID."], + [new GitHubCommitLookupError(403), "GitHub commit lookup failed (HTTP 403)."], + [new Error("synthetic-provider-secret"), "GitHub maintenance failed;"], + ])("reports known safe errors and hides unexpected details", async (error, expected) => { + const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true) + mocks.repair.mockRejectedValue(error) + + await runGitHubBuildAuthorRepair(["--repository-id", "56", "--build-id", buildId]) + + expect(stderr).toHaveBeenCalledWith(expect.stringContaining(expected)) + expect(stderr).not.toHaveBeenCalledWith(expect.stringContaining("synthetic-provider-secret")) + expect(mocks.disconnect).toHaveBeenCalledOnce() + expect(process.exitCode).toBe(1) + }) +}) diff --git a/hub/server/scripts/repair-github-build-author/run/index.ts b/hub/server/scripts/repair-github-build-author/run/index.ts new file mode 100644 index 000000000..4feb3bd04 --- /dev/null +++ b/hub/server/scripts/repair-github-build-author/run/index.ts @@ -0,0 +1,66 @@ +import { parseArgs } from "node:util" + +import { readGitHubAppConfiguration } from "../../../domains/authentication/read-github-app-configuration/index.ts" +import { repairGitHubBuildAuthor } from "../../../domains/github-builds/repair-github-build-author/index.ts" +import { createGitHubActionsClient } from "../../../integrations/github/create-github-actions-client/index.ts" +import { logger } from "../../../integrations/pino/index.ts" +import { GitHubMaintenanceInputError } from "../../github-maintenance-errors.ts" +import { presentGitHubMaintenanceFailure } from "../../present-github-maintenance-failure/index.ts" + +export async function runGitHubBuildAuthorRepair(args: string[]) { + try { + function parseArguments() { + try { + return parseArgs({ + args, + options: { + "repository-id": { type: "string" }, + "build-id": { type: "string" }, + apply: { type: "boolean", default: false }, + }, + }) + } catch (error) { + if ( + error instanceof TypeError && + "code" in error && + typeof error.code === "string" && + error.code.startsWith("ERR_PARSE_ARGS_") + ) { + throw new GitHubMaintenanceInputError("repair-usage") + } + throw error + } + } + const { values } = parseArguments() + const githubRepositoryId = Number(values["repository-id"]) + const buildId = values["build-id"] + if ( + !Number.isSafeInteger(githubRepositoryId) || + githubRepositoryId <= 0 || + !buildId || + !/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(buildId) + ) { + throw new GitHubMaintenanceInputError("repair-usage") + } + const credentials = readGitHubAppConfiguration(process.env).appCredentials + if (!credentials) throw new GitHubMaintenanceInputError("configuration") + const { prisma } = await import("../../../integrations/prisma/index.ts") + try { + const result = await repairGitHubBuildAuthor( + { + database: prisma, + client: createGitHubActionsClient({ ...credentials, githubRepositoryId }), + logger, + }, + { githubRepositoryId, buildId, apply: values.apply }, + ) + process.stdout.write(`${JSON.stringify({ buildId, ...result })}\n`) + if (result.status === "unavailable") process.exitCode = 1 + } finally { + await prisma.$disconnect() + } + } catch (error) { + process.stderr.write(`${presentGitHubMaintenanceFailure(error)}\n`) + process.exitCode = 1 + } +} diff --git a/hub/server/utils/get-github-build-author-action/index.ts b/hub/server/utils/get-github-build-author-action/index.ts new file mode 100644 index 000000000..74099ed8e --- /dev/null +++ b/hub/server/utils/get-github-build-author-action/index.ts @@ -0,0 +1,33 @@ +export function getGitHubBuildAuthorAction( + existing: { + authorProvider: string | null + authorIssuer: string | null + authorProviderUserId: string | null + authorLogin: string | null + authorName: string | null + authorAvatarUrl: string | null + }, + author: { providerUserId: string; login: string | null }, +): "would-update" | "unchanged" | "preserved" { + const noIdentity = + existing.authorProvider === null && + existing.authorIssuer === null && + existing.authorProviderUserId === null + const sameIdentity = + existing.authorProvider === "github" && + existing.authorIssuer === "https://github.com" && + existing.authorProviderUserId === author.providerUserId + if ( + (!noIdentity && !sameIdentity) || + (noIdentity && + (existing.authorLogin !== null || + existing.authorName !== null || + existing.authorAvatarUrl !== null)) + ) { + return "preserved" + } + if (noIdentity && !author.login) { + throw new Error("A GitHub author login is required to record a new identity.") + } + return noIdentity ? "would-update" : "unchanged" +} diff --git a/hub/server/utils/github-deselected-workflow-failure/index.ts b/hub/server/utils/github-deselected-workflow-failure/index.ts new file mode 100644 index 000000000..23ae276ea --- /dev/null +++ b/hub/server/utils/github-deselected-workflow-failure/index.ts @@ -0,0 +1,3 @@ +// This persisted summary also distinguishes a deselected run during recovery. Keep it stable. +export const githubDeselectedWorkflowFailure = + "The Build workflow is no longer selected by builds.workflows in .github/replica.json on the repository's default branch. Select it again and rerun the workflow to retry." diff --git a/hub/server/workers/github-recovery.test.ts b/hub/server/workers/github-recovery.test.ts index 7ad032a73..679de006f 100644 --- a/hub/server/workers/github-recovery.test.ts +++ b/hub/server/workers/github-recovery.test.ts @@ -60,6 +60,7 @@ describe("GitHub recovery workers", () => { listBranchHeads: vi.fn(() => Promise.resolve([])), isAncestor: vi.fn(() => Promise.resolve(false)), getWorkflowRun: vi.fn(() => Promise.resolve(null)), + getCommitAuthor: vi.fn(() => Promise.resolve(null)), listWorkflowRuns: vi.fn(() => Promise.resolve([])), listRunArtifacts: vi.fn(() => Promise.resolve([])), downloadArtifact: vi.fn(() => Promise.resolve({ status: "gone" as const })),