Skip to content

Commit

Permalink
Fix label addition issue and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
goldsrc committed Mar 5, 2024
1 parent 641ecb2 commit 5ac75ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# Add Labels

A GitHub Action to add labels on PR / Issue events
A GitHub Action to add labels on PR / Issues.

## Inputs

### `labels`

**Required** Labels to add.

### `issue_number`

**Optional** Issue/Pull Request number

## Example usage

```yaml
name: Add Label
on:
pull_request:
branches:
- master
- main
types:
- opened

Expand Down
18 changes: 14 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30920,15 +30920,25 @@ __nccwpck_require__.r(__webpack_exports__);
const octokit = (0,_actions_github__WEBPACK_IMPORTED_MODULE_1__.getOctokit)(githubToken);
const { owner, repo } = _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.repo;
const labels = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)("labels")
.split("\n")
.split(/[\n,]/)
.filter((x) => x !== "");
const issueNumber = _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.payload.number;
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(`Add labels: ${labels} to ${owner}/${repo}#${issueNumber}`);
const issueNumber = Number((0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)("issue_number", { required: false }) ||
_actions_github__WEBPACK_IMPORTED_MODULE_1__.context.payload.pull_request?.number ||
_actions_github__WEBPACK_IMPORTED_MODULE_1__.context.payload.number);
if (isNaN(issueNumber)) {
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed)("cannot find issue/PR number!");
return;
}
if (issueNumber <= 0) {
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed)("issue/PR number should be greater than 0!");
return;
}
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(`Adding labels: ${labels.join(", ")} to ${owner}/${repo}#${issueNumber}`);
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels,
issue_number: issueNumber,
});
}
catch (error) {
Expand Down
25 changes: 21 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getOctokit, context } from "@actions/github";
(async () => {
try {
const githubToken = process.env["GITHUB_TOKEN"];

if (!githubToken) {
setFailed("GITHUB_TOKEN does not exist.");
return;
Expand All @@ -13,18 +14,34 @@ import { getOctokit, context } from "@actions/github";
const { owner, repo } = context.repo;

const labels = getInput("labels")
.split("\n")
.split(/[\n,]/)
.filter((x) => x !== "");

const issueNumber = context.payload.number;
const issueNumber = Number(
getInput("issue_number", { required: false }) ||
context.payload.pull_request?.number ||
context.payload.number
);

info(`Add labels: ${labels} to ${owner}/${repo}#${issueNumber}`);
if (isNaN(issueNumber)) {
setFailed("cannot find issue/PR number!");
return;
}

if (issueNumber <= 0) {
setFailed("issue/PR number should be greater than 0!");
return;
}

info(
`Adding labels: ${labels.join(", ")} to ${owner}/${repo}#${issueNumber}`
);

await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels,
issue_number: issueNumber,
});
} catch (error) {
setFailed(error.message);
Expand Down

0 comments on commit 5ac75ab

Please sign in to comment.