Skip to content

Commit 65d195b

Browse files
jeff-miller-cfamheap
authored andcommitted
Add labels output which will contain all the matching/intersecting labels if the action is successful.
1 parent 5384b5b commit 65d195b

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ This action has three required inputs; `labels`, `mode` and `count`
1818

1919
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.
2020

21+
If successful, any matching labels will be output in `outputs.labels` as a comma separated string.
22+
2123
## Examples
2224

2325
### Complete example
@@ -149,3 +151,30 @@ jobs:
149151
- run: echo FAILURE && exit 1
150152
if: needs.label.outputs.status == 'failure'
151153
```
154+
155+
### Using Output Labels
156+
157+
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.
158+
159+
```yaml
160+
name: Pull Request Labels
161+
on:
162+
pull_request:
163+
types: [opened, labeled, unlabeled, synchronize]
164+
jobs:
165+
label:
166+
runs-on: ubuntu-latest
167+
steps:
168+
- id: check-labels
169+
uses: mheap/github-action-required-labels@v4
170+
with:
171+
mode: minimum
172+
count: 1
173+
labels: "feature-1, feature-2, feature-3"
174+
- run: |
175+
echo "Enabled Features:"
176+
for f in $(echo "{{steps.check-labels.outputs.labels}}" | sed "s/,/ /g")
177+
do
178+
echo "$f"
179+
done
180+
```

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ runs:
66
branding:
77
icon: check-square
88
color: blue
9+
outputs:
10+
labels:
11+
description: "The labels that were matched as a comma separated string"
912
inputs:
1013
token:
1114
description: The GitHub token to use when calling the API

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ async function action() {
103103
}
104104
}
105105

106+
core.setOutput("labels", intersection.join(","));
106107
core.setOutput("status", "success");
107108
} catch (e) {
108109
core.setFailed(e.message);

index.test.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ describe("Required Labels", () => {
5858
mockLabels(["enhancement", "bug"]);
5959

6060
await action();
61-
expect(core.setOutput).toBeCalledTimes(1);
61+
expect(core.setOutput).toBeCalledTimes(2);
6262
expect(core.setOutput).toBeCalledWith("status", "success");
63+
expect(core.setOutput).toBeCalledWith("labels", "enhancement");
6364
});
6465

6566
it("fetches the labels from the API (and fails)", async () => {
@@ -134,8 +135,9 @@ describe("Required Labels", () => {
134135

135136
await action();
136137

137-
expect(core.setOutput).toBeCalledTimes(1);
138+
expect(core.setOutput).toBeCalledTimes(2);
138139
expect(core.setOutput).toBeCalledWith("status", "success");
140+
expect(core.setOutput).toBeCalledWith("labels", "enhancement");
139141
});
140142

141143
it("at least X", async () => {
@@ -148,8 +150,9 @@ describe("Required Labels", () => {
148150

149151
await action();
150152

151-
expect(core.setOutput).toBeCalledTimes(1);
153+
expect(core.setOutput).toBeCalledTimes(2);
152154
expect(core.setOutput).toBeCalledWith("status", "success");
155+
expect(core.setOutput).toBeCalledWith("labels", "enhancement,bug");
153156
});
154157

155158
it("at most X", async () => {
@@ -163,9 +166,11 @@ describe("Required Labels", () => {
163166

164167
await action();
165168

166-
expect(core.setOutput).toBeCalledTimes(1);
169+
expect(core.setOutput).toBeCalledTimes(2);
167170
expect(core.setOutput).toBeCalledWith("status", "success");
171+
expect(core.setOutput).toBeCalledWith("labels", "enhancement,bug");
168172
});
173+
169174
});
170175

171176
describe("failure", () => {
@@ -305,8 +310,9 @@ describe("Required Labels", () => {
305310
mockLabels(["bug"]);
306311

307312
await action();
308-
expect(core.setOutput).toBeCalledTimes(1);
313+
expect(core.setOutput).toBeCalledTimes(2);
309314
expect(core.setOutput).toBeCalledWith("status", "success");
315+
expect(core.setOutput).toBeCalledWith("labels", "bug");
310316
});
311317
});
312318

0 commit comments

Comments
 (0)