-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathJira.js
116 lines (96 loc) · 2.4 KB
/
Jira.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const { get } = require('lodash')
const serviceName = 'jira'
const { format } = require('url')
const client = require('./client')(serviceName)
class Jira {
constructor ({ baseUrl, token, email }) {
this.baseUrl = baseUrl
this.token = token
this.email = email
}
async createIssue (body) {
return this.fetch('createIssue',
{ pathname: '/rest/api/2/issue' },
{ method: 'POST', body })
}
async getIssue (issueId, query = {}) {
const { fields = [], expand = [] } = query
try {
const res = await this.fetch('getIssue', {
pathname: `/rest/api/2/issue/${issueId}`,
query: {
fields: fields.join(','),
expand: expand.join(','),
},
})
return res
} catch (error) {
if (get(error, 'res.status') === 404) {
return
}
throw error
}
}
async getIssueTransitions (issueId) {
return this.fetch('getIssueTransitions', {
pathname: `/rest/api/2/issue/${issueId}/transitions`,
}, {
method: 'GET',
})
}
async transitionIssue (issueId, data) {
return this.fetch('transitionIssue', {
pathname: `/rest/api/3/issue/${issueId}/transitions`,
}, {
method: 'POST',
body: data,
})
}
async fetch (apiMethodName,
{ host, pathname, query },
{ method, body, headers = {} } = {}) {
const url = format({
host: host || this.baseUrl,
pathname,
query,
})
if (!method) {
method = 'GET'
}
if (headers['Content-Type'] === undefined) {
headers['Content-Type'] = 'application/json'
}
if (headers.Authorization === undefined) {
headers.Authorization = `Basic ${Buffer.from(`${this.email}:${this.token}`).toString('base64')}`
}
// strong check for undefined
// cause body variable can be 'false' boolean value
if (body && headers['Content-Type'] === 'application/json') {
body = JSON.stringify(body)
}
const state = {
req: {
method,
headers,
body,
url,
},
}
try {
await client(state, `${serviceName}:${apiMethodName}`)
} catch (error) {
const fields = {
originError: error,
source: 'jira',
}
delete state.req.headers
throw Object.assign(
new Error('Jira API error'),
state,
fields
)
}
return state.res.body
}
}
module.exports = Jira