1
|
|
|
const _ = require('lodash') |
2
|
|
|
const async = require('async') |
3
|
|
|
const debug = require('debug')('jira-resource') |
4
|
|
|
const request = require('request') |
5
|
|
|
|
6
|
|
|
const debugResponse = require('./debugResponse.js') |
7
|
|
|
|
8
|
|
|
module.exports = (issue, source, params, callback) => { |
9
|
|
|
if ( !issue ) { |
10
|
|
|
return callback(null) |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
if ( !params.transitions ) { |
14
|
|
|
return callback(null, issue) |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
const transitionUrl = source.url + '/rest/api/2/issue/' + issue.id + '/transitions/' |
18
|
|
|
|
19
|
|
|
async.eachSeries(params.transitions, (nextTransition, next) => { |
20
|
|
|
processTransition(transitionUrl, nextTransition, () => { |
21
|
|
|
next() |
22
|
|
|
}) |
23
|
|
|
}, () => { |
24
|
|
|
callback(null, issue) |
25
|
|
|
}) |
26
|
|
|
|
27
|
|
|
function processTransition (transitionUrl, transitionName, done) { |
|
|
|
|
28
|
|
|
async.waterfall([ |
29
|
|
|
(next) => { |
30
|
|
|
debug('Searching for available transitions...') |
31
|
|
|
|
32
|
|
|
request({ |
33
|
|
|
method: 'GET', |
34
|
|
|
uri: transitionUrl, |
35
|
|
|
auth: { |
36
|
|
|
username: source.username, |
37
|
|
|
password: source.password |
38
|
|
|
}, |
39
|
|
|
json: true |
40
|
|
|
}, (error, response, body) => { |
41
|
|
|
debugResponse(response) |
42
|
|
|
|
43
|
|
|
let transitionId = _.filter(body.transitions, (transition) => { |
44
|
|
|
return transition.name.toLowerCase() == transitionName.toLowerCase() |
45
|
|
|
})[0].id |
46
|
|
|
|
47
|
|
|
next(error, transitionId) |
48
|
|
|
}) |
49
|
|
|
}, |
50
|
|
|
(transitionId, done) => { |
51
|
|
|
debug('Performing transition: %s (%s)', transitionName, transitionId) |
52
|
|
|
|
53
|
|
|
request({ |
54
|
|
|
method: 'POST', |
55
|
|
|
uri: transitionUrl, |
56
|
|
|
auth: { |
57
|
|
|
username: source.username, |
58
|
|
|
password: source.password |
59
|
|
|
}, |
60
|
|
|
json: { |
61
|
|
|
transition: { |
62
|
|
|
id: transitionId |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
}, (error, response) => { |
66
|
|
|
debugResponse(response) |
67
|
|
|
done(error) |
68
|
|
|
}) |
69
|
|
|
} |
70
|
|
|
], done) |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|