-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
197 lines (166 loc) · 5.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'user strict';
const core = require('@actions/core');
const github = require('@actions/github');
const Github = require('./services/github');
const Jira = require('./services/jira');
const request = require('./services/request');
async function main() {
const githubToken = core.getInput('githubToken', { required: true });
const webhook = core.getInput('webhook');
const host = core.getInput('host', { required: true });
const email = core.getInput('email');
const token = core.getInput('token');
const project = core.getInput('project');
let transition = core.getInput('transition');
const version = core.getInput('version');
const component = core.getInput('component');
const type = core.getInput('type');
const board = core.getInput('board');
const isOnlyTransition = core.getInput('isOnlyTransition').toLowerCase() === 'true';
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 isAddFixVersionOnMerge = core.getInput('isAddFixVersionOnMerge').toLowerCase() === 'true';
const gitService = new Github({ github, githubToken });
const jira = new Jira({
host,
email,
token,
project,
version,
component,
type,
board,
});
const pr = github.context.payload.pull_request;
if (!pr) {
core.setFailed('Only support pull request trigger');
}
// `AB-1234` Jira issue key
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('-'));
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 } });
core.info('webhook complete');
if (pr.merged) {
if (isAddFixVersionOnMerge) {
const versionId = await jira.getVersionIdByPrefix(version);
await jira.putFixVersion(key, versionId);
}
process.exit(0);
}
if (foundInTitle) {
await gitService.updatePR({
body: `[${key}${issueTitle ? `: ${issueTitle}` : ''}](${host}/browse/${key})\n${pr.body}`,
});
} else {
// issue name not existed in title, update it
await gitService.updatePR({
title: `[${key}] ${pr.title}`,
body: `[${key}${issueTitle ? `: ${issueTitle}` : ''}](${host}/browse/${key})\n${pr.body}`,
});
}
if (email && token) {
await jira.postComment(key, {
type: 'doc',
version: 1,
content: [
{
type: 'blockCard',
attrs: {
url: pr.html_url,
},
},
],
});
}
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}${foundInTitle ? `: ${issueTitle}` : ''}](${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');
const userId = await jira.getUserIdByFuzzyName(github.context.actor).catch(core.info);
const issue = await jira.postIssue(pr.title, userId);
key = issue.key;
issueTitle = pr.title;
if (board) {
// move card to active sprint
const { values: [{ id: activeSprintId }] } = await jira.getSprints('active');
await jira.postMoveIssuesToSprint([key], activeSprintId);
}
}
if (!key) {
core.info('No jira issue detected in PR title/branch');
process.exit(0);
}
// transit issue
if (otherAssignedTransition) {
const isMeCreatedIssue = await jira.isMeCreatedIssue(key);
// if issue was assigned by other
if (!isMeCreatedIssue) transition = otherAssignedTransition;
}
if (transition) {
await jira.postTransitIssue(key, transition);
}
if (isOnlyTransition) {
core.info('transit completed');
process.exit(0);
}
if (isAssignToReporter) {
await jira.putAssignIssue(key, await jira.getIssueReporterId(key));
}
// comment on jira with this pr
await jira.postComment(key, {
type: 'doc',
version: 1,
content: [
{
type: 'blockCard',
attrs: {
url: pr.html_url,
},
},
],
});
// update pull request title and desc
const newPR = { body: `[${key}${foundInTitle ? `: ${issueTitle}` : ''}](${host}/browse/${key})\n${pr.body}` };
// if title has no jira issue, insert it
if (isCreateIssue || !foundInTitle) { newPR.title = `[${key}] ${pr.title}`; }
await gitService.updatePR(newPR);
core.info('New issue created');
}
main().catch(core.setFailed);