forked from atlassian/gajira-find-issue-key
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.js
69 lines (52 loc) · 1.5 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
const _ = require('lodash')
const Jira = require('./common/net/Jira')
const issueIdRegEx = /([a-zA-Z0-9]+-[0-9]+)/g
const eventTemplates = {
branch: '{{event.ref}}',
commits: "{{event.commits.map(c=>c.message).join(' ')}}",
}
module.exports = class {
constructor ({ githubEvent, argv, config }) {
this.Jira = new Jira({
baseUrl: config.baseUrl,
token: config.token,
email: config.email,
})
this.config = config
this.argv = argv
this.githubEvent = githubEvent
}
async execute () {
if (this.argv.string) {
const foundIssue = await this.findIssueKeyIn(this.argv.string)
if (foundIssue) return foundIssue
}
if (this.argv.from) {
const template = eventTemplates[this.argv.from]
if (template) {
const searchStr = this.preprocessString(template)
const foundIssue = await this.findIssueKeyIn(searchStr)
if (foundIssue) return foundIssue
}
}
}
async findIssueKeyIn (searchStr) {
const match = searchStr.match(issueIdRegEx)
console.log(`Searching in string: \n ${searchStr}`)
if (!match) {
console.log(`String does not contain issueKeys`)
return
}
for (const issueKey of match) {
const issue = await this.Jira.getIssue(issueKey)
if (issue) {
return { issue: issue.key }
}
}
}
preprocessString (str) {
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g
const tmpl = _.template(str)
return tmpl({ event: this.githubEvent })
}
}