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 support for output status #32

Merged
merged 1 commit into from Jun 25, 2022
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
36 changes: 35 additions & 1 deletion README.md
Expand Up @@ -83,5 +83,39 @@ To force an API call, set the `GITHUB_TOKEN` environment variable like so:
mode: minimum
count: 2
labels: "community-reviewed, team-reviewed, codeowner-reviewed"
exit_type: neutral # Can be: failure, neutral or success
exit_type: success # Can be: success or failure (default: failure)
```

You can set `exit_type` to success then inspect `outputs.status` to see if the action passed or failed. This is useful when you want to perform additional actions if a label is not present, but not fail the entire build.

If the action passed, `outputs.status` will be `success`. If it failed, `outputs.status` will be `failure`.

Here is a complete workflow example for this use case:

```yaml
name: Pull Request Labels
on:
pull_request:
types: [opened, labeled, unlabeled, synchronize]
jobs:
label:
runs-on: ubuntu-latest
outputs:
status: ${{ steps.check-labels.outputs.status }}
steps:
- id: check-labels
uses: mheap/github-action-required-labels@v2
with:
mode: exactly
count: 1
labels: "semver:patch, semver:minor, semver:major"
exit_type: success
do-other:
runs-on: ubuntu-latest
needs: label
steps:
- run: echo SUCCESS
if: needs.label.outputs.status == 'success'
- run: echo FAILURE && exit 1
if: needs.label.outputs.status == 'failure'
```
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -55,6 +55,7 @@ Toolkit.run(async (tools) => {
let intersection = allowedLabels.filter((x) => appliedLabels.includes(x));

if (mode === "exactly" && intersection.length !== count) {
tools.outputs.status = "failure";
tools.exit[exitType](
`Label error. Requires exactly ${count} of: ${allowedLabels.join(
", "
Expand All @@ -64,6 +65,7 @@ Toolkit.run(async (tools) => {
}

if (mode === "minimum" && intersection.length < count) {
tools.outputs.status = "failure";
tools.exit[exitType](
`Label error. Requires at least ${count} of: ${allowedLabels.join(
", "
Expand All @@ -73,6 +75,7 @@ Toolkit.run(async (tools) => {
}

if (mode === "maximum" && intersection.length > count) {
tools.outputs.status = "failure";
tools.exit[exitType](
`Label error. Requires at most ${count} of: ${allowedLabels.join(
", "
Expand All @@ -81,5 +84,6 @@ Toolkit.run(async (tools) => {
return;
}

tools.outputs.status = "success";
tools.exit.success("Complete");
});
14 changes: 14 additions & 0 deletions index.test.js
@@ -1,4 +1,5 @@
const { Toolkit } = require("actions-toolkit");
const core = require("@actions/core");
const mockedEnv = require("mocked-env");

describe("Required Labels", () => {
Expand Down Expand Up @@ -36,6 +37,7 @@ describe("Required Labels", () => {
tools.exit.success = jest.fn();
tools.exit.failure = jest.fn();
tools.exit.neutral = jest.fn();
core.setOutput = jest.fn();
});

afterEach(() => {
Expand All @@ -54,6 +56,8 @@ describe("Required Labels", () => {
});

action(tools);
expect(core.setOutput).toBeCalledTimes(1);
expect(core.setOutput).toBeCalledWith("status", "success");
expect(tools.exit.success).toBeCalledTimes(1);
expect(tools.exit.success).toBeCalledWith("Complete");
});
Expand All @@ -67,6 +71,8 @@ describe("Required Labels", () => {
});

action(tools);
expect(core.setOutput).toBeCalledTimes(1);
expect(core.setOutput).toBeCalledWith("status", "success");
expect(tools.exit.success).toBeCalledTimes(1);
expect(tools.exit.success).toBeCalledWith("Complete");
});
Expand All @@ -80,6 +86,8 @@ describe("Required Labels", () => {
});

action(tools);
expect(core.setOutput).toBeCalledTimes(1);
expect(core.setOutput).toBeCalledWith("status", "success");
expect(tools.exit.success).toBeCalledTimes(1);
expect(tools.exit.success).toBeCalledWith("Complete");
});
Expand All @@ -95,6 +103,8 @@ describe("Required Labels", () => {
});

action(tools);
expect(core.setOutput).toBeCalledTimes(1);
expect(core.setOutput).toBeCalledWith("status", "failure");
expect(tools.exit.failure).toBeCalledTimes(1);
expect(tools.exit.failure).toBeCalledWith(
"Label error. Requires exactly 1 of: enhancement, bug. Found: enhancement, bug"
Expand All @@ -110,6 +120,8 @@ describe("Required Labels", () => {
});

action(tools);
expect(core.setOutput).toBeCalledTimes(1);
expect(core.setOutput).toBeCalledWith("status", "failure");
expect(tools.exit.failure).toBeCalledTimes(1);
expect(tools.exit.failure).toBeCalledWith(
"Label error. Requires at least 2 of: enhancement, bug, triage. Found: enhancement"
Expand All @@ -125,6 +137,8 @@ describe("Required Labels", () => {
});

action(tools);
expect(core.setOutput).toBeCalledTimes(1);
expect(core.setOutput).toBeCalledWith("status", "failure");
expect(tools.exit.failure).toBeCalledTimes(1);
expect(tools.exit.failure).toBeCalledWith(
"Label error. Requires at most 2 of: enhancement, bug, triage. Found: enhancement, triage, bug"
Expand Down