|
| 1 | +import { getDynamoClient } from "lib/dynamo"; |
| 2 | + |
| 3 | +const OOT_TABLE = "torchci-oot-workflow-job"; |
| 4 | +const MAX_PAYLOAD_BYTES = 2 * 1024 * 1024; // 2MB |
| 5 | + |
| 6 | +// ---- Types ---- |
| 7 | + |
| 8 | +export interface RelayTrusted { |
| 9 | + verified_repo: string; |
| 10 | + downstream_repo_level?: string; // "L1" | "L2" | "L3" | "L4" — relay-determined from allowlist |
| 11 | + ci_metrics?: { |
| 12 | + queue_time?: number | null; |
| 13 | + execution_time?: number | null; |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +export interface RelayWorkflow { |
| 18 | + schema_version?: string; |
| 19 | + status: string; |
| 20 | + conclusion?: string | null; |
| 21 | + name: string; |
| 22 | + url: string; |
| 23 | + job_name?: string; |
| 24 | + check_run_id?: string; |
| 25 | + run_id?: string; |
| 26 | + run_attempt?: number | string; |
| 27 | + started_at?: string; |
| 28 | + completed_at?: string; |
| 29 | + test_results?: { |
| 30 | + passed?: number; |
| 31 | + failed?: number; |
| 32 | + skipped?: number; |
| 33 | + total?: number; |
| 34 | + }; |
| 35 | + artifact_url?: string; |
| 36 | +} |
| 37 | + |
| 38 | +export interface RelayCallbackPayload { |
| 39 | + event_type: string; |
| 40 | + delivery_id: string; |
| 41 | + payload: { |
| 42 | + pull_request?: { number: number; head?: { sha: string } }; |
| 43 | + repository?: { full_name: string }; |
| 44 | + [key: string]: any; |
| 45 | + }; |
| 46 | + workflow: RelayWorkflow; |
| 47 | +} |
| 48 | + |
| 49 | +export interface RelayUntrusted { |
| 50 | + callback_payload: RelayCallbackPayload; |
| 51 | +} |
| 52 | + |
| 53 | +export interface RelayPayload { |
| 54 | + trusted: RelayTrusted; |
| 55 | + untrusted: RelayUntrusted; |
| 56 | +} |
| 57 | + |
| 58 | +export interface OotWorkflowJobRecord { |
| 59 | + dynamoKey: string; |
| 60 | + status: string; |
| 61 | + downstream_repo: string; |
| 62 | + upstream_repo: string; |
| 63 | + pr_number: number; |
| 64 | + pytorch_head_sha: string; |
| 65 | + delivery_id: string; |
| 66 | + workflow_run_url: string; |
| 67 | + workflow_name: string; |
| 68 | + job_name: string; |
| 69 | + check_run_id: string; |
| 70 | + run_id: string; |
| 71 | + run_attempt: number; |
| 72 | + conclusion?: string; |
| 73 | + queue_time?: number | null; |
| 74 | + execution_time?: number | null; |
| 75 | + started_at?: string; |
| 76 | + completed_at?: string; |
| 77 | + total_tests?: number; |
| 78 | + passed_tests?: number; |
| 79 | + failed_tests?: number; |
| 80 | + skipped_tests?: number; |
| 81 | + downstream_repo_level?: string; |
| 82 | + artifact_url?: string; |
| 83 | + environment?: string; |
| 84 | +} |
| 85 | + |
| 86 | +// ---- Validation ---- |
| 87 | + |
| 88 | +export function validatePayloadSize(bodyString: string): void { |
| 89 | + if (Buffer.byteLength(bodyString, "utf-8") > MAX_PAYLOAD_BYTES) { |
| 90 | + throw new ApiError(413, "Payload exceeds 2MB limit"); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// ---- Extraction ---- |
| 95 | + |
| 96 | +export function extractDynamoRecord( |
| 97 | + payload: RelayPayload |
| 98 | +): OotWorkflowJobRecord { |
| 99 | + const { trusted, untrusted } = payload; |
| 100 | + const cb = untrusted.callback_payload; |
| 101 | + const wf = cb.workflow; |
| 102 | + const pr = cb.payload?.pull_request; |
| 103 | + const upstreamRepo = cb.payload?.repository?.full_name ?? "pytorch/pytorch"; |
| 104 | + |
| 105 | + if (!wf.job_name) { |
| 106 | + throw new ApiError(400, "Missing required field: workflow.job_name"); |
| 107 | + } |
| 108 | + if (wf.check_run_id == null) { |
| 109 | + throw new ApiError(400, "Missing required field: workflow.check_run_id"); |
| 110 | + } |
| 111 | + const jobName = wf.job_name; |
| 112 | + const checkRunId = String(wf.check_run_id); |
| 113 | + const runAttempt = Number(wf.run_attempt ?? 1) || 1; |
| 114 | + const dynamoKey = `${trusted.verified_repo}/${cb.delivery_id}/${wf.name}/${jobName}/${checkRunId}`; |
| 115 | + |
| 116 | + const record: OotWorkflowJobRecord = { |
| 117 | + dynamoKey, |
| 118 | + status: wf.status, |
| 119 | + downstream_repo: trusted.verified_repo, |
| 120 | + upstream_repo: upstreamRepo, |
| 121 | + pr_number: pr?.number ?? 0, |
| 122 | + pytorch_head_sha: pr?.head?.sha ?? "", |
| 123 | + delivery_id: cb.delivery_id, |
| 124 | + workflow_run_url: wf.url ?? "", |
| 125 | + workflow_name: wf.name, |
| 126 | + job_name: jobName, |
| 127 | + check_run_id: checkRunId, |
| 128 | + run_id: wf.run_id ?? "", |
| 129 | + run_attempt: runAttempt, |
| 130 | + }; |
| 131 | + |
| 132 | + if (trusted.downstream_repo_level) { |
| 133 | + record.downstream_repo_level = trusted.downstream_repo_level; |
| 134 | + } |
| 135 | + |
| 136 | + // Only set timing metrics when the relay provides a non-null value. |
| 137 | + // in_progress sets queue_time; completed sets execution_time. |
| 138 | + // Using UpdateItem ensures the completed callback doesn't clobber |
| 139 | + // queue_time with null. |
| 140 | + if (trusted.ci_metrics?.queue_time != null) { |
| 141 | + record.queue_time = trusted.ci_metrics.queue_time; |
| 142 | + } |
| 143 | + if (trusted.ci_metrics?.execution_time != null) { |
| 144 | + record.execution_time = trusted.ci_metrics.execution_time; |
| 145 | + } |
| 146 | + |
| 147 | + // Use downstream-reported timestamps, not HUD wall-clock time |
| 148 | + if (wf.started_at) { |
| 149 | + record.started_at = wf.started_at; |
| 150 | + } |
| 151 | + |
| 152 | + if (wf.artifact_url) { |
| 153 | + record.artifact_url = wf.artifact_url; |
| 154 | + } |
| 155 | + |
| 156 | + if (wf.status === "completed") { |
| 157 | + record.conclusion = wf.conclusion ?? undefined; |
| 158 | + if (wf.completed_at) { |
| 159 | + record.completed_at = wf.completed_at; |
| 160 | + } |
| 161 | + |
| 162 | + if (wf.test_results) { |
| 163 | + const tr = wf.test_results; |
| 164 | + if (typeof tr.passed === "number") record.passed_tests = tr.passed; |
| 165 | + if (typeof tr.failed === "number") record.failed_tests = tr.failed; |
| 166 | + if (typeof tr.skipped === "number") record.skipped_tests = tr.skipped; |
| 167 | + record.total_tests = |
| 168 | + typeof tr.total === "number" |
| 169 | + ? tr.total |
| 170 | + : (tr.passed ?? 0) + (tr.failed ?? 0) + (tr.skipped ?? 0); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return record; |
| 175 | +} |
| 176 | + |
| 177 | +// ---- DynamoDB Write (UpdateItem) ---- |
| 178 | + |
| 179 | +export async function writeToDynamo( |
| 180 | + record: OotWorkflowJobRecord |
| 181 | +): Promise<void> { |
| 182 | + const client = getDynamoClient(); |
| 183 | + |
| 184 | + // Build SET expression dynamically — only set non-undefined fields. |
| 185 | + // This prevents completed callbacks from clobbering in_progress-only |
| 186 | + // fields (queue_time, started_at) with null. |
| 187 | + const expressionParts: string[] = []; |
| 188 | + const expressionValues: Record<string, any> = {}; |
| 189 | + const expressionNames: Record<string, string> = {}; |
| 190 | + |
| 191 | + for (const [key, value] of Object.entries(record)) { |
| 192 | + if (key === "dynamoKey" || value === undefined) continue; |
| 193 | + const placeholder = `:v_${key}`; |
| 194 | + const nameAlias = `#n_${key}`; |
| 195 | + expressionParts.push(`${nameAlias} = ${placeholder}`); |
| 196 | + expressionValues[placeholder] = value; |
| 197 | + expressionNames[nameAlias] = key; |
| 198 | + } |
| 199 | + |
| 200 | + await client.update({ |
| 201 | + TableName: OOT_TABLE, |
| 202 | + Key: { dynamoKey: record.dynamoKey }, |
| 203 | + UpdateExpression: `SET ${expressionParts.join(", ")}`, |
| 204 | + ExpressionAttributeValues: expressionValues, |
| 205 | + ExpressionAttributeNames: expressionNames, |
| 206 | + }); |
| 207 | +} |
| 208 | + |
| 209 | +// ---- UI Helpers ---- |
| 210 | + |
| 211 | +export type ChipColor = "success" | "error" | "warning" | "info" | "default"; |
| 212 | + |
| 213 | +export function conclusionColor(status: string, conclusion: string): ChipColor { |
| 214 | + if (status === "in_progress") return "info"; |
| 215 | + switch (conclusion) { |
| 216 | + case "success": |
| 217 | + return "success"; |
| 218 | + case "failure": |
| 219 | + return "error"; |
| 220 | + case "cancelled": |
| 221 | + case "timed_out": |
| 222 | + return "warning"; |
| 223 | + default: |
| 224 | + return "default"; |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +export function conclusionLabel(status: string, conclusion: string): string { |
| 229 | + if (status === "in_progress") return "running"; |
| 230 | + return conclusion || status; |
| 231 | +} |
| 232 | + |
| 233 | +// ---- Error Helper ---- |
| 234 | + |
| 235 | +export class ApiError extends Error { |
| 236 | + statusCode: number; |
| 237 | + constructor(statusCode: number, message: string) { |
| 238 | + super(message); |
| 239 | + this.statusCode = statusCode; |
| 240 | + } |
| 241 | +} |
0 commit comments