UNPKG

@xray-app/xray-automation

Version:

Library for uploading test results to Xray Test Management

299 lines (298 loc) 12.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.XrayDatacenterClient = void 0; const axios_1 = __importDefault(require("axios")); const fs_1 = __importDefault(require("fs")); const form_data_1 = __importDefault(require("form-data")); const xray_error_response_1 = __importDefault(require("./xray-error-response")); // import XrayDatacenterResponseV1 from './xray-datacenter-response-v1'; const xray_datacenter_response_v2_1 = __importDefault(require("./xray-datacenter-response-v2")); const index_1 = require("./index"); class XrayDatacenterClient { constructor(xraySettings) { this.xraySettings = xraySettings; this.supportedFormats = [ index_1.XRAY_FORMAT, index_1.JUNIT_FORMAT, index_1.TESTNG_FORMAT, index_1.ROBOT_FORMAT, index_1.NUNIT_FORMAT, index_1.XUNIT_FORMAT, index_1.CUCUMBER_FORMAT, index_1.BEHAVE_FORMAT, ]; this.jiraBaseUrl = xraySettings.jiraBaseUrl; this.jiraUsername = xraySettings.jiraUsername; this.jiraPassword = xraySettings.jiraPassword; this.jiraToken = xraySettings.jiraToken; if (xraySettings.timeout !== undefined) this.timeout = xraySettings.timeout; else this.timeout = 50000; axios_1.default.defaults.timeout = this.timeout; } async submitResults(reportPath, config) { if (config.format === undefined) throw new xray_error_response_1.default("ERROR: format must be specified"); if (!this.supportedFormats.includes(config.format)) throw new xray_error_response_1.default("ERROR: unsupported format " + config.format); let endpointUrl; if (config.format === index_1.XRAY_FORMAT) { endpointUrl = this.jiraBaseUrl + "/rest/raven/2.0/import/execution"; } else { endpointUrl = this.jiraBaseUrl + "/rest/raven/2.0/import/execution/" + config.format; } let authorizationHeaderValue; if (this.jiraToken !== undefined) { authorizationHeaderValue = "Bearer " + this.jiraToken; } else { authorizationHeaderValue = "Basic " + Buffer.from(this.jiraUsername + ":" + this.jiraPassword).toString("base64"); } let reportContent; try { reportContent = fs_1.default.readFileSync(reportPath).toString(); } catch (error) { throw new xray_error_response_1.default(error.message); } // for xray, cucumber and behave reports send the report directly on the body if ([index_1.XRAY_FORMAT, index_1.CUCUMBER_FORMAT, index_1.BEHAVE_FORMAT].includes(config.format)) { // use a CancelToken as the timeout setting is not reliable const cancelTokenSource = axios_1.default.CancelToken.source(); const timeoutFn = setTimeout(() => { cancelTokenSource.cancel("request timeout"); }, this.timeout); return axios_1.default .post(endpointUrl, reportContent, { timeout: this.timeout, cancelToken: cancelTokenSource.token, headers: { Authorization: authorizationHeaderValue, "Content-Type": "application/json", }, }) .then((response) => { clearTimeout(timeoutFn); return new xray_datacenter_response_v2_1.default(response); }) .catch((error) => { throw new xray_error_response_1.default(error.response); }); } else { const params = {}; if (config.projectKey === undefined && config.testExecKey === undefined) { throw new xray_error_response_1.default("ERROR: projectKey or testExecKey must be defined"); } if (config.projectKey !== undefined) { params.projectKey = config.projectKey; } if (config.testPlanKey !== undefined) { params.testPlanKey = config.testPlanKey; } if (config.testExecKey !== undefined) { params.testExecKey = config.testExecKey; } if (config.version !== undefined) { params.fixVersion = config.version; } if (config.revision !== undefined) { params.revision = config.revision; } if (config.testEnvironment !== undefined) { params.testEnvironments = config.testEnvironment; } if (config.testEnvironments !== undefined) { params.testEnvironments = config.testEnvironments.join(";"); } const urlParams = new URLSearchParams({ ...params }).toString(); const url = endpointUrl + "?" + urlParams; const bodyFormData = new form_data_1.default(); let fileName; if ([ index_1.JUNIT_FORMAT, index_1.TESTNG_FORMAT, index_1.NUNIT_FORMAT, index_1.XUNIT_FORMAT, index_1.ROBOT_FORMAT, ].includes(config.format)) { fileName = "report.xml"; } else { fileName = "report.json"; } bodyFormData.append("file", reportContent, fileName); // use a CancelToken as the timeout setting is not reliable const cancelTokenSource = axios_1.default.CancelToken.source(); const timeoutFn = setTimeout(() => { cancelTokenSource.cancel("request timeout"); }, this.timeout); return axios_1.default .post(url, bodyFormData, { timeout: this.timeout, cancelToken: cancelTokenSource.token, headers: { Authorization: authorizationHeaderValue, ...bodyFormData.getHeaders(), }, }) .then((response) => { clearTimeout(timeoutFn); return new xray_datacenter_response_v2_1.default(response); }) .catch((error) => { throw new xray_error_response_1.default(error.message || error.response); }); } } async submitResultsMultipart(reportPath, config) { if (config.format === undefined) throw new xray_error_response_1.default("ERROR: format must be specified"); if (!this.supportedFormats.includes(config.format)) throw new xray_error_response_1.default("ERROR: unsupported format " + config.format); if (config.testExecInfoFile === undefined && config.testExecInfo === undefined) throw new xray_error_response_1.default("ERROR: testExecInfoFile or testExecInfo must be defined"); let endpointUrl; if (config.format === index_1.XRAY_FORMAT) { endpointUrl = this.jiraBaseUrl + "/rest/raven/2.0/import/execution/multipart"; } else { endpointUrl = this.jiraBaseUrl + "/rest/raven/2.0/import/execution/" + config.format + "/multipart"; } let authorizationHeaderValue; if (this.jiraToken !== undefined) { authorizationHeaderValue = "Bearer " + this.jiraToken; } else { authorizationHeaderValue = "Basic " + Buffer.from(this.jiraUsername + ":" + this.jiraPassword).toString("base64"); } let reportContent; let testInfoContent; let testExecInfoContent; try { reportContent = fs_1.default.readFileSync(reportPath).toString(); if (config.testInfoFile !== undefined) testInfoContent = fs_1.default.readFileSync(config.testInfoFile).toString(); if (config.testInfo !== undefined) testInfoContent = config.testInfo.toString(); if (config.testExecInfoFile !== undefined) testExecInfoContent = fs_1.default .readFileSync(config.testExecInfoFile) .toString(); else testExecInfoContent = config.testExecInfo.toString(); } catch (error) { throw new xray_error_response_1.default(error.message); } const bodyFormData = new form_data_1.default(); let filePartName; let fileName; if ([ index_1.JUNIT_FORMAT, index_1.TESTNG_FORMAT, index_1.NUNIT_FORMAT, index_1.XUNIT_FORMAT, index_1.ROBOT_FORMAT, ].includes(config.format)) { filePartName = "file"; fileName = "report.xml"; } else { filePartName = "result"; fileName = "report.json"; } bodyFormData.append(filePartName, reportContent, fileName); bodyFormData.append("info", testExecInfoContent, "info.json"); if (testInfoContent !== undefined && [ index_1.JUNIT_FORMAT, index_1.TESTNG_FORMAT, index_1.NUNIT_FORMAT, index_1.XUNIT_FORMAT, index_1.ROBOT_FORMAT, ].includes(config.format)) bodyFormData.append("testInfo", testInfoContent, "testInfo.json"); // use a CancelToken as the timeout setting is not reliable const cancelTokenSource = axios_1.default.CancelToken.source(); const timeoutFn = setTimeout(() => { cancelTokenSource.cancel("request timeout"); }, this.timeout); return axios_1.default .post(endpointUrl, bodyFormData, { timeout: this.timeout, cancelToken: cancelTokenSource.token, headers: { Authorization: authorizationHeaderValue, ...bodyFormData.getHeaders(), }, }) .then((response) => { clearTimeout(timeoutFn); return new xray_datacenter_response_v2_1.default(response); }) .catch((error) => { throw new xray_error_response_1.default(error.response); }); } async associateTestExecutionToTestPlan(testExecKey, testPlanKey) { let authorizationHeaderValue; if (this.jiraToken !== undefined) { authorizationHeaderValue = "Bearer " + this.jiraToken; } else { authorizationHeaderValue = "Basic " + Buffer.from(this.jiraUsername + ":" + this.jiraPassword).toString("base64"); } const content = { add: [testExecKey], }; const endpointUrl = `${this.jiraBaseUrl}/rest/raven/2.0/api/testplan/${testPlanKey}/testexecution`; // use a CancelToken as the timeout setting is not reliable const cancelTokenSource = axios_1.default.CancelToken.source(); const timeoutFn = setTimeout(() => { cancelTokenSource.cancel("request timeout"); }, this.timeout); return axios_1.default .post(endpointUrl, content, { timeout: this.timeout, cancelToken: cancelTokenSource.token, headers: { Authorization: authorizationHeaderValue, "Content-Type": "application/json", }, }) .then((response) => { clearTimeout(timeoutFn); if (response.data.length === 0) return testExecKey; else throw new xray_error_response_1.default(response.data[0]); }) .catch((error) => { if (error instanceof xray_error_response_1.default) throw error; else throw new xray_error_response_1.default(error.response); }); } } exports.XrayDatacenterClient = XrayDatacenterClient; /// export default { XrayClient };