forked from atlassian/gajira-transition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.js
93 lines (77 loc) · 2.62 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
const _ = require('lodash')
const Jira = require('./common/net/Jira')
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 () {
const { argv } = this
const issueId = argv.issue
const origIssue = await this.Jira.getIssue(issueId, {fields: ["project"]})
const projectId = _.get(origIssue, 'fields.project.id')
if (argv.fixVersion != undefined && argv.fixVersion != "") {
const versions = await this.Jira.getProjectVersions(projectId)
let versionToApply = _.find(versions, (v) => {
if (v.name.toLowerCase() === argv.fixVersion.toLowerCase()) return true
})
if (versionToApply) {
console.log("Version found.")
}
else {
console.log("Version not found, creating a new one.")
versionToApply = await this.Jira.createVersion({
archived: false,
name: argv.fixVersion,
projectId: projectId,
released: false
})
}
}
const { transitions } = await this.Jira.getIssueTransitions(issueId)
const transitionToApply = _.find(transitions, (t) => {
if (t.id === argv.transitionId) return true
if (t.name.toLowerCase() === argv.transition.toLowerCase()) return true
})
if (!transitionToApply) {
console.log('Please specify transition name or transition id.')
console.log('Possible transitions:')
transitions.forEach((t) => {
console.log(`{ id: ${t.id}, name: ${t.name} } transitions issue to '${t.to.name}' status.`)
})
return
}
console.log(`Selected transition:${JSON.stringify(transitionToApply, null, 4)}`)
let updatePayload = null
if (argv.fixVersion != undefined && argv.fixVersion != "") {
updatePayload = {
update: {
fixVersions: [
{
add: {
name: argv.fixVersion
}
}
]
}
}
}
await this.Jira.transitionIssue(issueId, Object.assign({
transition: {
id: transitionToApply.id,
}
},
updatePayload))
const transitionedIssue = await this.Jira.getIssue(issueId)
// console.log(`transitionedIssue:${JSON.stringify(transitionedIssue, null, 4)}`)
console.log(`Changed ${issueId} status to : ${_.get(transitionedIssue, 'fields.status.name')} .`)
console.log(`Link to issue: ${this.config.baseUrl}/browse/${issueId}`)
return {}
}
}