Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add labels option #410

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ jobs:
Auto-closing this issue.
```

### Close issue and add label(s)
```yml
- name: Close Issue
uses: peter-evans/close-issue@v2
with:
issue-number: 1
comment: Auto-closing issue
labels: 'wontfix'
```

> Add multiple labels separated by comma

### Action inputs

| Name | Description | Default |
Expand All @@ -44,6 +56,7 @@ jobs:
| `issue-number` | The number of the issue to close. | `github.event.issue.number` |
| `close-reason` | Reason for closing the issue; `completed` or `not_planned`. | `completed` |
| `comment` | A comment to make on the issue before closing. | |
| `labels` | Add labels to the issue. When adding more than one then separate by comma. | |

### Accessing issues in other repositories

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ inputs:
comment:
required: false
description: 'A comment to make on the issue before closing'
labels:
required: false
description: 'An array of labels to add to the issue. An empty array will clear all labels.'
default: ''
runs:
using: 'node16'
main: 'dist/index.js'
Expand Down
16 changes: 12 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ function getErrorMessage(error) {
return String(error);
}
function run() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const inputs = {
token: core.getInput('token'),
repository: core.getInput('repository'),
issueNumber: Number(core.getInput('issue-number')),
closeReason: core.getInput('close-reason'),
comment: core.getInput('comment')
comment: core.getInput('comment'),
labels: core.getInput('labels')
};
core.debug(`Inputs: ${(0, util_1.inspect)(inputs)}`);
const [owner, repo] = inputs.repository.split('/');
Expand All @@ -67,13 +69,19 @@ function run() {
});
}
core.info('Closing the issue as ' + inputs.closeReason);
yield octokit.rest.issues.update({
const params = {
owner: owner,
repo: repo,
issue_number: inputs.issueNumber,
state: 'closed',
state_reason: inputs.closeReason
});
state_reason: inputs.closeReason,
labels: inputs.labels.split(',')
};
if (!((_a = params === null || params === void 0 ? void 0 : params.labels) === null || _a === void 0 ? void 0 : _a.length)) {
delete params.labels;
}
yield octokit.rest.issues.update(params);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
core.debug((0, util_1.inspect)(error));
Expand Down
26 changes: 22 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import * as core from '@actions/core'
import * as github from '@actions/github'
import {inspect} from 'util'

type octokitParams = {
owner: string
repo: string
issue_number: number
state: string
state_reason: string
labels?: string[]
}

function getErrorMessage(error: unknown) {
if (error instanceof Error) return error.message
return String(error)
Expand All @@ -14,7 +23,8 @@ async function run(): Promise<void> {
repository: core.getInput('repository'),
issueNumber: Number(core.getInput('issue-number')),
closeReason: core.getInput('close-reason'),
comment: core.getInput('comment')
comment: core.getInput('comment'),
labels: core.getInput('labels')
}
core.debug(`Inputs: ${inspect(inputs)}`)

Expand All @@ -34,13 +44,21 @@ async function run(): Promise<void> {
}

core.info('Closing the issue as ' + inputs.closeReason)
await octokit.rest.issues.update({

const params: octokitParams = {
owner: owner,
repo: repo,
issue_number: inputs.issueNumber,
state: 'closed',
state_reason: inputs.closeReason
})
state_reason: inputs.closeReason,
labels: inputs.labels.split(',')
}

if (!params?.labels?.length) {
delete params.labels
}

await octokit.rest.issues.update(params)
} catch (error) {
core.debug(inspect(error))
core.setFailed(getErrorMessage(error))
Expand Down