UNPKG

@nu-art/jira

Version:
195 lines (194 loc) 8.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JiraModule = exports.ModuleBE_Jira_Class = void 0; /* * Permissions management system, define access level for each of * your server apis, and restrict users by giving them access levels * * Copyright (C) 2020 Adam van der Kruk aka TacB0sS * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const ts_common_1 = require("@nu-art/ts-common"); const backend_1 = require("@nu-art/thunderstorm/backend"); const thunderstorm_1 = require("@nu-art/thunderstorm"); const utils_1 = require("./utils"); const createFormData = (filename, buffer) => ({ file: { value: buffer, options: { filename } } }); class ModuleBE_Jira_Class extends ts_common_1.Module { constructor() { super(...arguments); this.versions = {}; this.buildHeaders = ({ apiKey, email }, check) => { const headers = { Authorization: `Basic ${Buffer.from(email + ':' + apiKey).toString('base64')}` }; if (!check) { headers['X-Atlassian-Token'] = 'no-check'; headers[thunderstorm_1.HeaderKey_ContentType] = 'multipart/form-data'; } else { headers.Accept = 'application/json'; headers[thunderstorm_1.HeaderKey_ContentType] = 'application/json'; } return headers; }; this.project = { query: async (projectKey) => { if (!this.projects) this.projects = await this.executeGetRequest(`/project`); const project = this.projects.find(_project => _project.key === projectKey); if (!project) throw new ts_common_1.BadImplementationException(`Could not find project: ${projectKey}`); return project; }, }; this.version = { query: async (projectId, versionName) => { if (!this.versions[projectId]) this.versions[projectId] = await this.executeGetRequest(`/project/${projectId}/versions`); return this.versions[projectId].find(version => version.name === versionName); }, create: async (projectId, versionName) => { const version = await this.executePostRequest(`/version`, { projectId, name: versionName }); this.versions[projectId].push(version); return version; } }; this.comment = { add: async (issueKey, comment) => { return this.executePostRequest(`/issue/${issueKey}/comment`, utils_1.JiraUtils.createText(comment)); } }; this.issue = { query: async (query) => { return (await this.executeGetRequest(`/search`, { jql: utils_1.JiraUtils.buildJQL(query) })).issues; }, get: async (issueId) => { return this.executeGetRequest(`/issue/${issueId}`); }, comment: this.comment, create: async (project, issueType, summary, descriptions, label) => { const issue = await this.executePostRequest('/issue', { fields: { project, issuetype: issueType, description: utils_1.JiraUtils.createText(...descriptions), summary, labels: label, assignee: { accountId: this.config.defaultAssignee.accountId } } }); issue.url = `${this.config.baseUrl}/browse/${issue.key}`; return issue; }, update: async (issueKey, fields) => { return this.executePutRequest(`/issue/${issueKey}`, { fields }); }, resolve: async (issueKey, projectKey, versionName, status) => { const project = await exports.JiraModule.project.query(projectKey); let version = await exports.JiraModule.version.query(projectKey, versionName); if (!version) version = await exports.JiraModule.version.create(project.id, versionName); return this.executePutRequest(`/issue/${issueKey}`, { fields: { fixVersions: [{ id: version.id }] } }); }, }; this.getIssueTypes = async (id) => { return this.executeGetRequest('/issue/createmetadata', { projectKeys: id }); }; this.query = async (query) => { return (await this.executeGetRequest(`/search`, { jql: utils_1.JiraUtils.buildJQL(query) })).issues; }; this.getIssueRequest = async (issueId) => { return this.executeGetRequest(`/issue/${issueId}`); }; this.addIssueAttachment = async (issue, file) => { return this.executeFormRequest(`/issue/${issue}/attachments`, file); }; this.executeFormRequest = async (url, buffer) => { const request = { headers: this.headersForm, uri: `${this.restUrl}${url}`, formData: createFormData('logs.zip', buffer), method: thunderstorm_1.HttpMethod.POST, }; return this.executeRequest(request); }; } init() { if (!this.config.baseUrl) throw new ts_common_1.ImplementationMissingException('Missing Jira baseUrl for JiraModule, please add the key baseUrl to the config'); this.restUrl = this.config.baseUrl + '/rest/api/3'; this.logInfo(`Rest URL: ${this.restUrl}`); if (!this.config.auth || !this.config.auth.apiKey || !this.config.auth.email) throw new ts_common_1.ImplementationMissingException('Missing auth config variables for JiraModule'); this.headersJson = this.buildHeaders(this.config.auth, true); this.headersForm = this.buildHeaders(this.config.auth, false); } async executePostRequest(url, body, label) { const request = { headers: this.headersJson, uri: `${this.restUrl}${url}`, body, method: thunderstorm_1.HttpMethod.POST, json: true }; return this.executeRequest(request); } async executePutRequest(url, body) { const request = { headers: this.headersJson, uri: `${this.restUrl}${url}`, body, method: thunderstorm_1.HttpMethod.PUT, json: true }; return this.executeRequest(request); } // async executeGetRequestNew<T>(url: string, _params?: { [k: string]: string }): Promise<T> { // if (!this.restUrl) // throw new ImplementationMissingException('Need a baseUrl'); // // return AxiosHttpModule // .createRequest(HttpMethod.GET, generateHex(8)) // .setOrigin(this.restUrl) // .setUrl(url) // .setUrlParams(_params) // .setHeaders(this.headersJson) // .executeSync(); // } handleResponse(response) { if (`${response.statusCode}`[0] !== '2') throw new ts_common_1.ApiException(response.statusCode, response.body); return response.toJSON().body; } async executeGetRequest(url, _params) { const request = { headers: this.headersJson, uri: `${(0, ts_common_1.composeUrl)(`${this.restUrl}${url}`, _params)}`, method: thunderstorm_1.HttpMethod.GET, json: true }; return this.executeRequest(request); } async executeRequest(request) { const response = await (0, backend_1.promisifyRequest)(request, false); return this.handleResponse(response); } } exports.ModuleBE_Jira_Class = ModuleBE_Jira_Class; exports.JiraModule = new ModuleBE_Jira_Class();