From fb2b7be4e75ec495054c5ab830b09e7a6c43a017 Mon Sep 17 00:00:00 2001 From: Timmatt-Lee <43662873+Timmatt-Lee@users.noreply.github.com> Date: Tue, 25 Aug 2020 14:00:50 +0800 Subject: [PATCH] feat: only update desc (#19) * feat: only update desc * 1 * 2 * 2 * 12 * 4 * e * r * r --- .github/workflows/desc.yml | 24 +++++++++++++++++++ README.md | 12 ++++++---- action.yml | 7 ++++++ dist/index.js | 47 ++++++++++++++++++++++++++++---------- index.js | 47 ++++++++++++++++++++++++++++---------- 5 files changed, 108 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/desc.yml diff --git a/.github/workflows/desc.yml b/.github/workflows/desc.yml new file mode 100644 index 0000000..2271dcc --- /dev/null +++ b/.github/workflows/desc.yml @@ -0,0 +1,24 @@ +on: + pull_request: + types: [opened, closed] + branches: + - master + +name: Test Only Update Desc + +jobs: + test: + name: Test Only Update Desc + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@master + + - name: Integration + uses: ./ + with: + host: ${{ secrets.JIRA_BASE_URL }} + githubToken: ${{ secrets.GITHUB_TOKEN }} + isOnlyAppendDesc: true + appendDescAfterRegex: "Jira Issue Link:" + diff --git a/README.md b/README.md index 1ca4ea7..43c96d7 100644 --- a/README.md +++ b/README.md @@ -98,11 +98,13 @@ jobs: githubToken: ${{ secrets.GITHUB_TOKEN }} project: ${{ secrets.JIRA_PROJECT_NAME }} transition: ${{ secrets.JIRA_PR_TRANSITION_NAME }} - type: ${{ secrets.JIRA_ISSUE_TYPE }} # optional, but required if you want to create issue - component: ${{ secrets.JIRA_COMPONENT_NAME }} # optional, created issue property - version: ${{ secrets.JIRA_VERSION_PREFIX }} # optional, created issue property - board: ${{ secrets.JIRA_BOARD_ID }} # optional, sprint detection for created issue - isCreateIssue: true # optional, if you want to auto create issue + type: ${{ secrets.JIRA_ISSUE_TYPE }} # optional, but required if you want to create issue + component: ${{ secrets.JIRA_COMPONENT_NAME }} # optional, created issue property + version: ${{ secrets.JIRA_VERSION_PREFIX }} # optional, created issue property + board: ${{ secrets.JIRA_BOARD_ID }} # optional, sprint detection for created issue + isCreateIssue: true # optional, if you want to auto create issue + isOnlyAppendDesc: true # optional, if you only need update PR description + appendDescAfterRegex: "Jira Issue Link:" # optional, insert link after regex in PR description ``` Create `.github/workflows/merge-jira.yml` diff --git a/action.yml b/action.yml index 964b0a3..e6b64c9 100644 --- a/action.yml +++ b/action.yml @@ -52,6 +52,13 @@ inputs: description: If you want to re-assign the issue to reporter required: false default: false + isOnlyAppendDesc: + description: true if you only want to update PR description + required: false + default: false + appendDescAfterRegex: + description: insert jira issue link after this regex of PR description + required: false runs: using: 'node12' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index 85d382e..d7fec68 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1809,6 +1809,8 @@ async function main() { const isCreateIssue = core.getInput('isCreateIssue').toLowerCase() === 'true'; const otherAssignedTransition = core.getInput('otherAssignedTransition'); const isAssignToReporter = core.getInput('isAssignToReporter').toLowerCase() === 'true'; + const isOnlyAppendDesc = core.getInput('isOnlyAppendDesc').toLowerCase() === 'true'; + const appendDescAfterRegex = core.getInput('appendDescAfterRegex'); const gitService = new Github({ github, githubToken }); @@ -1829,16 +1831,17 @@ async function main() { } // `AB-1234` Jira issue key - let [key] = pr.title.match('\\w+-\\d+'); + const foundInTitle = pr.title.match('\\w+-\\d+'); + let key; + if (foundInTitle) [key] = foundInTitle; + // no key detected in title, find in branch name + if (!key) { + const foundInBranch = pr.head.ref.match('\\w+-\\d+'); + if (foundInBranch)[key] = foundInBranch; + } // project = key.substring(0, key.indexOf('-')); - // if isOnlyTransition or webhook required, but no key detection - if (!key && (isOnlyTransition || webhook)) { - core.info('No jira issue detected in PR title'); - process.exit(0); - } - if (webhook) { await request({ url: webhook, method: 'post', data: { issues: [key], pr } }); await gitService.updatePR({ body: `[${key}](${host}/browse/${key})\n${pr.body}` }); @@ -1846,6 +1849,26 @@ async function main() { process.exit(0); } + if (isOnlyAppendDesc) { + let from = 0; + let length = 0; + if (appendDescAfterRegex) { + from = pr.body.search(appendDescAfterRegex); + if (from === -1) { + from = 0; + } else { + const [word] = pr.body.match(appendDescAfterRegex); + length = word.length; + } + } + + const body = `${pr.body.slice(0, from + length)}${from === 0 ? '' : ' '}[${key}](${host}/browse/${key})${from === 0 ? '\n' : ''}${pr.body.slice(from + length)}`; + + await gitService.updatePR({ body }); + core.info('update PR description complete'); + process.exit(0); + } + if (isCreateIssue) { if (!project) throw new Error('Creating issue need project'); if (!type) throw new Error('Creating issue need type'); @@ -1860,13 +1883,11 @@ async function main() { const { values: [{ id: activeSprintId }] } = await jira.getSprints('active'); await jira.postMoveIssuesToSprint([key], activeSprintId); } - } else { - core.info('Nothing process'); - process.exit(0); } if (!key) { - core.setFailed('Issue key parse error'); + core.info('No jira issue detected in PR title/branch'); + process.exit(0); } // transit issue @@ -1875,7 +1896,9 @@ async function main() { // if issue was assigned by other if (!isMeCreatedIssue) transition = otherAssignedTransition; } - await jira.postTransitIssue(key, transition); + if (transition) { + await jira.postTransitIssue(key, transition); + } if (isOnlyTransition) { core.info('transit completed'); diff --git a/index.js b/index.js index ba7f153..4ba4c18 100644 --- a/index.js +++ b/index.js @@ -22,6 +22,8 @@ async function main() { const isCreateIssue = core.getInput('isCreateIssue').toLowerCase() === 'true'; const otherAssignedTransition = core.getInput('otherAssignedTransition'); const isAssignToReporter = core.getInput('isAssignToReporter').toLowerCase() === 'true'; + const isOnlyAppendDesc = core.getInput('isOnlyAppendDesc').toLowerCase() === 'true'; + const appendDescAfterRegex = core.getInput('appendDescAfterRegex'); const gitService = new Github({ github, githubToken }); @@ -42,16 +44,17 @@ async function main() { } // `AB-1234` Jira issue key - let [key] = pr.title.match('\\w+-\\d+'); + const foundInTitle = pr.title.match('\\w+-\\d+'); + let key; + if (foundInTitle) [key] = foundInTitle; + // no key detected in title, find in branch name + if (!key) { + const foundInBranch = pr.head.ref.match('\\w+-\\d+'); + if (foundInBranch)[key] = foundInBranch; + } // project = key.substring(0, key.indexOf('-')); - // if isOnlyTransition or webhook required, but no key detection - if (!key && (isOnlyTransition || webhook)) { - core.info('No jira issue detected in PR title'); - process.exit(0); - } - if (webhook) { await request({ url: webhook, method: 'post', data: { issues: [key], pr } }); await gitService.updatePR({ body: `[${key}](${host}/browse/${key})\n${pr.body}` }); @@ -59,6 +62,26 @@ async function main() { process.exit(0); } + if (isOnlyAppendDesc) { + let from = 0; + let length = 0; + if (appendDescAfterRegex) { + from = pr.body.search(appendDescAfterRegex); + if (from === -1) { + from = 0; + } else { + const [word] = pr.body.match(appendDescAfterRegex); + length = word.length; + } + } + + const body = `${pr.body.slice(0, from + length)}${from === 0 ? '' : ' '}[${key}](${host}/browse/${key})${from === 0 ? '\n' : ''}${pr.body.slice(from + length)}`; + + await gitService.updatePR({ body }); + core.info('update PR description complete'); + process.exit(0); + } + if (isCreateIssue) { if (!project) throw new Error('Creating issue need project'); if (!type) throw new Error('Creating issue need type'); @@ -73,13 +96,11 @@ async function main() { const { values: [{ id: activeSprintId }] } = await jira.getSprints('active'); await jira.postMoveIssuesToSprint([key], activeSprintId); } - } else { - core.info('Nothing process'); - process.exit(0); } if (!key) { - core.setFailed('Issue key parse error'); + core.info('No jira issue detected in PR title/branch'); + process.exit(0); } // transit issue @@ -88,7 +109,9 @@ async function main() { // if issue was assigned by other if (!isMeCreatedIssue) transition = otherAssignedTransition; } - await jira.postTransitIssue(key, transition); + if (transition) { + await jira.postTransitIssue(key, transition); + } if (isOnlyTransition) { core.info('transit completed');