Passed
Push — master ( 2fc875...74c3e1 )
by André
45s
created

src/processTransitions.js   A

Size

Lines of Code 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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