export type CiCompletedEvent = { event: string action: string repo: string branch: string headSha: string conclusion: string htmlUrl: string | null checkName: string | null workflowName: string | null /** * GitHub check suite id. check_suite, workflow_run (check_suite_id) and * check_run (check_suite.id) for one CI run share it, so it keys the * deferred re-check once per suite no matter how many events arrive. */ checkSuiteId: string | null } /** Back-compat name: a completed CI event whose conclusion is a failure. */ export type CiFailureEvent = CiCompletedEvent const failureConclusions = new Set(['failure', 'timed_out']) export function isFailureConclusion(conclusion: string) { return failureConclusions.has(conclusion) } function asString(value: unknown) { return typeof value === 'string' ? value : '' } function asId(value: unknown) { if (typeof value === 'number' && Number.isFinite(value)) return String(value) if (typeof value === 'string' && value.trim()) return value.trim() return null } /** * Parse a GitHub check_suite / check_run / workflow_run webhook body into a * completed-CI candidate (any conclusion), or null when it is not a completed * CI event with a branch and head SHA. */ export function parseCiCompletedPayload(input: { event: string json: unknown }): CiCompletedEvent | null { const event = input.event const json = input.json if (!json || typeof json !== 'object') return null const body = json as Record const action = asString(body.action) const repository = (body.repository || {}) as Record const repo = asString(repository.full_name) if (!repo) return null if (action !== 'completed') return null if (event === 'check_suite') { const suite = (body.check_suite || {}) as Record const conclusion = asString(suite.conclusion) const branch = asString(suite.head_branch) const headSha = asString(suite.head_sha) if (!conclusion || !branch || !headSha) return null return { event, action, repo, branch, headSha, conclusion, htmlUrl: asString(suite.url) || null, checkName: null, workflowName: null, checkSuiteId: asId(suite.id), } } if (event === 'check_run') { const run = (body.check_run || {}) as Record const checkSuite = (run.check_suite || {}) as Record const conclusion = asString(run.conclusion) const branch = asString(checkSuite.head_branch) const headSha = asString(run.head_sha) || asString(checkSuite.head_sha) if (!conclusion || !branch || !headSha) return null return { event, action, repo, branch, headSha, conclusion, htmlUrl: asString(run.html_url) || null, checkName: asString(run.name) || null, workflowName: null, checkSuiteId: asId(checkSuite.id), } } if (event === 'workflow_run') { const run = (body.workflow_run || {}) as Record const conclusion = asString(run.conclusion) const branch = asString(run.head_branch) const headSha = asString(run.head_sha) if (!conclusion || !branch || !headSha) return null return { event, action, repo, branch, headSha, conclusion, htmlUrl: asString(run.html_url) || null, checkName: null, workflowName: asString(run.name) || null, checkSuiteId: asId(run.check_suite_id), } } return null } /** * Parse a GitHub webhook JSON body into a CI-failure candidate, or null. * Accepts check_suite / check_run / workflow_run completed failures * (conclusion `failure` or `timed_out`). */ export function parseCiFailurePayload(input: { event: string json: unknown }): CiFailureEvent | null { const parsed = parseCiCompletedPayload(input) if (!parsed || !isFailureConclusion(parsed.conclusion)) return null return parsed }