Open History to see the last commit
1import * as Schema from "effect/Schema";
2import type { PullRequestCheck } from "@t3tools/contracts";
3
4// The branch endpoint reports the governing protection rule with repository read access.
5export 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.
13const UNSUPPORTED_GLOB = /[[\]{}\\]/;
14const contextPattern = (glob: string) =>
15 new RegExp(
16 `^${Array.from(glob, (char) =>
17 char === "*" ? ".*" : char === "?" ? "." : char.replace(/[.+^$()|]/g, "\\$&"),
18 ).join("")}$`,
19 "su",
20 );
21
22export function markRequiredForgejoChecks(
23 checks: ReadonlyArray<PullRequestCheck>,
24 branch: typeof ForgejoBranchStatusChecks.Type,
25): ReadonlyArray<PullRequestCheck> {
26 const contexts = branch.enable_status_check ? (branch.status_check_contexts ?? []) : [];
27 if (contexts.length === 0) return checks;
28 const unsupported = [...new Set(contexts.filter((context) => UNSUPPORTED_GLOB.test(context)))];
29 if (unsupported.length > 0) {
30 // Keep all observed checks gating, but never pass a policy we cannot evaluate.
31 return [
32 ...checks,
33 ...unsupported.map((name) => ({
34 name,
35 status: "pending" as const,
36 description: "Required check pattern cannot be evaluated.",
37 url: null,
38 })),
39 ];
40 }
41 const patterns = [...new Set(contexts)].map((name) => ({ name, pattern: contextPattern(name) }));
42 const marked = checks.map((check) =>
43 patterns.some(({ pattern }) => pattern.test(check.name)) ? { ...check, required: true } : check,
44 );
45 // Forgejo keeps an unmatched required pattern pending, including wildcards.
46 const missing = patterns.flatMap(({ name, pattern }) =>
47 !name.trim() || checks.some((check) => pattern.test(check.name))
48 ? []
49 : [{ name, status: "pending", description: null, url: null, required: true } as const],
50 );
51 return [...marked, ...missing];
52}