The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Schema from "effect/Schema"; |
| 2 | import type { PullRequestCheck } from "@t3tools/contracts"; |
| 3 | |
| 4 | // The branch endpoint reports the governing protection rule with repository read access. |
| 5 | export const ForgejoBranchStatusChecks = Schema.Struct({ |
| 6 | enable_status_check: Schema.optional(Schema.Boolean), |
| 7 | status_check_contexts: Schema.optional(Schema.NullOr(Schema.Array(Schema.String))), |
| 8 | }); |
| 9 | |
| 10 | // Forgejo compiles each context with gobwas/glob and no separator, so `*` and `**` both match any |
| 11 | // run of characters, `/` included, and `?` matches one character. Classes, alternation and escapes |
| 12 | // are not handled. |
| 13 | const UNSUPPORTED_GLOB = /[[\]{}\\]/; |
| 14 | // Linear two-pointer match: a regex built from the glob backtracks polynomially per `*`. |
| 15 | const matchesContext = (glob: string, name: string) => { |
| 16 | const pattern = Array.from(glob); |
| 17 | const text = Array.from(name); |
| 18 | let p = 0; |
| 19 | let t = 0; |
| 20 | let star = -1; |
| 21 | let resume = 0; |
| 22 | while (t < text.length) { |
| 23 | // Wildcard first, so a literal `*` in the name never consumes the pattern's `*`. |
| 24 | if (pattern[p] === "*") { |
| 25 | star = p++; |
| 26 | resume = t; |
| 27 | } else if (p < pattern.length && (pattern[p] === "?" || pattern[p] === text[t])) { |
| 28 | p++; |
| 29 | t++; |
| 30 | } else if (star >= 0) { |
| 31 | p = star + 1; |
| 32 | t = ++resume; |
| 33 | } else { |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | while (pattern[p] === "*") p++; |
| 38 | return p === pattern.length; |
| 39 | }; |
| 40 | |
| 41 | export function markRequiredForgejoChecks( |
| 42 | checks: ReadonlyArray<PullRequestCheck>, |
| 43 | branch: typeof ForgejoBranchStatusChecks.Type, |
| 44 | ): ReadonlyArray<PullRequestCheck> { |
| 45 | const contexts = branch.enable_status_check ? (branch.status_check_contexts ?? []) : []; |
| 46 | if (contexts.length === 0) return checks; |
| 47 | const unsupported = [...new Set(contexts.filter((context) => UNSUPPORTED_GLOB.test(context)))]; |
| 48 | if (unsupported.length > 0) { |
| 49 | // Keep all observed checks gating, but never pass a policy we cannot evaluate. |
| 50 | return [ |
| 51 | ...checks, |
| 52 | ...unsupported.map((name) => ({ |
| 53 | name, |
| 54 | status: "pending" as const, |
| 55 | description: "Required check pattern cannot be evaluated.", |
| 56 | url: null, |
| 57 | })), |
| 58 | ]; |
| 59 | } |
| 60 | const patterns = [...new Set(contexts)]; |
| 61 | const marked = checks.map((check) => |
| 62 | patterns.some((pattern) => matchesContext(pattern, check.name)) |
| 63 | ? { ...check, required: true } |
| 64 | : check, |
| 65 | ); |
| 66 | // Forgejo keeps an unmatched required pattern pending, including wildcards. |
| 67 | const missing = patterns.flatMap((name) => |
| 68 | !name.trim() || checks.some((check) => matchesContext(name, check.name)) |
| 69 | ? [] |
| 70 | : [{ name, status: "pending", description: null, url: null, required: true } as const], |
| 71 | ); |
| 72 | return [...marked, ...missing]; |
| 73 | } |