diff --git a/action.yml b/action.yml index e9b892f5..b0506c2b 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,9 @@ inputs: ignore-if-labeled: description: "True/False value to indicate if no labels should be added or removed if the issue already has labels." required: false + issue-number: + description: "An issue number or PR number or project card number. Optional, if not specified, will use the one available in github event `github.event.pull_request` or `github.event.issue`" + required: false branding: icon: zap-off color: orange diff --git a/label.js b/label.js index edf28906..8dad1dd6 100644 --- a/label.js +++ b/label.js @@ -11,6 +11,34 @@ var labelsToRemove = core .split(",") .map(x => x.trim()); +/** + * Obtain the issue number either from input or from the context + * @param core - the core object + * @param context - the context object + * @returns {*|number} - issue/card/pr number if not provided by user. + */ +function getIssueNumber(core, context) { + let issueNumber = core.getInput("issue-number"); + + // return what is provided + if (issueNumber) return issueNumber; + + // return the one found in issue + issueNumber = context.payload.issue && context.payload.issue.number; + if (issueNumber) return issueNumber; + + // return the one found in PR + issueNumber = + context.payload.pull_request && context.payload.issue.pull_request; + if (issueNumber) return issueNumber; + + let card_url = + context.payload.project_card && context.payload.project_card.content_url; + issueNumber = card_url && card_url.split("/").pop(); + + return issueNumber; +} + async function label() { const myToken = core.getInput("repo-token"); const ignoreIfAssigned = core.getInput("ignore-if-assigned"); @@ -19,18 +47,7 @@ async function label() { const context = github.context; const repoName = context.payload.repository.name; const ownerName = context.payload.repository.owner.login; - var issueNumber; - - if (context.payload.issue !== undefined) { - issueNumber = context.payload.issue.number; - } else if (context.payload.pull_request !== undefined) { - issueNumber = context.payload.pull_request.number; - } else if ( - context.payload.project_card !== undefined && - context.payload.project_card.content_url - ) { - issueNumber = context.payload.project_card.content_url.split("/").pop(); - } + let issueNumber = getIssueNumber(core, context); if (issueNumber === undefined) { return "No action being taken. Ignoring because issueNumber was not identified";