Skip to content
File

Blob: tests/worker/webhooks/public-helpers.ts

typescript72 lines
1import {
2 CommitSha,
3 UnixTimestampMs,
4 type BranchName as BranchNameValue,
5 type ProjectId,
6 RunId,
7 type UserId,
8} from "@/contracts";
9import { expectTrusted } from "@/worker/contracts";
10import { fillManualRunQueueWithoutAlarmOrRunInitialization } from "../../helpers/project-do";
11import { getProjectStub } from "../../helpers/runtime";
12 
13export const BEFORE_SHA = "1111111111111111111111111111111111111111";
14export const AFTER_SHA = "2222222222222222222222222222222222222222";
15export const THIRD_SHA = "3333333333333333333333333333333333333333";
16 
17export const buildVerifiedPushDeliveryInput = (input: {
18 projectId: ProjectId;
19 repoUrl: string;
20 deliveryId: string;
21 branch: BranchNameValue;
22 verifiedWebhookUpdatedAt: number;
23}) => ({
24 projectId: input.projectId,
25 payload: {
26 provider: "github" as const,
27 deliveryId: input.deliveryId,
28 eventKind: "push" as const,
29 eventName: "push",
30 repoUrl: input.repoUrl,
31 ref: "refs/heads/main",
32 branch: input.branch,
33 commitSha: expectTrusted(CommitSha, AFTER_SHA, "CommitSha"),
34 beforeSha: expectTrusted(CommitSha, BEFORE_SHA, "CommitSha"),
35 },
36 outcome: "accepted" as const,
37 verifiedWebhookUpdatedAt: expectTrusted(UnixTimestampMs, input.verifiedWebhookUpdatedAt, "UnixTimestampMs"),
38});
39 
40export const fillProjectQueueToCapacity = async (input: {
41 projectId: ProjectId;
42 userId: UserId;
43 branch: BranchNameValue;
44}): Promise<void> =>
45 await fillManualRunQueueWithoutAlarmOrRunInitialization(
46 getProjectStub(input.projectId),
47 {
48 projectId: input.projectId,
49 triggeredByUserId: input.userId,
50 branch: input.branch,
51 },
52 20,
53 );
54 
55export const freeOneQueuedSlot = async (projectId: ProjectId, runId: string): Promise<void> => {
56 const claim = await getProjectStub(projectId).claimRunWork({
57 projectId,
58 runId: expectTrusted(RunId, runId, "RunId"),
59 });
60 if (claim.kind !== "execute") {
61 throw new Error(`Expected queued slot release claim to execute, got ${claim.kind}.`);
62 }
63 
64 await getProjectStub(projectId).finalizeRunExecution({
65 projectId,
66 runId: expectTrusted(RunId, runId, "RunId"),
67 terminalStatus: "failed",
68 lastError: "capacity_freed_for_webhook_resend",
69 sandboxDestroyed: true,
70 });
71};