|
| 1 | +"use strict"; |
| 2 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 | +}; |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +const axios_1 = __importDefault(require("axios")); |
| 7 | +const util_1 = require("util"); |
| 8 | +const picocolors_1 = require("picocolors"); |
| 9 | +function isAxiosError(error) { |
| 10 | + return error.isAxiosError === true; |
| 11 | +} |
| 12 | +class XrayService { |
| 13 | + constructor(options) { |
| 14 | + // Init vars |
| 15 | + this.xray = ""; |
| 16 | + this.username = ""; |
| 17 | + this.password = ""; |
| 18 | + this.requestUrl = ""; |
| 19 | + // Set Jira URL |
| 20 | + if (!options.jira.url) |
| 21 | + throw new Error('"jira.url" option is missed. Please, provide it in the config'); |
| 22 | + this.jira = options.jira.url; |
| 23 | + // Set Jira Server Type |
| 24 | + if (!options.jira.type) |
| 25 | + throw new Error('"jira.type" option is missed. Please, provide it in the config'); |
| 26 | + this.type = options.jira.type; |
| 27 | + // Init axios instance |
| 28 | + this.axios = axios_1.default; |
| 29 | + switch (this.type) { |
| 30 | + case 'cloud': |
| 31 | + // Set Xray Server URL |
| 32 | + this.xray = 'https://xray.cloud.getxray.app/'; |
| 33 | + // Set Xray Credencials |
| 34 | + if (!options.cloud?.client_id || !options.cloud?.client_secret) |
| 35 | + throw new Error('"cloud.client_id" and/or "cloud.client_secret" options are missed. Please provide them in the config'); |
| 36 | + this.username = options.cloud?.client_id; |
| 37 | + this.password = options.cloud?.client_secret; |
| 38 | + // Set Request URL |
| 39 | + this.requestUrl = this.xray + 'api/v2'; |
| 40 | + //Create Axios Instance with Auth |
| 41 | + axios_1.default |
| 42 | + .post(this.requestUrl + '/authenticate', { |
| 43 | + client_id: this.username, |
| 44 | + client_secret: this.password, |
| 45 | + }) |
| 46 | + .then((request) => { |
| 47 | + this.axios = axios_1.default.create({ |
| 48 | + baseURL: this.xray, |
| 49 | + headers: { |
| 50 | + 'Content-Type': 'application/json', |
| 51 | + Authorization: `Bearer ${request.data}`, |
| 52 | + }, |
| 53 | + }); |
| 54 | + }) |
| 55 | + .catch((error) => { |
| 56 | + throw new Error(`Failed to autenticate do host ${this.xray} with error: ${error}`); |
| 57 | + }); |
| 58 | + break; |
| 59 | + case 'server': |
| 60 | + // Set Xray Server URL |
| 61 | + if (!options.server?.url) |
| 62 | + throw new Error('"host" option is missed. Please, provide it in the config'); |
| 63 | + this.xray = options.server?.url; |
| 64 | + // Set Xray Credencials |
| 65 | + if (!options.server?.username || !options.server?.password) |
| 66 | + throw new Error('"server.username" and/or "server.password" options are missed. Please provide them in the config'); |
| 67 | + this.username = options.server?.username; |
| 68 | + this.password = options.server?.password; |
| 69 | + // Set Request URL |
| 70 | + this.requestUrl = this.xray + 'rest/raven/2.0/api'; |
| 71 | + //Create Axios Instance with Auth |
| 72 | + const token = `${this.username}:${this.password}`; |
| 73 | + const encodedToken = Buffer.from(token).toString('base64'); |
| 74 | + this.axios = axios_1.default.create({ |
| 75 | + baseURL: this.xray, |
| 76 | + headers: { |
| 77 | + 'Content-Type': 'application/json', |
| 78 | + Authorization: `Basic ${encodedToken}`, |
| 79 | + }, |
| 80 | + }); |
| 81 | + break; |
| 82 | + } |
| 83 | + // Set Project Key |
| 84 | + if (!options.projectKey) |
| 85 | + throw new Error('"projectKey" option is missed. Please, provide it in the config'); |
| 86 | + // Set Test Plan |
| 87 | + if (!options.testPlan) |
| 88 | + throw new Error('"testPlan" option are missed. Please provide them in the config'); |
| 89 | + this.xrayOptions = options; |
| 90 | + } |
| 91 | + async createRun(results) { |
| 92 | + const URL = `${this.requestUrl}/import/execution`; |
| 93 | + try { |
| 94 | + const response = await this.axios.post(URL, JSON.stringify(results)); |
| 95 | + if (response.status !== 200) |
| 96 | + throw new Error(`${response.status} - Failed to create test cycle`); |
| 97 | + const { data: { key, id }, } = response; |
| 98 | + console.log(`${picocolors_1.bold(picocolors_1.green(`✅ Test cycle ${key} has been created`))}`); |
| 99 | + console.log(`${picocolors_1.bold(picocolors_1.green('👇 Check out the test result'))}`); |
| 100 | + console.log(`${picocolors_1.bold(picocolors_1.green(`🔗 ${this.jira}/browse/${id}`))}`); |
| 101 | + } |
| 102 | + catch (error) { |
| 103 | + if (isAxiosError(error)) { |
| 104 | + console.error(`Config: ${util_1.inspect(error.config)}`); |
| 105 | + if (error.response) { |
| 106 | + throw new Error(`\nStatus: ${error.response.status} \nHeaders: ${util_1.inspect(error.response.headers)} \nData: ${util_1.inspect(error.response.data)}`); |
| 107 | + } |
| 108 | + else if (error.request) { |
| 109 | + throw new Error(`The request was made but no response was received. \n Error: ${util_1.inspect(error.toJSON())}`); |
| 110 | + } |
| 111 | + else { |
| 112 | + throw new Error(`Something happened in setting up the request that triggered an Error\n : ${util_1.inspect(error.message)}`); |
| 113 | + } |
| 114 | + } |
| 115 | + throw new Error(`\nUnknown error: ${error}`); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +exports.XrayService = XrayService; |
0 commit comments