cypress-xray-plugin
Version:
A Cypress plugin for uploading test results to Xray (test management for Jira)
260 lines (257 loc) • 13.4 kB
JavaScript
;
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.XrayClientCloud = void 0;
const form_data_1 = __importDefault(require("form-data"));
const dedent_1 = require("../../util/dedent");
const logging_1 = require("../../util/logging");
const util_1 = require("../util");
const xray_client_1 = require("./xray-client");
let XrayClientCloud = (() => {
var _a;
let _classSuper = xray_client_1.AbstractXrayClient;
let _instanceExtraInitializers = [];
let _addEvidenceToTestRun_decorators;
let _getTestRunResults_decorators;
return _a = class XrayClientCloud extends _classSuper {
/**
* Construct a new Xray cloud client using the provided credentials.
*
* @param url - the base URL
* @param credentials - the credentials to use during authentication
* @param httpClient - the HTTP client to use for dispatching requests
*/
constructor(url, credentials, httpClient) {
super(`${url}/api/${_a.VERSION}`, credentials, httpClient);
__runInitializers(this, _instanceExtraInitializers);
}
/**
* Mutation used to add evidence to a test run.
*
* @param variables - the GraphQL variable values
* @returns the result
*
* @see https://us.xray.cloud.getxray.app/doc/graphql/addevidencetotestrun.doc.html
*/
async addEvidenceToTestRun(variables) {
const authorizationHeader = await this.credentials.getAuthorizationHeader();
const mutation = (0, dedent_1.dedent)(`
mutation($id: String!, $evidence: [AttachmentDataInput]!) {
addEvidenceToTestRun(id: $id, evidence: $evidence) {
addedEvidence
warnings
}
}
`);
const response = await this.httpClient.post(`${this.apiBaseUrl}/graphql`, {
query: mutation,
variables: {
evidence: variables.evidence.map((evidence) => {
return {
data: evidence.data,
filename: evidence.filename,
mimeType: evidence.contentType,
};
}),
id: variables.id,
},
}, { headers: { ...authorizationHeader } });
for (const evidence of variables.evidence) {
logging_1.LOG.message("debug", `Successfully added evidence ${evidence.filename} to test run ${variables.id}.`);
}
return response.data.data.addEvidenceToTestRun;
}
async getTestRunResults(options) {
var _b;
const authorizationHeader = await this.credentials.getAuthorizationHeader();
logging_1.LOG.message("debug", "Retrieving test run results...");
const runResults = [];
let total = 0;
let start = 0;
const query = (0, dedent_1.dedent)(`
query($testIssueIds: [String], $testExecIssueIds: [String], $start: Int!, $limit: Int!) {
getTestRuns( testIssueIds: $testIssueIds, testExecIssueIds: $testExecIssueIds, limit: $limit, start: $start) {
total
limit
start
results {
id
status {
name
}
test {
jira(fields: ["key"])
}
evidence {
filename
downloadLink
}
iterations(limit: $limit) {
results {
parameters {
name
value
}
status {
name
}
}
}
}
}
}
`);
do {
const paginatedRequest = {
query: query,
variables: {
limit: _a.GRAPHQL_LIMIT,
start: start,
testExecIssueIds: options.testExecIssueIds,
testIssueIds: options.testIssueIds,
},
};
const response = await this.httpClient.post(`${this.apiBaseUrl}/graphql`, paginatedRequest, {
headers: {
...authorizationHeader,
},
});
const data = response.data.data.getTestRuns;
total = (_b = data === null || data === void 0 ? void 0 : data.total) !== null && _b !== void 0 ? _b : total;
if (data === null || data === void 0 ? void 0 : data.results) {
if (typeof data.start === "number") {
start = data.start + data.results.length;
}
for (const test of data.results) {
runResults.push(test);
}
}
} while (start && start < total);
logging_1.LOG.message("debug", "Successfully retrieved test run results");
return runResults;
}
onRequest(event, ...args) {
switch (event) {
case "import-execution-cucumber-multipart": {
// Cast valid because of overload.
const [cucumberJson, cucumberInfo] = args;
const formData = new form_data_1.default();
formData.append("results", JSON.stringify(cucumberJson), {
filename: "results.json",
});
formData.append("info", JSON.stringify(cucumberInfo), {
filename: "info.json",
});
return formData;
}
case "import-execution-multipart": {
// Cast valid because of overload.
const [executionResults, info] = args;
const formData = new form_data_1.default();
formData.append("results", JSON.stringify(executionResults), {
filename: "results.json",
});
formData.append("info", JSON.stringify(info), {
filename: "info.json",
});
return formData;
}
}
}
onResponse(event, ...args) {
switch (event) {
case "import-feature": {
// Cast valid because of overload.
const [cloudResponse] = args;
const response = {
errors: [],
updatedOrCreatedIssues: [],
};
if (cloudResponse.errors.length > 0) {
response.errors.push(...cloudResponse.errors);
logging_1.LOG.message("debug", (0, dedent_1.dedent)(`
Encountered some errors during feature file import:
${cloudResponse.errors.map((error) => `- ${error}`).join("\n")}
`));
}
if (cloudResponse.updatedOrCreatedTests.length > 0) {
const testKeys = cloudResponse.updatedOrCreatedTests.map((test) => test.key);
response.updatedOrCreatedIssues.push(...testKeys);
logging_1.LOG.message("debug", (0, dedent_1.dedent)(`
Successfully updated or created test issues:
${testKeys.join("\n")}
`));
}
if (cloudResponse.updatedOrCreatedPreconditions.length > 0) {
const preconditionKeys = cloudResponse.updatedOrCreatedPreconditions.map((test) => test.key);
response.updatedOrCreatedIssues.push(...preconditionKeys);
logging_1.LOG.message("debug", (0, dedent_1.dedent)(`
Successfully updated or created precondition issues:
${preconditionKeys.join(", ")}
`));
}
return response;
}
case "import-execution-cucumber-multipart":
case "import-execution-multipart":
case "import-execution": {
// Cast valid because of overload.
const [cloudResponse] = args;
return cloudResponse.key;
}
}
}
},
(() => {
var _b;
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create((_b = _classSuper[Symbol.metadata]) !== null && _b !== void 0 ? _b : null) : void 0;
_addEvidenceToTestRun_decorators = [(0, util_1.loggedRequest)({ purpose: "add evidence to test run" })];
_getTestRunResults_decorators = [(0, util_1.loggedRequest)({ purpose: "get test run results" })];
__esDecorate(_a, null, _addEvidenceToTestRun_decorators, { kind: "method", name: "addEvidenceToTestRun", static: false, private: false, access: { has: obj => "addEvidenceToTestRun" in obj, get: obj => obj.addEvidenceToTestRun }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getTestRunResults_decorators, { kind: "method", name: "getTestRunResults", static: false, private: false, access: { has: obj => "getTestRunResults" in obj, get: obj => obj.getTestRunResults }, metadata: _metadata }, null, _instanceExtraInitializers);
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
})(),
/**
* The version of Xray's Cloud API. API v1 would also work, but let's stick to the recent one.
*/
_a.VERSION = "v2",
_a.GRAPHQL_LIMIT = 100,
_a;
})();
exports.XrayClientCloud = XrayClientCloud;