From def9d53966e46f1a84db0b629b7e16a093dc2454 Mon Sep 17 00:00:00 2001 From: Timmatt Date: Thu, 3 Sep 2020 18:17:21 +0800 Subject: [PATCH] feat: auto comment and rename title --- dist/index.js | 47 +++++++++++++++++++++++++++++++++++++++++++---- index.js | 41 +++++++++++++++++++++++++++++++++++++---- services/jira.js | 6 ++++++ test/jira.js | 5 +++++ 4 files changed, 91 insertions(+), 8 deletions(-) diff --git a/dist/index.js b/dist/index.js index b37c028..d69eb0d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -513,6 +513,12 @@ class Jira { return assigneeId === reporterId; } + async getIssueSummary(key) { + const { fields: { summary } } = await this.getIssue(key); + + return summary; + } + async putAssignIssue(key, accountId) { return this.request(`/rest/api/3/issue/${key}/assignee`, 'put', { accountId }); } @@ -1842,13 +1848,45 @@ async function main() { // project = key.substring(0, key.indexOf('-')); + let issueTitle; + if (email && token && key) { + issueTitle = await jira.getIssueSummary(key); + } + if (webhook) { if (!key) { core.info('No jira issue detected in PR title/branch'); process.exit(0); } + await request({ url: webhook, method: 'post', data: { issues: [key], pr } }); - await gitService.updatePR({ body: `[${key}](${host}/browse/${key})\n${pr.body}` }); + if (foundInTitle) { + await gitService.updatePR({ + body: `[${key}](${host}/browse/${key})\n${pr.body}${issueTitle ? '' : ` ${issueTitle}`}`, + }); + } else { + // issue name not existed in title, update it + await gitService.updatePR({ + title: `[${key}] ${pr.title}`, + body: `[${key}](${host}/browse/${key})\n${pr.body}${issueTitle ? '' : ` ${issueTitle}`}`, + }); + } + + if (email && token) { + await jira.postComment(key, { + type: 'doc', + version: 1, + content: [ + { + type: 'blockCard', + attrs: { + url: pr.html_url, + }, + }, + ], + }); + } + core.info('webhook complete'); process.exit(0); } @@ -1866,7 +1904,7 @@ async function main() { } } - const body = `${pr.body.slice(0, from + length)}${from === 0 ? '' : ' '}[${key}](${host}/browse/${key})${from === 0 ? '\n' : ''}${pr.body.slice(from + length)}`; + const body = `${pr.body.slice(0, from + length)}${from === 0 ? '' : ' '}[${key}](${host}/browse/${key})${issueTitle ? '' : ` ${issueTitle}`}${from === 0 ? '\n' : ''}${pr.body.slice(from + length)}`; await gitService.updatePR({ body }); core.info('update PR description complete'); @@ -1881,6 +1919,7 @@ async function main() { const issue = await jira.postIssue(pr.title, userId); key = issue.key; + issueTitle = pr.title; if (board) { // move card to active sprint @@ -1928,9 +1967,9 @@ async function main() { }); // update pull request title and desc - const newPR = { body: `[${key}](${host}/browse/${key})\n${pr.body}` }; + const newPR = { body: `[${key}](${host}/browse/${key})\n${pr.body}${issueTitle ? '' : ` ${issueTitle}`}` }; // if title has no jira issue, insert it - if (isCreateIssue) { newPR.title = `[${key}] ${pr.title}`; } + if (isCreateIssue || !foundInTitle) { newPR.title = `[${key}] ${pr.title}`; } await gitService.updatePR(newPR); diff --git a/index.js b/index.js index 7d0ec5d..1c4c18c 100644 --- a/index.js +++ b/index.js @@ -55,13 +55,45 @@ async function main() { // project = key.substring(0, key.indexOf('-')); + let issueTitle; + if (email && token && key) { + issueTitle = await jira.getIssueSummary(key); + } + if (webhook) { if (!key) { core.info('No jira issue detected in PR title/branch'); process.exit(0); } + await request({ url: webhook, method: 'post', data: { issues: [key], pr } }); - await gitService.updatePR({ body: `[${key}](${host}/browse/${key})\n${pr.body}` }); + if (foundInTitle) { + await gitService.updatePR({ + body: `[${key}](${host}/browse/${key})\n${pr.body}${issueTitle ? '' : ` ${issueTitle}`}`, + }); + } else { + // issue name not existed in title, update it + await gitService.updatePR({ + title: `[${key}] ${pr.title}`, + body: `[${key}](${host}/browse/${key})\n${pr.body}${issueTitle ? '' : ` ${issueTitle}`}`, + }); + } + + if (email && token) { + await jira.postComment(key, { + type: 'doc', + version: 1, + content: [ + { + type: 'blockCard', + attrs: { + url: pr.html_url, + }, + }, + ], + }); + } + core.info('webhook complete'); process.exit(0); } @@ -79,7 +111,7 @@ async function main() { } } - const body = `${pr.body.slice(0, from + length)}${from === 0 ? '' : ' '}[${key}](${host}/browse/${key})${from === 0 ? '\n' : ''}${pr.body.slice(from + length)}`; + const body = `${pr.body.slice(0, from + length)}${from === 0 ? '' : ' '}[${key}](${host}/browse/${key})${issueTitle ? '' : ` ${issueTitle}`}${from === 0 ? '\n' : ''}${pr.body.slice(from + length)}`; await gitService.updatePR({ body }); core.info('update PR description complete'); @@ -94,6 +126,7 @@ async function main() { const issue = await jira.postIssue(pr.title, userId); key = issue.key; + issueTitle = pr.title; if (board) { // move card to active sprint @@ -141,9 +174,9 @@ async function main() { }); // update pull request title and desc - const newPR = { body: `[${key}](${host}/browse/${key})\n${pr.body}` }; + const newPR = { body: `[${key}](${host}/browse/${key})\n${pr.body}${issueTitle ? '' : ` ${issueTitle}`}` }; // if title has no jira issue, insert it - if (isCreateIssue) { newPR.title = `[${key}] ${pr.title}`; } + if (isCreateIssue || !foundInTitle) { newPR.title = `[${key}] ${pr.title}`; } await gitService.updatePR(newPR); diff --git a/services/jira.js b/services/jira.js index ac73ca3..f8815af 100644 --- a/services/jira.js +++ b/services/jira.js @@ -146,6 +146,12 @@ class Jira { return assigneeId === reporterId; } + async getIssueSummary(key) { + const { fields: { summary } } = await this.getIssue(key); + + return summary; + } + async putAssignIssue(key, accountId) { return this.request(`/rest/api/3/issue/${key}/assignee`, 'put', { accountId }); } diff --git a/test/jira.js b/test/jira.js index 559ec06..205d4c6 100644 --- a/test/jira.js +++ b/test/jira.js @@ -125,4 +125,9 @@ describe('jira issue', () => { const a = await jira.getIssueAssigneeId(this.issue.key); expect(a).equal(null); }); + + it('title', async () => { + const t = await jira.getIssueSummary(this.issue.key); + expect(t).equal('title of issue'); + }); });