forked from Broadshield/gajira-find-issue-key
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
426 lines (362 loc) · 15.9 KB
/
action.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
const _ = require('lodash')
const core = require('@actions/core')
const github = require('@actions/github')
const YAML = require('yaml')
const Jira = require('./common/net/Jira')
const J2M = require('./lib/J2M')
const { titleCase } = require('title-case')
const issueIdRegEx = /([a-zA-Z0-9]+-[0-9]+)/g
const startJiraToken = 'JIRA-ISSUE-TEXT-START'
const endJiraToken = 'JIRA-ISSUE-TEXT-END'
const eventTemplates = {
branch: '{{event.ref}}',
commits: "{{event.commits.map(c=>c.message).join(' ')}}",
}
const { context } = github
async function getPreviousReleaseRef(octo) {
if (!context.repository || !octo) {
return
}
const releases = await octo.repos.getLatestRelease({
...context.repo,
})
const {
tag_name
} = releases.payload
return tag_name
}
// function upperCaseFirst(str) {
// return str.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1))
// }
module.exports = class {
constructor({
githubEvent,
argv,
config
}) {
this.Jira = new Jira({
baseUrl: config.baseUrl,
token: config.token,
email: config.email,
})
this.jiraUrl = config.baseUrl
this.J2M = new J2M()
core.debug(`Config found: ${JSON.stringify(config)}`)
core.debug(`Args found: ${JSON.stringify(argv)}`)
this.config = config
this.argv = argv
this.githubEvent = githubEvent || context.payload
this.github = null
this.updatePRTitle = argv.updatePRTitle
this.commitMessageList = null
this.foundKeys = null
this.jiraTransition = null
this.transitionChain = []
if (argv.transitionChain) {
this.transitionChain = argv.transitionChain.split(',')
}
if (context.eventName === 'pull_request') {
if (context.payload.action in ['closed'] && context.payload.pull_request.merged === 'true') {
this.jiraTransition = argv.transitionOnPrMerge
} else if (context.payload.action in ['opened']) {
this.jiraTransition = argv.transitionOnPrOpen
}
} else if (context.eventName === 'pull_request_review') {
if (context.payload.state === 'APPROVED') {
this.jiraTransition = argv.transitionOnPrApproval
}
} else if (context.eventName in ['create']) {
this.jiraTransition = argv.transitionOnNewBranch
}
this.github = github.getOctokit(argv.githubToken) || null
if (Object.prototype.hasOwnProperty.call(githubEvent, 'pull_request')) {
this.headRef = githubEvent.pull_request.head.ref || null
this.baseRef = githubEvent.pull_request.base.ref || null
} else if (Object.prototype.hasOwnProperty.call(githubEvent, 'ref')) {
this.headRef = githubEvent.ref || null
this.baseRef = null
}
if (context.eventName === 'pull_request') {
this.headRef = this.headRef || context.payload.pull_request.head.ref || null
this.baseRef = this.baseRef || context.payload.pull_request.base.ref || null
} else if (context.eventName === 'push') {
if (context.payload.ref.startsWith('refs/tags')) {
this.baseRef = this.baseRef || getPreviousReleaseRef(this.github)
}
this.headRef = this.headRef || context.payload.ref || null
}
this.headRef = argv.headRef || this.headRef || null
this.baseRef = argv.baseRef || this.baseRef || null
}
async updateStringByToken(startToken, endToken, fullText, insertText) {
const regex = new RegExp(
`(?<start>\\[\\/]: \\/ "${startToken}"\\n)(?<text>(?:.|\\s)+)(?<end>\\n\\[\\/]: \\/ "${endToken}"(?:\\s)?)`,
'gm',
)
if (regex.test(fullText)) {
return fullText.replace(regex, `$1${insertText}$3`)
}
const possibleFullText = fullText || ''
return `${possibleFullText.trim()}\n\n[/]: / "${startToken}"\n${insertText}\n[/]: / "${endToken}"`
}
async updatePullRequestBody(startToken, endToken) {
if (!this.githubEvent.pull_request && !context.payload.pull_request) {
core.debug(
`Skipping pull request update, pull_request not found in current github context, or received event`,
)
return
}
const issues = await this.formattedIssueList()
const text = `### Jira Issues:\n\n${issues}\n`
const { number, body, title } = this.githubEvent.pull_request || context.payload.pull_request
core.debug(`Updating PR number ${number}`)
core.debug(`With text:\n ${text}`)
let newTitle = title.trim()
core.debug(`Should update PR title: ${this.updatePRTitle}`)
if (this.updatePRTitle) {
core.debug(`Current PR Title: ${title}`)
const issueKeys = this.foundKeys.map((a) => a.get('key'))
if (Array.isArray(issueKeys)) {
try {
const re = /(?:\[)?(?<issues>(?:(?:[\w]{2,8})(?:[-_ ])(?:[\d]{3,5})(?:[, ]+)?)+)(?:\(\+\d*\))?(?:[-:_ \]]+)(?<title>.*)?/
const matches = newTitle.match(re)
let groupTitle = newTitle
if (matches) {
const groups = matches.groups
groupTitle = (groups.title || '').trim()
core.debug(`The title match found: ${YAML.stringify(groups)}`)
}
const branchRe = /(?<branch>(feat|bug|chore|task)\/hlw[-|\s][\d]*\/)/i
core.debug('Checking for branch matches...')
const branchMatches = newTitle.match(branchRe)
if (branchMatches) {
groupTitle = groupTitle.replace(branchMatches.groups.branch, '')
core.debug(`Branch match found: ${YAML.stringify(branchMatches.groups)}`)
}
if (issueKeys.length > 2) {
newTitle = `${issueKeys.slice(0, 1).join(', ')} (+${issueKeys.length - 1}): ${titleCase(groupTitle)}`.slice(0, 71)
}
else {
newTitle = `${issueKeys.join(', ')}: ${titleCase(groupTitle)}`.slice(0, 71)
}
core.setOutput('title', `${titleCase(groupTitle)}`)
} catch (error) {
core.warning("catch 1 >>")
core.warning(error)
core.warning("<< catch 1")
}
}
}
if (issues) {
const bodyUpdate = await this.updateStringByToken(startToken, endToken, body, text)
await this.github.pulls.update({
...context.repo,
title: newTitle,
body: bodyUpdate,
pull_number: number,
})
}
}
async getJiraKeysFromGitRange() {
let match = null
if (!(this.baseRef && this.headRef)) {
core.debug('Base ref and head ref not found')
return
}
core.debug(`Getting list of github commits between ${this.baseRef} and ${this.headRef}`)
// This will work fine up to 250 commit messages
const commits = await this.github.repos.compareCommits({
...context.repo,
base: this.baseRef,
head: this.headRef,
})
if (!commits || !commits.data) {
return
}
const fullArray = []
const {
title
} = this.githubEvent.pull_request || context.payload.pull_request
if (title) {
match = title.match(issueIdRegEx)
if (match) {
for (const issueKey of match) {
fullArray.push(issueKey)
}
}
}
match = this.headRef.match(issueIdRegEx)
if (match) {
for (const issueKey of match) {
fullArray.push(issueKey)
}
}
for (const item of commits.data.commits) {
if (item.commit && item.commit.message) {
match = item.commit.message.match(issueIdRegEx)
if (match) {
let skipCommit = false
if (
item.commit.message.startsWith('Merge branch') ||
item.commit.message.startsWith('Merge pull')
) {
if (!this.argv.includeMergeMessages) {
skipCommit = true
}
}
if (skipCommit === false) {
for (const issueKey of match) {
fullArray.push(issueKey)
}
}
}
}
}
// Make the array Unique
const uniqueKeys = [...new Set(fullArray.map((a) => a.toUpperCase()))]
core.debug(`Unique Keys: ${uniqueKeys}\n`)
// Verify that the strings that look like key match real Jira keys
this.foundKeys = []
for (const issueKey of uniqueKeys) {
// Version 3 includes Sprint information, but description is in Atlassian Document Format
// Which is used only by atlassian, and we need a converter to Markdown.
// Version 2 uses Atlassian RichText for its Descriptions,
// and this can be converted to Markdown
// TODO: Harass Atlassian about conversion between their own products
const issue = await this.Jira.getIssue(issueKey, {}, '3')
const issueV2 = await this.Jira.getIssue(issueKey, {
fields: ['description', 'sprint']
}, '2')
const issueObject = new Map()
if (issue) {
core.debug(`Issue ${issue.key}: \n${YAML.stringify(issue)}`)
issueObject.set('key', issue.key)
try {
issueObject.set('projectName', issue.fields.project.name)
core.debug(`Jira ${issue.key} project name: ${issue.fields.project.name}`)
issueObject.set('projectKey', issue.fields.project.key)
core.debug(`Jira ${issue.key} project key: ${issue.fields.project.key}`)
issueObject.set('priority', issue.fields.priority.name)
core.debug(`Jira ${issue.key} priority: ${issue.fields.priority.name}`)
issueObject.set('status', issue.fields.status.name)
core.debug(`Jira ${issue.key} status: ${issue.fields.status.name}`)
issueObject.set('statusCategory', issue.fields.status.statusCategory.name)
core.debug(`Jira ${issue.key} statusCategory: ${issue.fields.status.statusCategory.name}`)
if (Array.isArray(issue.fields.customfield_11306)) {
// Assigned to
core.debug(
`Jira ${issue.key} displayName: ${issue.fields.customfield_11306[0].displayName}`,
)
}
issueObject.set('summary', issue.fields.summary)
core.debug(`Jira ${issue.key} summary: ${issue.fields.summary}`)
if (issueV2.fields.description) {
issueObject.set('descriptionJira', issueV2.fields.description)
issueObject.set('description', this.J2M.toM(issueV2.fields.description))
}
if (issue.fields.sprint) {
issueObject.set('sprint', issue.fields.sprint.name)
issueObject.set('duedate', issue.fields.sprint.endDate)
core.debug(`Jira ${issue.key} sprint: \n${YAML.stringify(issue.fields.sprint)}`)
}
if (issueV2.fields.sprint) {
issueObject.set('sprint', issueV2.fields.sprint.name)
issueObject.set('duedate', issueV2.fields.sprint.endDate)
core.debug(`Jira ${issue.key} sprint: \n${YAML.stringify(issueV2.fields.sprint)}`)
}
// issue.fields.comment.comments[]
// issue.fields.worklog.worklogs[]
} finally {
this.foundKeys.push(issueObject)
}
}
}
core.debug(`Found Jira Keys : ${this.foundKeys.map((a) => a.get('key'))}\n`)
core.debug(`Found GitHub Keys: ${this.foundKeys.map((a) => a.get('ghNumber'))}\n`)
return this.foundKeys
}
async transitionIssues() {
for (const a of this.foundKeys) {
const issueId = a.get('key')
if (this.jiraTransition && this.transitionChain) {
const {
transitions
} = await this.Jira.getIssueTransitions(issueId)
const idxJT = this.transitionChain.indexOf(this.jiraTransition)
for (let i = 0; i < idxJT; i++) {
const link = this.transitionChain[i]
const transitionToApply = _.find(transitions, (t) => {
if (t.id === link) return true
if (t.name.toLowerCase() === link.toLowerCase()) return true
})
if (transitionToApply) {
core.info(`Applying transition:${JSON.stringify(transitionToApply, null, 4)}`)
await this.Jira.transitionIssue(issueId, {
transition: {
id: transitionToApply.id,
},
})
}
}
}
const transitionedIssue = await this.Jira.getIssue(issueId)
const statusName = _.get(transitionedIssue, 'fields.status.name')
core.debug(`Jira ${issueId} status is: ${statusName}.`)
core.debug(`Link to issue: ${this.config.baseUrl}/browse/${issueId}`)
a.set('status', statusName)
}
}
async formattedIssueList() {
return this.foundKeys
.map(
(a) =>
`* **[${a.get('key')}](${this.jiraUrl}/browse/${a.get('key')})** [${a.get(
'status',
'Jira Status Unknown',
)}] ${a.get('summary')}`,
)
.join('\n')
}
async outputReleaseNotes() {
const issues = await this.formattedIssueList()
core.setOutput('notes', `### Release Notes:\n\n${issues}`)
}
async execute() {
const issues = await this.getJiraKeysFromGitRange()
if (issues) {
await this.transitionIssues()
await this.updatePullRequestBody(startJiraToken, endJiraToken)
await this.outputReleaseNotes()
return issues
}
const template = eventTemplates[this.argv.from] || this.argv._.join(' ')
const extractString = this.preprocessString(template)
if (!extractString) {
core.warning(`This github event is not compatible with this usage.`)
return
}
const match = extractString.match(issueIdRegEx)
if (!match) {
core.warning(`String "${extractString}" does not contain issueKeys`)
return
}
for (const issueKey of match) {
const issue = await this.Jira.getIssue(issueKey)
if (issue) {
core.debug(`Jira issue: ${JSON.stringify(issue)}`)
return new Map(['key', issue.key])
}
}
}
preprocessString(str) {
try {
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g
const tmpl = _.template(str)
return tmpl({
event: this.githubEvent
})
} catch (error) {
core.error(error)
}
}
}