Skip to content

Commit

Permalink
Add labels output which will contain all the matching/intersecting …
Browse files Browse the repository at this point in the history
…labels if the action is successful.
  • Loading branch information
jeff-miller-cfa authored and mheap committed Jun 5, 2023
1 parent 5384b5b commit 65d195b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
29 changes: 29 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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
```
3 changes: 3 additions & 0 deletions action.yml
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -103,6 +103,7 @@ async function action() {
}
}

core.setOutput("labels", intersection.join(","));
core.setOutput("status", "success");
} catch (e) {
core.setFailed(e.message);
Expand Down
16 changes: 11 additions & 5 deletions index.test.js
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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");
});
});

Expand Down

0 comments on commit 65d195b

Please sign in to comment.