| 1 | import type { GuardrailSettings, RunKind } from "@g1t/contracts"; |
| 2 | |
| 3 | /** A number from a field; empty means inherit. Not a number is kept, to be refused. */ |
| 4 | function amount(value: FormDataEntryValue | null): number | null { |
| 5 | const text = String(value ?? "").trim().replace(/^\$/, ""); |
| 6 | if (!text) return null; |
| 7 | const number = Number(text); |
| 8 | return Number.isFinite(number) ? number : Number.NaN; |
| 9 | } |
| 10 | |
| 11 | /** The kinds of run that still use a sandbox: checks, the merge queue and merge checks. */ |
| 12 | export const SANDBOX_RUN_KINDS = ["checks", "queue", "mergecheck"] as const satisfies readonly RunKind[]; |
| 13 | |
| 14 | /** |
| 15 | * What one level's form says, as the settings that level keeps: a time cap |
| 16 | * for each kind of run it sets, the rest inherited. |
| 17 | */ |
| 18 | export function settingsFromForm(form: FormData, catalog: { kinds: readonly RunKind[] }): GuardrailSettings { |
| 19 | const minutes: Record<string, number> = {}; |
| 20 | for (const kind of catalog.kinds) { |
| 21 | const cap = amount(form.get(`minutes:${kind}`)); |
| 22 | if (cap != null) minutes[kind] = Number.isNaN(cap) ? 0 : Math.trunc(cap); |
| 23 | } |
| 24 | return { minutes }; |
| 25 | } |