Issues (15)

src/createOrUpdateIssue.js (7 issues)

1
'use strict'
2
3
const _ = require('lodash')
4
const debug = require('debug')('jira-resource')
5
const moment = require('moment')
6
const request = require('request')
7
8
const debugResponse = require('./debugResponse.js')
9
const replaceTextFileString = require('./replaceTextFileString.js')
10
11
module.exports = (baseFileDir, existingIssue, source, params, callback) => {
12
13
    if ( existingIssue ) {
14
        return updateIssue((error) => {
15
            callback(error, existingIssue)
16
        })
17
    }
18
19
    return createIssue((error, newIssue) => {
20
        callback(error, newIssue)
21
    })
22
23
    function createIssue (done) {
0 ignored issues
show
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...
24
        debug('Issue doesn\'t exist, creating new issue...')
25
26
        return requestIssue(source.url + '/rest/api/2/issue/', 'POST', (error, response, body) => {
27
            if ( !error && !body ) {
28
                return done(new Error('Could not create issue.'))
29
            }
30
31
            done(error, body)
32
        })
33
    }
34
35
    function updateIssue (done) {
0 ignored issues
show
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...
36
        let issueId = existingIssue.id
37
        let issueKey = existingIssue.key
38
39
        debug('Issue exists [%s], updating issue...', issueKey)
40
41
        return requestIssue(source.url + '/rest/api/2/issue/' + issueId, 'PUT', done)
42
    }
43
44
    function requestIssue (issueUrl, method, callback) {
0 ignored issues
show
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...
45
46
        let issue = {
47
            fields: processFields()
48
        }
49
50
        debug('Sending issue: %s', JSON.stringify(issue, null, 2))
51
52
        request({
53
            method: method,
54
            uri:    issueUrl,
55
            auth:   {
56
                username: source.username,
57
                password: source.password
58
            },
59
            json:   issue
60
        }, (error, response, body) => {
61
            if ( error ) {
62
                return callback(error)
63
            }
64
65
            debugResponse(response)
66
67
            if ( response.statusCode < 200 || 300 <= response.statusCode ) {
68
                return callback(new Error('Could not update Jira.'))
69
            }
70
71
            callback(error, response, body)
72
        })
73
    }
74
75
    function processFields () {
0 ignored issues
show
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...
76
        let fields = params.fields || {}
77
78
        fields.summary = params.summary
79
80
        fields = _.merge(parseCustomFields(params), fields)
0 ignored issues
show
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...
81
82
        fields = _(fields)
83
            .mapValues((value) => {
84
                return replaceTextFileString(baseFileDir, value)
85
            })
86
            .mapValues(replaceNowString)
87
            .value()
88
89
        fields.project = { key: source.project }
90
91
        if ( params.issue_type ) {
92
            fields.issuetype = { name: params.issue_type }
93
        }
94
95
        return fields
96
    }
97
98
    function replaceNowString (value) {
0 ignored issues
show
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...
99
        value = String(value)
100
101
        return value.replace(/\$NOW([-+][0-9]+)?([ywdhms])?/, (match, change, unit) => {
102
            let date = moment()
103
104
            unit = unit || 'm'
105
106
            if ( change ) {
107
                date = date.add(change, unit)
108
            }
109
110
            return date.format()
111
        })
112
    }
113
114
    function parseCustomFields (params) {
0 ignored issues
show
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...
115
        if ( !params.custom_fields ) {
116
            return {}
117
        }
118
119
        return _(params.custom_fields)
120
            .mapKeys((value) => {
121
                return 'customfield_' + value.id
122
            })
123
            .mapValues((value) => {
124
                return value.value
125
            })
126
            .value()
127
    }
128
}
129