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

Stop failing the run and let the user decide how to proceed #136

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 21 additions & 10 deletions __tests__/commandHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ import { join } from "path";
import nock from "nock";

import * as core from "@actions/core";
jest.mock("@actions/core", () => {
return {
debug: jest.fn(),
setFailed: jest.fn(),
setOutput: jest.fn(),
};
});

import { context } from "@actions/github";

import { CommandHandler } from "../src/commandHandler";

describe("commandHandler", () => {
beforeEach(() => {
jest.spyOn(core, "setOutput");

context.action = "run1";
context.actor = "test-user";
context.eventName = "issue_comment";
Expand All @@ -25,6 +19,10 @@ describe("commandHandler", () => {
context.workflow = "Issue comments";
});

afterEach(() => {
delete process.exitCode;
});

describe("process", () => {
it("should return false when incorrect action", async () => {
context.payload = require(join(__dirname, "payloads", "edited.json"));
Expand All @@ -39,6 +37,7 @@ describe("commandHandler", () => {
);

await expect(commandHandler.process()).resolves.toBe(false);
expect(process.exitCode).toBeUndefined();
});

it("should return false when no slash command in comment", async () => {
Expand All @@ -54,6 +53,7 @@ describe("commandHandler", () => {
);

await expect(commandHandler.process()).resolves.toBe(false);
expect(process.exitCode).toBeUndefined();
});

it("should return false when incorrect slash command", async () => {
Expand All @@ -69,11 +69,10 @@ describe("commandHandler", () => {
);

await expect(commandHandler.process()).resolves.toBe(false);
expect(process.exitCode).toBeUndefined();
});

it("should return false when correct slash command but incorrect repo access", async () => {
const mockedSetOutput = core.setOutput as jest.Mock<typeof core.setOutput>;

context.payload = require(join(__dirname, "payloads", "created.json"));

const commandHandler = new CommandHandler(
Expand All @@ -90,6 +89,7 @@ describe("commandHandler", () => {
.reply(200, { permission: "read" });

await expect(commandHandler.process()).resolves.toBe(false);
expect(process.exitCode).toBe(core.ExitCode.Failure);

expect(scoped.isDone()).toBe(true);
});
Expand All @@ -115,6 +115,7 @@ describe("commandHandler", () => {
.reply(201);

await expect(commandHandler.process()).resolves.toBe(true);
expect(process.exitCode).toBeUndefined();

expect(permissionScope.isDone()).toBe(true);
expect(reactionScope.isDone()).toBe(false);
Expand All @@ -141,6 +142,7 @@ describe("commandHandler", () => {
.reply(201);

await expect(commandHandler.process()).resolves.toBe(true);
expect(process.exitCode).toBeUndefined();

expect(permissionScope.isDone()).toBe(true);
expect(reactionScope.isDone()).toBe(true);
Expand All @@ -165,6 +167,7 @@ describe("commandHandler", () => {
.reply(200, { permission: "write" });

await expect(commandHandler.process()).resolves.toBe(true);
expect(process.exitCode).toBeUndefined();

expect(scoped.isDone()).toBe(true);

Expand All @@ -187,6 +190,7 @@ describe("commandHandler", () => {
);

expect(commandHandler.shouldRunForAction()).toBe(true);
expect(process.exitCode).toBeUndefined();
});

it("returns 'true' when action is 'edited' and allowEdits is 'true'", () => {
Expand All @@ -202,6 +206,7 @@ describe("commandHandler", () => {
);

expect(commandHandler.shouldRunForAction()).toBe(true);
expect(process.exitCode).toBeUndefined();
});

it("returns 'false' when action is 'edited' and allowEdits is 'false'", () => {
Expand All @@ -217,6 +222,7 @@ describe("commandHandler", () => {
);

expect(commandHandler.shouldRunForAction()).toBe(false);
expect(process.exitCode).toBeUndefined();
});

it("returns 'false' when action is 'deleted'", () => {
Expand All @@ -232,6 +238,7 @@ describe("commandHandler", () => {
);

expect(commandHandler.shouldRunForAction()).toBe(false);
expect(process.exitCode).toBeUndefined();
});

it("returns 'false' when action is unknown", () => {
Expand All @@ -247,6 +254,7 @@ describe("commandHandler", () => {
);

expect(commandHandler.shouldRunForAction()).toBe(false);
expect(process.exitCode).toBeUndefined();
});
});

Expand All @@ -271,6 +279,7 @@ describe("commandHandler", () => {

expect(scoped.isDone()).toBe(true);
expect(result).toBe("admin");
expect(process.exitCode).toBeUndefined();
});
});

Expand All @@ -290,6 +299,7 @@ describe("commandHandler", () => {
);

await expect(commandHandler.createReaction(123)).resolves.toBe(false);
expect(process.exitCode).toBeUndefined();
});

it("creates reaction if enabled", async () => {
Expand All @@ -307,6 +317,7 @@ describe("commandHandler", () => {
.reply(201);

await expect(commandHandler.createReaction(123)).resolves.toBe(true);
expect(process.exitCode).toBeUndefined();

expect(scoped.isDone()).toBe(true);
});
Expand Down
4 changes: 2 additions & 2 deletions src/commandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class CommandHandler {
const commandResults = this.command.checkComment(comment.body);

if (!commandResults) {
setFailed("Comment didn't contain a valid slash command");
debug("Comment didn't contain a valid slash command");

return false;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ export class CommandHandler {
return true;
}

setFailed("Comment was edited and allow edits is disabled, no action to take");
debug("Comment was edited and allow edits is disabled, no action to take");

return false;
}
Expand Down