Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions apps/server/src/factory/domain/approval.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { describe, expect, it } from "vite-plus/test";

import { refusalReason, unapprovedComment, type Refusal } from "./approval.ts";

const AT = "2026-10-04T10:00:00Z";

const NOT_BY_INSTRUCTOR = "factory:ready was not applied by an instructor";
const EDITED_AFTER =
"the issue text was edited after factory:ready was applied by someone who is not an instructor";
const CANNOT_READ_LABEL = "the factory could not read who applied factory:ready";
const CANNOT_READ_HISTORY = "the factory could not read the full edit history of the issue text";

const OPENING =
"The factory did not start on this issue and has moved it back to `factory:proposed`.";
const REVIEW_AND_RELABEL =
"Review the issue text, then apply `factory:ready` again. An instructor must apply the label, and the text must not be edited afterwards by anyone who is not an instructor.";
const FILE_A_NEW_ISSUE =
"The issue's edit history is too long for the factory to check. File a new issue with the final text and apply `factory:ready` to it.";

const cases: ReadonlyArray<readonly [Refusal, string, string]> = [
[{ kind: "label-not-found" }, CANNOT_READ_LABEL, REVIEW_AND_RELABEL],
[{ kind: "label-history-incomplete" }, CANNOT_READ_LABEL, REVIEW_AND_RELABEL],
[
{ kind: "labeled-by-non-instructor", by: "mallory", labeledAt: AT },
NOT_BY_INSTRUCTOR,
REVIEW_AND_RELABEL,
],
[
{ kind: "labeled-by-non-instructor", by: null, labeledAt: AT },
NOT_BY_INSTRUCTOR,
REVIEW_AND_RELABEL,
],
[{ kind: "edited-after-label", labeledAt: AT }, EDITED_AFTER, REVIEW_AND_RELABEL],
[{ kind: "edit-history-incomplete", labeledAt: AT }, CANNOT_READ_HISTORY, FILE_A_NEW_ISSUE],
];

describe("refusalReason", () => {
it.each(cases)("%j has softfac's fixed reason", (refusal, reason) => {
expect(refusalReason(refusal)).toBe(reason);
});

it("gives the unreadable label history and the missing label one reason", () => {
expect(refusalReason({ kind: "label-not-found" })).toBe(
refusalReason({ kind: "label-history-incomplete" }),
);
});
});

describe("unapprovedComment", () => {
it.each(cases)("%j is the factory's fixed text", (refusal, reason, whatToDo) => {
expect(unapprovedComment(refusal)).toBe(`${OPENING}\n\nReason: ${reason}\n\n${whatToDo}`);
});

it("is softfac's literal comment for an edit after the label", () => {
expect(unapprovedComment({ kind: "edited-after-label", labeledAt: AT })).toBe(
"The factory did not start on this issue and has moved it back to `factory:proposed`.\n\nReason: the issue text was edited after factory:ready was applied by someone who is not an instructor\n\nReview the issue text, then apply `factory:ready` again. An instructor must apply the label, and the text must not be edited afterwards by anyone who is not an instructor.",
);
});

it("is softfac's literal comment for a history that is too long", () => {
expect(unapprovedComment({ kind: "edit-history-incomplete", labeledAt: AT })).toBe(
"The factory did not start on this issue and has moved it back to `factory:proposed`.\n\nReason: the factory could not read the full edit history of the issue text\n\nThe issue's edit history is too long for the factory to check. File a new issue with the final text and apply `factory:ready` to it.",
);
});

it("quotes nothing from the issue or the login", () => {
const text = unapprovedComment({
kind: "labeled-by-non-instructor",
by: "mallory",
labeledAt: AT,
});
expect(text).not.toContain("mallory");
expect(text).not.toContain(AT);
});
});
67 changes: 67 additions & 0 deletions apps/server/src/factory/domain/approval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* The approval rule's verdicts and the words the factory says when it refuses. The rule itself
* reads GitHub's history and lives with the tracker; this file holds what it concludes and
* what is then written on the issue. Pure: no I/O.
*/

/**
* Why an issue is not approved. A fixed set, so nothing from the issue can reach a comment.
* Times are GitHub's `YYYY-MM-DDTHH:MM:SSZ`.
*/
export type Refusal =
/** No `factory:ready` event in force among the label events read. It has no label time. */
| { readonly kind: "label-not-found" }
/** A full page of a longer label history without the event in force. It has no label time. */
| { readonly kind: "label-history-incomplete" }
/** The label in force was applied by someone who is not an instructor (`by` null: a deleted account). */
| {
readonly kind: "labeled-by-non-instructor";
readonly by: string | null;
readonly labeledAt: string;
}
/** Someone who is not an instructor edited the body or renamed the title after the label. */
| { readonly kind: "edited-after-label"; readonly labeledAt: string }
/** The body's edit history is cut off, so it cannot show every edit. */
| { readonly kind: "edit-history-incomplete"; readonly labeledAt: string };

