@testmonitor/testmonitor-cli
Version:
TestMonitor CLI tool
83 lines (72 loc) • 2.44 kB
JavaScript
import fs from 'fs';
import axios from 'axios';
import FormData from 'form-data';
import chalk from 'chalk';
import { table, getBorderCharacters } from 'table';
export class SubmitReport {
constructor({ domain, apiKey, integrationId, file, name, type, milestoneId, milestoneName, preserveNames, skipRootSuite, testEnvironmentId }) {
this.domain = domain;
this.apiKey = apiKey;
this.integrationId = integrationId;
this.file = file;
this.name = name;
this.type = type;
this.milestoneId = milestoneId;
this.milestoneName = milestoneName;
this.preserveNames = preserveNames;
this.skipRootSuite = skipRootSuite;
this.testEnvironmentId = testEnvironmentId;
}
async run() {
const formData = new FormData();
// Required
formData.append('integration_id', this.integrationId);
formData.append('report', fs.createReadStream(this.file));
// Optional
const optional = {
name: this.name,
type: this.type,
milestone: this.milestoneName,
milestone_id: this.milestoneId,
preserve_names: this.preserveNames ? 1: null,
skip_root_suite: this.skipRootSuite ? 1 : null,
test_environment_id: this.testEnvironmentId,
};
for (const [key, value] of Object.entries(optional)) {
if (value !== undefined && value !== null) {
formData.append(key, value);
}
}
try {
const response = await axios.post(
`https://${this.domain}/api/v1/reporters/junit/submit`,
formData,
{
headers: {
...formData.getHeaders(),
Authorization: `Bearer ${this.apiKey}`,
},
}
);
const { data } = response.data;
const config = {
border: getBorderCharacters('ramac'),
singleLine: true,
};
const rows = [
[chalk.bold('File'), data.name],
[chalk.bold('Date'), data.created_at],
[chalk.bold('ID'), data.test_run.id],
[chalk.bold('Code'), data.test_run.code],
[chalk.bold('Name'), data.test_run.name],
[chalk.bold('URL'), data.test_run.links.show],
];
console.log(table(rows, config));
console.log('Report submitted ' + chalk.green('successfully') + '.');
} catch (error) {
const message = error.response?.data?.message || error.message;
console.error(chalk.red('Error') + ': ' + message);
process.exit(1);
}
}
}