Skip to content

Commit 79eaa70

Browse files
Copilotgh-aw-bot
andauthored
Scope PR history checks to agent ancestry
Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>
1 parent 82dbe26 commit 79eaa70

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎actions/setup/js/safe_outputs_handlers.cjs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,7 @@ function createHandlers(server, appendSafeOutput, config = {}) {
14841484
}
14851485

14861486
const prHeadBaseline = resolvePRHeadBaselineForPush(entry.branch, itemRepo, effectivePushPRNumber, server);
1487+
const incrementalBaseRef = prHeadBaseline?.sha || prHeadBaseline?.ref || `refs/remotes/origin/${entry.branch}`;
14871488
if (prHeadBaseline?.headRepo && prHeadBaseline.headRepo.toLowerCase() !== itemRepo.toLowerCase() && prHeadBaseline.headRepo.toLowerCase() !== configuredHeadRepo.toLowerCase()) {
14881489
return {
14891490
content: [
@@ -1556,10 +1557,9 @@ function createHandlers(server, appendSafeOutput, config = {}) {
15561557
// only local refs (no extra fetch); a detection miss simply preserves the
15571558
// existing behavior.
15581559
if (!useBundle && !patchFormatExplicit && entry.branch) {
1559-
const rangeBaseRef = prHeadBaseline?.sha || prHeadBaseline?.ref || `refs/remotes/origin/${entry.branch}`;
1560-
const hasMerges = hasMergeCommitsInRange(rangeBaseRef, entry.branch, { cwd: repoCwd || undefined });
1560+
const hasMerges = hasMergeCommitsInRange(incrementalBaseRef, entry.branch, { cwd: repoCwd || undefined });
15611561
if (hasMerges) {
1562-
server.debug(`push_to_pull_request_branch: detected merge commit(s) in incremental range ${rangeBaseRef}..${entry.branch}; auto-switching to bundle transport (set patch-format: am to override).`);
1562+
server.debug(`push_to_pull_request_branch: detected merge commit(s) in incremental range ${incrementalBaseRef}..${entry.branch}; auto-switching to bundle transport (set patch-format: am to override).`);
15631563
useBundle = true;
15641564
}
15651565
}
@@ -1604,8 +1604,7 @@ function createHandlers(server, appendSafeOutput, config = {}) {
16041604
if (!pushPinnedSha) {
16051605
server.debug("Incremental allowed-files check skipped: branch SHA not pinned (non-bundle path)");
16061606
} else {
1607-
const incrementalBase = prHeadBaseline?.sha || prHeadBaseline?.ref || `refs/remotes/origin/${entry.branch}`;
1608-
const agentFiles = execGitSync(["log", "--name-only", "--pretty=format:", `${incrementalBase}..${pushPinnedSha}`, "--"], { cwd: pushGitCwd })
1607+
const agentFiles = execGitSync(["log", "--ancestry-path", "--name-only", "--pretty=format:", `${incrementalBaseRef}..${pushPinnedSha}`, "--"], { cwd: pushGitCwd })
16091608
.toString()
16101609
.split("\n")
16111610
.map(s => s.trim())

‎actions/setup/js/safe_outputs_handlers.test.cjs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,8 @@ describe("safe_outputs_handlers", () => {
26192619
fs.writeFileSync(path.join(repoDir, "agent.js"), "console.log('agent');\n");
26202620
execSync("git add agent.js", { cwd: repoDir, stdio: "pipe" });
26212621
execSync("git commit -m 'agent script'", { cwd: repoDir, stdio: "pipe" });
2622+
execSync("git rm agent.js", { cwd: repoDir, stdio: "pipe" });
2623+
execSync("git commit -m 'revert agent script'", { cwd: repoDir, stdio: "pipe" });
26222624

26232625
process.env.GITHUB_BASE_REF = "main";
26242626
process.env.GITHUB_WORKSPACE = repoDir;
@@ -2637,6 +2639,33 @@ describe("safe_outputs_handlers", () => {
26372639
}
26382640
});
26392641

2642+
it("does not reject disallowed files merged from the updated base branch", async () => {
2643+
const { repoDir } = createRepoWithDisallowedHistoryFile();
2644+
execSync("git checkout main", { cwd: repoDir, stdio: "pipe" });
2645+
fs.writeFileSync(path.join(repoDir, "upstream.js"), "console.log('upstream');\n");
2646+
execSync("git add upstream.js", { cwd: repoDir, stdio: "pipe" });
2647+
execSync("git commit -m 'update base branch'", { cwd: repoDir, stdio: "pipe" });
2648+
execSync("git checkout feature/work", { cwd: repoDir, stdio: "pipe" });
2649+
execSync("git merge --no-ff main -m 'merge updated base'", { cwd: repoDir, stdio: "pipe" });
2650+
2651+
process.env.GITHUB_BASE_REF = "main";
2652+
process.env.GITHUB_WORKSPACE = repoDir;
2653+
const localHandlers = createHandlers(mockServer, mockAppendSafeOutput, {
2654+
push_to_pull_request_branch: { allowed_files: ["*.md"] },
2655+
});
2656+
try {
2657+
const result = await localHandlers.pushToPullRequestBranchHandler({ branch: "feature/work" });
2658+
if (result.isError) {
2659+
const data = JSON.parse(result.content[0].text);
2660+
expect(data.error).not.toContain("allowed-files configuration");
2661+
expect(data.disallowed_files).toBeUndefined();
2662+
}
2663+
} finally {
2664+
delete process.env.GITHUB_BASE_REF;
2665+
process.env.GITHUB_WORKSPACE = testWorkspaceDir;
2666+
}
2667+
});
2668+
26402669
it("does not block when agent files are within allowed_files", async () => {
26412670
const repoDir = path.join(testWorkspaceDir, "allowed-files-ok-repo");
26422671
fs.mkdirSync(repoDir, { recursive: true });

‎actions/setup/js/work_queue_compaction.test.cjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { createHash } = require("node:crypto");
55
const fs = require("node:fs");
66
const os = require("node:os");
77
const path = require("node:path");
8-
const { test } = require("node:test");
8+
import { test } from "vitest";
99
const { canonical } = require("./work_queue_codec.cjs");
1010
const { main: planCompaction, planFor } = require("./work_queue_compaction_plan.cjs");
1111
const { isRetryablePublicationError, main: apply, PLAN_MAX_BYTES, readPlan } = require("./work_queue_compaction_apply.cjs");

0 commit comments

Comments
 (0)