From ab0cff415de259112d46d4d403db1e4a97842484 Mon Sep 17 00:00:00 2001 From: Jakub Mucha Date: Tue, 10 Nov 2026 12:33:58 +0000 Subject: [PATCH 1/4] fix: open the PR for a bump branch that has none bump-upstream assumed an existing bump branch already had a PR, so a branch whose PR creation was refused (e.g. GitHub Actions not allowed to create pull requests) made every later run skip that release. --- .../bumpUpstream/github/branchNeedsPr.ts | 23 +++++++++++ .../bumpUpstream/github/index.ts | 1 + .../githubActions/bumpUpstream/index.ts | 13 ++++++- src/providers/github/Github.ts | 18 +++++++++ .../gaBumpUpstream/branchNeedsPr.test.ts | 39 +++++++++++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/commands/githubActions/bumpUpstream/github/branchNeedsPr.ts create mode 100644 test/commands/gaBumpUpstream/branchNeedsPr.test.ts diff --git a/src/commands/githubActions/bumpUpstream/github/branchNeedsPr.ts b/src/commands/githubActions/bumpUpstream/github/branchNeedsPr.ts new file mode 100644 index 00000000..3e0e2509 --- /dev/null +++ b/src/commands/githubActions/bumpUpstream/github/branchNeedsPr.ts @@ -0,0 +1,23 @@ +/** The two Github methods this needs. */ +interface BranchPrSource { + branchExists(branch: string): Promise; + getPrsFromBranch(branch: string): Promise; +} + +/** + * True when a bump branch was pushed but never got a PR, open or closed, for + * example because the token was not allowed to create pull requests. Such a + * branch would otherwise make every later run skip that release. A branch whose + * PR was closed stays skipped: closing it was a decision. + */ +export async function branchNeedsPr({ + repo, + branchName +}: { + repo: BranchPrSource; + branchName: string; +}): Promise { + if (!(await repo.branchExists(branchName))) return false; + const prs = await repo.getPrsFromBranch(branchName); + return prs.length === 0; +} diff --git a/src/commands/githubActions/bumpUpstream/github/index.ts b/src/commands/githubActions/bumpUpstream/github/index.ts index 1cc496fb..357b169b 100644 --- a/src/commands/githubActions/bumpUpstream/github/index.ts +++ b/src/commands/githubActions/bumpUpstream/github/index.ts @@ -1,3 +1,4 @@ +export * from "./branchNeedsPr.js"; export * from "./closeOldPrs.js"; export * from "./getBumpPrBody.js"; export * from "./getGithubSettings.js"; diff --git a/src/commands/githubActions/bumpUpstream/index.ts b/src/commands/githubActions/bumpUpstream/index.ts index 34d2d160..841852e2 100644 --- a/src/commands/githubActions/bumpUpstream/index.ts +++ b/src/commands/githubActions/bumpUpstream/index.ts @@ -15,6 +15,7 @@ import { GitSettings, GithubSettings, UpstreamSettings } from "./types.js"; import { printSettings, getInitialSettings } from "./settings/index.js"; import { ManifestFormat } from "../../../files/manifest/types.js"; import { + branchNeedsPr, closeOldPrs, getBumpPrBody, getGithubSettings, @@ -107,7 +108,17 @@ async function gaBumpUpstreamHandler({ const githubSettings = await getGithubSettings(dir, upstreamSettings); const { branchName, repo } = githubSettings; if (!(await isBranchNew({ branchName, repo, dir }))) { - // We assume the PR was already opened + if (await branchNeedsPr({ repo, branchName })) { + // An earlier run pushed the branch but could not open its PR + console.log(`Branch ${branchName} exists without a PR, opening it`); + await attemptToOpenPR({ + commitMsg: createCommitMessage(upstreamSettings), + upstreamSettings, + githubSettings + }); + return; + } + // The PR was already opened console.log(`Branch ${branchName} already exists`); return; } diff --git a/src/providers/github/Github.ts b/src/providers/github/Github.ts index 12a125e1..3325c597 100644 --- a/src/providers/github/Github.ts +++ b/src/providers/github/Github.ts @@ -443,6 +443,24 @@ export class Github { return openPrsResponse.data; } + /** + * Returns PRs in any state (open or closed) where head branch equals `branch` + * Only branches and PRs originating from the same repo + */ + async getPrsFromBranch(branch: string) { + const prsResponse = await this.octokit.rest.pulls.list({ + owner: this.owner, + repo: this.repo, + state: "all", + headers: { + "X-GitHub-Api-Version": "2022-11-28" + }, + head: `${this.owner}:${branch}` + }); + + return prsResponse.data; + } + /** * Returns all branches; cleanup must never infer deletion from a partial page. * @returns branch = { diff --git a/test/commands/gaBumpUpstream/branchNeedsPr.test.ts b/test/commands/gaBumpUpstream/branchNeedsPr.test.ts new file mode 100644 index 00000000..af6c7581 --- /dev/null +++ b/test/commands/gaBumpUpstream/branchNeedsPr.test.ts @@ -0,0 +1,39 @@ +import { expect } from "chai"; +import { branchNeedsPr } from "../../../src/commands/githubActions/bumpUpstream/github/index.js"; + +const branchName = "dappnodebot/bump-upstream/org/repo@1.2.0"; + +function fakeRepo({ + remoteBranch, + prStates +}: { + remoteBranch: boolean; + prStates: string[]; +}) { + return { + branchExists: async () => remoteBranch, + getPrsFromBranch: async () => prStates.map(state => ({ state })) + }; +} + +describe("command / gaBumpUpstream / branchNeedsPr", () => { + it("is true for a pushed bump branch that never got a PR", async () => { + const repo = fakeRepo({ remoteBranch: true, prStates: [] }); + expect(await branchNeedsPr({ repo, branchName })).to.equal(true); + }); + + it("is false when the branch has an open PR", async () => { + const repo = fakeRepo({ remoteBranch: true, prStates: ["open"] }); + expect(await branchNeedsPr({ repo, branchName })).to.equal(false); + }); + + it("is false when the branch's PR was closed", async () => { + const repo = fakeRepo({ remoteBranch: true, prStates: ["closed"] }); + expect(await branchNeedsPr({ repo, branchName })).to.equal(false); + }); + + it("is false when the branch exists only locally", async () => { + const repo = fakeRepo({ remoteBranch: false, prStates: [] }); + expect(await branchNeedsPr({ repo, branchName })).to.equal(false); + }); +}); From b21997fe37755fa2953e6e1e768c0be6e7af3609 Mon Sep 17 00:00:00 2001 From: Jakub Mucha Date: Wed, 11 Nov 2026 10:59:41 +0000 Subject: [PATCH 2/4] fix: build a recovered bump branch like a new one Recovery now checks the branch out, honours SKIP_COMMIT, and continues to closeOldPrs and the build, upload and comment steps instead of returning after opening the PR. --- .../bumpUpstream/github/index.ts | 1 + .../bumpUpstream/github/recoverBumpBranch.ts | 43 +++++++++++ .../githubActions/bumpUpstream/index.ts | 73 ++++++++++--------- .../gaBumpUpstream/recoverBumpBranch.test.ts | 73 +++++++++++++++++++ 4 files changed, 157 insertions(+), 33 deletions(-) create mode 100644 src/commands/githubActions/bumpUpstream/github/recoverBumpBranch.ts create mode 100644 test/commands/gaBumpUpstream/recoverBumpBranch.test.ts diff --git a/src/commands/githubActions/bumpUpstream/github/index.ts b/src/commands/githubActions/bumpUpstream/github/index.ts index 357b169b..4abb7447 100644 --- a/src/commands/githubActions/bumpUpstream/github/index.ts +++ b/src/commands/githubActions/bumpUpstream/github/index.ts @@ -5,3 +5,4 @@ export * from "./getGithubSettings.js"; export * from "./isBranchNew.js"; export * from "./isValidRelease.js"; export * from "./pickLatestRelease.js"; +export * from "./recoverBumpBranch.js"; diff --git a/src/commands/githubActions/bumpUpstream/github/recoverBumpBranch.ts b/src/commands/githubActions/bumpUpstream/github/recoverBumpBranch.ts new file mode 100644 index 00000000..e230fee6 --- /dev/null +++ b/src/commands/githubActions/bumpUpstream/github/recoverBumpBranch.ts @@ -0,0 +1,43 @@ +import { shell } from "../../../../utils/shell.js"; + +/** + * Recovers a bump branch an earlier run pushed but could not open a PR for: + * checks it out from origin, so the build that follows uses its content, then + * opens its PR. With SKIP_COMMIT set it only checks the branch out, as a + * debugging run makes no remote changes. + */ +export async function recoverBumpBranch({ + dir, + branchName, + skipCommit, + openPr +}: { + dir: string; + branchName: string; + skipCommit: boolean; + openPr: () => Promise; +}): Promise { + await shell(["git", "check-ref-format", "--branch", branchName], { + cwd: dir + }); + await shell( + [ + "git", + "fetch", + "origin", + `+refs/heads/${branchName}:refs/remotes/origin/${branchName}` + ], + { cwd: dir } + ); + await shell( + ["git", "checkout", "-B", branchName, `refs/remotes/origin/${branchName}`], + { cwd: dir } + ); + + if (skipCommit) { + console.log("SKIP_COMMIT=true"); + return; + } + + await openPr(); +} diff --git a/src/commands/githubActions/bumpUpstream/index.ts b/src/commands/githubActions/bumpUpstream/index.ts index 841852e2..18c02dd5 100644 --- a/src/commands/githubActions/bumpUpstream/index.ts +++ b/src/commands/githubActions/bumpUpstream/index.ts @@ -19,7 +19,8 @@ import { closeOldPrs, getBumpPrBody, getGithubSettings, - isBranchNew + isBranchNew, + recoverBumpBranch } from "./github/index.js"; import path from "path"; import { getAllVariantsInPath } from "../../../files/variants/getAllPackageVariants.js"; @@ -108,43 +109,49 @@ async function gaBumpUpstreamHandler({ const githubSettings = await getGithubSettings(dir, upstreamSettings); const { branchName, repo } = githubSettings; if (!(await isBranchNew({ branchName, repo, dir }))) { - if (await branchNeedsPr({ repo, branchName })) { - // An earlier run pushed the branch but could not open its PR - console.log(`Branch ${branchName} exists without a PR, opening it`); - await attemptToOpenPR({ - commitMsg: createCommitMessage(upstreamSettings), - upstreamSettings, - githubSettings - }); + if (!(await branchNeedsPr({ repo, branchName }))) { + // The PR was already opened + console.log(`Branch ${branchName} already exists`); return; } - // The PR was already opened - console.log(`Branch ${branchName} already exists`); - return; - } - - updateComposeUpstreamVersions(dir, compose, upstreamSettings); + // An earlier run pushed the branch but could not open its PR. Recover it, + // then close old PRs and build from it like a freshly pushed branch. + console.log(`Branch ${branchName} exists without a PR, opening it`); + await recoverBumpBranch({ + dir, + branchName, + skipCommit: Boolean(process.env.SKIP_COMMIT), + openPr: () => + attemptToOpenPR({ + commitMsg: createCommitMessage(upstreamSettings), + upstreamSettings, + githubSettings + }) + }); + } else { + updateComposeUpstreamVersions(dir, compose, upstreamSettings); - updateManifestUpstreamVersion({ - manifest, - manifestFormat, - upstreamSettings, - dir - }); + updateManifestUpstreamVersion({ + manifest, + manifestFormat, + upstreamSettings, + dir + }); - await updateManifestPkgVersion({ - dir, - ethProvider, - allVariants: useVariants, - variantsDir - }); + await updateManifestPkgVersion({ + dir, + ethProvider, + allVariants: useVariants, + variantsDir + }); - await prepareAndCommitChanges({ - dir, - gitSettings, - upstreamSettings, - githubSettings - }); + await prepareAndCommitChanges({ + dir, + gitSettings, + upstreamSettings, + githubSettings + }); + } try { await closeOldPrs(repo, branchName); diff --git a/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts b/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts new file mode 100644 index 00000000..2a67f5b3 --- /dev/null +++ b/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts @@ -0,0 +1,73 @@ +import { expect } from "chai"; +import { execFileSync } from "child_process"; +import fs from "fs"; +import os from "os"; +import path from "path"; +import { recoverBumpBranch } from "../../../src/commands/githubActions/bumpUpstream/github/index.js"; + +const branchName = "dappnodebot/bump-upstream/org/repo@1.2.0"; + +function git(cwd: string, ...args: string[]): string { + return execFileSync("git", args, { cwd, encoding: "utf8" }).trim(); +} + +/** + * A bare "origin" holding the default branch and a pushed bump branch, and a + * fresh clone of it on the default branch, as a CI checkout would be. + */ +function setupRepos(): { clone: string; bumpCommit: string } { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "recover-bump-")); + const origin = path.join(root, "origin.git"); + const work = path.join(root, "work"); + const clone = path.join(root, "clone"); + git(root, "init", "-q", "--bare", "-b", "main", origin); + git(root, "clone", "-q", origin, work); + git(work, "config", "user.email", "test@example.com"); + git(work, "config", "user.name", "test"); + fs.writeFileSync(path.join(work, "dappnode_package.json"), '{"version":"0.1.0"}\n'); + git(work, "add", "."); + git(work, "commit", "-q", "-m", "init"); + git(work, "push", "-q", "origin", "main"); + git(work, "checkout", "-q", "-b", branchName); + fs.writeFileSync(path.join(work, "dappnode_package.json"), '{"version":"0.1.1"}\n'); + git(work, "commit", "-q", "-am", "bump org/repo to 1.2.0"); + git(work, "push", "-q", "origin", branchName); + const bumpCommit = git(work, "rev-parse", "HEAD"); + git(root, "clone", "-q", "-b", "main", origin, clone); + return { clone, bumpCommit }; +} + +describe("command / gaBumpUpstream / recoverBumpBranch", function () { + this.timeout(30 * 1000); + + it("checks out the pushed branch and opens its PR", async () => { + const { clone, bumpCommit } = setupRepos(); + let opened = 0; + await recoverBumpBranch({ + dir: clone, + branchName, + skipCommit: false, + openPr: async () => { + opened++; + } + }); + expect(git(clone, "rev-parse", "HEAD")).to.equal(bumpCommit); + expect(git(clone, "rev-parse", "--abbrev-ref", "HEAD")).to.equal(branchName); + expect(opened).to.equal(1); + }); + + it("checks out the branch but opens no PR when SKIP_COMMIT is set", async () => { + const { clone, bumpCommit } = setupRepos(); + let opened = 0; + await recoverBumpBranch({ + dir: clone, + branchName, + skipCommit: true, + openPr: async () => { + opened++; + } + }); + expect(git(clone, "rev-parse", "HEAD")).to.equal(bumpCommit); + expect(opened).to.equal(0); + }); +}); From 58255dad63f9d3f5a910fcd5eaf2c23f173a0119 Mon Sep 17 00:00:00 2001 From: Jakub Mucha Date: Wed, 11 Nov 2026 11:44:05 +0000 Subject: [PATCH 3/4] fix: say recovering in the orphan branch log --- src/commands/githubActions/bumpUpstream/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/githubActions/bumpUpstream/index.ts b/src/commands/githubActions/bumpUpstream/index.ts index 18c02dd5..b0cc6ed0 100644 --- a/src/commands/githubActions/bumpUpstream/index.ts +++ b/src/commands/githubActions/bumpUpstream/index.ts @@ -116,7 +116,7 @@ async function gaBumpUpstreamHandler({ } // An earlier run pushed the branch but could not open its PR. Recover it, // then close old PRs and build from it like a freshly pushed branch. - console.log(`Branch ${branchName} exists without a PR, opening it`); + console.log(`Branch ${branchName} exists without a PR, recovering it`); await recoverBumpBranch({ dir, branchName, From 65d1818aee91f15cce352f582984c383fd1d2fd3 Mon Sep 17 00:00:00 2001 From: Jakub Mucha Date: Wed, 11 Nov 2026 11:44:05 +0000 Subject: [PATCH 4/4] test: remove recover bump branch temp dirs --- test/commands/gaBumpUpstream/recoverBumpBranch.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts b/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts index 2a67f5b3..844921ca 100644 --- a/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts +++ b/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts @@ -7,6 +7,9 @@ import { recoverBumpBranch } from "../../../src/commands/githubActions/bumpUpstr const branchName = "dappnodebot/bump-upstream/org/repo@1.2.0"; +/** Temp dir of the test in progress, removed after it. */ +let root: string; + function git(cwd: string, ...args: string[]): string { return execFileSync("git", args, { cwd, encoding: "utf8" }).trim(); } @@ -16,7 +19,7 @@ function git(cwd: string, ...args: string[]): string { * fresh clone of it on the default branch, as a CI checkout would be. */ function setupRepos(): { clone: string; bumpCommit: string } { - const root = fs.mkdtempSync(path.join(os.tmpdir(), "recover-bump-")); + root = fs.mkdtempSync(path.join(os.tmpdir(), "recover-bump-")); const origin = path.join(root, "origin.git"); const work = path.join(root, "work"); const clone = path.join(root, "clone"); @@ -40,6 +43,8 @@ function setupRepos(): { clone: string; bumpCommit: string } { describe("command / gaBumpUpstream / recoverBumpBranch", function () { this.timeout(30 * 1000); + afterEach(() => fs.rmSync(root, { recursive: true, force: true })); + it("checks out the pushed branch and opens its PR", async () => { const { clone, bumpCommit } = setupRepos(); let opened = 0;