diff --git a/README.md b/README.md index 21e7646..8e47e03 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Add Labels -A GitHub Action to add labels on PR / Issue events +A GitHub Action to add labels on PR / Issues. ## Inputs @@ -8,6 +8,10 @@ A GitHub Action to add labels on PR / Issue events **Required** Labels to add. +### `issue_number` + +**Optional** Issue/Pull Request number + ## Example usage ```yaml @@ -15,7 +19,7 @@ name: Add Label on: pull_request: branches: - - master + - main types: - opened diff --git a/dist/index.js b/dist/index.js index 50e5c80..1312179 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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) { diff --git a/src/index.ts b/src/index.ts index 2435bf7..cfab144 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -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);