Skip to content

Commit

Permalink
chore: compress attachments (#257)
Browse files Browse the repository at this point in the history
* chore: compress attachments

* chore: bump version
  • Loading branch information
ASaiAnudeep authored Oct 26, 2024
1 parent 0f46f6d commit bf47a98
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
4 changes: 2 additions & 2 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.1.5",
"version": "2.1.6",
"description": "Publish test results to Microsoft Teams, Google Chat, Slack and InfluxDB",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down
46 changes: 44 additions & 2 deletions src/beats/beats.attachments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const stream = require('stream');
const FormData = require('form-data-lite');
const ml = require('mime-lite')
const TestResult = require('test-results-parser/src/models/TestResult');
const { BeatsApi } = require('./beats.api');
const logger = require('../utils/logger');
Expand All @@ -23,12 +26,14 @@ class BeatsAttachments {
this.test_run_id = test_run_id;
this.failed_test_cases = [];
this.attachments = [];
this.compressed_attachment_paths = [];
}

async upload() {
this.#setAllFailedTestCases();
this.#setAttachments();
await this.#uploadAttachments();
this.#deleteCompressedAttachments();
}

#setAllFailedTestCases() {
Expand Down Expand Up @@ -67,17 +72,22 @@ class BeatsAttachments {
form.append('test_run_id', this.test_run_id);
const file_images = []
for (const attachment of attachments_subset) {
const attachment_path = this.#getAttachmentFilePath(attachment);
let attachment_path = this.#getAttachmentFilePath(attachment);
if (!attachment_path) {
logger.warn(`⚠️ Unable to find attachment ${attachment.path}`);
continue;
}
attachment_path = await this.#compressAttachment(attachment_path);
const stats = fs.statSync(attachment_path);
if (stats.size > MAX_ATTACHMENT_SIZE) {
logger.warn(`⚠️ Attachment ${attachment.path} is too big (${stats.size} bytes). Allowed size is ${MAX_ATTACHMENT_SIZE} bytes.`);
continue;
}
form.append('images', fs.readFileSync(attachment_path), { filename: path.basename(attachment_path), filepath: attachment_path });
form.append('images', fs.readFileSync(attachment_path), {
filename: path.basename(attachment_path),
filepath: attachment_path,
contentType: ml.getType(attachment.path),
});
file_images.push({
file_name: attachment.name,
file_path: attachment.path,
Expand Down Expand Up @@ -123,7 +133,39 @@ class BeatsAttachments {
return null;
}

/**
*
* @param {string} attachment_path
*/
#compressAttachment(attachment_path) {
return new Promise((resolve, _) => {
console.log(attachment_path)
if (attachment_path.endsWith('.br') || attachment_path.endsWith('.gz') || attachment_path.endsWith('.zst') || attachment_path.endsWith('.zip') || attachment_path.endsWith('.7z') || attachment_path.endsWith('.png') || attachment_path.endsWith('.jpg') || attachment_path.endsWith('.jpeg') || attachment_path.endsWith('.svg') || attachment_path.endsWith('.gif') || attachment_path.endsWith('.webp')) {
resolve(attachment_path);
return;
}
const read_stream = fs.createReadStream(attachment_path);
const br = zlib.createBrotliCompress();

const compressed_file_path = attachment_path + '.br';
const write_stream = fs.createWriteStream(compressed_file_path);
stream.pipeline(read_stream, br, write_stream, (err) => {
if (err) {
resolve(attachment_path);
return;
}
this.compressed_attachment_paths.push(compressed_file_path);
resolve(compressed_file_path);
return;
});
});
}

#deleteCompressedAttachments() {
for (const attachment_path of this.compressed_attachment_paths) {
fs.unlinkSync(attachment_path);
}
}

}

Expand Down
4 changes: 2 additions & 2 deletions test/beats.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('TestBeats', () => {
assert.equal(mock.getInteraction(id4).exercised, true);
});

it('should send results with attachments to beats', async () => {
it('should send results with image attachments to beats', async () => {
const id1 = mock.addInteraction('post test results to beats');
const id2 = mock.addInteraction('get test results from beats');
const id3 = mock.addInteraction('upload attachments');
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('TestBeats', () => {
assert.equal(mock.getInteraction(id5).exercised, true);
});

it('should send results with attachments from cucumber to beats', async () => {
it('should send results with file attachments from cucumber to beats', async () => {
const id1 = mock.addInteraction('post test results to beats');
const id2 = mock.addInteraction('get test results from beats');
const id3 = mock.addInteraction('upload attachments');
Expand Down
4 changes: 4 additions & 0 deletions test/data/cucumber/failed-tests-with-attachments.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
{
"data": "eyJyZXEiOnsidXJsIjoiaHR0cHM6Ly9yZXFyZXMuaW4vYXBpL3VzZXJzIiwibWV0aG9kIjoiR0VUIn0sInJlcyI6eyJzdGF0dXMiOjIwMCwiYm9keSI6eyJwYWdlIjoxLCJwZXJfcGFnZSI6NiwidG90YWwiOjEyLCJ0b3RhbF9wYWdlcyI6MiwiZGF0YSI6W3siaWQiOjEsImVtYWlsIjoiZ2VvcmdlLmJsdXRoQHJlcXJlcy5pbiIsImZpcnN0X25hbWUiOiJHZW9yZ2UiLCJsYXN0X25hbWUiOiJCbHV0aCIsImF2YXRhciI6Imh0dHBzOi8vcmVxcmVzLmluL2ltZy9mYWNlcy8xLWltYWdlLmpwZyJ9LHsiaWQiOjIsImVtYWlsIjoiamFuZXQud2VhdmVyQHJlcXJlcy5pbiIsImZpcnN0X25hbWUiOiJKYW5ldCIsImxhc3RfbmFtZSI6IldlYXZlciIsImF2YXRhciI6Imh0dHBzOi8vcmVxcmVzLmluL2ltZy9mYWNlcy8yLWltYWdlLmpwZyJ9LHsiaWQiOjMsImVtYWlsIjoiZW1tYS53b25nQHJlcXJlcy5pbiIsImZpcnN0X25hbWUiOiJFbW1hIiwibGFzdF9uYW1lIjoiV29uZyIsImF2YXRhciI6Imh0dHBzOi8vcmVxcmVzLmluL2ltZy9mYWNlcy8zLWltYWdlLmpwZyJ9LHsiaWQiOjQsImVtYWlsIjoiZXZlLmhvbHRAcmVxcmVzLmluIiwiZmlyc3RfbmFtZSI6IkV2ZSIsImxhc3RfbmFtZSI6IkhvbHQiLCJhdmF0YXIiOiJodHRwczovL3JlcXJlcy5pbi9pbWcvZmFjZXMvNC1pbWFnZS5qcGcifSx7ImlkIjo1LCJl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LWVuZHBvaW50cyI6Imhlcm9rdS1uZWw9aHR0cHM6Ly9uZWwuaGVyb2t1LmNvbS9yZXBvcnRzP3RzPTE3MjQ2ODA2NDImc2lkPWM0Yzk3MjVmLTFhYjAtNDRkOC04MjBmLTQzMGRmMjcxOGUxMSZzPWJIR2RiVXoxc3ZkUUtqaFFHeHpRc2FqZElET0FlR2xGNFlJTUJlQno4JTJGRSUzRCIsIm5lbCI6IntcInJlcG9ydF90b1wiOlwiaGVyb2t1LW5lbFwiLFwibWF4X2FnZVwiOjM2MDAsXCJzdWNjZXNzX2ZyYWN0aW9uXCI6MC4wMDUsXCJmYWlsdXJlX2ZyYWN0aW9uXCI6MC4wNSxcInJlc3BvbnNlX2hlYWRlcnNcIjpbXCJWaWFcIl19IiwieC1wb3dlcmVkLWJ5IjoiRXhwcmVzcyIsImFjY2Vzcy1jb250cm9sLWFsbG93LW9yaWdpbiI6IioiLCJldGFnIjoiVy9cIjNlNC0yUkxYdnI1d1RnOVlRNmFIOTVDa1lvRk51TzhcIiIsInZpYSI6IjEuMSB2ZWd1ciIsImNhY2hlLWNvbnRyb2wiOiJtYXgtYWdlPTE0NDAwIiwiY2YtY2FjaGUtc3RhdHVzIjoiSElUIiwiYWdlIjoiNTAxIiwiYWNjZXB0LXJhbmdlcyI6ImJ5dGVzIiwic2VydmVyIjoiY2xvdWRmbGFyZSIsImNmLXJheSI6IjhiYzU3YTUwOGQ4ZDJlODUtSFlEIn19fQ==",
"mime_type": "application/json"
},
{
"data": "dGVzdC9kYXRhL2N1Y3VtYmVyL2ZhaWxlZC10ZXN0cy13aXRoLWF0dGFjaG1lbnRzLmpzb24=",
"mime_type": "application/json"
}
]
}
Expand Down

0 comments on commit bf47a98

Please sign in to comment.