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..4abb7447 100644 --- a/src/commands/githubActions/bumpUpstream/github/index.ts +++ b/src/commands/githubActions/bumpUpstream/github/index.ts @@ -1,6 +1,8 @@ +export * from "./branchNeedsPr.js"; export * from "./closeOldPrs.js"; export * from "./getBumpPrBody.js"; 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 34d2d160..b0cc6ed0 100644 --- a/src/commands/githubActions/bumpUpstream/index.ts +++ b/src/commands/githubActions/bumpUpstream/index.ts @@ -15,10 +15,12 @@ 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, - isBranchNew + isBranchNew, + recoverBumpBranch } from "./github/index.js"; import path from "path"; import { getAllVariantsInPath } from "../../../files/variants/getAllPackageVariants.js"; @@ -107,33 +109,49 @@ 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 - console.log(`Branch ${branchName} already exists`); - return; - } - - updateComposeUpstreamVersions(dir, compose, upstreamSettings); + if (!(await branchNeedsPr({ repo, branchName }))) { + // The PR was already opened + console.log(`Branch ${branchName} already exists`); + return; + } + // 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, recovering 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/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); + }); +}); diff --git a/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts b/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts new file mode 100644 index 00000000..844921ca --- /dev/null +++ b/test/commands/gaBumpUpstream/recoverBumpBranch.test.ts @@ -0,0 +1,78 @@ +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"; + +/** 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(); +} + +/** + * 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 } { + 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); + + 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; + 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); + }); +});