Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next: handle baseBranch not on remote on fresh repo #1871

Merged
merged 1 commit into from Mar 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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