Skip to content

Commit

Permalink
feat: send errors to targets (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep authored Aug 17, 2024
1 parent 602ab5d commit 0e4de0a
Show file tree
Hide file tree
Showing 11 changed files with 471 additions and 21 deletions.
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "testbeats",
"version": "2.0.8",
"version": "2.0.9",
"description": "Publish test results to Microsoft Teams, Google Chat, Slack and InfluxDB",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down
33 changes: 31 additions & 2 deletions src/commands/publish.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PublishCommand {
*/
constructor(opts) {
this.opts = opts;
this.errors = [];
}

async publish() {
Expand All @@ -31,7 +32,7 @@ class PublishCommand {
this.#validateConfig();
this.#processResults();
await this.#publishResults();
logger.info('✅ Results published successfully!');
await this.#publishErrors();
}

#validateEnvDetails() {
Expand Down Expand Up @@ -184,13 +185,24 @@ class PublishCommand {
} else if (result_options.type === 'jmeter') {
this.results.push(prp.parse(result_options));
} else {
this.results.push(trp.parse(result_options));
const { result, errors } = trp.parseV2(result_options);
if (result) {
this.results.push(result);
}
if (errors) {
this.errors = this.errors.concat(errors);
}
}
}
}
}

async #publishResults() {
if (!this.results.length) {
logger.warn('⚠️ No results to publish');
return;
}

for (const config of this.configs) {
for (let i = 0; i < this.results.length; i++) {
const result = this.results[i];
Expand All @@ -207,6 +219,23 @@ class PublishCommand {
}
}
}
logger.info('✅ Results published successfully!');
}

async #publishErrors() {
if (!this.errors.length) {
logger.debug('⚠️ No errors to publish');
return;
}
logger.info('🛑 Publishing errors...');
for (const config of this.configs) {
if (config.targets) {
for (const target of config.targets) {
await target_manager.handleErrors({ target, errors: this.errors });
}
}
}
throw new Error(this.errors.join('\n'));
}

}
Expand Down
24 changes: 24 additions & 0 deletions src/targets/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,31 @@ const default_inputs = {
]
};

async function handleErrors({ target, errors }) {
let title = 'Error: Reporting Test Results';
title = target.inputs.title ? title + ' - ' + target.inputs.title : title;

const root_payload = getRootPayload();
const payload = root_payload.cards[0];

payload.sections.push({
"widgets": [
{
"textParagraph": {
text: `<b>${title}</b><br><br><b>Errors</b>: <br>${errors.join('<br>')}`
}
}
]
});

return request.post({
url: target.inputs.url,
body: root_payload
});
}

module.exports = {
run,
handleErrors,
default_options
}
10 changes: 9 additions & 1 deletion src/targets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ async function run(target, result) {
}
}

async function handleErrors({ target, errors }) {
const target_runner = getTargetRunner(target);
if (target_runner.handleErrors) {
await target_runner.handleErrors({ target, errors });
}
}

module.exports = {
run
run,
handleErrors
}
38 changes: 38 additions & 0 deletions src/targets/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,45 @@ const default_inputs = {
]
}

async function handleErrors({ target, errors }) {
let title = 'Error: Reporting Test Results';
title = target.inputs.title ? title + ' - ' + target.inputs.title : title;

const blocks = [];

blocks.push({
"type": "section",
"text": {
"type": "mrkdwn",
"text": title
}
});
blocks.push({
"type": "section",
"text": {
"type": "mrkdwn",
"text": errors.join('\n\n')
}
});

const payload = {
"attachments": [
{
"color": COLORS.DANGER,
"blocks": blocks,
"fallback": title,
}
]
};

return request.post({
url: target.inputs.url,
body: payload
});
}

module.exports = {
run,
handleErrors,
default_options
}
33 changes: 33 additions & 0 deletions src/targets/teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,40 @@ const default_inputs = {
]
}

async function handleErrors({ target, errors }) {
let title = 'Error: Reporting Test Results';
title = target.inputs.title ? title + ' - ' + target.inputs.title : title;

const root_payload = getRootPayload();
const payload = getMainPayload(target);

payload.body.push({
"type": "TextBlock",
"text": title,
"size": "medium",
"weight": "bolder",
"wrap": true
});

payload.body.push({
"type": "TextBlock",
"text": errors.join('\n'),
"size": "medium",
"weight": "bolder",
"wrap": true
});

setRootPayload(root_payload, payload);


return request.post({
url: target.inputs.url,
body: root_payload
});
}

module.exports = {
run,
handleErrors,
default_options
}
Loading

0 comments on commit 0e4de0a

Please sign in to comment.