/**
* Whether the factory may build the issue: an instructor applied the label in force, and
* every edit and rename since was an instructor's.
*/
export type Approval =
| { readonly kind: "approved"; readonly labeledAt: string; readonly labeledBy: string }
| { readonly kind: "refused"; readonly refusal: Refusal };

/**
* Whether the issue text is as an instructor left it: `unchanged`, `changed` by someone who is
* not an instructor, or `unjudged` when the histories do not reach back far enough to tell.
*/
export type TextCheck = "unchanged" | "changed" | "unjudged";

/** The fixed reason for a refusal. Nothing from the issue, not even a login, is in it. */
export const refusalReason = (refusal: Refusal): string => {
switch (refusal.kind) {
case "labeled-by-non-instructor":
return "factory:ready was not applied by an instructor";
case "edited-after-label":
return "the issue text was edited after factory:ready was applied by someone who is not an instructor";
case "label-not-found":
case "label-history-incomplete":
return "the factory could not read who applied factory:ready";
case "edit-history-incomplete":
return "the factory could not read the full edit history of the issue text";
}
};

/**
* The comment on an issue the factory refused to build: its own fixed text, quoting nothing
* from the issue.
*/
export const unapprovedComment = (refusal: Refusal): string => {
const whatToDo =
refusal.kind === "edit-history-incomplete"
? // Applying the label again cannot help: the history only grows.
"The issue's edit history is too long for the factory to check. File a new issue with the final text and apply `factory:ready` to it."
: "Review the issue text, then apply `factory:ready` again. An instructor must apply the label, and the text must not be edited afterwards by anyone who is not an instructor.";
return `The factory did not start on this issue and has moved it back to \`factory:proposed\`.\n\nReason: ${refusalReason(refusal)}\n\n${whatToDo}`;
};
81 changes: 81 additions & 0 deletions apps/server/src/factory/domain/dependsOn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { assert, it } from "@effect/vitest";

import { dependsOn, type IssueRef } from "./dependsOn.ts";

const same = (number: number): IssueRef => ({ repo: null, number });
const cross = (repo: string, number: number): IssueRef => ({ repo, number });

it("depends on reads same repo references", () => {
const body = "Intro\n\n## Depends on\n\n- #13\n- #15 until it lands\n";
assert.deepStrictEqual(dependsOn(body), [same(13), same(15)]);
});

it("depends on reads cross repo references", () => {
const body = "## Depends on\nacme/widgets#7, my-org/some.repo_x#42\n";
assert.deepStrictEqual(dependsOn(body), [
cross("acme/widgets", 7),
cross("my-org/some.repo_x", 42),
]);
});

it("depends on reads both kinds in order and once", () => {
const body = "## Depends on\n#3 and acme/widgets#7 and #3 again, #9\n";
assert.deepStrictEqual(dependsOn(body), [same(3), cross("acme/widgets", 7), same(9)]);
});

it("depends on does not count the number of a cross repo reference twice", () => {
assert.deepStrictEqual(dependsOn("## Depends on\nacme/widgets#7\n"), [cross("acme/widgets", 7)]);
});

it("depends on none or empty or missing names nothing", () => {
assert.deepStrictEqual(dependsOn("## Depends on\n\nNone\n"), []);
assert.deepStrictEqual(dependsOn("## Depends on\nnone.\n## Next\n#4\n"), []);
assert.deepStrictEqual(dependsOn("## Depends on\n"), []);
assert.deepStrictEqual(dependsOn("## Depends on\n\n## Fix\n#4\n"), []);
assert.deepStrictEqual(dependsOn("## Cause\nSee #4\n"), []);
assert.deepStrictEqual(dependsOn(""), []);
});

it("depends on ignores text after the next heading", () => {
const body = "## Depends on\n#13\n### Notes\n#99\n## Fix\nacme/widgets#98\n";
assert.deepStrictEqual(dependsOn(body), [same(13)]);
});

it("depends on ignores references before the section", () => {
assert.deepStrictEqual(dependsOn("## Cause\nSee #5\n\n## Depends on\n#13\n"), [same(13)]);
});

it("depends on heading is case insensitive and allows trailing spaces", () => {
assert.deepStrictEqual(dependsOn("## depends ON \r\n#13\r\n"), [same(13)]);
});

