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 new has-command output parameter #137

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ Name | Allowed values | Description
`allow-edits` | `true`, `false` (default) | Indicates if the action should run on comment edits, or the initial comment only.
`permission-level` | `admin`, `write` (default), `read`, `none` | The user's [permission level](https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level) needed to act on the command.

## Outputs

Name | Description
-- | --
`has-command` | If a command was found in the comment. Will be a string value of `true` or `false`.

## License

The scripts and documentation in this project are released under the [MIT License](LICENSE)
17 changes: 13 additions & 4 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as main from "../src/main";
import { CommandHandler } from "../src/commandHandler";
import * as core from "@actions/core";

jest.mock("../src/commandHandler");

Expand All @@ -9,6 +10,8 @@ describe("main", () => {
beforeEach(() => {
mockCommandHandler.mockClear();

jest.spyOn(core, "setOutput");

process.env["INPUT_REPO-TOKEN"] = "abc";
process.env["INPUT_COMMAND"] = "test";
process.env["INPUT_REACTION"] = "true";
Expand Down Expand Up @@ -38,14 +41,20 @@ describe("main", () => {
await expect(main.run()).rejects.toThrow('command');
});

it("calls 'process'", async () => {
await main.run();
it("calls 'process' and sets output", async () => {
const mockedSetOutput = core.setOutput as jest.Mock<typeof core.setOutput>;

const mockCommandHandledInstance = mockCommandHandler.mock.instances[0];
const mockProcess = jest.fn().mockResolvedValue(true);
mockCommandHandler.prototype.process = mockProcess;

await main.run();

expect(mockCommandHandler).toHaveBeenCalled();
expect(mockCommandHandler).toHaveBeenCalledWith("abc", "test", true, "eyes", true, "write");

expect(mockCommandHandledInstance.process).toHaveBeenCalledTimes(1);
expect(mockProcess).toHaveBeenCalledTimes(1);
expect(mockProcess).toReturnWith(Promise.resolve(true));

expect(mockedSetOutput).toHaveBeenCalledWith("has-command", "true");
});
});
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ outputs:
command-arguments:
description: The arguments of the command

has-command:
description: If a command was found in the comment

runs:
using: "node12"
main: "dist/index.js"
24 changes: 15 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import * as core from "@actions/core";
import {
getInput,
setFailed,
setOutput,
} from "@actions/core";

import { CommandHandler } from "./commandHandler";
import { PermissionLevel } from "./interfaces";

export async function run(): Promise<void> {
try {
await new CommandHandler(
core.getInput("repo-token", { required: true }),
core.getInput("command", { required: true }),
core.getInput("reaction") === "true",
core.getInput("reaction-type"),
core.getInput("allow-edits") === "true",
core.getInput("permission-level") as PermissionLevel,
const hasCommand = await new CommandHandler(
getInput("repo-token", { required: true }),
getInput("command", { required: true }),
getInput("reaction") === "true",
getInput("reaction-type"),
getInput("allow-edits") === "true",
getInput("permission-level") as PermissionLevel,
).process();

setOutput("has-command", hasCommand.toString());
} catch (error) {
core.setFailed(error.message);
setFailed(error.message);
throw error;
}
}
Expand Down