@testmonitor/testmonitor-cli
Version:
The TestMonitor CLI lets you interact with the TestMonitor platform directly from your terminal or CI pipelines.
105 lines (87 loc) • 2.88 kB
JavaScript
import os from 'os';
import fs from 'fs';
import axios from 'axios';
import FormData from 'form-data';
import { appVersion } from './app-version.js';
const cliVersion = await appVersion();
const nodeVersion = `NodeJS/${process.nodeVersion}`;
const osVersion = `${os.platform()} ${os.release()}`;
const userAgent = `TestMonitorCLI/${cliVersion} (${nodeVersion}; ${osVersion}; +https://www.testmonitor.com/)`;
export class APIClient {
constructor({ domain, httpClient }) {
this.baseURL = `https://${domain}/api/v1`;
this.httpClient =
httpClient ||
axios.create({
baseURL: this.baseURL,
headers: {
'User-Agent': userAgent,
},
});
}
async get(path, config = {}) {
return this._request('get', path, null, config);
}
async post(path, data = {}, config = {}) {
return this._request('post', path, data, config);
}
async put(path, data = {}, config = {}) {
return this._request('put', path, data, config);
}
async delete(path, config = {}) {
return this._request('delete', path, null, config);
}
async _request(method, path, data, config) {
const isFormData = data instanceof FormData;
const headers = {
...(isFormData ? data.getHeaders() : { 'Content-Type': 'application/json' }),
...(config.headers || {}),
};
try {
const response = await this.httpClient.request({
method,
url: path,
data,
headers,
...config,
});
return response.data;
} catch (err) {
const message = err.response?.data?.message || err.message;
throw new Error(message);
}
}
/**
* Submit a JUnit report using plain data and file path
*/
async submitJUnitReport({
token,
filePath,
name,
automationType,
milestoneId,
milestoneName,
preserveNames,
skipRootSuite,
testEnvironmentId,
}) {
const form = new FormData();
form.append('token', token);
form.append('report', fs.createReadStream(filePath));
const optional = {
name,
milestone: milestoneName,
milestone_id: milestoneId,
preserve_names: preserveNames ? 1 : null,
skip_root_suite: skipRootSuite ? 1 : null,
test_environment_id: testEnvironmentId,
type: automationType,
};
for (const [key, value] of Object.entries(optional)) {
if (value !== undefined && value !== null) {
form.append(key, value);
}
}
return this.post('/reporters/junit/submit', form);
}
}