Migrate review automation to OpenCode

This commit is contained in:
lezi-fun committed 2026-08-13 10:55:00 +08:00
1 parent 27db22cd78
commit 8aaecb4dc7
21 files changed
+2703 -713

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import {
containsBotMention,
createRepositorySnapshot,
isTrustedChatPermission
} from "../src/chat/processor.js";
test("PR chat recognizes @bot and the configured bot login", () => {
assert.equal(containsBotMention("@bot can this merge?", "github-actions[bot]"), true);
assert.equal(containsBotMention("Could @github-actions explain this?", "github-actions[bot]"), true);
assert.equal(containsBotMention("Ping @github-actions[bot]", "github-actions[bot]"), true);
});
test("PR chat does not trigger on partial usernames or ordinary text", () => {
assert.equal(containsBotMention("@botany please check", "github-actions[bot]"), false);
assert.equal(containsBotMention("This mentions bot without an at sign", "github-actions[bot]"), false);
});
test("only collaborators with write access can invoke the full repository agent", () => {
assert.equal(isTrustedChatPermission("admin"), true);
assert.equal(isTrustedChatPermission("maintain"), true);
assert.equal(isTrustedChatPermission("write"), true);
assert.equal(isTrustedChatPermission("triage"), false);
assert.equal(isTrustedChatPermission("read"), false);
assert.equal(isTrustedChatPermission(null), false);
});
test("PR chat snapshot excludes repository instructions, secrets, git data, and symlinks", async () => {
const source = await fs.mkdtemp(path.join(os.tmpdir(), "ghbot-chat-source-"));
let snapshot: string | undefined;
try {
await fs.mkdir(path.join(source, ".git"));
await fs.mkdir(path.join(source, ".opencode"));
await fs.mkdir(path.join(source, ".agents"));
await fs.mkdir(path.join(source, "src"));
await Promise.all([
fs.writeFile(path.join(source, ".git", "config"), "credential data"),
fs.writeFile(path.join(source, ".opencode", "plugin.ts"), "untrusted plugin"),
fs.writeFile(path.join(source, ".agents", "SKILL.md"), "untrusted skill"),
fs.writeFile(path.join(source, ".env"), "TOKEN=secret"),
fs.writeFile(path.join(source, ".env.local"), "TOKEN=local-secret"),
fs.writeFile(path.join(source, "AGENTS.md"), "untrusted instructions"),
fs.writeFile(path.join(source, "opencode.json"), "{}"),
fs.writeFile(path.join(source, "src", "index.ts"), "export const safe = true;\n"),
fs.symlink(path.join(source, ".env"), path.join(source, "secret-link"))
]);
snapshot = await createRepositorySnapshot(source);
assert.equal(await fs.readFile(path.join(snapshot, "src", "index.ts"), "utf8"), "export const safe = true;\n");
for (const excluded of [
".git",
".opencode",
".agents",
".env",
".env.local",
"AGENTS.md",
"opencode.json",
"secret-link"
]) {
await assert.rejects(fs.lstat(path.join(snapshot, excluded)), { code: "ENOENT" });
}
} finally {
await fs.rm(source, { recursive: true, force: true });
if (snapshot) {
await fs.rm(snapshot, { recursive: true, force: true });
}
}
});
+9
View File
@@ -0,0 +1,9 @@
import assert from "node:assert/strict";
import test from "node:test";
import { reviewCacheKeyPrefix } from "../src/review/cache.js";
test("review cache keys are isolated by repository and pull request", () => {
assert.equal(reviewCacheKeyPrefix("12345", 17), "ghbot-review-12345-pr-17-");
assert.notEqual(reviewCacheKeyPrefix("12345", 17), reviewCacheKeyPrefix("12345", 18));
assert.notEqual(reviewCacheKeyPrefix("12345", 17), reviewCacheKeyPrefix("67890", 17));
});
+127
View File
@@ -0,0 +1,127 @@
import assert from "node:assert/strict";
import test from "node:test";
import type { ReviewDecision } from "../src/types.js";
import {
approvedLoginsForHead,
branchPatternToRegExp,
evaluateReviewDecision,
formatReviewExternalId,
formatReviewStateMarker,
parseReviewExternalId,
parseReviewStateMarker
} from "../src/review/policy.js";
import { config } from "../src/config.js";
const cleanDecision: ReviewDecision = {
review: [],
change: [],
comment: "Clean",
result: {
canMerge: true,
summary: "Safe",
shouldClosePullRequest: false,
closeReason: ""
}
};
test("clean reviews approve under the default policy", () => {
assert.equal(config.reviewPolicy, "require_approval");
assert.deepEqual(evaluateReviewDecision(cleanDecision), {
event: "APPROVE",
blocksMerge: false,
requiresAdminApproval: false,
outcome: "pass"
});
});
test("ordinary review notes require administrator approval by default", () => {
const decision = {
...cleanDecision,
review: [{ path: "src/a.ts", line: 1, title: "Check this", body: "Concrete note" }]
};
assert.deepEqual(evaluateReviewDecision(decision), {
event: "COMMENT",
blocksMerge: false,
requiresAdminApproval: true,
outcome: "pass"
});
});
test("allow policy approves ordinary review notes", () => {
const decision = {
...cleanDecision,
review: [{ path: "src/a.ts", line: 1, title: "Check this", body: "Concrete note" }]
};
assert.deepEqual(evaluateReviewDecision(decision, "allow"), {
event: "APPROVE",
blocksMerge: false,
requiresAdminApproval: false,
outcome: "pass"
});
});
test("reject policy blocks ordinary review notes", () => {
const decision = {
...cleanDecision,
review: [{ path: "src/a.ts", line: 1, title: "Check this", body: "Concrete note" }]
};
assert.deepEqual(evaluateReviewDecision(decision, "reject"), {
event: "REQUEST_CHANGES",
blocksMerge: true,
requiresAdminApproval: false,
outcome: "block"
});
});
test("required changes always block", () => {
const decision = {
...cleanDecision,
result: { ...cleanDecision.result, canMerge: false },
change: [{ path: "src/a.ts", line: 1, title: "Broken", body: "Concrete failure" }]
};
assert.deepEqual(evaluateReviewDecision(decision), {
event: "REQUEST_CHANGES",
blocksMerge: true,
requiresAdminApproval: false,
outcome: "block"
});
});
test("review state markers round trip", () => {
const disposition = evaluateReviewDecision({
...cleanDecision,
review: [{ path: "src/a.ts", line: 1, title: "Check", body: "Note" }]
});
assert.deepEqual(
parseReviewStateMarker(formatReviewStateMarker("strict", disposition, 1, 0)),
{ mode: "strict", outcome: "pass", requiresAdminApproval: true }
);
assert.deepEqual(
parseReviewExternalId(formatReviewExternalId("lenient", disposition)),
{ mode: "lenient", outcome: "pass", requiresAdminApproval: true }
);
});
test("branch glob stars respect slash boundaries", () => {
assert.equal(branchPatternToRegExp("main").test("main"), true);
assert.equal(branchPatternToRegExp("release/*").test("release/1.0"), true);
assert.equal(branchPatternToRegExp("release/*").test("release/mobile/1.0"), false);
assert.equal(branchPatternToRegExp("release/**").test("release/mobile/1.0"), true);
assert.equal(branchPatternToRegExp("release/?.x").test("release/1.x"), true);
});
test("only the latest decisive review per user can approve the current head", () => {
assert.deepEqual(
approvedLoginsForHead(
[
{ id: 1, state: "APPROVED", commitId: "head", login: "alice", submittedAt: "2026-08-13T01:00:00Z" },
{ id: 2, state: "COMMENTED", commitId: "head", login: "alice", submittedAt: "2026-08-13T02:00:00Z" },
{ id: 3, state: "APPROVED", commitId: "old-head", login: "bob", submittedAt: "2026-08-13T03:00:00Z" },
{ id: 4, state: "APPROVED", commitId: "head", login: "carol", submittedAt: "2026-08-13T04:00:00Z" },
{ id: 5, state: "CHANGES_REQUESTED", commitId: "head", login: "carol", submittedAt: "2026-08-13T05:00:00Z" }
],
"head"
),
["alice"]
);
});