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
16 changes: 11 additions & 5 deletions .github/scripts/run-agent-task/runner-workspace-publisher.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export async function publishRunnerWorkspace({ request, changedFiles, publicatio
let pull = null
let parent = baseSha
let reusedPull = false
let replacedSha = ""
if (existing) {
const pulls = await openPulls(head)
pull = pulls.find((candidate) => isExactPull(candidate, head)) || null
Expand All @@ -89,10 +90,15 @@ export async function publishRunnerWorkspace({ request, changedFiles, publicatio
if (pull) {
const existingSha = string(existing?.object?.sha)
const comparison = await api("GET", `/compare/${baseSha}...${existingSha}`)
if (!existingSha || !["ahead", "identical"].includes(string(comparison.status)) || string(comparison.merge_base_commit?.sha) !== baseSha) {
throw new Error(`Existing publication branch ${head} has an open pull request but does not descend from current base ${base}@${baseSha}; rebase the pull request before publishing again.`)
if (!existingSha) throw new Error(`Existing publication branch ${head} did not resolve to a commit SHA.`)
if (["ahead", "identical"].includes(string(comparison.status)) && string(comparison.merge_base_commit?.sha) === baseSha) {
parent = existingSha
} else {
// The publication branch is runner-owned and the workspace was seeded
// from the current base, so a stale branch is replaced by a commit on
// the current base (force-updated ref) while the open PR is kept.
replacedSha = existingSha
}
parent = existingSha
reusedPull = true
}
}
Expand All @@ -110,9 +116,9 @@ export async function publishRunnerWorkspace({ request, changedFiles, publicatio
}
const nextTree = await api("POST", "/git/trees", { base_tree: string(parentCommit.tree?.sha), tree })
const commit = await api("POST", "/git/commits", { message: string(config.commit_message || request.workload?.label || "Apply agent task changes"), tree: string(nextTree.sha), parents: [parent] })
if (pull) await api("PATCH", `/git/refs/heads/${head.split("/").map(encodeURIComponent).join("/")}`, { sha: string(commit.sha), force: false })
if (pull) await api("PATCH", `/git/refs/heads/${head.split("/").map(encodeURIComponent).join("/")}`, { sha: string(commit.sha), force: Boolean(replacedSha) })
else await api("POST", "/git/refs", { ref: `refs/heads/${head}`, sha: string(commit.sha) })
pull ||= await api("POST", "/pulls", { title: string(config.title || request.workload?.label || "Apply agent task changes"), head, base, body: publicationPullBody(config, captured) })
if (!isExactPull(pull, head) || !/^https:\/\/github\.com\//.test(string(pull.html_url))) throw new Error("GitHub publication response did not match the requested repository and branches.")
return { schema: "wp-codebox/runner-workspace-publication-result/v1", success: true, status: "published", backend: "github-rest", branch: { base, base_sha: baseSha, head, name: head }, commit: { sha: string(commit.sha), parent_sha: parent }, pull_request: { number: pull.number, url: pull.html_url, reused: reusedPull, opened: !reusedPull } }
return { schema: "wp-codebox/runner-workspace-publication-result/v1", success: true, status: "published", backend: "github-rest", branch: { base, base_sha: baseSha, head, name: head }, commit: { sha: string(commit.sha), parent_sha: parent, ...(replacedSha ? { replaced_sha: replacedSha } : {}) }, pull_request: { number: pull.number, url: pull.html_url, reused: reusedPull, opened: !reusedPull } }
}
21 changes: 19 additions & 2 deletions tests/runner-workspace-publisher.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ for (const defaultBranch of [undefined, "invalid branch"]) {
assert.deepEqual(treeRequest?.[2]?.tree, [{ path: "README.md", mode: "100644", type: "blob", sha: "blob" }], "the prior branch tree remains the base while only approved changed files are replaced")
const commitRequest = mock.calls.find(([url, method]) => new URL(url).pathname.endsWith("/git/commits") && method === "POST")
assert.deepEqual(commitRequest?.[2]?.parents, ["old"], "existing branch commits must use the existing branch head as their parent")
assert.equal(mock.calls.find(([, method]) => method === "PATCH")?.[2]?.force, false, "descending branches are fast-forwarded, never forced")
assert.equal(result.commit.replaced_sha, undefined)
}
{
const mock = fetchMock(true, "main", undefined, false)
Expand All @@ -93,9 +95,24 @@ for (const defaultBranch of [undefined, "invalid branch"]) {
assert.equal(result.branch.head, "wp-codebox/agent-task/run-1-base", "a PR for a different base must not be reused")
}
{
// wp-codebox#2638: a runner-owned branch with an open PR that no longer
// descends from base is replaced by a commit on the current base.
const mock = fetchMock(true, "main", undefined, true, "diverged")
await assert.rejects(() => publishRunnerWorkspace({ request, changedFiles: ["README.md"], publicationFiles, token: "secret", fetchImpl: mock.fetch }), /does not descend from current base/)
assert(!mock.calls.some(([, method]) => method === "POST" || method === "PATCH"), "an unsafe open PR branch must fail before publication writes")
const result = await publishRunnerWorkspace({ request, changedFiles: ["README.md"], publicationFiles, token: "secret", fetchImpl: mock.fetch })
assert.equal(result.branch.head, "wp-codebox/agent-task/run-1", "a stale open-PR branch keeps its name")
assert.equal(result.pull_request.number, 4, "the open PR is kept")
assert.equal(result.pull_request.reused, true)
assert.equal(result.pull_request.opened, false)
assert.equal(result.commit.parent_sha, "base")
assert.equal(result.commit.replaced_sha, "old", "the superseded branch head is reported")
const treeRequest = mock.calls.find(([url, method]) => new URL(url).pathname.endsWith("/git/trees") && method === "POST")
assert.equal(treeRequest?.[2]?.base_tree, "tree", "replacement must build on the current base tree")
const commitRequest = mock.calls.find(([url, method]) => new URL(url).pathname.endsWith("/git/commits") && method === "POST")
assert.deepEqual(commitRequest?.[2]?.parents, ["base"], "replacement commit must be parented on the current base")
const refUpdate = mock.calls.find(([, method]) => method === "PATCH")
assert.deepEqual(refUpdate?.[2], { sha: "commit", force: true }, "the branch ref is force-updated to the replacement commit")
assert(!mock.calls.some(([url, method]) => method === "POST" && new URL(url).pathname.endsWith("/pulls")), "no new PR is opened")
assert(!mock.calls.some(([url, method]) => method === "POST" && new URL(url).pathname.endsWith("/git/refs")), "no new ref is created")
}
{
const mock = fetchMock(true, "main", undefined, false, "ahead", "main", true)
Expand Down
Loading