playwright-testrail-sync
Version:
TestRail Integration for Playwright with comprehensive logging and error handling
124 lines • 5 kB
JavaScript
;
/**
* Attachment handler
* Handles file attachment operations for TestRail
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addAttachmentToResult = addAttachmentToResult;
exports.addAttachmentToCase = addAttachmentToCase;
const form_data_1 = __importDefault(require("form-data"));
const filename_generator_1 = require("./filename-generator");
const content_type_detector_1 = require("./content-type-detector");
/**
* Add attachment to a test result
*/
async function addAttachmentToResult(resultId, filePath, fileName, apiClient, logger) {
logger.verboseAttachment("Attachment Adding to result", {
resultId,
filePath,
});
try {
const fs = await Promise.resolve().then(() => __importStar(require("fs/promises")));
const fileBuffer = await fs.readFile(filePath);
const formData = new form_data_1.default();
// Generate proper filename with extension
const finalFileName = (0, filename_generator_1.generateFileName)(filePath, fileName);
formData.append("attachment", fileBuffer, {
filename: finalFileName || "attachment",
contentType: (0, content_type_detector_1.getContentType)(filePath),
});
logger.verboseApiCall("Making POST request to TestRail", {
url: `/add_attachment_to_result/${resultId}`,
resultId,
filePath,
fileName: finalFileName,
fileSize: fileBuffer.length,
});
const attachment = (await apiClient.postFormData(`/add_attachment_to_result/${resultId}`, formData));
logger.verboseApiCall("TestRail POST response received", {
resultId,
attachmentId: attachment.id,
status: "success",
});
logger.verboseAttachment("Attachment Added to result successfully", {
attachmentId: attachment.id,
resultId,
filePath,
size: `${(fileBuffer.length / 1024 / 1024).toFixed(2)}MB`,
});
return attachment;
}
catch (error) {
logger.error("Failed to add attachment to result", error);
throw error;
}
}
/**
* Add attachment to a test case
*/
async function addAttachmentToCase(caseId, filePath, fileName, apiClient, logger) {
logger.verboseAttachment("Attachment Adding to case", {
caseId,
filePath,
});
try {
const fs = await Promise.resolve().then(() => __importStar(require("fs/promises")));
const fileBuffer = await fs.readFile(filePath);
const formData = new form_data_1.default();
// Generate proper filename with extension
const finalFileName = (0, filename_generator_1.generateFileName)(filePath, fileName);
formData.append("attachment", fileBuffer, {
filename: finalFileName || "attachment",
contentType: (0, content_type_detector_1.getContentType)(filePath),
});
const attachment = (await apiClient.postFormData(`/add_attachment_to_case/${caseId}`, formData));
logger.verboseAttachment("Attachment Added to case successfully", {
attachmentId: attachment.id,
caseId,
filePath,
size: `${(fileBuffer.length / 1024 / 1024).toFixed(2)}MB`,
});
return attachment;
}
catch (error) {
logger.error("Failed to add attachment to case", error);
throw error;
}
}
//# sourceMappingURL=attachment-handler.js.map