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
148 changes: 147 additions & 1 deletion apps/api/src/handlers/github/__tests__/recordWebhook.test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 40 additions & 1 deletion apps/api/src/handlers/github/recordWebhook.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { db, webhooks as webhooksTable, eq } from '@roomote/db/server';
import {
and,
db,
webhooks as webhooksTable,
eq,
isNull,
} from '@roomote/db/server';
import type { SourceControlProvider } from '@roomote/types';

import type { WebhookResponse } from '../../types';
import { redactWebhookPayload } from '../webhook-payload-redaction';

const UNKNOWN_HANDLER_OUTCOME_ERROR =
'Webhook handler outcome is unknown because a redelivery found its durable claim nonterminal; the handler was not replayed';

/**
* Records a webhook after executing the handler, setting status based on the response.
* Uses INSERT ... ON CONFLICT DO NOTHING to atomically claim the deliveryId before
Expand Down Expand Up @@ -50,6 +59,36 @@ export async function recordWebhook<T>(

// Skip only if there was a conflict (not if there was a DB error)
if (!hadInsertError && insertedRecord === undefined) {
// A nonterminal duplicate can be in progress or missing its final audit update.
// Never replay it; the original handler can still overwrite this unknown result.
try {
const [recovered] = await db
.update(webhooksTable)
.set({
failedAt: new Date(),
error: UNKNOWN_HANDLER_OUTCOME_ERROR,
})
.where(
and(
eq(webhooksTable.provider, provider),
eq(webhooksTable.deliveryId, deliveryId),
isNull(webhooksTable.succeededAt),
isNull(webhooksTable.failedAt),
),
)
.returning({ id: webhooksTable.id });

if (recovered) {
console.warn(
`[recordWebhook] Finalized unclassified ${provider} webhook ${deliveryId} without replaying its handler`,
);
}
} catch (recoveryError) {
console.error(
`[recordWebhook] Failed to finalize unclassified ${provider} webhook ${deliveryId}:`,
recoveryError instanceof Error ? recoveryError.message : recoveryError,
);
}
return;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading