From 2fc1d613dac98f3e72e40bd8196cb953fa94e34f Mon Sep 17 00:00:00 2001 From: Aleksandar Colic Date: Tue, 17 Oct 2023 10:33:43 +0200 Subject: [PATCH] Add ignore_branches option --- README.md | 1 + action.yml | 3 ++ dist/index.js | 33 +++++++----- dist/index.js.map | 2 +- src/index.ts | 130 ++++++++++++++++++++++++---------------------- 5 files changed, 94 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 2fd5a61..69d55ab 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ jobs: | JIRA_KEY_MULTIPLE | Boolean | false | If true and JIRA_KEY is a project key, post a comment for every story key found. | | JIRA_PARTIAL_KEY_SILENT_FAILURE | Boolean | false | If true, not finding a story key in a pull request if a project key is specified, only throws a silent error. | | DISABLE_PULL_REQUEST_COMMENT | Boolean | false | If true, using the action will not create or update a pull request comment. Useful for only fetching the issue details from the output. | +| IGNORE_BRANCHES | Regex | - | If source branch name mathces this regex ignore pull request. | ### Outputs | Output | Type | Description | diff --git a/action.yml b/action.yml index 53b38ae..890bf2d 100644 --- a/action.yml +++ b/action.yml @@ -35,6 +35,9 @@ inputs: DISABLE_PULL_REQUEST_COMMENT: description: If true, using the action will not create or update a pull request comment. Useful for only fetching the issue details from the output. + IGNORE_BRANCHES: + description: If source branch name mathces this regex ignore pull request. + outputs: title: description: The title of the Jira story. diff --git a/dist/index.js b/dist/index.js index c2ed992..d97c03f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -40,7 +40,6 @@ function getDescription(description) { return `*No description available.*`; } } -; async function execute(storyKey) { console.debug("Getting the story detail from Jira..."); const issueDetails = await (0, getIssueDetails_1.default)(storyKey); @@ -57,12 +56,12 @@ async function execute(storyKey) { console.debug("Checking for existing story comment..."); const comments = await octokit.rest.issues.listComments({ ...github_1.context.repo, - issue_number: github_1.context.payload.pull_request.number + issue_number: github_1.context.payload.pull_request.number, }); const existingComment = comments.data.find((comment) => { if (!comment.body) return false; - const lines = comment.body.split('\n'); + const lines = comment.body.split("\n"); if (!lines.length) return false; if (!lines[0].startsWith(`## [${issueDetails.key}]`)) @@ -74,8 +73,8 @@ async function execute(storyKey) { const body = [ `## [${issueDetails.key}](${(0, core_1.getInput)("JIRA_BASE_URL")}/browse/${issueDetails.key})`, `### ${issueDetails.fields.summary}`, - description - ].join('\n'); + description, + ].join("\n"); if (existingComment) { console.debug("Existing comment exists for story."); if (existingComment.body === body) { @@ -85,7 +84,7 @@ async function execute(storyKey) { await octokit.rest.issues.updateComment({ ...github_1.context.repo, comment_id: existingComment.id, - body + body, }); } else { @@ -93,27 +92,35 @@ async function execute(storyKey) { await octokit.rest.issues.createComment({ ...github_1.context.repo, issue_number: github_1.context.payload.pull_request.number, - body + body, }); } } } -; async function init() { const jiraKey = (0, core_1.getInput)("JIRA_KEY"); - if (!jiraKey.includes('-')) { + if (!jiraKey.includes("-")) { if (!github_1.context.payload.pull_request) return (0, core_1.setFailed)("Partial Jira key can only be used in pull requests!"); const pullRequest = await octokit.rest.pulls.get({ ...github_1.context.repo, pull_number: github_1.context.payload.pull_request.number, }); + const ignore_branches = (0, core_1.getInput)("IGNORE_BRANCHES"); + if (ignore_branches) { + const ignore_pattern = new RegExp(ignore_branches); + const match = ignore_pattern.exec(pullRequest.data.head.ref ?? ""); + if (match?.length) { + console.info("Skip adding Jira summary because source branch is to be ignored"); + return; + } + } const inputs = [ pullRequest.data.head.ref, pullRequest.data.title, - pullRequest.data.body + pullRequest.data.body, ]; - const regex = new RegExp(`${jiraKey}-([0-9]{1,6})`, 'g'); + const regex = new RegExp(`${jiraKey}-([0-9]{1,6})`, "g"); const storyKeys = []; for (let input of inputs) { const matches = regex.exec(input ?? ""); @@ -127,8 +134,9 @@ async function init() { console.error("Failed to find a Jira key starting with " + jiraKey); console.info("Executing silent error because JIRA_PARTIAL_KEY_SILENT_FAILURE is true."); } - else + else { (0, core_1.setFailed)("Failed to find a Jira key starting with " + jiraKey); + } return; } if ((0, core_1.getInput)("JIRA_KEY_MULTIPLE") !== "") { @@ -141,7 +149,6 @@ async function init() { else execute(jiraKey); } -; try { init(); } diff --git a/dist/index.js.map b/dist/index.js.map index 13af7d7..caffc86 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wCAA+D;AAC/D,4CAAsD;AACtD,2FAAmE;AAEnE,kBAAkB;AAClB,kDAAoC;AAEpC,MAAM,OAAO,GAAG,IAAA,mBAAU,EAAC,IAAA,eAAQ,EAAC,cAAc,CAAC,CAAC,CAAC;AAErD,SAAS,cAAc,CAAC,WAAgB;IACvC,IAAI;QACH,OAAO,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC;KAC1C;IACD,MAAM;QACL,OAAO,6BAA6B,CAAC;KACrC;AACF,CAAC;AAAA,CAAC;AAEF,KAAK,UAAU,OAAO,CAAC,QAAgB;IACrC,OAAO,CAAC,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAEvD,MAAM,YAAY,GAAG,MAAM,IAAA,yBAAe,EAAC,QAAQ,CAAC,CAAC;IAErD,MAAM,WAAW,GAAG,cAAc,CAAC,YAAY,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAEpE,IAAG,IAAA,eAAQ,EAAC,mBAAmB,CAAC,KAAK,EAAE,EAAE;QACvC,IAAA,gBAAS,EAAC,OAAO,EAAE,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAChD,IAAA,gBAAS,EAAC,aAAa,EAAE,WAAW,CAAC,CAAC;KACvC;IAED,IAAG,gBAAO,CAAC,OAAO,CAAC,YAAY,EAAE;QAC/B,IAAG,IAAA,eAAQ,EAAC,8BAA8B,CAAC,KAAK,EAAE,EAAE;YAClD,OAAO,CAAC,IAAI,CAAC,mFAAmF,CAAC,CAAC;YAElG,OAAO;SACR;QAED,OAAO,CAAC,KAAK,CAAC,wCAAw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3B,SAAS;aACV;SACF;QAED,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE;YACrB,IAAI,IAAA,eAAQ,EAAC,iCAAiC,CAAC,KAAK,EAAE,EAAE;gBACtD,OAAO,CAAC,KAAK,CAAC,0CAA0C,GAAG,OAAO,CAAC,CAAC;gBAEpE,OAAO,CAAC,IAAI,CACV,yEAAyE,CAC1E,CAAC;aACH;iBAAM;gBACL,IAAA,gBAAS,EAAC,0CAA0C,GAAG,OAAO,CAAC,CAAC;aACjE;YAED,OAAO;SACR;QAED,IAAI,IAAA,eAAQ,EAAC,mBAAmB,CAAC,KAAK,EAAE,EAAE;YACxC,KAAK,IAAI,QAAQ,IAAI,SAAS;gBAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;SACnD;;YAAM,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;KAC9B;;QAAM,OAAO,CAAC,OAAO,CAAC,CAAC;AAC1B,CAAC;AAED,IAAI;IACF,IAAI,EAAE,CAAC;CACR;AAAC,OAAO,KAAK,EAAE;IACd,IAAI,KAAK,YAAY,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,IAAA,gBAAS,EAAC,KAAK,CAAC,CAAC;;QACrE,IAAA,gBAAS,EAAC,iBAAiB,GAAG,KAAK,CAAC,CAAC;CAC3C"} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 08f2bc1..c86d317 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,13 +8,12 @@ import * as adf2md from "adf-to-md"; const octokit = getOctokit(getInput("GITHUB_TOKEN")); function getDescription(description: any) { - try { - return adf2md.convert(description).result; - } - catch { - return `*No description available.*`; - } -}; + try { + return adf2md.convert(description).result; + } catch { + return `*No description available.*`; + } +} async function execute(storyKey: string) { console.debug("Getting the story detail from Jira..."); @@ -23,14 +22,16 @@ async function execute(storyKey: string) { const description = getDescription(issueDetails.fields.description); - if(getInput("JIRA_KEY_MULTIPLE") !== "") { + if (getInput("JIRA_KEY_MULTIPLE") !== "") { setOutput("title", issueDetails.fields.summary); setOutput("description", description); } - - if(context.payload.pull_request) { - if(getInput("DISABLE_PULL_REQUEST_COMMENT") !== "") { - console.info("Not creating or update any comments because DISABLE_PULL_REQUEST_COMMENT is true."); + + if (context.payload.pull_request) { + if (getInput("DISABLE_PULL_REQUEST_COMMENT") !== "") { + console.info( + "Not creating or update any comments because DISABLE_PULL_REQUEST_COMMENT is true." + ); return; } @@ -39,38 +40,38 @@ async function execute(storyKey: string) { const comments = await octokit.rest.issues.listComments({ ...context.repo, - issue_number: context.payload.pull_request.number + issue_number: context.payload.pull_request.number, }); const existingComment = comments.data.find((comment) => { - if(!comment.body) - return false; + if (!comment.body) return false; - const lines = comment.body.split('\n'); + const lines = comment.body.split("\n"); - if(!lines.length) - return false; + if (!lines.length) return false; - if(!lines[0].startsWith(`## [${issueDetails.key}]`)) - return false; + if (!lines[0].startsWith(`## [${issueDetails.key}]`)) return false; - if(!lines[1].startsWith("###")) - return false; + if (!lines[1].startsWith("###")) return false; return true; }); const body = [ - `## [${issueDetails.key}](${getInput("JIRA_BASE_URL")}/browse/${issueDetails.key})`, + `## [${issueDetails.key}](${getInput("JIRA_BASE_URL")}/browse/${ + issueDetails.key + })`, `### ${issueDetails.fields.summary}`, - description - ].join('\n'); + description, + ].join("\n"); - if(existingComment) { + if (existingComment) { console.debug("Existing comment exists for story."); - if(existingComment.body === body) { - console.info("Skipping updating previous comment because content is the same."); + if (existingComment.body === body) { + console.info( + "Skipping updating previous comment because content is the same." + ); return; } @@ -78,26 +79,25 @@ async function execute(storyKey: string) { await octokit.rest.issues.updateComment({ ...context.repo, comment_id: existingComment.id, - body + body, }); - } - else { + } else { console.debug("Creating a new comment with story summary..."); await octokit.rest.issues.createComment({ ...context.repo, issue_number: context.payload.pull_request.number, - body + body, }); } } -}; +} async function init() { const jiraKey = getInput("JIRA_KEY"); - - if(!jiraKey.includes('-')) { - if(!context.payload.pull_request) + + if (!jiraKey.includes("-")) { + if (!context.payload.pull_request) return setFailed("Partial Jira key can only be used in pull requests!"); const pullRequest = await octokit.rest.pulls.get({ @@ -105,55 +105,63 @@ async function init() { pull_number: context.payload.pull_request.number, }); + const ignore_branches = getInput("IGNORE_BRANCHES"); + + if (ignore_branches) { + + const ignore_pattern = new RegExp(ignore_branches); + + const match = ignore_pattern.exec(pullRequest.data.head.ref ?? ""); + + if (match?.length) { + console.info("Skip adding Jira summary because source branch is to be ignored"); + return; + } + } + const inputs = [ pullRequest.data.head.ref, pullRequest.data.title, - pullRequest.data.body + pullRequest.data.body, ]; - const regex = new RegExp(`${jiraKey}-([0-9]{1,6})`, 'g'); + const regex = new RegExp(`${jiraKey}-([0-9]{1,6})`, "g"); const storyKeys: string[] = []; - for(let input of inputs) { + for (let input of inputs) { const matches = regex.exec(input ?? ""); - if(matches?.length) { + if (matches?.length) { storyKeys.push(matches[0]); continue; } } - if(!storyKeys.length) { - if(getInput("JIRA_PARTIAL_KEY_SILENT_FAILURE") !== "") { + if (!storyKeys.length) { + if (getInput("JIRA_PARTIAL_KEY_SILENT_FAILURE") !== "") { console.error("Failed to find a Jira key starting with " + jiraKey); - console.info("Executing silent error because JIRA_PARTIAL_KEY_SILENT_FAILURE is true."); - } - else + console.info( + "Executing silent error because JIRA_PARTIAL_KEY_SILENT_FAILURE is true." + ); + } else { setFailed("Failed to find a Jira key starting with " + jiraKey); + } return; } - if(getInput("JIRA_KEY_MULTIPLE") !== "") { - for(let storyKey of storyKeys) - execute(storyKey); - } - else - execute(storyKeys[0]); - } - else - execute(jiraKey); -}; + if (getInput("JIRA_KEY_MULTIPLE") !== "") { + for (let storyKey of storyKeys) execute(storyKey); + } else execute(storyKeys[0]); + } else execute(jiraKey); +} try { init(); -} -catch(error) { - if(error instanceof Error || typeof error === "string") - setFailed(error); - else - setFailed("Unknown error: " + error); +} catch (error) { + if (error instanceof Error || typeof error === "string") setFailed(error); + else setFailed("Unknown error: " + error); }