From 65d195bf4a9b738e8f2a9c540d0cc182bb80df72 Mon Sep 17 00:00:00 2001 From: JJ Miller Date: Fri, 2 Jun 2023 15:12:21 -0400 Subject: [PATCH] Add `labels` output which will contain all the matching/intersecting labels if the action is successful. --- README.md | 29 +++++++++++++++++++++++++++++ action.yml | 3 +++ index.js | 1 + index.test.js | 16 +++++++++++----- 4 files changed, 44 insertions(+), 5 deletions(-) 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 aba5a2f..362569d 100644 --- a/index.js +++ b/index.js @@ -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 c6d0e1e..a8437da 100644 --- a/index.test.js +++ b/index.test.js @@ -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"); }); });