Skip to content

Commit

Permalink
Merge pull request #1871 from intuit/fresh
Browse files Browse the repository at this point in the history
next: handle baseBranch not on remote on fresh repo
  • Loading branch information
hipstersmoothie committed Mar 13, 2021
2 parents b348140 + 1179c9b commit c9d3adf
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 7 deletions.
106 changes: 106 additions & 0 deletions packages/core/src/__tests__/next.test.ts
@@ -0,0 +1,106 @@
import { Auto } from "../auto";
import { dummyLog } from "../utils/logger";
import makeCommitFromMsg from "./make-commit-from-msg";
import execPromise from "../utils/exec-promise";

const exec = jest.fn();
jest.mock("../utils/exec-promise");
// @ts-ignore
execPromise.mockImplementation(exec);
exec.mockResolvedValue("");

jest.mock("../utils/git-reset.ts");
jest.mock("../utils/load-plugins.ts");
jest.mock("env-ci", () => () => ({ isCi: false, branch: "next" }));

const defaults = {
baseBranch: "main",
owner: "foo",
repo: "bar",
};

jest.mock("@octokit/rest", () => {
const Octokit = class MockOctokit {
static plugin = () => Octokit;

authenticate = () => undefined;

search = {
issuesAndPullRequests: () => ({ data: { items: [] } }),
};

repos = {
get: jest.fn().mockReturnValue({}),
};

hook = {
error: () => undefined,
};

issues = {
listLabelsOnIssue: jest.fn().mockReturnValue({ data: [] }),
};

users = {
getAuthenticated: jest.fn().mockResolvedValue({}),
};
};

return { Octokit };
});

// @ts-ignore
jest.mock("gitlog", () => ({
gitlogPromise: () =>
Promise.resolve([
{
rawBody: "foo",
hash: "123",
},
{
rawBody: "foo",
hash: "456",
},
]),
}));

test("falls back to local baseBranch if it doesn't exist on origin", async () => {
const auto = new Auto({ ...defaults, plugins: [] });

// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
auto.logger = dummyLog();
await auto.loadConfig();
auto.remote = "origin";
auto.git!.publish = () => Promise.resolve({ data: {} } as any);
auto.git!.getLastTagNotInBaseBranch = () => Promise.reject(new Error("Test"));
auto.git!.getLatestTagInBranch = () => Promise.reject(new Error("Test"));
auto.git!.getLatestRelease = () => Promise.resolve("abcd");
auto.release!.generateReleaseNotes = () => Promise.resolve("notes");
auto.release!.getCommitsInRelease = () =>
Promise.resolve([
makeCommitFromMsg("Test Commit", { labels: ["skip-release"] }),
]);

jest.spyOn(auto.release!, "getCommits").mockImplementation();
const next = jest.fn();
auto.hooks.next.tap("test", next);

await auto.next({});
expect(exec).toHaveBeenCalledWith("git", [
"rev-list",
"--boundary",
"next...origin/main",
"--left-only",
]);

auto.git!.shaExists = () => Promise.resolve(false);

await auto.next({});
expect(exec).toHaveBeenCalledWith("git", [
"rev-list",
"--boundary",
"next...main",
"--left-only",
]);
});
10 changes: 6 additions & 4 deletions packages/core/src/auto.ts
Expand Up @@ -1362,11 +1362,15 @@ export default class Auto {
);

const currentBranch = getCurrentBranch();
const baseBranch = (await this.git.shaExists(`origin/${this.baseBranch}`))
? `origin/${this.baseBranch}`
: this.baseBranch;

const forkPoints = (
await execPromise("git", [
"rev-list",
"--boundary",
`${currentBranch}...origin/${this.baseBranch}`,
`${currentBranch}...${baseBranch}`,
"--left-only",
])
)
Expand All @@ -1384,9 +1388,7 @@ export default class Auto {
const [, latestTagInBranch] = await on(
this.git.getLatestTagInBranch(currentBranch)
);
const [, tagsInBaseBranch] = await on(
this.git.getTags(`origin/${this.baseBranch}`)
);
const [, tagsInBaseBranch] = await on(this.git.getTags(baseBranch));
const [latestTagInBaseBranch] = (tagsInBaseBranch || []).reverse();
const lastTag =
lastTagNotInBaseBranch ||
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/git.ts
Expand Up @@ -901,9 +901,12 @@ export default class Git {
first?: boolean;
} = {}
) {
const baseTags = (
await this.getTags(`origin/${this.options.baseBranch}`)
).reverse();
const baseBranch = (await this.shaExists(
`origin/${this.options.baseBranch}`
))
? `origin/${this.options.baseBranch}`
: this.options.baseBranch;
const baseTags = (await this.getTags(baseBranch)).reverse();
let branchTags = (await this.getTags(`heads/${branch}`)).reverse();
const branchTagsWithPrereleaseSuffix = branchTags.filter(
(tag) => tag.indexOf(`-${branch.toLowerCase()}`) >= 0
Expand Down

0 comments on commit c9d3adf

Please sign in to comment.