@intuitionrobotics/jira
Version:
184 lines • 9.19 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JiraModule = exports.JiraModule_Class = void 0;
const ts_common_1 = require("@intuitionrobotics/ts-common");
const backend_1 = require("@intuitionrobotics/thunderstorm/backend");
const thunderstorm_1 = require("@intuitionrobotics/thunderstorm");
const utils_1 = require("./utils");
class JiraModule_Class extends ts_common_1.Module {
constructor() {
super("JiraModule");
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['Content-Type'] = 'multipart/form-data';
}
else {
headers.Accept = 'application/json';
headers['Content-Type'] = 'application/json';
}
return headers;
};
this.project = {
query: (projectKey) => __awaiter(this, void 0, void 0, function* () {
if (!this.projects)
this.projects = yield 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: (projectId, versionName) => __awaiter(this, void 0, void 0, function* () {
if (!this.versions[projectId])
this.versions[projectId] = yield this.executeGetRequest(`/project/${projectId}/versions`);
return this.versions[projectId].find(v => v.name === versionName);
}),
create: (projectId, versionName) => __awaiter(this, void 0, void 0, function* () {
const version = yield this.executePostRequest(`/version`, {
projectId,
name: versionName
});
this.versions[projectId].push(version);
return version;
})
};
this.comment = {
add: (issueKey, comment) => __awaiter(this, void 0, void 0, function* () {
return this.executePostRequest(`/issue/${issueKey}/comment`, (0, utils_1.createText)(comment));
})
};
this.issue = {
query: (query) => __awaiter(this, void 0, void 0, function* () {
return (yield this.executeGetRequest(`/search`, { jql: (0, utils_1.buildJQL)(query) })).issues;
}),
get: (issueId) => __awaiter(this, void 0, void 0, function* () {
return this.executeGetRequest(`/issue/${issueId}`);
}),
comment: this.comment,
create: (project, issueType, summary, descriptions, label, extraFields) => __awaiter(this, void 0, void 0, function* () {
const issue = yield this.executePostRequest('/issue', {
fields: Object.assign({ project, issuetype: issueType, description: (0, utils_1.createText)(...descriptions), summary, labels: label || [], assignee: {
accountId: this.config.defaultAssignee.accountId
} }, extraFields)
});
issue.url = `${this.config.baseUrl}/browse/${issue.key}`;
return issue;
}),
update: (issueKey, fields) => __awaiter(this, void 0, void 0, function* () {
return this.executePutRequest(`/issue/${issueKey}`, { fields });
}),
resolve: (issueKey, projectKey, versionName, status) => __awaiter(this, void 0, void 0, function* () {
const project = yield exports.JiraModule.project.query(projectKey);
let version = yield exports.JiraModule.version.query(projectKey, versionName);
if (!version)
version = yield exports.JiraModule.version.create(project.id, versionName);
return this.executePutRequest(`/issue/${issueKey}`, { fields: { fixVersions: [{ id: version.id }] } });
})
};
this.getIssueTypes = (id) => __awaiter(this, void 0, void 0, function* () {
return this.executeGetRequest('/issue/createmetadata', { projectKeys: id });
});
this.query = (query) => __awaiter(this, void 0, void 0, function* () {
return (yield this.executeGetRequest(`/search`, { jql: (0, utils_1.buildJQL)(query) })).issues;
});
this.getIssueRequest = (issueId) => __awaiter(this, void 0, void 0, function* () {
return this.executeGetRequest(`/issue/${issueId}`);
});
this.addIssueAttachment = (issue, file) => __awaiter(this, void 0, void 0, function* () {
return this.executeFormRequest(`/issue/${issue}/attachments`, file);
});
this.executeFormRequest = (url, buffer) => __awaiter(this, void 0, void 0, function* () {
const request = {
headers: this.getHeadersForm(),
url: `${this.getRestUrl()}${url}`,
data: (0, utils_1.createFormData)('logs.zip', buffer),
method: thunderstorm_1.HttpMethod.POST
};
return this.executeRequest(request);
});
}
getHeadersJson() {
if (!this.config.auth || !this.config.auth.apiKey || !this.config.auth.email)
throw new ts_common_1.ImplementationMissingException('Missing auth config variables for JiraModule');
return this.buildHeaders(this.config.auth, true);
}
getHeadersForm() {
if (!this.config.auth || !this.config.auth.apiKey || !this.config.auth.email)
throw new ts_common_1.ImplementationMissingException('Missing auth config variables for JiraModule');
return this.buildHeaders(this.config.auth, false);
}
getRestUrl() {
if (!this.config.baseUrl)
throw new ts_common_1.ImplementationMissingException("Missing Jira baseUrl for JiraModule, please add the key baseUrl to the config");
return this.config.baseUrl + '/rest/api/3';
}
executePostRequest(url, body, label) {
return __awaiter(this, void 0, void 0, function* () {
const request = {
headers: this.getHeadersJson(),
url: `${this.getRestUrl()}${url}`,
data: body,
method: thunderstorm_1.HttpMethod.POST,
responseType: 'json'
};
return this.executeRequest(request);
});
}
executePutRequest(url, body) {
return __awaiter(this, void 0, void 0, function* () {
const request = {
headers: this.getHeadersJson(),
url: `${this.getRestUrl()}${url}`,
data: body,
method: thunderstorm_1.HttpMethod.PUT,
responseType: 'json'
};
return this.executeRequest(request);
});
}
executeGetRequest(url, _params) {
return __awaiter(this, void 0, void 0, function* () {
const params = _params && Object.keys(_params).map((key) => {
return `${key}=${_params[key]}`;
});
let urlParams = "";
if (params && params.length > 0)
urlParams = `?${params.join("&")}`;
const request = {
headers: this.getHeadersJson(),
url: `${this.getRestUrl()}${url}${urlParams}`,
method: thunderstorm_1.HttpMethod.GET,
responseType: "json"
};
return this.executeRequest(request);
});
}
executeRequest(request) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield (0, backend_1.promisifyRequest)(request);
const statusCode = response.status;
// TODO: need to handle 1XX and 3XX
if (statusCode < 200 || statusCode >= 300)
throw new backend_1.ApiException(statusCode, response.data);
return response.data;
});
}
}
exports.JiraModule_Class = JiraModule_Class;
exports.JiraModule = new JiraModule_Class();
//# sourceMappingURL=JiraModule.js.map