diff --git a/README.md b/README.md index 41c39f5..5b33eda 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ This action has three required inputs; `labels`, `mode` and `count` This action calls the GitHub API to fetch labels for a PR rather than reading `event.json`. This allows the action to run as intended when an earlier step adds a label. It will use `github.token` by default, and you can set the `token` input to provide alternative authentication. +If successful, any matching labels will be output in `outputs.labels` as a comma separated string. + ## Examples ### Complete example @@ -149,3 +151,30 @@ jobs: - run: echo FAILURE && exit 1 if: needs.label.outputs.status == 'failure' ``` + +### Using Output Labels + +If the action was successful you can access the matching labels via `outputs.labels`. This is useful if you want to use the labels in a later step. + +```yaml +name: Pull Request Labels +on: + pull_request: + types: [opened, labeled, unlabeled, synchronize] +jobs: + label: + runs-on: ubuntu-latest + steps: + - id: check-labels + uses: mheap/github-action-required-labels@v4 + with: + mode: minimum + count: 1 + labels: "feature-1, feature-2, feature-3" + - run: | + echo "Enabled Features:" + for f in $(echo "{{steps.check-labels.outputs.labels}}" | sed "s/,/ /g") + do + echo "$f" + done +``` \ No newline at end of file diff --git a/action.yml b/action.yml index 6654ce7..f0e10d2 100644 --- a/action.yml +++ b/action.yml @@ -6,6 +6,9 @@ runs: branding: icon: check-square color: blue +outputs: + labels: + description: "The labels that were matched as a comma separated string" inputs: token: description: The GitHub token to use when calling the API diff --git a/index.js b/index.js index 1e5cce9..362569d 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ const core = require("@actions/core"); const github = require("@actions/github"); -const matchToken = ``; +const matchToken = `\n`; async function action() { try { const token = core.getInput("token", { required: true }); @@ -103,6 +103,7 @@ async function action() { } } + core.setOutput("labels", intersection.join(",")); core.setOutput("status", "success"); } catch (e) { core.setFailed(e.message); diff --git a/index.test.js b/index.test.js index 1b22771..a8437da 100644 --- a/index.test.js +++ b/index.test.js @@ -6,7 +6,7 @@ const mockedEnv = require("mocked-env"); const nock = require("nock"); nock.disableNetConnect(); -const matchToken = ``; +const matchToken = `\n`; describe("Required Labels", () => { let restore; @@ -58,8 +58,9 @@ describe("Required Labels", () => { mockLabels(["enhancement", "bug"]); await action(); - expect(core.setOutput).toBeCalledTimes(1); + expect(core.setOutput).toBeCalledTimes(2); expect(core.setOutput).toBeCalledWith("status", "success"); + expect(core.setOutput).toBeCalledWith("labels", "enhancement"); }); it("fetches the labels from the API (and fails)", async () => { @@ -134,8 +135,9 @@ describe("Required Labels", () => { await action(); - expect(core.setOutput).toBeCalledTimes(1); + expect(core.setOutput).toBeCalledTimes(2); expect(core.setOutput).toBeCalledWith("status", "success"); + expect(core.setOutput).toBeCalledWith("labels", "enhancement"); }); it("at least X", async () => { @@ -148,8 +150,9 @@ describe("Required Labels", () => { await action(); - expect(core.setOutput).toBeCalledTimes(1); + expect(core.setOutput).toBeCalledTimes(2); expect(core.setOutput).toBeCalledWith("status", "success"); + expect(core.setOutput).toBeCalledWith("labels", "enhancement,bug"); }); it("at most X", async () => { @@ -163,9 +166,11 @@ describe("Required Labels", () => { await action(); - expect(core.setOutput).toBeCalledTimes(1); + expect(core.setOutput).toBeCalledTimes(2); expect(core.setOutput).toBeCalledWith("status", "success"); + expect(core.setOutput).toBeCalledWith("labels", "enhancement,bug"); }); + }); describe("failure", () => { @@ -305,8 +310,9 @@ describe("Required Labels", () => { mockLabels(["bug"]); await action(); - expect(core.setOutput).toBeCalledTimes(1); + expect(core.setOutput).toBeCalledTimes(2); expect(core.setOutput).toBeCalledWith("status", "success"); + expect(core.setOutput).toBeCalledWith("labels", "bug"); }); });
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: