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

add canary target flag/option #1893

Merged
merged 1 commit into from Mar 19, 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
9 changes: 9 additions & 0 deletions packages/cli/src/parse-args.ts
Expand Up @@ -566,6 +566,15 @@ export const commands: AutoCommand[] = [
description:
"Build number to use to create the canary version. Detected in CI env",
},
{
name: "target",
type: String,
group: "main",
defaultValue: "pr-body",
description: "How the canary version should be attached to a PR",
typeLabel: "pr-body | comment | status",
config: true,
},
{
...message,
description:
Expand Down
50 changes: 49 additions & 1 deletion packages/core/src/__tests__/auto-in-pr-ci.test.ts
Expand Up @@ -37,6 +37,8 @@ jest.mock("@octokit/rest", () => {
};

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

Expand Down Expand Up @@ -73,7 +75,7 @@ describe("canary in ci", () => {
);
});

test("comments on PR in CI", async () => {
test("add to pr-body on PR in CI", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
Expand All @@ -93,6 +95,52 @@ describe("canary in ci", () => {
expect(version!.newVersion).toBe("1.2.4-canary.123.1");
});

test("adds comment on PR in CI", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);

auto.logger = dummyLog();
await auto.loadConfig();
auto.git!.getLatestRelease = () => Promise.resolve("1.2.3");
auto.release!.getCommitsInRelease = () =>
Promise.resolve([makeCommitFromMsg("Test Commit")]);
const addToPrBody = jest.fn();
auto.git!.addToPrBody = addToPrBody;
const comment = jest.fn();
auto.comment = comment;
jest.spyOn(auto.release!, "getCommits").mockImplementation();
auto.hooks.canary.tap("test", () => "1.2.4-canary.123.1");

const version = await auto.canary({ pr: 123, build: 1, target: "comment" });
expect(addToPrBody).not.toHaveBeenCalled();
expect(comment).toHaveBeenCalled();
expect(version!.newVersion).toBe("1.2.4-canary.123.1");
});

test("adds status on PR in CI", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);

auto.logger = dummyLog();
await auto.loadConfig();
auto.git!.getLatestRelease = () => Promise.resolve("1.2.3");
auto.release!.getCommitsInRelease = () =>
Promise.resolve([makeCommitFromMsg("Test Commit")]);
const addToPrBody = jest.fn();
auto.git!.addToPrBody = addToPrBody;
const prStatus = jest.fn();
auto.prStatus = prStatus;
jest.spyOn(auto.release!, "getCommits").mockImplementation();
auto.hooks.canary.tap("test", () => "1.2.4-canary.123.1");

const version = await auto.canary({ pr: 123, build: 1, target: "status" });
expect(addToPrBody).not.toHaveBeenCalled();
expect(prStatus).toHaveBeenCalled();
expect(version!.newVersion).toBe("1.2.4-canary.123.1");
});

test("should fail when canaries not implemented", async () => {
const auto = new Auto({ ...defaults, plugins: [] });
// @ts-ignore
Expand Down
33 changes: 28 additions & 5 deletions packages/core/src/auto.ts
Expand Up @@ -1295,16 +1295,39 @@ export default class Auto {
);

if (options.message !== "false" && pr) {
const prNumber = Number(pr);
const message =
typeof result === "string"
? messageHeader
: makeDetail(messageHeader, result.details);

await this.prBody({
pr: Number(pr),
context: "canary-version",
message,
});
switch (options.target) {
case "comment":
await this.comment({
pr: prNumber,
context: "canary-version",
message,
});
break;

case "status":
await this.prStatus({
pr: prNumber,
context: "canary-version",
description: messageHeader,
state: "success",
url: "buildUrl" in env ? env.buildUrl : "",
});
break;

default:
await this.prBody({
pr: prNumber,
context: "canary-version",
message,
});
break;
}
}

this.logger.log.success(
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Expand Up @@ -117,6 +117,12 @@ export const globalOptions = t.partial({
force: t.boolean,
/** The message used when attaching the canary version to a PR */
message: t.union([t.literal(false), t.string]),
/** How the canary version should be attached to a PR */
target: t.union([
t.literal("pr-body"),
t.literal("comment"),
t.literal("status"),
]),
}),
/** Options to pass to "auto next" */
next: t.partial({
Expand Down