Skip to content

Commit 1373cad

Browse files
authored
Merge pull request #436 from peter-evans/labels-input
Add labels input
2 parents ffd4cc9 + 70c7e8e commit 1373cad

5 files changed

Lines changed: 48 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ jobs:
7373
with:
7474
issue-number: ${{ steps.ciff.outputs.issue-number }}
7575
comment: '[CI] test ${{ matrix.target }}'
76+
labels: |
77+
wontfix
7678
7779
package:
7880
if: github.event_name == 'push' && github.ref == 'refs/heads/main'

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ jobs:
3535
Auto-closing this issue.
3636
```
3737
38+
### Close issue and add label(s)
39+
```yml
40+
- name: Close Issue
41+
uses: peter-evans/close-issue@v2
42+
with:
43+
issue-number: 1
44+
comment: Auto-closing issue
45+
labels: |
46+
wontfix
47+
```
48+
49+
> Add multiple labels separated by comma
50+
3851
### Action inputs
3952
4053
| Name | Description | Default |
@@ -44,6 +57,7 @@ jobs:
4457
| `issue-number` | The number of the issue to close. | `github.event.issue.number` |
4558
| `close-reason` | Reason for closing the issue; `completed` or `not_planned`. | `completed` |
4659
| `comment` | A comment to make on the issue before closing. | |
60+
| `labels` | A comma or newline separated list of labels. | |
4761

4862
### Accessing issues in other repositories
4963

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ inputs:
2020
comment:
2121
required: false
2222
description: 'A comment to make on the issue before closing'
23+
labels:
24+
required: false
25+
description: 'A comma or newline separated list of labels.'
2326
runs:
2427
using: 'node16'
2528
main: 'dist/index.js'

dist/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ function getErrorMessage(error) {
4343
return error.message;
4444
return String(error);
4545
}
46+
function getInputAsArray(name, options) {
47+
return getStringAsArray(core.getInput(name, options));
48+
}
49+
function getStringAsArray(str) {
50+
return str
51+
.split(/[\n,]+/)
52+
.map(s => s.trim())
53+
.filter(x => x !== '');
54+
}
4655
function run() {
4756
return __awaiter(this, void 0, void 0, function* () {
4857
try {
@@ -51,7 +60,8 @@ function run() {
5160
repository: core.getInput('repository'),
5261
issueNumber: Number(core.getInput('issue-number')),
5362
closeReason: core.getInput('close-reason'),
54-
comment: core.getInput('comment')
63+
comment: core.getInput('comment'),
64+
labels: getInputAsArray('labels')
5565
};
5666
core.debug(`Inputs: ${(0, util_1.inspect)(inputs)}`);
5767
const [owner, repo] = inputs.repository.split('/');
@@ -72,7 +82,8 @@ function run() {
7282
repo: repo,
7383
issue_number: inputs.issueNumber,
7484
state: 'closed',
75-
state_reason: inputs.closeReason
85+
state_reason: inputs.closeReason,
86+
labels: inputs.labels.length > 0 ? inputs.labels : undefined
7687
});
7788
}
7889
catch (error) {

src/main.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@ function getErrorMessage(error: unknown) {
77
return String(error)
88
}
99

10+
function getInputAsArray(name: string, options?: core.InputOptions): string[] {
11+
return getStringAsArray(core.getInput(name, options))
12+
}
13+
14+
function getStringAsArray(str: string): string[] {
15+
return str
16+
.split(/[\n,]+/)
17+
.map(s => s.trim())
18+
.filter(x => x !== '')
19+
}
20+
1021
async function run(): Promise<void> {
1122
try {
1223
const inputs = {
1324
token: core.getInput('token'),
1425
repository: core.getInput('repository'),
1526
issueNumber: Number(core.getInput('issue-number')),
1627
closeReason: core.getInput('close-reason'),
17-
comment: core.getInput('comment')
28+
comment: core.getInput('comment'),
29+
labels: getInputAsArray('labels')
1830
}
1931
core.debug(`Inputs: ${inspect(inputs)}`)
2032

@@ -34,12 +46,14 @@ async function run(): Promise<void> {
3446
}
3547

3648
core.info('Closing the issue as ' + inputs.closeReason)
49+
3750
await octokit.rest.issues.update({
3851
owner: owner,
3952
repo: repo,
4053
issue_number: inputs.issueNumber,
4154
state: 'closed',
42-
state_reason: inputs.closeReason
55+
state_reason: inputs.closeReason,
56+
labels: inputs.labels.length > 0 ? inputs.labels : undefined
4357
})
4458
} catch (error) {
4559
core.debug(inspect(error))

0 commit comments

Comments
 (0)