Skip to content

Commit

Permalink
feat: auto comment and rename title
Browse files Browse the repository at this point in the history
  • Loading branch information
Timmatt-Lee committed Sep 3, 2020
1 parent 9f623a3 commit def9d53
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
47 changes: 43 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
41 changes: 37 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions services/jira.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
5 changes: 5 additions & 0 deletions test/jira.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit def9d53

Please sign in to comment.