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
23 changes: 23 additions & 0 deletions src/commands/githubActions/bumpUpstream/github/branchNeedsPr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** The two Github methods this needs. */
interface BranchPrSource {
branchExists(branch: string): Promise<boolean>;
getPrsFromBranch(branch: string): Promise<unknown[]>;
}

/**
* 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<boolean> {
if (!(await repo.branchExists(branchName))) return false;
const prs = await repo.getPrsFromBranch(branchName);
return prs.length === 0;
}
2 changes: 2 additions & 0 deletions src/commands/githubActions/bumpUpstream/github/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Original file line number Diff line number Diff line change
@@ -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<void>;
}): Promise<void> {
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();
}
68 changes: 43 additions & 25 deletions src/commands/githubActions/bumpUpstream/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/providers/github/Github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
39 changes: 39 additions & 0 deletions test/commands/gaBumpUpstream/branchNeedsPr.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
78 changes: 78 additions & 0 deletions test/commands/gaBumpUpstream/recoverBumpBranch.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading