Passed
Branch qa (4c4729)
by André
02:10
created

T_CONST ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
const _ = require('lodash')
0 ignored issues
show
Unused Code introduced by
The constant _ seems to be never used. Consider removing it.
Loading history...
2
const debug = require('debug')('jira-resource')
3
const moment = require('moment')
0 ignored issues
show
Unused Code introduced by
The constant moment seems to be never used. Consider removing it.
Loading history...
4
const request = require('request')
0 ignored issues
show
Unused Code introduced by
The constant request seems to be never used. Consider removing it.
Loading history...
5
6
const debugResponse = require('./debugResponse.js')
0 ignored issues
show
Unused Code introduced by
The constant debugResponse seems to be never used. Consider removing it.
Loading history...
7
const replaceTextFileString = require('./replaceTextFileString.js')
0 ignored issues
show
Unused Code introduced by
The constant replaceTextFileString seems to be never used. Consider removing it.
Loading history...
8
9
module.exports = (baseFileDir, existingIssue, source, params, callback) => {
10
11
    if ( existingIssue ) {
12
        return updateIssue((error) => {
13
            callback(error, existingIssue)
14
        })
15
    }
16
17
    return createIssue((error, newIssue) => {
18
        callback(error, newIssue)
19
    })
20
21
    function createIssue (done) {
0 ignored issues
show
Bug introduced by
The function createIssue is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var createIssue = function() { /* ... */ }; instead.
Loading history...
22
        debug('Issue doesn\'t exist, creating new issue...')
23
24
        return requestIssue(source.url + '/rest/api/2/issue/', 'POST', (error, response, body) => {
25
            if ( !error && !body ) {
26
                return done(new Error('Could not create issue.'))
27
            }
28
29
            done(error, body)
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
30
        })
31
    }
32
33
    function updateIssue (done) {
0 ignored issues
show
Bug introduced by
The function updateIssue is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var updateIssue = function() { /* ... */ }; instead.
Loading history...
34
        let issueId = existingIssue.id
35
        let issueKey = existingIssue.key
36
37
        debug('Issue exists [%s], updating issue...', issueKey)
38
39
        return requestIssue(source.url + '/rest/api/2/issue/' + issueId, 'PUT', done)
40
    }
41
42
    function requestIssue (issueUrl, method, callback) {
0 ignored issues
show
Bug introduced by
The function requestIssue is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var requestIssue = function() { /* ... */ }; instead.
Loading history...
43
44
        let issue = {
45
            fields: processFields()
46
        }
47
48
        debug('Sending issue: %s', JSON.stringify(issue, null, 2))
49
50
        request({
51
            method: method,
52
            uri:    issueUrl,
53
            auth:   {
54
                username: source.username,
55
                password: source.password
56
            },
57
            json:   issue
58
        }, (error, response, body) => {
59
            if ( error ) {
60
                return callback(error)
61
            }
62
63
            debugResponse(response)
64
65
            if ( response.statusCode < 200 || 300 <= response.statusCode ) {
66
                return callback(new Error('Could not update Jira.'))
67
            }
68
69
            callback(error, response, body)
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
70
        })
71
    }
72
73
    function processFields () {
0 ignored issues
show
Bug introduced by
The function processFields is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var processFields = function() { /* ... */ }; instead.
Loading history...
74
        let fields = params.fields || {}
75
76
        fields.summary = params.summary
77
78
        fields = _.merge(parseCustomFields(params), fields)
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
79
80
        fields = _(fields)
81
            .mapValues((value) => {
82
                return replaceTextFileString(baseFileDir, value)
83
            })
84
            .mapValues(replaceNowString)
85
            .value()
86
87
        fields.project = { key: source.project }
88
89
        if ( params.issue_type ) {
90
            fields.issuetype = { name: params.issue_type }
91
        }
92
        if ( params.parent ) {
93
            fields.parent = { key: params.parent }
94
        }
95
96
        return fields
97
    }
98
99
    function replaceNowString (value) {
0 ignored issues
show
Bug introduced by
The function replaceNowString is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var replaceNowString = function() { /* ... */ }; instead.
Loading history...
100
        value = String(value)
101
102
        return value.replace(/\$NOW([-+][0-9]+)?([ywdhms])?/, (match, change, unit) => {
103
            let date = moment()
104
105
            unit = unit || 'm'
106
107
            if ( change ) {
108
                date = date.add(change, unit)
109
            }
110
111
            return date.format()
112
        })
113
    }
114
115
    function parseCustomFields (params) {
0 ignored issues
show
Bug introduced by
The function parseCustomFields is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var parseCustomFields = function() { /* ... */ }; instead.
Loading history...
116
        if ( !params.custom_fields ) {
117
            return {}
118
        }
119
120
        return _(params.custom_fields)
121
            .mapKeys((value) => {
122
                return 'customfield_' + value.id
123
            })
124
            .mapValues((value) => {
125
                return value.value
126
            })
127
            .value()
128
    }
129
}
130