it("depends on needs digits after the hash", () => {
assert.deepStrictEqual(dependsOn("## Depends on\n#abc # 4 #\n"), []);
});

it("depends on skips a hash that word characters run into", () => {
assert.deepStrictEqual(dependsOn("## Depends on\nsee page#12 and /widgets#7, then #3\n"), [
same(3),
]);
});

it("depends on ends at a heading line with nothing after the hashes", () => {
assert.deepStrictEqual(dependsOn("## Depends on\n#3\n##\n#4\n"), [same(3)]);
});

it("depends on heading needs two hashes and a space", () => {
assert.deepStrictEqual(dependsOn("##Depends on\n#3\n"), []);
assert.deepStrictEqual(dependsOn("### Depends on\n#3\n"), []);
assert.deepStrictEqual(dependsOn("# Depends on\n#3\n"), []);
});

it("depends on skips an owner or repo made only of dots, which would leave the API path", () => {
assert.deepStrictEqual(
dependsOn("## Depends on\n../..#3 ./widgets#4 acme/..#5 acme/.#6 ...//x#8\n"),
[],
);
assert.deepStrictEqual(dependsOn("## Depends on\nacme/.github#7 a.b/c..d#9\n"), [
cross("acme/.github", 7),
cross("a.b/c..d", 9),
]);
});
81 changes: 81 additions & 0 deletions apps/server/src/factory/domain/dependsOn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/** The `## Depends on` section of an issue body: pure text parsing, no `gh` call. */

/** An issue named in the `## Depends on` section of an issue body. */
export interface IssueRef {
/** `owner/name` when the text names a repo; null means the repo of the issue it was written in. */
readonly repo: string | null;
readonly number: number;
}

const isRepoChar = (char: string): boolean => /^[A-Za-z0-9._-]$/.test(char);
const isDigit = (char: string): boolean => char >= "0" && char <= "9";
const isDots = (segment: string): boolean => /^\.+$/.test(segment);

/** The lines of a text as Rust's `lines()` gives them: split on `\n`, one `\r` before it dropped. */
const textLines = (text: string): Array<string> => {
const lines = text.split("\n");
if (lines[lines.length - 1] === "") lines.pop();
return lines.map((line) => (line.endsWith("\r") ? line.slice(0, -1) : line));
};

/** A markdown heading: `#`s then a space, a tab or the end of the line. `#13` is a reference. */
const isHeading = (line: string): boolean => {
const rest = line.replace(/^#+/, "");
return rest.length < line.length && (rest === "" || rest[0] === " " || rest[0] === "\t");
};

/** The lines of the `## Depends on` section: after its heading, up to the next heading line. */
const dependsOnLines = (body: string): Array<string> => {
const lines: Array<string> = [];
let inSection = false;
for (const line of textLines(body)) {
if (inSection) {
if (isHeading(line)) break;
lines.push(line);
} else {
inSection =
line.startsWith("## ") || line.startsWith("##\t")
? line.slice(3).trim().toLowerCase() === "depends on"
: false;
}
}
return lines;
};

/**
* The issues named in the `## Depends on` section of an issue body, in order of first
* appearance, each once. The section runs from its heading to the next heading line. It names
* `#<n>` and `<owner>/<repo>#<n>`; "None", an empty section and no section name nothing.
*/
export const dependsOn = (body: string): Array<IssueRef> => {
const found: Array<IssueRef> = [];
for (const line of dependsOnLines(body)) {
for (let at = 0; at < line.length; at++) {
if (line[at] !== "#") continue;
let end = at + 1;
while (end < line.length && isDigit(line[end]!)) end++;
const number = Number(line.slice(at + 1, end));
if (end === at + 1 || !Number.isSafeInteger(number)) continue;
let repoStart = at;
while (repoStart > 0 && isRepoChar(line[repoStart - 1]!)) repoStart--;
let repo: string | null = null;
if (repoStart < at && repoStart > 0 && line[repoStart - 1] === "/") {
let ownerStart = repoStart - 1;
while (ownerStart > 0 && isRepoChar(line[ownerStart - 1]!)) ownerStart--;
if (ownerStart === repoStart - 1) continue;
const owner = line.slice(ownerStart, repoStart - 1);
const name = line.slice(repoStart, at);
// `.` and `..` would step out of `repos/<owner>/<name>` in the API path: not a repo.
if (isDots(owner) || isDots(name)) continue;
repo = `${owner}/${name}`;
} else if (repoStart < at) {
// Word characters run straight into the `#`, as in `page#12`: not an issue.
continue;
}
if (!found.some((issue) => issue.repo === repo && issue.number === number)) {
found.push({ repo, number });
}
}
}
return found;
};
Loading