|
| 1 | +const { get } = require('lodash') |
| 2 | + |
| 3 | +const serviceName = 'jira' |
| 4 | +const { format } = require('url') |
| 5 | +const client = require('./client')(serviceName) |
| 6 | + |
| 7 | +class Jira { |
| 8 | + constructor ({ baseUrl, token, email }) { |
| 9 | + this.baseUrl = baseUrl |
| 10 | + this.token = token |
| 11 | + this.email = email |
| 12 | + } |
| 13 | + |
| 14 | + async getIssue (issueId, query = {}) { |
| 15 | + const { fields = [], expand = [] } = query |
| 16 | + |
| 17 | + try { |
| 18 | + return this.fetch('getIssue', { |
| 19 | + pathname: `/rest/api/2/issue/${issueId}`, |
| 20 | + query: { |
| 21 | + fields: fields.join(','), |
| 22 | + expand: expand.join(','), |
| 23 | + }, |
| 24 | + }) |
| 25 | + } catch (error) { |
| 26 | + if (get(error, 'res.status') === 404) { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + throw error |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + async getIssueTransitions (issueId) { |
| 35 | + return this.fetch('getIssueTransitions', { |
| 36 | + pathname: `/rest/api/2/issue/${issueId}/transitions`, |
| 37 | + }, { |
| 38 | + method: 'GET', |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + async transitionIssue (issueId, data) { |
| 43 | + return this.fetch('transitionIssue', { |
| 44 | + pathname: `/rest/api/2/issue/${issueId}/transitions`, |
| 45 | + }, { |
| 46 | + method: 'POST', |
| 47 | + body: data, |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + async fetch (apiMethodName, |
| 52 | + { host, pathname, query }, |
| 53 | + { method, body, headers = {} } = {}) { |
| 54 | + const url = format({ |
| 55 | + host: host || this.baseUrl, |
| 56 | + pathname, |
| 57 | + query, |
| 58 | + }) |
| 59 | + |
| 60 | + if (!method) { |
| 61 | + method = 'GET' |
| 62 | + } |
| 63 | + |
| 64 | + if (headers['Content-Type'] === undefined) { |
| 65 | + headers['Content-Type'] = 'application/json' |
| 66 | + } |
| 67 | + |
| 68 | + if (headers.Authorization === undefined) { |
| 69 | + headers.Authorization = `Basic ${Buffer.from(`${this.email}:${this.token}`).toString('base64')}` |
| 70 | + } |
| 71 | + |
| 72 | + // strong check for undefined |
| 73 | + // cause body variable can be 'false' boolean value |
| 74 | + if (body && headers['Content-Type'] === 'application/json') { |
| 75 | + body = JSON.stringify(body) |
| 76 | + } |
| 77 | + |
| 78 | + const state = { |
| 79 | + req: { |
| 80 | + method, |
| 81 | + headers, |
| 82 | + body, |
| 83 | + url, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + await client(state, `${serviceName}:${apiMethodName}`) |
| 89 | + } catch (error) { |
| 90 | + const fields = { |
| 91 | + originError: error, |
| 92 | + source: 'jira', |
| 93 | + } |
| 94 | + |
| 95 | + delete state.req.headers |
| 96 | + |
| 97 | + throw Object.assign( |
| 98 | + new Error('Jira API error'), |
| 99 | + state, |
| 100 | + fields |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + return state.res.body |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +module.exports = Jira |
0 commit comments