Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/app/server/routes/publish.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export default eventHandler(async (event) => {
}
const runId = Number(runIdHeader);
const workflowsBucket = useWorkflowsBucket(event);
const debugBucket = useDebugBucket(event);
const workflowData = await workflowsBucket.getItem(key);
const webhookDebugData = await debugBucket.getItem(key);

if (!workflowData) {
throw createError({
Expand Down Expand Up @@ -323,6 +325,7 @@ export default eventHandler(async (event) => {
workflowData,
key,
runId,
webhookDebug: webhookDebugData,
},
};
});
Expand Down
29 changes: 25 additions & 4 deletions packages/app/server/routes/webhook.post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PullRequestEvent } from "@octokit/webhooks-types";
import type { HandlerFunction } from "@octokit/webhooks/dist-types/types";
import type { PullRequestData, WorkflowData } from "../types";
import type { PullRequestData, WorkflowData, WebhookDebugData } from "../types";
import { hash } from "ohash";

// mark a PR as a PR :)
Expand All @@ -16,6 +16,7 @@ export default eventHandler(async (event) => {
const { test } = useRuntimeConfig(event);
const workflowsBucket = useWorkflowsBucket(event);
const pullRequestNumbersBucket = usePullRequestNumbersBucket(event);
const debugBucket = useDebugBucket(event);

const workflowHandler: HandlerFunction<"workflow_run", unknown> = async ({
payload,
Expand Down Expand Up @@ -51,9 +52,8 @@ export default eventHandler(async (event) => {
await pullRequestNumbersBucket.hasItem(oldPrDataHash);

const isPullRequest = isNewPullRequest || isOldPullRequest;
const prNumber = await pullRequestNumbersBucket.getItem(
isNewPullRequest ? prKey : oldPrDataHash,
);
const lookupKey = isNewPullRequest ? prKey : oldPrDataHash;
const prNumber = await pullRequestNumbersBucket.getItem(lookupKey);

const data: WorkflowData = {
owner,
Expand All @@ -64,6 +64,27 @@ export default eventHandler(async (event) => {

// Publishing is only available throughout the lifetime of a workflow_job
await workflowsBucket.setItem(hashKey, data);

const debugData: WebhookDebugData = {
action: payload.action,
head_branch: payload.workflow_run.head_branch,
head_repository_full_name:
payload.workflow_run.head_repository?.full_name || null,
full_name: payload.repository.full_name,

isPullRequest,
prNumber,
prNumberType: typeof prNumber,
isNewPullRequest,
isOldPullRequest,

prKey,
oldPrDataHash,
lookupKey,
data,
};

await debugBucket.setItem(hashKey, debugData);
}
};

Expand Down
19 changes: 19 additions & 0 deletions packages/app/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,22 @@ export interface Cursor {
timestamp: number;
sha: string;
}

export interface WebhookDebugData {
webhookAction: string;
originalHeadBranch: string | null;
originalHeadRepository: string | null;
originalRepositoryFullName: string | null;

isPullRequest: boolean;
prNumber: number | null;
prNumberType: string;
isNewPullRequest: boolean;
isOldPullRequest: boolean;

prKey: string;
oldPrDataHash: string;
lookupKey: string;

finalWorkflowData: WorkflowData;
}
10 changes: 9 additions & 1 deletion packages/app/server/utils/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { H3EventContext } from "h3";
import type { Cursor, WorkflowData } from "../types";
import type { Cursor, WorkflowData, WebhookDebugData } from "../types";
import { createStorage, joinKeys, prefixStorage } from "unstorage";
import cloudflareR2BindingDriver from "unstorage/drivers/cloudflare-r2-binding";
import { getR2Binding } from "unstorage/drivers/utils/cloudflare";
Expand Down Expand Up @@ -112,3 +112,11 @@ usePullRequestNumbersBucket.base = joinKeys(
useBucket.base,
usePullRequestNumbersBucket.key,
);

export function useDebugBucket(event: Event) {
const storage = useBucket(event);
return prefixStorage<WebhookDebugData>(storage, useDebugBucket.key);
}

useDebugBucket.key = "debug";
useDebugBucket.base = joinKeys(useBucket.base, useDebugBucket.key);
Loading