import { expect } from "@playwright/test"; import { spindleGet } from "../harness/api"; import { config } from "../harness/config"; import { type Ctx, bobbin, flow, hydrant, knot, page, patchId, pullId, record, rkeyOf, settingsAt, target, testid, through, unique } from "../harness/flow"; import { base, branches, finished, merges, onBranches, pipelineFor, renamesTo, workflow, workflowPath } from "../harness/shared"; import { WEBHOOKS, delivers, dropsHook, hookOn, hookUrl, nothingMore } from "../harness/webhooks"; const secret = "e2e-webhook-secret"; // the base push has to be through spindle before the hook exists, or it would deliver that push too const onHookedRepo = (name: string, events: string[], options: { secret?: string } = {}) => flow(name) .actor("owner") .repo({ spindle: config.spindleDid }) .push({ ...base, [workflowPath]: workflow }) .step( "spindle has a pipeline for the first push", pipelineFor("push", (ctx) => ctx.pushed.sha) ) .step("point a webhook at the receiver", hookOn(events, options)); const tips = new WeakMap(); const rememberTip = async (ctx: Ctx) => { tips.set(ctx, ctx.repo.heads[ctx.repo.defaultBranch]); }; const pushDelivered = delivers( "push", (ctx) => `Tangled-Hook/${ctx.pushed.sha.slice(0, 7)}`, (body, ctx) => { expect(body).toMatchObject({ ref: `refs/heads/${ctx.repo.defaultBranch}`, before: tips.get(ctx), after: ctx.pushed.sha, pusher: { did: ctx.actor.did }, repository: { name: ctx.repo.name } }); } ); onHookedRepo("pushing delivers one signed push webhook", ["push"], { secret }) .step("remember the branch tip", rememberTip) .push({ "e2e/more.txt": "more\n" }) .step("the receiver got the push", pushDelivered) .step("nothing else arrived", nothingMore) .step("take the webhook away", dropsHook); onHookedRepo("a push that skips ci still delivers an unsigned push webhook", ["push"]) .step("remember the branch tip", rememberTip) .push({ "e2e/more.txt": "more\n" }, { options: ["skip-ci"] }) .step("the receiver got the push", pushDelivered) .step("nothing else arrived", nothingMore) .step("take the webhook away", dropsHook); const hookSwitch = target("the webhook switch", (tab, ctx) => testid("webhookSwitch", { url: hookUrl(ctx) }).locate(tab, ctx) ); onHookedRepo("a webhook switched off gets nothing", ["push"], { secret }) .open(settingsAt("hooks")) .click(hookSwitch) .step("the switch goes off", async (ctx) => { await expect(hookSwitch.locate(ctx.page, ctx)).not.toBeChecked(); }) .step("spindle lists it inactive", async (ctx) => { const listed = await spindleGet(ctx.actor, `${WEBHOOKS}.listWebhooks`, { repo: ctx.repo.did }); expect(listed.body.webhooks).toMatchObject([{ url: hookUrl(ctx), active: false }]); }) .push({ "e2e/more.txt": "more\n" }) .step( "spindle has a pipeline for the push", pipelineFor("push", (ctx) => ctx.pushed.sha) ) .step("nothing arrived", nothingMore) .step("take the webhook away", dropsHook); let renamedTo = ""; flow("renaming a repo delivers one repository:renamed webhook") .actor("owner") .repo({ spindle: config.spindleDid }) .step("point a webhook at the receiver", hookOn(["repository:renamed"], { secret })) .step("pick the new name", async (ctx) => { renamedTo = `${ctx.repo.name}-renamed`; }) .use(renamesTo(() => renamedTo)) .step("the rename finishes on the new settings page", (ctx) => finished( ctx, "the rename did not finish", ctx.page.waitForURL(`${config.webUrl}/${ctx.actor.handle}/${renamedTo}/settings`, { timeout: 5000 }) ) ) .step( "the receiver got the rename", delivers("repository:renamed", "Tangled-Hook/rename", (body, ctx) => { expect(body).toMatchObject({ old_name: ctx.repo.name, new_name: renamedTo, sender: { did: ctx.actor.did }, repository: { name: renamedTo, full_name: `${ctx.actor.did}/${renamedTo}` } }); }) ) .step("nothing else arrived", nothingMore) .step("take the webhook away", dropsHook); const pullEvents = [ "pull_request:created", "pull_request:resubmitted", "pull_request:merged", "pull_request:closed", "pull_request:reopened" ]; const pullTitle = unique("a pull to hook"); // who the page acts as changes with the step, so a pull names its author and its sender apart const pullDelivered = ( action: string, who: (ctx: Ctx) => string, state: string, round: number, sha: (ctx: Ctx) => string ) => delivers(`pull_request:${action}`, "Tangled-Hook/pull_request", (body, ctx) => { expect(body).toMatchObject({ action, sender: { did: who(ctx) }, pull_request: { title: pullTitle, state, target_branch: branches.into, round_number: round, owner: { did: author }, source: { sha: sha(ctx) } } }); }); let author = ""; const resubmittedId = record("org.tangled.review.patch"); // the author opens it so the payload's owner and sender can be told apart once the owner acts const openedPull = (name: string) => onBranches(name, { ci: true }) .step( "spindle has a pipeline for the first push", pipelineFor("push", (ctx) => ctx.repo.heads[branches.into]) ) .step("point a webhook at the receiver", hookOn(pullEvents, { secret })) .actor("author") .step("remember the author", async (ctx) => { author = ctx.actor.did; }) .pull(pullId, patchId, { ...branches, title: pullTitle }) .step( "the receiver got the new pull", pullDelivered( "created", (ctx) => ctx.actor.did, "open", 1, (ctx) => ctx.repo.heads[branches.from] ) ); openedPull("pull request webhooks follow a pull from opening to merging") .actor("owner") .open(page.pull(pullId)) .changeState(pullId, "closed", testid("requestClose"), testid("changeState")) .step( "the receiver got the close", pullDelivered( "closed", (ctx) => ctx.actor.did, "closed", 1, (ctx) => ctx.repo.heads[branches.from] ) ) .changeState(pullId, "open", testid("changeState"), testid("requestClose")) .step( "the receiver got the reopen", pullDelivered( "reopened", (ctx) => ctx.actor.did, "open", 1, (ctx) => ctx.repo.heads[branches.from] ) ) .use(merges) .step( "the receiver got the merge", pullDelivered( "merged", (ctx) => ctx.actor.did, "merged", 1, (ctx) => ctx.repo.heads[branches.from] ) ) .step("nothing else arrived", nothingMore) .step("take the webhook away", dropsHook); openedPull("resubmitting a pull delivers pull_request:resubmitted") .actor("owner") .push({ "e2e/more.txt": "more\n" }, { branch: branches.from }) .mirrored() .actor("author") .open(page.pull(pullId)) .click(testid("resubmit")) .sends(resubmittedId, knot, (ctx) => ({ patch: { ticket: rkeyOf(ctx, pullId) } }), "create") .trace(resubmittedId, through(knot, hydrant, bobbin)) .step( "the receiver got the resubmit", pullDelivered( "resubmitted", (ctx) => ctx.actor.did, "open", 2, (ctx) => ctx.repo.heads[branches.from] ) ) .step("nothing else arrived", nothingMore) .step("take the webhook away", dropsHook);