126 lines
6.2 KiB
TypeScript
126 lines
6.2 KiB
TypeScript
// PEP-191: the default branch of a project is protected in Gitea — only peppa-bot pushes and merges.
|
|
// Tests are named after the scenarios of openspec/specs/branch-protection.
|
|
import assert from "node:assert/strict";
|
|
import { execFileSync } from "node:child_process";
|
|
import { mkdtempSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { after, before, describe, test } from "node:test";
|
|
import { like } from "drizzle-orm";
|
|
import { telegramOutbox } from "@peppa/db";
|
|
import { core, db, runner, superadmin, teardown } from "./helpers.ts";
|
|
import { MockGitea } from "../src/mock-gitea.ts";
|
|
|
|
const ROOT = resolve(fileURLToPath(import.meta.url), "../../../..");
|
|
const TMP = mkdtempSync(join(tmpdir(), "peppa-protect-"));
|
|
process.env.PEPPA_SECRET = "mock-secret-for-tests";
|
|
process.env.PEPPA_WORKSPACES = join(TMP, "workspaces");
|
|
|
|
const gitea = new MockGitea(join(TMP, "gitea"));
|
|
const ALICE = { id: 91, login: "mock-prot-alice", token: "mock-token-prot-alice" };
|
|
const BOT = { id: 92, login: "peppa-bot", token: "mock-token-prot-bot" };
|
|
type U = Awaited<ReturnType<typeof core.upsertUserFromGitea>>;
|
|
let alice: U;
|
|
let templateUrl = "";
|
|
|
|
before(async () => {
|
|
await gitea.start();
|
|
process.env.GITEA_URL = gitea.url;
|
|
process.env.GITEA_TOKEN = BOT.token;
|
|
for (const u of [ALICE, BOT]) gitea.addUser(u);
|
|
const tpl = gitea.addRepo("peppa-bot", "template");
|
|
execFileSync("bash", ["-c", `cd ${JSON.stringify(join(ROOT, "template"))} && T=$(mktemp -d) && cp -R . "$T" && cd "$T" && git init -q -b main && git add -A && git -c user.name=t -c user.email=t@t commit -qm tpl && git push -q ${JSON.stringify(tpl.path)} main`]);
|
|
templateUrl = tpl.path;
|
|
alice = await core.upsertUserFromGitea(db, ALICE, { access_token: ALICE.token });
|
|
});
|
|
after(async () => {
|
|
gitea.protectionDown = false;
|
|
await gitea.stop();
|
|
await teardown();
|
|
});
|
|
|
|
const provision = async (over: Partial<Parameters<typeof runner.provisionProject>[1]> & { slug: string; key: string }) =>
|
|
runner.provisionProject(db, {
|
|
gitea: await core.giteaForUser(db, alice),
|
|
owner: ALICE.login,
|
|
ownerIsUser: true,
|
|
name: `Mock ${over.key}`,
|
|
ownerId: alice.id,
|
|
templateUrl,
|
|
...over,
|
|
});
|
|
|
|
const rule = (slug: string) => gitea.repos.get(`${ALICE.login}/${slug}`)!.protections.get("main");
|
|
const protectAlerts = async () => (await db.select().from(telegramOutbox).where(like(telegramOutbox.dedupeKey, "protect:%"))).length;
|
|
|
|
describe("branch protection", () => {
|
|
test("new project gets main protected with peppa-bot", async () => {
|
|
await provision({ slug: "mock-prot-new", key: "MPNEW" });
|
|
const r = rule("mock-prot-new")!;
|
|
assert.deepEqual(r.push_whitelist_usernames, ["peppa-bot"]);
|
|
assert.deepEqual(r.merge_whitelist_usernames, ["peppa-bot"]);
|
|
assert.equal(r.enable_push_whitelist, true);
|
|
assert.equal(r.push_whitelist_deploy_keys, false);
|
|
assert.equal(r.required_approvals, 0);
|
|
});
|
|
|
|
test("a connected existing repository gets it too", async () => {
|
|
const repo = gitea.addRepo(ALICE.login, "mock-prot-existing", { files: { "README.md": "# x\n" } });
|
|
repo.collaborators["peppa-bot"] = "write";
|
|
await provision({ slug: "mock-prot-existing", key: "MPEXI", existing: true });
|
|
assert.deepEqual(rule("mock-prot-existing")!.push_whitelist_usernames, ["peppa-bot"]);
|
|
});
|
|
|
|
test("accept merges into protected main; another user is refused", async () => {
|
|
const p = await provision({ slug: "mock-prot-merge", key: "MPMRG" });
|
|
const repo = gitea.repos.get(`${ALICE.login}/mock-prot-merge`)!;
|
|
const work = join(TMP, "work-merge");
|
|
execFileSync("bash", ["-c", `git clone -q ${JSON.stringify(repo.path)} ${JSON.stringify(work)} && cd ${JSON.stringify(work)} && git checkout -q -b task/x && echo 1 > f.txt && git add -A && git -c user.name=t -c user.email=t@t commit -qm f && git push -q origin task/x`]);
|
|
gitea.openPull(ALICE.login, "mock-prot-merge", "task/x");
|
|
const aliceClient = await core.giteaForUser(db, alice);
|
|
await assert.rejects(aliceClient.mergePullRequest(ALICE.login, "mock-prot-merge", 1), /403/);
|
|
await assert.rejects(aliceClient.writeFile(ALICE.login, "mock-prot-merge", "evil.txt", { text: "x", branch: "main", message: "m" }), /403/);
|
|
await core.giteaForBot().mergePullRequest(ALICE.login, "mock-prot-merge", 1);
|
|
assert.ok(gitea.files(ALICE.login, "mock-prot-merge").includes("f.txt"));
|
|
assert.ok(p);
|
|
});
|
|
|
|
test("a failed setup does not break the project and tells the superadmin once", async () => {
|
|
await superadmin();
|
|
gitea.protectionDown = true;
|
|
const before = await protectAlerts();
|
|
const p = await provision({ slug: "mock-prot-fail", key: "MPFAL" });
|
|
assert.ok(p.id);
|
|
assert.equal(rule("mock-prot-fail"), undefined);
|
|
assert.equal(await protectAlerts(), before + 1);
|
|
assert.equal(await runner.ensureBranchProtection(db, p), false);
|
|
assert.equal(await protectAlerts(), before + 1, "no second message while it stays broken");
|
|
gitea.protectionDown = false;
|
|
assert.equal(await runner.ensureBranchProtection(db, p), true);
|
|
assert.deepEqual(rule("mock-prot-fail")!.push_whitelist_usernames, ["peppa-bot"]);
|
|
});
|
|
|
|
test("the daily check repairs a changed rule; ensure is idempotent", async () => {
|
|
const p = await provision({ slug: "mock-prot-tick", key: "MPTIK" });
|
|
rule("mock-prot-tick")!.push_whitelist_usernames.push("mock-prot-alice");
|
|
assert.equal((await runner.branchProtectionState(db, p))!.protected, false);
|
|
process.env.PEPPA_BRANCH_PROTECT = "on";
|
|
try {
|
|
await runner.branchProtectionTick(db, { now: new Date(Date.now() + 3 * 86400_000) });
|
|
} finally {
|
|
process.env.PEPPA_BRANCH_PROTECT = "off";
|
|
}
|
|
assert.equal((await runner.branchProtectionState(db, p))!.protected, true);
|
|
assert.equal(await runner.ensureBranchProtection(db, p), true);
|
|
});
|
|
|
|
test("saving AGENTS.md works while main is protected", async () => {
|
|
const p = await provision({ slug: "mock-prot-agents", key: "MPAGT" });
|
|
const cur = await runner.readAgentsMd(db, p);
|
|
assert.equal(cur.error, null);
|
|
await runner.saveAgentsMd(db, alice, p, `${cur.text}\n- PEP-191 проверка\n`, cur.sha);
|
|
assert.match(gitea.show(ALICE.login, "mock-prot-agents", "AGENTS.md"), /PEP-191 проверка/);
|
|
});
|
|
});
|