Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Separate change approval policies from reporting

Manav Rathi45bbc888ca66
diff --git a/.github/checks/change-approval/changes.mjs b/.github/checks/change-approval/changes.mjs
new file mode 100644
index 0000000000..1752b47427
--- /dev/null
+++ b/.github/checks/change-approval/changes.mjs
@@ -0,0 +1,113 @@
+import { execFileSync } from "node:child_process";
+import {
+ closeSync,
+ lstatSync,
+ openSync,
+ readFileSync,
+ readSync,
+} from "node:fs";
+import { resolve } from "node:path";
+
+export function readChanges(base, local) {
+ const root = execFileSync("git", ["rev-parse", "--show-toplevel"], {
+ encoding: "utf8",
+ }).trim();
+ const git = (...args) =>
+ execFileSync("git", args, {
+ cwd: root,
+ encoding: "utf8",
+ maxBuffer: 64 * 1024 * 1024,
+ });
+ const mergeBase = git("merge-base", base, "HEAD").trim();
+ const numstat = (...filter) =>
+ git(
+ "diff",
+ "--numstat",
+ "--no-renames",
+ "-z",
+ ...filter,
+ mergeBase,
+ ...(local ? [] : ["HEAD"]),
+ )
+ .split("\0")
+ .filter(Boolean)
+ .map((row) => {
+ const [, added, , path] = row.match(
+ /^([^\t]+)\t([^\t]+)\t(.*)$/s,
+ );
+ return { path, binary: added === "-" };
+ });
+ const untracked = local
+ ? git("ls-files", "--others", "--exclude-standard", "-z")
+ .split("\0")
+ .filter(
+ (path) =>
+ path && !lstatSync(resolve(root, path)).isSymbolicLink(),
+ )
+ .map((path) => ({ path, binary: isBinary(resolve(root, path)) }))
+ : [];
+ const changed = [...numstat(), ...untracked];
+ const pathsBefore = new Set(
+ numstat("--diff-filter=a").map(({ path }) => path),
+ );
+ const pathsAfter = new Set(
+ [...numstat("--diff-filter=d"), ...untracked].map(({ path }) => path),
+ );
+ const sizes = new Map();
+ if (local) {
+ for (const path of pathsAfter) {
+ const stat = lstatSync(resolve(root, path));
+ if (stat.isFile()) sizes.set(path, stat.size);
+ }
+ } else if (pathsAfter.size) {
+ const tree = git(
+ "--literal-pathspecs",
+ "ls-tree",
+ "-l",
+ "-z",
+ "HEAD",
+ "--",
+ ...pathsAfter,
+ );
+ for (const entry of tree.split("\0")) {
+ const blob = entry.match(/ blob \S+ +(\d+)\t(.*)$/s);
+ if (blob) {
+ const [, size, path] = blob;
+ sizes.set(path, Number(size));
+ }
+ }
+ }
+ const readBefore = (path) => git("show", `${mergeBase}:${path}`);
+ const readAfter = (path) =>
+ local
+ ? readFileSync(resolve(root, path), "utf8")
+ : git("show", `HEAD:${path}`);
+
+ return {
+ files: changed.map(({ path, binary }) => ({
+ path,
+ binary,
+ added: !pathsBefore.has(path),
+ deleted: !pathsAfter.has(path),
+ size: sizes.get(path),
+ })),
+ baseFiles: () =>
+ git("ls-tree", "-r", "--name-only", "-z", mergeBase)
+ .split("\0")
+ .filter(Boolean),
+ readBefore,
+ readAfter,
+ readVersions: (path) => ({
+ before: pathsBefore.has(path) ? readBefore(path) : "",
+ after: sizes.has(path) ? readAfter(path) : "",
+ }),
+ };
+}
+
+function isBinary(path) {
+ const fd = openSync(path, "r");
+ const buffer = Buffer.alloc(8000);
+ const length = readSync(fd, buffer);
+ closeSync(fd);
+ return buffer.subarray(0, length).includes(0);
+}
diff --git a/.github/checks/change-approval/check.mjs b/.github/checks/change-approval/check.mjs
index a1a19a19c9..5496048ccd 100644
--- a/.github/checks/change-approval/check.mjs
+++ b/.github/checks/change-approval/check.mjs
@@ -1,295 +1,14 @@
-import { execFileSync } from "node:child_process";
-import { appendFileSync, closeSync, lstatSync, openSync, readFileSync, readSync } from "node:fs";
-import path from "node:path";
+import { readChanges } from "./changes.mjs";
+import { checkDependencies } from "./dependencies.mjs";
+import { checkFiles } from "./files.mjs";
+import { writeReport } from "./report.mjs";
+import { checkRust } from "./rust.mjs";
const [base = "origin/main"] = process.argv.slice(2);
+const changes = readChanges(base, !process.env.GITHUB_OUTPUT);
-const routineBinary = (file) =>
- /\.(png|jpe?g|webp|gif|ico|icns|ttf|otf|woff2?|riv|mp3)$/i.test(file) || file.includes("Assets.xcassets/");
-const guardrailDirs = [
- ".github/",
- "mobile/checks/",
- "mobile/scripts/",
- "rust/checks/",
- "rust/scripts/",
- "web/checks/",
- "web/scripts/",
- "server/scripts/",
- "web/packages/build-config/",
-];
-const guardrailFiles = new Set([
- "eslint.config.mjs",
- ".prettierrc.json",
- "analysis_options.yaml",
-]);
-const configFile =
- /(^|\/)(\.gitattributes|\.?clippy\.toml|rust-toolchain\.toml|\.cargo\/(config|audit)\.toml|\.npmrc|\.nvmrc|\.tool-versions|\.node-version|\.python-version|gradle-wrapper\.properties)$/;
-
-const cargoLints = (source) =>
- execFileSync("python3", ["-c", `
-import json, sys, tomllib
-cargo = tomllib.loads(sys.stdin.read())
-workspace = cargo.get("workspace", {})
-selection = {key: sorted(workspace[key]) if key in workspace else None for key in ("members", "exclude", "default-members")}
-print(json.dumps([cargo.get("lints"), workspace.get("lints"), selection], sort_keys=True))
-`], { encoding: "utf8", input: source });
-
-function rustPolicy(source) {
- const lexer = /\/\/[^\n]*|\/\*|[bc]?r(#+)?"[\s\S]*?"\1|[bc]?"(?:\\[\s\S]|[^"\\])*"|b?'(?:\\(?:u\{[\da-fA-F_]+\}|x[\da-fA-F]{2}|[\s\S])|[^'\\\r\n])'|(?:r#)?[a-zA-Z_]\w*|[^\s]/gu;
- const tokens = [];
- let match;
- while ((match = lexer.exec(source))) {
- const token = match[0];
- if (token.startsWith("//")) continue;
- if (token === "/*") {
- const comments = /\/\*|\*\//g;
- comments.lastIndex = lexer.lastIndex;
- for (let depth = 1; depth;) {
- const comment = comments.exec(source);
- if (!comment) throw new Error("Unclosed Rust comment");
- depth += comment[0] === "/*" ? 1 : -1;
- }
- lexer.lastIndex = comments.lastIndex;
- } else {
- tokens.push(token);
- }
- }
- const pairs = { "(": ")", "[": "]", "{": "}" };
- let cursor = 0;
- const group = (closing) => {
- const nodes = [];
- while (cursor < tokens.length) {
- const token = tokens[cursor++];
- if (token === closing) return nodes;
- if (Object.hasOwn(pairs, token)) nodes.push([token, ...group(pairs[token]), pairs[token]]);
- else if ([")", "]", "}"].includes(token)) throw new Error("Unmatched Rust delimiter");
- else nodes.push(token);
- }
- if (closing) throw new Error("Unclosed Rust delimiter");
- return nodes;
- };
- const lint = (nodes) => nodes.some((node, i) =>
- Array.isArray(node) ? lint(node) : /^(?:r#)?(?:allow|expect|warn|deny|forbid)$/.test(node) && nodes[i + 1]?.[0] === "(",
- );
- const declarations = [];
- const visit = (nodes) => {
- for (let i = 0; i < nodes.length; i++) {
- if (nodes[i] === "#") {
- let end = i;
- let hasLint = false;
- while (nodes[end] === "#") {
- const bang = nodes[end + 1] === "!";
- const attribute = nodes[end + (bang ? 2 : 1)];
- if (!Array.isArray(attribute) || attribute[0] !== "[") break;
- hasLint ||= lint(attribute);
- end += bang ? 3 : 2;
- }
- if (hasLint) {
- let stop = end;
- if (nodes[i + 1] !== "!") {
- while (stop < nodes.length && !["{", ";", ",", "="].includes(Array.isArray(nodes[stop]) ? nodes[stop][0] : nodes[stop])) stop++;
- }
- declarations.push(JSON.stringify(nodes.slice(i, stop)));
- }
- if (end > i) {
- i = end - 1;
- continue;
- }
- }
- if (Array.isArray(nodes[i])) visit(nodes[i].slice(1, -1));
- }
- };
- visit(group());
- return { unsafe: tokens.includes("unsafe"), declarations: JSON.stringify(declarations.sort()) };
-}
-
-const tomlPackages = (text) =>
- text
- .split("[[package]]\n")
- .slice(1)
- .map((block) => ({
- name: block.match(/^name = "(.+)"$/m)[1],
- version: block.match(/^version = "(.+)"$/m)[1],
- source: block.match(/^source = "?(.+?)"?$/m)?.[1]?.replace(/[?#][^"]*/, ""),
- }));
-const lockfiles = {
- "Cargo.lock": (text) => tomlPackages(text).map((p) => ({ ...p, source: p.source ?? "workspace", local: !p.source })),
- "uv.lock": (text) => tomlPackages(text).map((p) => ({ ...p, local: !/^\{ (registry|git|url) = /.test(p.source) })),
- "package-lock.json": (text) =>
- Object.entries(JSON.parse(text).packages)
- .filter(([key]) => key.includes("node_modules/"))
- .map(([key, { name, version, resolved, link }]) => {
- const local = link === true || resolved?.startsWith("file:") === true;
- return {
- name: name ?? key.slice(key.lastIndexOf("node_modules/") + 13),
- version,
- source: local || !resolved ? resolved : resolved.replace(/#.*$/, "").replace(/\/-\/[^/]+\.tgz$/, ""),
- local,
- };
- }),
- "pubspec.lock": (text) =>
- [...text.matchAll(/^ (\S+):\n((?: .*\n)+)/gm)].map(([, name, body]) => {
- const source = [body.match(/^ source: (.+)$/m)[1], body.match(/^ url: "(.+)"$/m)?.[1]]
- .filter(Boolean)
- .join(" ");
- return { name, version: body.match(/^ version: "(.+)"$/m)[1], source, local: source === "path" };
- }),
- "go.sum": (text) =>
- [...text.matchAll(/^(\S+) (\S+?)(?:\/go\.mod)? h1:/gm)].map(([, name, version]) => ({ name, version })),
- "Package.resolved": (text) =>
- JSON.parse(text).pins.map(({ identity, location, state }) => ({
- name: identity,
- version: state.version,
- source: location,
- })),
- "Podfile.lock": (text) => {
- const section = (title) => text.match(new RegExp(`^${title}:\\n((?: .*\\n)+)`, "m"))?.[1] ?? "";
- const sources = new Map();
- for (const [, repo, pods] of section("SPEC REPOS").matchAll(/^ (\S+):\n((?: - .*\n)+)/gm)) {
- for (const [, pod] of pods.matchAll(/^ - (.+)$/gm)) sources.set(pod, { source: repo });
- }
- for (const [, pod, body] of section("EXTERNAL SOURCES").matchAll(/^ (\S+):\n((?: .*\n)+)/gm)) {
- const props = Object.fromEntries([...body.matchAll(/^ :(\w+): (.+)$/gm)].map(([, key, value]) => [key, value]));
- const source = props.git ?? props.podspec ?? props.path;
- sources.set(pod, { source, local: !props.git && !/^"?https?:\/\//.test(source) });
- }
- return [...section("PODS").matchAll(/^ - "?([^/" ]+)\S* \(([^)]+)\)/gm)].map(([, name, version]) => ({
- name,
- version,
- ...sources.get(name),
- }));
- },
-};
-
-const opts = { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 };
-const git = (...args) => execFileSync("git", args, opts);
-const list = (items) => items.map((item) => `- ${item}`).join("\n");
-const code = (file) => `\`${file}\``;
-const binary = (file) => {
- const fd = openSync(file, "r");
- const buffer = Buffer.alloc(8000);
- const length = readSync(fd, buffer);
- closeSync(fd);
- return buffer.subarray(0, length).includes(0);
-};
-
-const { GITHUB_OUTPUT, GITHUB_STEP_SUMMARY } = process.env;
-const local = !GITHUB_OUTPUT;
-
-process.chdir(git("rev-parse", "--show-toplevel").trim());
-const mergeBase = git("merge-base", base, "HEAD").trim();
-const numstat = (...filter) =>
- git("diff", "--numstat", "--no-renames", "-z", ...filter, mergeBase, ...(local ? [] : ["HEAD"]))
- .split("\0")
- .filter(Boolean)
- .map((row) => row.match(/^([^\t]+)\t([^\t]+)\t(.*)$/s).slice(1));
-const untracked = local
- ? git("ls-files", "--others", "--exclude-standard", "-z")
- .split("\0")
- .filter((file) => file && !lstatSync(file).isSymbolicLink())
- : [];
-const additions = untracked.map((file) => [binary(file) ? "-" : "0", "0", file]);
-const present = [...numstat("--diff-filter=d"), ...additions];
-const kept = numstat("--diff-filter=a").map(([, , file]) => file);
-
-const treeSizes = () =>
- git("--literal-pathspecs", "ls-tree", "-l", "-z", "HEAD", "--", ...present.map(([, , file]) => file))
- .split("\0")
- .map((line) => line.match(/ blob \S+ +(\d+)\t(.*)$/s))
- .filter(Boolean)
- .map(([, size, file]) => [file, Number(size)]);
-const diskSizes = () =>
- present.flatMap(([, , file]) => {
- const stat = lstatSync(file);
- return stat.isFile() ? [[file, stat.size]] : [];
- });
-const sizes = new Map(local ? diskSizes() : present.length ? treeSizes() : []);
-const withSize = (file) => `${code(file)} (${sizes.get(file)} bytes)`;
-
-const binaries = present
- .filter(([added, , file]) => added === "-" && !routineBinary(file))
- .map(([, , file]) => withSize(file));
-const large = [...numstat("--diff-filter=A"), ...additions]
- .map(([, , file]) => file)
- .filter((file) => sizes.get(file) > 1024 * 1024)
- .map(withSize);
-
-const known = new Map();
-for (const file of git("ls-tree", "-r", "--name-only", "-z", mergeBase).split("\0")) {
- const kind = path.basename(file);
- if (!lockfiles[kind]) continue;
- if (!known.has(kind)) known.set(kind, new Map());
- const names = known.get(kind);
- for (const { name, source } of lockfiles[kind](git("show", `${mergeBase}:${file}`))) {
- if (!names.has(name)) names.set(name, new Set());
- names.get(name).add(source);
- }
-}
-const dependencies = present
- .map(([, , file]) => file)
- .filter((file) => lockfiles[path.basename(file)])
- .flatMap((file) => {
- const before = known.get(path.basename(file)) ?? new Map();
- const after = lockfiles[path.basename(file)](local ? readFileSync(file, "utf8") : git("show", `HEAD:${file}`)).filter(
- ({ local }) => !local,
- );
- const added = new Set(after.filter(({ name }) => !before.has(name)).map(({ name, version }) => `${name} ${version}`));
- const moved = new Set(
- after
- .filter(({ name, source }) => source && before.has(name) && !before.get(name).has(source))
- .map(({ name, source }) => `${name}: ${[...before.get(name)].join(", ")} -> ${source}`),
- );
- return added.size + moved.size ? [{ file, added: [...added], moved: [...moved] }] : [];
- });
-const added = dependencies.reduce((n, { added }) => n + added.length, 0);
-const moved = dependencies.reduce((n, { moved }) => n + moved.length, 0);
-
-const guardrails = [
- ...kept.filter((file) => guardrailDirs.some((dir) => file.startsWith(dir)) || guardrailFiles.has(path.basename(file))),
- ...[...numstat("--diff-filter=A"), ...additions].map(([, , file]) => file).filter((file) => file.startsWith(".github/")),
-];
-const configs = [...numstat(), ...additions].map(([, , file]) => file).filter((file) => configFile.test(file));
-const lintPolicies = [...numstat(), ...additions]
- .map(([, , file]) => file)
- .filter((file) => file.endsWith(".rs") || path.basename(file) === "Cargo.toml")
- .filter((file) => {
- const before = kept.includes(file) ? git("show", `${mergeBase}:${file}`) : "";
- const after = sizes.has(file) ? (local ? readFileSync(file, "utf8") : git("show", `HEAD:${file}`)) : "";
- if (!file.endsWith(".rs")) return cargoLints(before) !== cargoLints(after);
- const oldPolicy = rustPolicy(before);
- const newPolicy = rustPolicy(after);
- return oldPolicy.unsafe || newPolicy.unsafe || oldPolicy.declarations !== newPolicy.declarations;
- });
-
-const categories = [
- ["binary file", "binary files", binaries.length],
- ["large file", "large files", large.length],
- ["new dependency", "new dependencies", added],
- ["dependency source change", "dependency source changes", moved],
- ["guardrail file", "guardrail files", guardrails.length],
- ["config file", "config files", configs.length],
- ["Rust lint policy file", "Rust lint policy files", lintPolicies.length],
-].filter(([, , n]) => n);
-const summary = categories.map(([one, many, n]) => `${n} ${n === 1 ? one : many}`).join(", ");
-const detail = [
- binaries.length && `## Binary files\n\n${list(binaries)}`,
- large.length && `## Large files\n\n${list(large)}`,
- dependencies.length &&
- `## New dependencies\n\n${dependencies
- .map(({ file, added, moved }) => `${code(file)}\n\n${list([...added, ...moved])}`)
- .join("\n\n")}`,
- guardrails.length && `## Guardrail changes\n\n${list(guardrails.map(code))}`,
- configs.length && `## Toolchain and registry config\n\n${list(configs.map(code))}`,
- lintPolicies.length && `## Rust lint declarations and files containing unsafe\n\n${list(lintPolicies.map(code))}`,
-]
- .filter(Boolean)
- .join("\n\n");
-
-if (local) {
- if (summary) console.log(`${summary}\n\n${detail}\n`);
- process.exit(0);
-}
-
-appendFileSync(GITHUB_OUTPUT, `categories=${JSON.stringify(categories.map(([, many]) => many))}\n`);
-if (summary) console.log(`::warning title=Change approval needed::${summary}`);
-appendFileSync(GITHUB_STEP_SUMMARY, summary ? `${summary}\n\n${detail}\n` : "No approval needed.\n");
+writeReport({
+ files: checkFiles(changes),
+ dependencies: checkDependencies(changes),
+ rust: checkRust(changes),
+});
diff --git a/.github/checks/change-approval/dependencies.mjs b/.github/checks/change-approval/dependencies.mjs
new file mode 100644
index 0000000000..a82ce17fde
--- /dev/null
+++ b/.github/checks/change-approval/dependencies.mjs
@@ -0,0 +1,147 @@
+import { basename } from "node:path";
+
+const tomlPackages = (text) =>
+ text
+ .split("[[package]]\n")
+ .slice(1)
+ .map((block) => ({
+ name: block.match(/^name = "(.+)"$/m)[1],
+ version: block.match(/^version = "(.+)"$/m)[1],
+ source: block
+ .match(/^source = "?(.+?)"?$/m)?.[1]
+ ?.replace(/[?#][^"]*/, ""),
+ }));
+const lockfiles = {
+ "Cargo.lock": (text) =>
+ tomlPackages(text).map((p) => ({
+ ...p,
+ source: p.source ?? "workspace",
+ local: !p.source,
+ })),
+ "uv.lock": (text) =>
+ tomlPackages(text).map((p) => ({
+ ...p,
+ local: !/^\{ (registry|git|url) = /.test(p.source),
+ })),
+ "package-lock.json": (text) =>
+ Object.entries(JSON.parse(text).packages)
+ .filter(([key]) => key.includes("node_modules/"))
+ .map(([key, { name, version, resolved, link }]) => {
+ const local =
+ link === true || resolved?.startsWith("file:") === true;
+ return {
+ name:
+ name ??
+ key.slice(key.lastIndexOf("node_modules/") + 13),
+ version,
+ source:
+ local || !resolved
+ ? resolved
+ : resolved
+ .replace(/#.*$/, "")
+ .replace(/\/-\/[^/]+\.tgz$/, ""),
+ local,
+ };
+ }),
+ "pubspec.lock": (text) =>
+ [...text.matchAll(/^ (\S+):\n((?: .*\n)+)/gm)].map(
+ ([, name, body]) => {
+ const source = [
+ body.match(/^ source: (.+)$/m)[1],
+ body.match(/^ url: "(.+)"$/m)?.[1],
+ ]
+ .filter(Boolean)
+ .join(" ");
+ return {
+ name,
+ version: body.match(/^ version: "(.+)"$/m)[1],
+ source,
+ local: source === "path",
+ };
+ },
+ ),
+ "go.sum": (text) =>
+ [...text.matchAll(/^(\S+) (\S+?)(?:\/go\.mod)? h1:/gm)].map(
+ ([, name, version]) => ({ name, version }),
+ ),
+ "Package.resolved": (text) =>
+ JSON.parse(text).pins.map(({ identity, location, state }) => ({
+ name: identity,
+ version: state.version,
+ source: location,
+ })),
+ "Podfile.lock": (text) => {
+ const section = (title) =>
+ text.match(new RegExp(`^${title}:\\n((?: .*\\n)+)`, "m"))?.[1] ??
+ "";
+ const sources = new Map();
+ for (const [, repo, pods] of section("SPEC REPOS").matchAll(
+ /^ (\S+):\n((?: - .*\n)+)/gm,
+ )) {
+ for (const [, pod] of pods.matchAll(/^ - (.+)$/gm))
+ sources.set(pod, { source: repo });
+ }
+ for (const [, pod, body] of section("EXTERNAL SOURCES").matchAll(
+ /^ (\S+):\n((?: .*\n)+)/gm,
+ )) {
+ const props = Object.fromEntries(
+ [...body.matchAll(/^ :(\w+): (.+)$/gm)].map(
+ ([, key, value]) => [key, value],
+ ),
+ );
+ const source = props.git ?? props.podspec ?? props.path;
+ sources.set(pod, {
+ source,
+ local: !props.git && !/^"?https?:\/\//.test(source),
+ });
+ }
+ return [
+ ...section("PODS").matchAll(/^ - "?([^/" ]+)\S* \(([^)]+)\)/gm),
+ ].map(([, name, version]) => ({ name, version, ...sources.get(name) }));
+ },
+};
+
+export function checkDependencies({ files, baseFiles, readBefore, readAfter }) {
+ const known = new Map();
+ for (const file of baseFiles()) {
+ const kind = basename(file);
+ if (!lockfiles[kind]) continue;
+ if (!known.has(kind)) known.set(kind, new Map());
+ const names = known.get(kind);
+ for (const { name, source } of lockfiles[kind](readBefore(file))) {
+ if (!names.has(name)) names.set(name, new Set());
+ names.get(name).add(source);
+ }
+ }
+ const dependencies = [];
+ for (const { path: file, deleted } of files) {
+ const kind = basename(file);
+ if (deleted || !lockfiles[kind]) continue;
+ const before = known.get(kind) ?? new Map();
+ const added = new Map();
+ const moved = new Map();
+ for (const { name, version, source, local } of lockfiles[kind](
+ readAfter(file),
+ )) {
+ if (local) continue;
+ const sources = before.get(name);
+ if (!sources) {
+ added.set(JSON.stringify([name, version]), { name, version });
+ } else if (source && !sources.has(source)) {
+ moved.set(JSON.stringify([name, source]), {
+ name,
+ sourcesBefore: [...sources],
+ sourceAfter: source,
+ });
+ }
+ }
+ if (added.size || moved.size) {
+ dependencies.push({
+ file,
+ added: [...added.values()],
+ moved: [...moved.values()],
+ });
+ }
+ }
+ return dependencies;
+}
diff --git a/.github/checks/change-approval/files.mjs b/.github/checks/change-approval/files.mjs
new file mode 100644
index 0000000000..27be282f7d
--- /dev/null
+++ b/.github/checks/change-approval/files.mjs
@@ -0,0 +1,50 @@
+import { basename } from "node:path";
+
+const routineBinary = (file) =>
+ /\.(png|jpe?g|webp|gif|ico|icns|ttf|otf|woff2?|riv|mp3)$/i.test(file) ||
+ file.includes("Assets.xcassets/");
+const guardrailDirs = [
+ ".github/",
+ "mobile/checks/",
+ "mobile/scripts/",
+ "rust/checks/",
+ "rust/scripts/",
+ "web/checks/",
+ "web/scripts/",
+ "server/scripts/",
+ "web/packages/build-config/",
+];
+const guardrailFiles = new Set([
+ "eslint.config.mjs",
+ ".prettierrc.json",
+ "analysis_options.yaml",
+]);
+const configFile =
+ /(^|\/)(\.gitattributes|\.?clippy\.toml|rust-toolchain\.toml|\.cargo\/(config|audit)\.toml|\.npmrc|\.nvmrc|\.tool-versions|\.node-version|\.python-version|gradle-wrapper\.properties)$/;
+
+export function checkFiles({ files }) {
+ const binaries = files
+ .filter(
+ ({ path, binary, deleted }) =>
+ binary && !deleted && !routineBinary(path),
+ )
+ .map(({ path, size }) => ({ path, size }));
+ const large = files
+ .filter(({ added, size }) => added && size > 1024 * 1024)
+ .map(({ path, size }) => ({ path, size }));
+ const guardrails = [
+ ...files.filter(
+ ({ path, added }) =>
+ !added &&
+ (guardrailDirs.some((dir) => path.startsWith(dir)) ||
+ guardrailFiles.has(basename(path))),
+ ),
+ ...files.filter(
+ ({ path, added }) => added && path.startsWith(".github/"),
+ ),
+ ].map(({ path }) => path);
+ const configs = files
+ .filter(({ path }) => configFile.test(path))
+ .map(({ path }) => path);
+ return { binaries, large, guardrails, configs };
+}
diff --git a/.github/checks/change-approval/report.mjs b/.github/checks/change-approval/report.mjs
new file mode 100644
index 0000000000..bad12821f7
--- /dev/null
+++ b/.github/checks/change-approval/report.mjs
@@ -0,0 +1,110 @@
+import { appendFileSync } from "node:fs";
+
+export function writeReport({
+ files: { binaries, large, guardrails, configs },
+ dependencies,
+ rust,
+}) {
+ const categories = [
+ {
+ singular: "binary file",
+ plural: "binary files",
+ count: binaries.length,
+ },
+ { singular: "large file", plural: "large files", count: large.length },
+ {
+ singular: "new dependency",
+ plural: "new dependencies",
+ count: dependencies.reduce(
+ (count, { added }) => count + added.length,
+ 0,
+ ),
+ },
+ {
+ singular: "dependency source change",
+ plural: "dependency source changes",
+ count: dependencies.reduce(
+ (count, { moved }) => count + moved.length,
+ 0,
+ ),
+ },
+ {
+ singular: "guardrail file",
+ plural: "guardrail files",
+ count: guardrails.length,
+ },
+ {
+ singular: "config file",
+ plural: "config files",
+ count: configs.length,
+ },
+ {
+ singular: "Rust lint policy file",
+ plural: "Rust lint policy files",
+ count: rust.length,
+ },
+ ].filter(({ count }) => count);
+ const summary = categories
+ .map(
+ ({ singular, plural, count }) =>
+ `${count} ${count === 1 ? singular : plural}`,
+ )
+ .join(", ");
+ const sections = [];
+ if (binaries.length)
+ sections.push(`## Binary files\n\n${list(binaries.map(withSize))}`);
+ if (large.length)
+ sections.push(`## Large files\n\n${list(large.map(withSize))}`);
+ if (dependencies.length) {
+ const entries = dependencies.map(
+ ({ file, added, moved }) =>
+ `${code(file)}\n\n${list([
+ ...added.map(({ name, version }) => `${name} ${version}`),
+ ...moved.map(
+ ({ name, sourcesBefore, sourceAfter }) =>
+ `${name}: ${sourcesBefore.join(", ")} -> ${sourceAfter}`,
+ ),
+ ])}`,
+ );
+ sections.push(`## New dependencies\n\n${entries.join("\n\n")}`);
+ }
+ if (guardrails.length)
+ sections.push(`## Guardrail changes\n\n${list(guardrails.map(code))}`);
+ if (configs.length)
+ sections.push(
+ `## Toolchain and registry config\n\n${list(configs.map(code))}`,
+ );
+ if (rust.length)
+ sections.push(
+ `## Rust lint declarations and files containing unsafe\n\n${list(rust.map(code))}`,
+ );
+ const detail = sections.join("\n\n");
+
+ const { GITHUB_OUTPUT, GITHUB_STEP_SUMMARY } = process.env;
+ if (!GITHUB_OUTPUT) {
+ if (summary) console.log(`${summary}\n\n${detail}\n`);
+ return;
+ }
+ appendFileSync(
+ GITHUB_OUTPUT,
+ `categories=${JSON.stringify(categories.map(({ plural }) => plural))}\n`,
+ );
+ if (summary)
+ console.log(`::warning title=Change approval needed::${summary}`);
+ appendFileSync(
+ GITHUB_STEP_SUMMARY,
+ summary ? `${summary}\n\n${detail}\n` : "No approval needed.\n",
+ );
+}
+
+function list(items) {
+ return items.map((item) => `- ${item}`).join("\n");
+}
+
+function code(path) {
+ return `\`${path}\``;
+}
+
+function withSize({ path, size }) {
+ return `${code(path)} (${size} bytes)`;
+}
diff --git a/.github/checks/change-approval/rust.mjs b/.github/checks/change-approval/rust.mjs
new file mode 100644
index 0000000000..7d6db20a90
--- /dev/null
+++ b/.github/checks/change-approval/rust.mjs
@@ -0,0 +1,127 @@
+import { execFileSync } from "node:child_process";
+import { basename } from "node:path";
+
+const cargoLints = (source) =>
+ execFileSync(
+ "python3",
+ [
+ "-c",
+ `
+import json, sys, tomllib
+cargo = tomllib.loads(sys.stdin.read())
+workspace = cargo.get("workspace", {})
+selection = {key: sorted(workspace[key]) if key in workspace else None for key in ("members", "exclude", "default-members")}
+print(json.dumps([cargo.get("lints"), workspace.get("lints"), selection], sort_keys=True))
+`,
+ ],
+ { encoding: "utf8", input: source },
+ );
+
+function rustPolicy(source) {
+ const lexer =
+ /\/\/[^\n]*|\/\*|[bc]?r(#+)?"[\s\S]*?"\1|[bc]?"(?:\\[\s\S]|[^"\\])*"|b?'(?:\\(?:u\{[\da-fA-F_]+\}|x[\da-fA-F]{2}|[\s\S])|[^'\\\r\n])'|(?:r#)?[a-zA-Z_]\w*|[^\s]/gu;
+ const tokens = [];
+ let match;
+ while ((match = lexer.exec(source))) {
+ const token = match[0];
+ if (token.startsWith("//")) continue;
+ if (token === "/*") {
+ const comments = /\/\*|\*\//g;
+ comments.lastIndex = lexer.lastIndex;
+ for (let depth = 1; depth; ) {
+ const comment = comments.exec(source);
+ if (!comment) throw new Error("Unclosed Rust comment");
+ depth += comment[0] === "/*" ? 1 : -1;
+ }
+ lexer.lastIndex = comments.lastIndex;
+ } else {
+ tokens.push(token);
+ }
+ }
+ const pairs = { "(": ")", "[": "]", "{": "}" };
+ let cursor = 0;
+ const group = (closing) => {
+ const nodes = [];
+ while (cursor < tokens.length) {
+ const token = tokens[cursor++];
+ if (token === closing) return nodes;
+ if (Object.hasOwn(pairs, token))
+ nodes.push([token, ...group(pairs[token]), pairs[token]]);
+ else if ([")", "]", "}"].includes(token))
+ throw new Error("Unmatched Rust delimiter");
+ else nodes.push(token);
+ }
+ if (closing) throw new Error("Unclosed Rust delimiter");
+ return nodes;
+ };
+ const lint = (nodes) =>
+ nodes.some((node, i) =>
+ Array.isArray(node)
+ ? lint(node)
+ : /^(?:r#)?(?:allow|expect|warn|deny|forbid)$/.test(node) &&
+ nodes[i + 1]?.[0] === "(",
+ );
+ const declarations = [];
+ const visit = (nodes) => {
+ for (let i = 0; i < nodes.length; i++) {
+ if (nodes[i] === "#") {
+ let end = i;
+ let hasLint = false;
+ while (nodes[end] === "#") {
+ const bang = nodes[end + 1] === "!";
+ const attribute = nodes[end + (bang ? 2 : 1)];
+ if (!Array.isArray(attribute) || attribute[0] !== "[")
+ break;
+ hasLint ||= lint(attribute);
+ end += bang ? 3 : 2;
+ }
+ if (hasLint) {
+ let stop = end;
+ if (nodes[i + 1] !== "!") {
+ while (
+ stop < nodes.length &&
+ !["{", ";", ",", "="].includes(
+ Array.isArray(nodes[stop])
+ ? nodes[stop][0]
+ : nodes[stop],
+ )
+ )
+ stop++;
+ }
+ declarations.push(JSON.stringify(nodes.slice(i, stop)));
+ }
+ if (end > i) {
+ i = end - 1;
+ continue;
+ }
+ }
+ if (Array.isArray(nodes[i])) visit(nodes[i].slice(1, -1));
+ }
+ };
+ visit(group());
+ return {
+ unsafe: tokens.includes("unsafe"),
+ declarations: JSON.stringify(declarations.sort()),
+ };
+}
+
+export function checkRust({ files, readVersions }) {
+ return files
+ .filter(
+ ({ path }) =>
+ path.endsWith(".rs") || basename(path) === "Cargo.toml",
+ )
+ .filter(({ path }) => {
+ const { before, after } = readVersions(path);
+ if (!path.endsWith(".rs"))
+ return cargoLints(before) !== cargoLints(after);
+ const oldPolicy = rustPolicy(before);
+ const newPolicy = rustPolicy(after);
+ return (
+ oldPolicy.unsafe ||
+ newPolicy.unsafe ||
+ oldPolicy.declarations !== newPolicy.declarations
+ );
+ })
+ .map(({ path }) => path);
+}
diff --git a/.github/checks/change-approval/test.mjs b/.github/checks/change-approval/test.mjs
index c0412dfe1e..c765b84fb1 100644
--- a/.github/checks/change-approval/test.mjs
+++ b/.github/checks/change-approval/test.mjs
@@ -1,6 +1,15 @@
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
-import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
+import {
+ cpSync,
+ existsSync,
+ mkdirSync,
+ mkdtempSync,
+ readFileSync,
+ rmSync,
+ symlinkSync,
+ writeFileSync,
+} from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import test from "node:test";
@@ -16,10 +25,23 @@ const env = {
GIT_COMMITTER_EMAIL: "[email protected]",
};
-const scan = (t, base, change, { commit: committed = true, gitlink, symlink, ci = false } = {}) => {
+const scan = (
+ t,
+ base,
+ change,
+ {
+ commit: committed = true,
+ gitlink,
+ symlink,
+ ci = false,
+ archive = false,
+ directory = ".",
+ } = {},
+) => {
const repo = mkdtempSync(join(tmpdir(), "change-approval-"));
t.after(() => rmSync(repo, { recursive: true }));
- const git = (...args) => execFileSync("git", args, { cwd: repo, env, encoding: "utf8" });
+ const git = (...args) =>
+ execFileSync("git", args, { cwd: repo, env, encoding: "utf8" });
const write = (files) => {
for (const [file, content] of Object.entries(files)) {
if (content === null) {
@@ -32,20 +54,60 @@ const scan = (t, base, change, { commit: committed = true, gitlink, symlink, ci
};
const commit = (revision) => {
git("add", "-A");
- if (gitlink) git("update-index", "--add", "--cacheinfo", `160000,${String(revision).repeat(40)},${gitlink}`);
+ if (gitlink)
+ git(
+ "update-index",
+ "--add",
+ "--cacheinfo",
+ `160000,${String(revision).repeat(40)},${gitlink}`,
+ );
git("commit", "-q", "--allow-empty", "-m", "change");
return git("rev-parse", "HEAD").trim();
};
git("init", "-q", "-b", "main");
+ const checkerDir = ".github/checks/change-approval";
+ if (archive)
+ cpSync(import.meta.dirname, join(repo, checkerDir), {
+ recursive: true,
+ });
write(base);
const sha = commit(1);
write(change);
if (symlink) symlinkSync("missing-target", join(repo, symlink));
if (committed) commit(2);
- const outputs = ci ? { GITHUB_OUTPUT: join(repo, ".output"), GITHUB_STEP_SUMMARY: join(repo, ".summary") } : {};
- const stdout = execFileSync(process.execPath, [script, sha], { cwd: repo, env: { ...env, ...outputs }, encoding: "utf8" });
+ let entry = script;
+ if (archive) {
+ const extracted = mkdtempSync(
+ join(tmpdir(), "change-approval-trusted-"),
+ );
+ t.after(() => rmSync(extracted, { recursive: true }));
+ execFileSync("tar", ["-x", "-C", extracted], {
+ input: execFileSync("git", ["archive", sha, checkerDir], {
+ cwd: repo,
+ env,
+ }),
+ });
+ entry = join(extracted, checkerDir, "check.mjs");
+ }
+ const outputs = ci
+ ? {
+ GITHUB_OUTPUT: join(repo, ".output"),
+ GITHUB_STEP_SUMMARY: join(repo, ".summary"),
+ }
+ : {};
+ const stdout = execFileSync(process.execPath, [entry, sha], {
+ cwd: join(repo, directory),
+ env: { ...env, ...outputs },
+ encoding: "utf8",
+ });
const read = (file) => (existsSync(file) ? readFileSync(file, "utf8") : "");
- return ci ? { stdout, output: read(outputs.GITHUB_OUTPUT), summary: read(outputs.GITHUB_STEP_SUMMARY) } : stdout;
+ return ci
+ ? {
+ stdout,
+ output: read(outputs.GITHUB_OUTPUT),
+ summary: read(outputs.GITHUB_STEP_SUMMARY),
+ }
+ : stdout;
};
const registry = "registry+https://github.com/rust-lang/crates.io-index";
@@ -56,6 +118,7 @@ const cargo = (packages) =>
`[[package]]\nname = "${name}"\nversion = "${version}"\n${source ? `source = "${source}"\n` : ""}`,
)
.join("\n");
+
const npm = (packages) => JSON.stringify({ packages: { "": {}, ...packages } });
const tarball = (name, version, host = "registry.npmjs.org") => ({
version,
@@ -73,7 +136,12 @@ const pub = (packages) =>
)
.join("")}sdks:\n dart: ">=3.0.0 <4.0.0"\n`;
const gosum = (modules) =>
- modules.map(([name, version]) => `${name} ${version} h1:x=\n${name} ${version}/go.mod h1:y=\n`).join("");
+ modules
+ .map(
+ ([name, version]) =>
+ `${name} ${version} h1:x=\n${name} ${version}/go.mod h1:y=\n`,
+ )
+ .join("");
const pins = (entries) =>
JSON.stringify({
pins: entries.map(([identity, version]) => ({
@@ -83,23 +151,47 @@ const pins = (entries) =>
})),
});
const podfile = ({ pods, repos = {}, external = {} }) =>
- `PODS:\n${pods.map((spec) => ` - ${spec}\n`).join("")}\nSPEC REPOS:\n${Object.entries(repos)
- .map(([repo, names]) => ` ${repo}:\n${names.map((name) => ` - ${name}\n`).join("")}`)
+ `PODS:\n${pods.map((spec) => ` - ${spec}\n`).join("")}\nSPEC REPOS:\n${Object.entries(
+ repos,
+ )
+ .map(
+ ([repo, names]) =>
+ ` ${repo}:\n${names.map((name) => ` - ${name}\n`).join("")}`,
+ )
.join("")}\nEXTERNAL SOURCES:\n${Object.entries(external)
- .map(([name, props]) => ` ${name}:\n${Object.entries(props).map(([key, value]) => ` :${key}: ${value}\n`).join("")}`)
+ .map(
+ ([name, props]) =>
+ ` ${name}:\n${Object.entries(props)
+ .map(([key, value]) => ` :${key}: ${value}\n`)
+ .join("")}`,
+ )
.join("")}\nCOCOAPODS: 1.17.0\n`;
const uv = (entries) =>
entries
- .map(([name, version, source = '{ registry = "https://pypi.org/simple" }']) => `[[package]]\nname = "${name}"\nversion = "${version}"\nsource = ${source}\n`)
+ .map(
+ ([
+ name,
+ version,
+ source = '{ registry = "https://pypi.org/simple" }',
+ ]) =>
+ `[[package]]\nname = "${name}"\nversion = "${version}"\nsource = ${source}\n`,
+ )
.join("\n");
test("binary added", (t) => {
const output = scan(t, {}, { "a.bin": Buffer.alloc(16) });
- assert.equal(output, "1 binary file\n\n## Binary files\n\n- `a.bin` (16 bytes)\n\n");
+ assert.equal(
+ output,
+ "1 binary file\n\n## Binary files\n\n- `a.bin` (16 bytes)\n\n",
+ );
});
test("binary modified", (t) => {
- const output = scan(t, { "a.bin": Buffer.alloc(16) }, { "a.bin": Buffer.alloc(32) });
+ const output = scan(
+ t,
+ { "a.bin": Buffer.alloc(16) },
+ { "a.bin": Buffer.alloc(32) },
+ );
assert.match(output, /^1 binary file\n/);
assert.match(output, /`a.bin` \(32 bytes\)/);
});
@@ -112,17 +204,30 @@ test("CI preserves tabs and newlines in binary and large filenames", (t) => {
const binary = "image.png\tpayload.jar";
const large = "large\nfile.txt";
const size = 1024 * 1024 + 1;
- const { output, summary } = scan(t, {}, { [binary]: Buffer.alloc(16), [large]: "a".repeat(size) }, { ci: true });
+ const { output, summary } = scan(
+ t,
+ {},
+ { [binary]: Buffer.alloc(16), [large]: "a".repeat(size) },
+ { ci: true },
+ );
assert.equal(output, 'categories=["binary files","large files"]\n');
- assert.equal(summary, `1 binary file, 1 large file\n\n## Binary files\n\n- \`${binary}\` (16 bytes)\n\n## Large files\n\n- \`${large}\` (${size} bytes)\n`);
+ assert.equal(
+ summary,
+ `1 binary file, 1 large file\n\n## Binary files\n\n- \`${binary}\` (16 bytes)\n\n## Large files\n\n- \`${large}\` (${size} bytes)\n`,
+ );
});
test("routine image, font, and xcassets binaries are ignored", (t) => {
- const output = scan(t, {}, {
- "src/logo.PNG": Buffer.alloc(16),
- "web/fonts/a.woff2": Buffer.alloc(16),
- "ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents": Buffer.alloc(16),
- });
+ const output = scan(
+ t,
+ {},
+ {
+ "src/logo.PNG": Buffer.alloc(16),
+ "web/fonts/a.woff2": Buffer.alloc(16),
+ "ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents":
+ Buffer.alloc(16),
+ },
+ );
assert.equal(output, "");
});
@@ -131,7 +236,11 @@ test("large files added, binary or text, not grown", (t) => {
const output = scan(
t,
{ "big.txt": "a" },
- { "big.txt": "a".repeat(size), "big.bin": Buffer.alloc(size), "new.txt": "b".repeat(size) },
+ {
+ "big.txt": "a".repeat(size),
+ "big.bin": Buffer.alloc(size),
+ "new.txt": "b".repeat(size),
+ },
);
assert.equal(
output,
@@ -142,20 +251,46 @@ test("large files added, binary or text, not grown", (t) => {
test("Cargo.lock new package, not version bump", (t) => {
const output = scan(
t,
- { "rust/Cargo.lock": cargo([["a", "1.0.0"], ["b", "1.0.0"]]) },
- { "rust/Cargo.lock": cargo([["a", "1.1.0"], ["b", "1.0.0"], ["c", "2.0.0"]]) },
+ {
+ "rust/Cargo.lock": cargo([
+ ["a", "1.0.0"],
+ ["b", "1.0.0"],
+ ]),
+ },
+ {
+ "rust/Cargo.lock": cargo([
+ ["a", "1.1.0"],
+ ["b", "1.0.0"],
+ ["c", "2.0.0"],
+ ]),
+ },
+ );
+ assert.equal(
+ output,
+ "1 new dependency\n\n## New dependencies\n\n`rust/Cargo.lock`\n\n- c 2.0.0\n\n",
);
- assert.equal(output, "1 new dependency\n\n## New dependencies\n\n`rust/Cargo.lock`\n\n- c 2.0.0\n\n");
});
test("workspace-local packages are not dependencies", (t) => {
const output = scan(
t,
- { "Cargo.lock": cargo([["a", "1.0.0"]]), "pubspec.lock": pub([["a", "1.0.0"]]), "package-lock.json": npm({}) },
{
- "Cargo.lock": cargo([["a", "1.0.0"], ["member", "0.0.0", ""]]),
- "pubspec.lock": pub([["a", "1.0.0"], ["local", "0.0.1", "path"]]),
- "package-lock.json": npm({ "node_modules/w": { resolved: "apps/w", link: true } }),
+ "Cargo.lock": cargo([["a", "1.0.0"]]),
+ "pubspec.lock": pub([["a", "1.0.0"]]),
+ "package-lock.json": npm({}),
+ },
+ {
+ "Cargo.lock": cargo([
+ ["a", "1.0.0"],
+ ["member", "0.0.0", ""],
+ ]),
+ "pubspec.lock": pub([
+ ["a", "1.0.0"],
+ ["local", "0.0.1", "path"],
+ ]),
+ "package-lock.json": npm({
+ "node_modules/w": { resolved: "apps/w", link: true },
+ }),
},
);
assert.equal(output, "");
@@ -165,10 +300,20 @@ test("dependency source change, even beside the original version", (t) => {
const fork = "git+https://github.com/x/a";
const output = scan(
t,
- { "package-lock.json": npm({ "node_modules/y": tarball("y", "1.0.0") }), "rust/Cargo.lock": cargo([["a", "1.0.0"]]) },
{
- "package-lock.json": npm({ "node_modules/y": tarball("y", "1.0.0", "npm.example.com") }),
- "rust/Cargo.lock": cargo([["a", "1.0.0"], ["a", "1.1.0", fork]]),
+ "package-lock.json": npm({
+ "node_modules/y": tarball("y", "1.0.0"),
+ }),
+ "rust/Cargo.lock": cargo([["a", "1.0.0"]]),
+ },
+ {
+ "package-lock.json": npm({
+ "node_modules/y": tarball("y", "1.0.0", "npm.example.com"),
+ }),
+ "rust/Cargo.lock": cargo([
+ ["a", "1.0.0"],
+ ["a", "1.1.0", fork],
+ ]),
},
);
assert.equal(
@@ -182,12 +327,30 @@ test("git rev bump keeps the source, repository change does not", (t) => {
const output = scan(
t,
{
- "rust/Cargo.lock": cargo([["h", "0.2.0", at("https://github.com/ente/heic-decoder.git", "aaa")], ["r", "1.0.0", at("https://github.com/a/r.git", "x")]]),
- "uv.lock": uv([["u", "1.0.0", '{ git = "https://github.com/x/u?rev=1#1" }']]),
+ "rust/Cargo.lock": cargo([
+ [
+ "h",
+ "0.2.0",
+ at("https://github.com/ente/heic-decoder.git", "aaa"),
+ ],
+ ["r", "1.0.0", at("https://github.com/a/r.git", "x")],
+ ]),
+ "uv.lock": uv([
+ ["u", "1.0.0", '{ git = "https://github.com/x/u?rev=1#1" }'],
+ ]),
},
{
- "rust/Cargo.lock": cargo([["h", "0.2.0", at("https://github.com/ente/heic-decoder.git", "bbb")], ["r", "1.0.0", at("https://github.com/b/r.git", "x")]]),
- "uv.lock": uv([["u", "1.0.0", '{ git = "https://github.com/x/u?rev=2#2" }']]),
+ "rust/Cargo.lock": cargo([
+ [
+ "h",
+ "0.2.0",
+ at("https://github.com/ente/heic-decoder.git", "bbb"),
+ ],
+ ["r", "1.0.0", at("https://github.com/b/r.git", "x")],
+ ]),
+ "uv.lock": uv([
+ ["u", "1.0.0", '{ git = "https://github.com/x/u?rev=2#2" }'],
+ ]),
},
);
assert.equal(
@@ -199,7 +362,11 @@ test("git rev bump keeps the source, repository change does not", (t) => {
test("package-lock.json new nested package", (t) => {
const output = scan(
t,
- { "package-lock.json": npm({ "node_modules/x": tarball("x", "1.0.0") }) },
+ {
+ "package-lock.json": npm({
+ "node_modules/x": tarball("x", "1.0.0"),
+ }),
+ },
{
"package-lock.json": npm({
"node_modules/x": tarball("x", "1.0.1"),
@@ -212,7 +379,16 @@ test("package-lock.json new nested package", (t) => {
});
test("pubspec.lock new package", (t) => {
- const output = scan(t, { "pubspec.lock": pub([["a", "1.0.0"]]) }, { "pubspec.lock": pub([["a", "1.0.0"], ["b", "3.1.4"]]) });
+ const output = scan(
+ t,
+ { "pubspec.lock": pub([["a", "1.0.0"]]) },
+ {
+ "pubspec.lock": pub([
+ ["a", "1.0.0"],
+ ["b", "3.1.4"],
+ ]),
+ },
+ );
assert.match(output, /^1 new dependency\n/);
assert.match(output, /- b 3\.1\.4\n/);
});
@@ -222,16 +398,30 @@ test("Package.resolved, Podfile.lock, and uv.lock new packages", (t) => {
t,
{
"Package.resolved": pins([["a", "1.0.0"]]),
- "Podfile.lock": podfile({ pods: ["A/Core (1.0.0)"], repos: { trunk: ["A"] } }),
+ "Podfile.lock": podfile({
+ pods: ["A/Core (1.0.0)"],
+ repos: { trunk: ["A"] },
+ }),
"uv.lock": uv([["a", "1.0.0"]]),
},
{
- "Package.resolved": pins([["a", "1.0.0"], ["b", "2.0.0"]]),
+ "Package.resolved": pins([
+ ["a", "1.0.0"],
+ ["b", "2.0.0"],
+ ]),
"Podfile.lock": podfile({
- pods: ["A/Core (1.0.0)", "A/Extra (1.0.0)", '"B/Sub+x (3.0.0)"'],
+ pods: [
+ "A/Core (1.0.0)",
+ "A/Extra (1.0.0)",
+ '"B/Sub+x (3.0.0)"',
+ ],
repos: { trunk: ["A", "B"] },
}),
- "uv.lock": uv([["a", "1.0.0"], ["c", "4.0.0"], ["me", "0.0.0", '{ virtual = "." }']]),
+ "uv.lock": uv([
+ ["a", "1.0.0"],
+ ["c", "4.0.0"],
+ ["me", "0.0.0", '{ virtual = "." }'],
+ ]),
},
);
assert.equal(
@@ -243,11 +433,21 @@ test("Package.resolved, Podfile.lock, and uv.lock new packages", (t) => {
test("Podfile.lock pod moving from a spec repo to git is a source change", (t) => {
const output = scan(
t,
- { "ios/Podfile.lock": podfile({ pods: ["Sentry (8.0.0)"], repos: { trunk: ["Sentry"] } }) },
{
"ios/Podfile.lock": podfile({
pods: ["Sentry (8.0.0)"],
- external: { Sentry: { branch: "main", git: "https://github.com/x/sentry-cocoa.git" } },
+ repos: { trunk: ["Sentry"] },
+ }),
+ },
+ {
+ "ios/Podfile.lock": podfile({
+ pods: ["Sentry (8.0.0)"],
+ external: {
+ Sentry: {
+ branch: "main",
+ git: "https://github.com/x/sentry-cocoa.git",
+ },
+ },
}),
},
);
@@ -260,14 +460,24 @@ test("Podfile.lock pod moving from a spec repo to git is a source change", (t) =
test("local path packages are not dependencies until they leave the tree", (t) => {
const output = scan(
t,
- { "ios/Podfile.lock": podfile({ pods: ["A (1.0.0)"], repos: { trunk: ["A"] } }), "uv.lock": uv([["b", "1.0.0", '{ directory = "../b" }']]) },
+ {
+ "ios/Podfile.lock": podfile({
+ pods: ["A (1.0.0)"],
+ repos: { trunk: ["A"] },
+ }),
+ "uv.lock": uv([["b", "1.0.0", '{ directory = "../b" }']]),
+ },
{
"ios/Podfile.lock": podfile({
pods: ["A (1.0.0)", "Local (1.0.0)"],
repos: { trunk: ["A"] },
external: { Local: { path: '"../Local"' } },
}),
- "uv.lock": uv([["b", "1.0.0"], ["c", "2.0.0", '{ directory = "../c" }'], ["d", "3.0.0", '{ editable = "." }']]),
+ "uv.lock": uv([
+ ["b", "1.0.0"],
+ ["c", "2.0.0", '{ directory = "../c" }'],
+ ["d", "3.0.0", '{ editable = "." }'],
+ ]),
},
);
assert.equal(
@@ -279,11 +489,18 @@ test("local path packages are not dependencies until they leave the tree", (t) =
test("npm alias of a present package is not new", (t) => {
const output = scan(
t,
- { "package-lock.json": npm({ "node_modules/react": tarball("react", "18.3.1") }) },
{
"package-lock.json": npm({
"node_modules/react": tarball("react", "18.3.1"),
- "node_modules/react-alias": { name: "react", ...tarball("react", "18.3.1") },
+ }),
+ },
+ {
+ "package-lock.json": npm({
+ "node_modules/react": tarball("react", "18.3.1"),
+ "node_modules/react-alias": {
+ name: "react",
+ ...tarball("react", "18.3.1"),
+ },
}),
},
);
@@ -293,25 +510,45 @@ test("npm alias of a present package is not new", (t) => {
test("bundled npm entry without resolved is new by name only", (t) => {
const output = scan(
t,
- { "package-lock.json": npm({ "node_modules/x": tarball("x", "1.0.0") }) },
+ {
+ "package-lock.json": npm({
+ "node_modules/x": tarball("x", "1.0.0"),
+ }),
+ },
{
"package-lock.json": npm({
"node_modules/x": tarball("x", "1.0.1"),
- "node_modules/x/node_modules/y": { version: "1.0.0", inBundle: true },
+ "node_modules/x/node_modules/y": {
+ version: "1.0.0",
+ inBundle: true,
+ },
}),
},
);
- assert.equal(output, "1 new dependency\n\n## New dependencies\n\n`package-lock.json`\n\n- y 1.0.0\n\n");
+ assert.equal(
+ output,
+ "1 new dependency\n\n## New dependencies\n\n`package-lock.json`\n\n- y 1.0.0\n\n",
+ );
});
test("npm file: entry is local until it moves to the registry", (t) => {
const output = scan(
t,
- { "package-lock.json": npm({ "node_modules/x": { version: "1.0.0", resolved: "file:vendor/x-1.0.0.tgz" } }) },
+ {
+ "package-lock.json": npm({
+ "node_modules/x": {
+ version: "1.0.0",
+ resolved: "file:vendor/x-1.0.0.tgz",
+ },
+ }),
+ },
{
"package-lock.json": npm({
"node_modules/x": tarball("x", "1.0.0"),
- "node_modules/z": { version: "2.0.0", resolved: "file:vendor/z-2.0.0.tgz" },
+ "node_modules/z": {
+ version: "2.0.0",
+ resolved: "file:vendor/z-2.0.0.tgz",
+ },
}),
},
);
@@ -325,7 +562,12 @@ test("go.sum new module", (t) => {
const output = scan(
t,
{ "go.sum": gosum([["a.com/x", "v1.0.0"]]) },
- { "go.sum": gosum([["a.com/x", "v1.2.0"], ["b.org/y/v2", "v2.0.1"]]) },
+ {
+ "go.sum": gosum([
+ ["a.com/x", "v1.2.0"],
+ ["b.org/y/v2", "v2.0.1"],
+ ]),
+ },
);
assert.match(output, /^1 new dependency\n/);
assert.match(output, /- b\.org\/y\/v2 v2\.0\.1\n/);
@@ -367,7 +609,8 @@ test("new GitHub workflows, actions and policies need approval", (t) => {
const { output, summary } = scan(t, {}, files, { ci: true });
assert.equal(output, 'categories=["guardrail files"]\n');
assert.match(summary, /3 guardrail files/);
- for (const file of Object.keys(files)) assert.ok(summary.includes(`\`${file}\``));
+ for (const file of Object.keys(files))
+ assert.ok(summary.includes(`\`${file}\``));
assert.match(scan(t, {}, files, { commit: false }), /^3 guardrail files\n/);
});
@@ -375,7 +618,11 @@ test("toolchain and registry config added, modified, or deleted", (t) => {
const output = scan(
t,
{ "web/.npmrc": "", "rust/.cargo/config.toml": "" },
- { "web/.npmrc": "registry=https://example.com\n", "rust/.cargo/config.toml": null, ".nvmrc": "24\n" },
+ {
+ "web/.npmrc": "registry=https://example.com\n",
+ "rust/.cargo/config.toml": null,
+ ".nvmrc": "24\n",
+ },
);
assert.equal(
output,
@@ -384,15 +631,31 @@ test("toolchain and registry config added, modified, or deleted", (t) => {
});
test("new root .cargo/config.toml is a config file, even untracked", (t) => {
- const expected = "1 config file\n\n## Toolchain and registry config\n\n- `.cargo/config.toml`\n\n";
- assert.equal(scan(t, { "a.txt": "a\n" }, { ".cargo/config.toml": "[registries]\n" }), expected);
- assert.equal(scan(t, { "a.txt": "a\n" }, { ".cargo/config.toml": "[registries]\n" }, { commit: false }), expected);
+ const expected =
+ "1 config file\n\n## Toolchain and registry config\n\n- `.cargo/config.toml`\n\n";
+ assert.equal(
+ scan(t, { "a.txt": "a\n" }, { ".cargo/config.toml": "[registries]\n" }),
+ expected,
+ );
+ assert.equal(
+ scan(
+ t,
+ { "a.txt": "a\n" },
+ { ".cargo/config.toml": "[registries]\n" },
+ { commit: false },
+ ),
+ expected,
+ );
});
test("Git attributes require config approval even when they hide binary changes", (t) => {
const { output, summary } = scan(
t,
- { "modified/.gitattributes": "", "deleted/.gitattributes": "", "a.jar": Buffer.alloc(16) },
+ {
+ "modified/.gitattributes": "",
+ "deleted/.gitattributes": "",
+ "a.jar": Buffer.alloc(16),
+ },
{
".gitattributes": "*.jar diff\n",
"added/.gitattributes": "*.jar diff\n",
@@ -403,14 +666,20 @@ test("Git attributes require config approval even when they hide binary changes"
{ ci: true },
);
assert.equal(output, 'categories=["config files"]\n');
- assert.equal(summary, "4 config files\n\n## Toolchain and registry config\n\n- `.gitattributes`\n- `added/.gitattributes`\n- `deleted/.gitattributes`\n- `modified/.gitattributes`\n");
+ assert.equal(
+ summary,
+ "4 config files\n\n## Toolchain and registry config\n\n- `.gitattributes`\n- `added/.gitattributes`\n- `deleted/.gitattributes`\n- `modified/.gitattributes`\n",
+ );
});
test("uncommitted and untracked changes are scanned locally", (t) => {
const output = scan(
t,
{ "a.bin": Buffer.alloc(16) },
- { "a.bin": Buffer.alloc(32), "rust/Cargo.lock": cargo([["a", "1.0.0"]]) },
+ {
+ "a.bin": Buffer.alloc(32),
+ "rust/Cargo.lock": cargo([["a", "1.0.0"]]),
+ },
{ commit: false },
);
assert.equal(
@@ -420,83 +689,170 @@ test("uncommitted and untracked changes are scanned locally", (t) => {
});
test("CI mode writes categories and the step summary, and tolerates gitlinks", (t) => {
- const { stdout, output, summary } = scan(t, { "a.txt": "a\n" }, { "a.bin": Buffer.alloc(16) }, { gitlink: "sub", ci: true });
- assert.equal(stdout, "::warning title=Change approval needed::1 binary file\n");
+ const { stdout, output, summary } = scan(
+ t,
+ { "a.txt": "a\n" },
+ { "a.bin": Buffer.alloc(16) },
+ { gitlink: "sub", ci: true },
+ );
+ assert.equal(
+ stdout,
+ "::warning title=Change approval needed::1 binary file\n",
+ );
assert.equal(output, 'categories=["binary files"]\n');
- assert.equal(summary, "1 binary file\n\n## Binary files\n\n- `a.bin` (16 bytes)\n");
+ assert.equal(
+ summary,
+ "1 binary file\n\n## Binary files\n\n- `a.bin` (16 bytes)\n",
+ );
});
test("untracked dangling symlink is skipped locally", (t) => {
- const output = scan(t, { "a.txt": "a\n" }, { "a.bin": Buffer.alloc(16) }, { commit: false, symlink: "link" });
- assert.equal(output, "1 binary file\n\n## Binary files\n\n- `a.bin` (16 bytes)\n\n");
+ const output = scan(
+ t,
+ { "a.txt": "a\n" },
+ { "a.bin": Buffer.alloc(16) },
+ { commit: false, symlink: "link" },
+ );
+ assert.equal(
+ output,
+ "1 binary file\n\n## Binary files\n\n- `a.bin` (16 bytes)\n\n",
+ );
});
test("CI mode with nothing flagged", (t) => {
- const { stdout, output, summary } = scan(t, { "a.txt": "a\n" }, { "a.txt": "b\n" }, { ci: true });
+ const { stdout, output, summary } = scan(
+ t,
+ { "a.txt": "a\n" },
+ { "a.txt": "b\n" },
+ { ci: true },
+ );
assert.equal(stdout, "");
assert.equal(output, "categories=[]\n");
assert.equal(summary, "No approval needed.\n");
});
test("ordinary change is silent", (t) => {
- assert.equal(scan(t, { "src/a.txt": "a\n" }, { "src/a.txt": "b\n", "src/b.txt": "c\n" }), "");
+ assert.equal(
+ scan(
+ t,
+ { "src/a.txt": "a\n" },
+ { "src/a.txt": "b\n", "src/b.txt": "c\n" },
+ ),
+ "",
+ );
});
test("Cargo lint changes need approval across TOML layouts", (t) => {
for (const [before, after] of [
- ['', '[workspace.lints.rust]\nunsafe_code = "deny"\n'],
- ['[workspace.lints.rust]\nunsafe_code = "deny"\n', '[workspace.lints.rust]\nunsafe_code = "warn"\n'],
- ['[workspace.lints.rust]\nunsafe_code = "deny"\n', ''],
- ['[lints]\nworkspace = true\n', '[lints.rust]\nunsafe_code = "allow"\n'],
- ['', 'workspace.lints = { rust = { unsafe_code = "allow" } }\n'],
- ['[workspace]\nmembers = ["a", "b"]\n', '[workspace]\nmembers = ["a", "b"]\ndefault-members = ["a"]\n'],
- ['[workspace]\nmembers = ["a", "b"]\n', '[workspace]\nmembers = ["a"]\nexclude = ["b"]\n'],
+ ["", '[workspace.lints.rust]\nunsafe_code = "deny"\n'],
+ [
+ '[workspace.lints.rust]\nunsafe_code = "deny"\n',
+ '[workspace.lints.rust]\nunsafe_code = "warn"\n',
+ ],
+ ['[workspace.lints.rust]\nunsafe_code = "deny"\n', ""],
+ [
+ "[lints]\nworkspace = true\n",
+ '[lints.rust]\nunsafe_code = "allow"\n',
+ ],
+ ["", 'workspace.lints = { rust = { unsafe_code = "allow" } }\n'],
+ [
+ '[workspace]\nmembers = ["a", "b"]\n',
+ '[workspace]\nmembers = ["a", "b"]\ndefault-members = ["a"]\n',
+ ],
+ [
+ '[workspace]\nmembers = ["a", "b"]\n',
+ '[workspace]\nmembers = ["a"]\nexclude = ["b"]\n',
+ ],
]) {
- const { output, summary } = scan(t, { "rust/Cargo.toml": before }, { "rust/Cargo.toml": after }, { ci: true });
+ const { output, summary } = scan(
+ t,
+ { "rust/Cargo.toml": before },
+ { "rust/Cargo.toml": after },
+ { ci: true },
+ );
assert.equal(output, 'categories=["Rust lint policy files"]\n');
- assert.match(summary, /## Rust lint declarations and files containing unsafe\n\n- `rust\/Cargo.toml`/);
+ assert.match(
+ summary,
+ /## Rust lint declarations and files containing unsafe\n\n- `rust\/Cargo.toml`/,
+ );
}
});
test("Cargo dependency edits and equivalent lint layouts need no lint approval", (t) => {
- assert.equal(scan(t, {
- "rust/Cargo.toml": '[workspace.lints.rust]\nunsafe_code = "deny"\ndead_code = "warn"\n[workspace.dependencies]\nserde = "1"\n',
- }, {
- "rust/Cargo.toml": 'workspace.lints.rust = { dead_code = "warn", unsafe_code = "deny" }\n[workspace.dependencies]\nserde = "2"\n',
- }), "");
+ assert.equal(
+ scan(
+ t,
+ {
+ "rust/Cargo.toml":
+ '[workspace.lints.rust]\nunsafe_code = "deny"\ndead_code = "warn"\n[workspace.dependencies]\nserde = "1"\n',
+ },
+ {
+ "rust/Cargo.toml":
+ 'workspace.lints.rust = { dead_code = "warn", unsafe_code = "deny" }\n[workspace.dependencies]\nserde = "2"\n',
+ },
+ ),
+ "",
+ );
});
test("reordering Cargo workspace selection lists needs no approval", (t) => {
for (const key of ["members", "exclude", "default-members"]) {
- assert.equal(scan(t, {
- "rust/Cargo.toml": `[workspace]\n${key} = ["a", "b"]\n`,
- }, {
- "rust/Cargo.toml": `[workspace]\n${key} = ["b", "a"]\n`,
- }), "");
+ assert.equal(
+ scan(
+ t,
+ { "rust/Cargo.toml": `[workspace]\n${key} = ["a", "b"]\n` },
+ { "rust/Cargo.toml": `[workspace]\n${key} = ["b", "a"]\n` },
+ ),
+ "",
+ );
}
});
test("Rust lint declarations, reasons and conditions need approval", (t) => {
const expect = '#[expect(dead_code, reason = "Shared helper")]';
- const body = 'fn helper() {}';
+ const body = "fn helper() {}";
for (const [before, after] of [
[body, `${expect}\n${body}`],
[`${expect}\n${body}`, body],
[`${expect}\n${body}`, null],
- [`${expect}\n${body}`, `${expect.replace("Shared helper", "New reason")}\n${body}`],
- [`${expect}\n${body}`, `${expect.replace("dead_code", "unused_variables")}\n${body}`],
+ [
+ `${expect}\n${body}`,
+ `${expect.replace("Shared helper", "New reason")}\n${body}`,
+ ],
+ [
+ `${expect}\n${body}`,
+ `${expect.replace("dead_code", "unused_variables")}\n${body}`,
+ ],
[`#[cfg(unix)]\n${expect}\n${body}`, `${expect}\n${body}`],
- [`#[cfg_attr(unix, expect(dead_code))]\n${body}`, `#[cfg_attr(test, expect(dead_code))]\n${body}`],
+ [
+ `#[cfg_attr(unix, expect(dead_code))]\n${body}`,
+ `#[cfg_attr(test, expect(dead_code))]\n${body}`,
+ ],
[`#[cfg_attr(unix, cfg_attr(test, expect(dead_code)))]\n${body}`, body],
- [`#[path = "a.rs"]\n${expect}\nmod support;`, `#[path = "b.rs"]\n${expect}\nmod support;`],
- [body, `#![allow(clippy::allow_attributes, clippy::allow_attributes_without_reason, dead_code)]\n${body}`],
+ [
+ `#[path = "a.rs"]\n${expect}\nmod support;`,
+ `#[path = "b.rs"]\n${expect}\nmod support;`,
+ ],
+ [
+ body,
+ `#![allow(clippy::allow_attributes, clippy::allow_attributes_without_reason, dead_code)]\n${body}`,
+ ],
[body, `#[allow(dead_code, reason = "Shared helper")]\n${body}`],
- [`#[deny(dead_code)]\nmod guarded {}`, 'mod guarded {}'],
+ [`#[deny(dead_code)]\nmod guarded {}`, "mod guarded {}"],
[body, `#[r#expect(dead_code, reason = "Shared helper")]\n${body}`],
- [`${expect}\nmod support {}`, `${expect.replace("#[", "#![")}\nmod support {}`],
- [`${expect}\n${body}\nfn other() {}`, `${body}\n${expect}\nfn other() {}`],
+ [
+ `${expect}\nmod support {}`,
+ `${expect.replace("#[", "#![")}\nmod support {}`,
+ ],
+ [
+ `${expect}\n${body}\nfn other() {}`,
+ `${body}\n${expect}\nfn other() {}`,
+ ],
]) {
- assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), /^1 Rust lint policy file\n/);
+ assert.match(
+ scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }),
+ /^1 Rust lint policy file\n/,
+ );
}
});
@@ -504,48 +860,77 @@ test("ordinary code under existing lint declarations needs no approval", (t) =>
for (const before of [
'#[expect(dead_code, reason = "Shared helper")] fn helper() { first(); }',
'#![expect(dead_code, reason = "Shared helpers")] fn helper() { first(); }',
- '#![forbid(unsafe_code)] fn helper() { first(); }',
+ "#![forbid(unsafe_code)] fn helper() { first(); }",
'fn helper() { #[expect(unused_variables, reason = "Temporary binding")] let value = first(); }',
]) {
- assert.equal(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('first()', 'second()') }), "");
+ assert.equal(
+ scan(
+ t,
+ { "src/lib.rs": before },
+ { "src/lib.rs": before.replace("first()", "second()") },
+ ),
+ "",
+ );
}
});
test("any edit in a file containing unsafe needs approval", (t) => {
for (const before of [
- 'fn call() { #[expect(unsafe_code)] if ready() {} else { unsafe { first(); } } }',
- 'unsafe impl Send for Context {}\nfn unrelated() { first(); }',
- 'unsafe fn ffi() {}\n// first',
+ "fn call() { #[expect(unsafe_code)] if ready() {} else { unsafe { first(); } } }",
+ "unsafe impl Send for Context {}\nfn unrelated() { first(); }",
+ "unsafe fn ffi() {}\n// first",
]) {
- assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": before.replace('first', 'second') }), /^1 Rust lint policy file\n/);
+ assert.match(
+ scan(
+ t,
+ { "src/lib.rs": before },
+ { "src/lib.rs": before.replace("first", "second") },
+ ),
+ /^1 Rust lint policy file\n/,
+ );
}
});
test("unsafe in either revision needs approval, including new and deleted files", (t) => {
- const source = 'fn call() { unsafe { ffi(); } }';
+ const source = "fn call() { unsafe { ffi(); } }";
for (const [before, after] of [
- ['fn call() {}', source],
- [source, 'fn call() {}'],
+ ["fn call() {}", source],
+ [source, "fn call() {}"],
[source, null],
]) {
- assert.match(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), /^1 Rust lint policy file\n/);
+ assert.match(
+ scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }),
+ /^1 Rust lint policy file\n/,
+ );
}
- assert.match(scan(t, {}, { "src/lib.rs": source }, { commit: false }), /^1 Rust lint policy file\n/);
+ assert.match(
+ scan(t, {}, { "src/lib.rs": source }, { commit: false }),
+ /^1 Rust lint policy file\n/,
+ );
});
test("external module edits need approval only when the edited file contains unsafe", (t) => {
for (const [before, needsApproval] of [
- ['pub fn call() { unsafe {\n first();\n} }', true],
- ['fn helper() { first(); }', false],
+ ["pub fn call() { unsafe {\n first();\n} }", true],
+ ["fn helper() { first(); }", false],
]) {
- const { output } = scan(t, {
- "src/lib.rs": '#[expect(dead_code, unsafe_code, reason = "Shared helpers")] #[path = "support/mod.rs"] mod support;',
- "src/support/mod.rs": 'mod nested;',
- "src/support/nested.rs": before,
- }, {
- "src/support/nested.rs": before.replace('first()', 'second()'),
- }, { ci: true });
- assert.equal(output, needsApproval ? 'categories=["Rust lint policy files"]\n' : 'categories=[]\n');
+ const { output } = scan(
+ t,
+ {
+ "src/lib.rs":
+ '#[expect(dead_code, unsafe_code, reason = "Shared helpers")] #[path = "support/mod.rs"] mod support;',
+ "src/support/mod.rs": "mod nested;",
+ "src/support/nested.rs": before,
+ },
+ { "src/support/nested.rs": before.replace("first()", "second()") },
+ { ci: true },
+ );
+ assert.equal(
+ output,
+ needsApproval
+ ? 'categories=["Rust lint policy files"]\n'
+ : "categories=[]\n",
+ );
}
});
@@ -562,8 +947,63 @@ fn example() {
fn helper() {}
`;
const after = before
- .replaceAll('unsafe', 'example')
- .replace('expect(dead_code, reason', 'expect(\n dead_code, /* comment */\n reason')
- .replace('fn helper() {}', 'fn helper() { let c = \'[\'; }');
- assert.equal(scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }), "");
+ .replaceAll("unsafe", "example")
+ .replace(
+ "expect(dead_code, reason",
+ "expect(\n dead_code, /* comment */\n reason",
+ )
+ .replace("fn helper() {}", "fn helper() { let c = '['; }");
+ assert.equal(
+ scan(t, { "src/lib.rs": before }, { "src/lib.rs": after }),
+ "",
+ );
+});
+
+test("checks started in a subdirectory inspect repository-wide changes", (t) => {
+ const summary =
+ "1 binary file, 1 new dependency\n\n## Binary files\n\n- `new.bin` (16 bytes)\n\n## New dependencies\n\n`rust/Cargo.lock`\n\n- b 2.0.0\n";
+ for (const ci of [false, true]) {
+ const result = scan(
+ t,
+ {
+ "nested/a.txt": "a\n",
+ "rust/Cargo.lock": cargo([["a", "1.0.0"]]),
+ },
+ {
+ "new.bin": Buffer.alloc(16),
+ "rust/Cargo.lock": cargo([
+ ["a", "1.0.0"],
+ ["b", "2.0.0"],
+ ]),
+ },
+ { ci, commit: ci, directory: "nested" },
+ );
+ if (ci) {
+ assert.deepEqual(result, {
+ stdout: "::warning title=Change approval needed::1 binary file, 1 new dependency\n",
+ output: 'categories=["binary files","new dependencies"]\n',
+ summary,
+ });
+ } else {
+ assert.equal(result, `${summary}\n`);
+ }
+ }
+});
+
+test("archived checker imports trusted modules while inspecting changed files", (t) => {
+ const { output, summary } = scan(
+ t,
+ {},
+ {
+ ".github/checks/change-approval/rust.mjs":
+ 'throw new Error("loaded an untrusted rule");',
+ "src/lib.rs": "pub unsafe fn call() {}",
+ },
+ { ci: true, archive: true },
+ );
+ assert.equal(
+ output,
+ 'categories=["guardrail files","Rust lint policy files"]\n',
+ );
+ assert.match(summary, /- `src\/lib.rs`/);
});
diff --git a/.github/workflows/change-approval.yml b/.github/workflows/change-approval.yml
index e98c58dd0f..3fb077759e 100644
--- a/.github/workflows/change-approval.yml
+++ b/.github/workflows/change-approval.yml
@@ -27,8 +27,8 @@ jobs:
id: scan
run: |
git fetch --no-tags --depth=1 origin +refs/heads/main:refs/remotes/origin/main
- git show origin/main:.github/checks/change-approval/check.mjs > "$RUNNER_TEMP/change-approval.mjs"
- node "$RUNNER_TEMP/change-approval.mjs" HEAD^1
+ git archive origin/main .github/checks/change-approval | tar -x -C "$RUNNER_TEMP"
+ node "$RUNNER_TEMP/.github/checks/change-approval/check.mjs" HEAD^1
- name: Run workflow security checks
run: |