diff --git a/README.md b/README.md index 48c3d28..9375874 100644 --- a/README.md +++ b/README.md @@ -88,3 +88,13 @@ When you reach a point where you'd like to update your labels and regular expres Set `versioned-regex` to any valid regular expression that should be used to capture the version number from the issue. The first match will be used should multiple be found. Set `body-missing-regex-label` to the name of the label that should be added to an issue where the specified `version-regex` can't be found. This is useful for when your users accidentally delete this value. Leave this blank if you don't want to use this functionality. + +### Pull request support + +The labeler action is also available for pull requests. Make sure the workflow is triggered by pull requests. + +``` +on: + pull_requests: + types: [opened, edited] +``` diff --git a/lib/main.js b/lib/main.js index 503a2f6..6c62756 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,4 +1,23 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -8,13 +27,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(require("@actions/core")); const github = __importStar(require("@actions/github")); @@ -28,11 +40,15 @@ function run() { const enableVersionedRegex = parseInt(core.getInput('enable-versioned-regex', { required: true })); const versionedRegex = new RegExp(core.getInput('versioned-regex', { required: false })); const notBefore = Date.parse(core.getInput('not-before', { required: false })); - const bodyMissingRexexLabel = core.getInput('body-missing-regex-label', { required: false }); - const issue_number = getIssueNumber(); - const issue_body = getIssueBody(); - if (!issue_number || !issue_body) { - console.log('Could not get issue number or issue body from context, exiting'); + const bodyMissingRegexLabel = core.getInput('body-missing-regex-label', { required: false }); + const issue_number = getIssueOrPullRequestNumber(); + if (issue_number === undefined) { + console.log('Could not get issue or pull request number from context, exiting'); + return; + } + const issue_body = getIssueOrPullRequestBody(); + if (issue_body === undefined) { + console.log('Could not get issue or pull request body from context, exiting'); return; } // A client to load data from GitHub @@ -42,15 +58,15 @@ function run() { if (enableVersionedRegex == 1) { const regexVersion = versionedRegex.exec(issue_body); if (!regexVersion || !regexVersion[1]) { - if (bodyMissingRexexLabel) { - addLabels(client, issue_number, [bodyMissingRexexLabel]); + if (bodyMissingRegexLabel) { + addLabels(client, issue_number, [bodyMissingRegexLabel]); } console.log(`Issue #${issue_number} does not contain regex version in the body of the issue, exiting.`); return 0; } else { - if (bodyMissingRexexLabel) { - removeLabelItems.push(bodyMissingRexexLabel); + if (bodyMissingRegexLabel) { + removeLabelItems.push(bodyMissingRegexLabel); } } configPath = regexifyConfigPath(configPath, regexVersion[1]); @@ -93,19 +109,27 @@ function run() { } }); } -function getIssueNumber() { +function getIssueOrPullRequestNumber() { const issue = github.context.payload.issue; - if (!issue) { - return; + if (issue) { + return issue.number; + } + const pull_request = github.context.payload.pull_request; + if (pull_request) { + return pull_request.number; } - return issue.number; + return; } -function getIssueBody() { +function getIssueOrPullRequestBody() { const issue = github.context.payload.issue; - if (!issue) { - return; + if (issue) { + return issue.body; + } + const pull_request = github.context.payload.pull_request; + if (pull_request) { + return pull_request.body; } - return issue.body; + return; } function regexifyConfigPath(configPath, version) { var lastIndex = configPath.lastIndexOf('.'); diff --git a/src/main.ts b/src/main.ts index 27e167a..5a1470f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,11 +11,15 @@ async function run() { const versionedRegex = new RegExp(core.getInput('versioned-regex', { required: false })); const notBefore = Date.parse(core.getInput('not-before', { required: false })); const bodyMissingRegexLabel = core.getInput('body-missing-regex-label', { required: false }); - const issue_number = getIssueNumber(); - const issue_body = getIssueBody(); + const issue_number = getIssueOrPullRequestNumber(); + if (issue_number === undefined) { + console.log('Could not get issue or pull request number from context, exiting'); + return; + } - if (issue_number === undefined || issue_body === undefined) { - console.log('Could not get issue number or issue body from context, exiting'); + const issue_body = getIssueOrPullRequestBody(); + if (issue_body === undefined) { + console.log('Could not get issue or pull request body from context, exiting'); return; } @@ -86,22 +90,32 @@ async function run() { } } -function getIssueNumber(): number | undefined { +function getIssueOrPullRequestNumber(): number | undefined { const issue = github.context.payload.issue; - if (!issue) { - return; + if (issue) { + return issue.number; + } + + const pull_request = github.context.payload.pull_request; + if (pull_request) { + return pull_request.number; } - return issue.number; + return; } -function getIssueBody(): string | undefined { +function getIssueOrPullRequestBody(): string | undefined { const issue = github.context.payload.issue; - if (!issue) { - return; + if (issue) { + return issue.body; + } + + const pull_request = github.context.payload.pull_request; + if (pull_request) { + return pull_request.body; } - return issue.body; + return; } function regexifyConfigPath(configPath: string, version: string) {