-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcf78a9
commit baeb72f
Showing
12 changed files
with
315 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const FormData = require('form-data-lite'); | ||
const TestResult = require('test-results-parser/src/models/TestResult'); | ||
const { BeatsApi } = require('./beats.api'); | ||
const logger = require('../utils/logger'); | ||
|
||
const MAX_ATTACHMENTS_PER_REQUEST = 5; | ||
const MAX_ATTACHMENTS_PER_RUN = 20; | ||
const MAX_ATTACHMENT_SIZE = 1024 * 1024; | ||
|
||
class BeatsAttachments { | ||
|
||
/** | ||
* @param {import('../index').PublishReport} config | ||
* @param {TestResult} result | ||
*/ | ||
constructor(config, result, test_run_id) { | ||
this.config = config; | ||
this.result = result; | ||
this.api = new BeatsApi(config); | ||
this.test_run_id = test_run_id; | ||
this.failed_test_cases = []; | ||
this.attachments = []; | ||
} | ||
|
||
async upload() { | ||
this.#setAllFailedTestCases(); | ||
this.#setAttachments(); | ||
await this.#uploadAttachments(); | ||
} | ||
|
||
#setAllFailedTestCases() { | ||
for (const suite of this.result.suites) { | ||
for (const test of suite.cases) { | ||
if (test.status === 'FAIL') { | ||
this.failed_test_cases.push(test); | ||
} | ||
} | ||
} | ||
} | ||
|
||
#setAttachments() { | ||
for (const test_case of this.failed_test_cases) { | ||
for (const attachment of test_case.attachments) { | ||
this.attachments.push(attachment); | ||
} | ||
} | ||
} | ||
|
||
async #uploadAttachments() { | ||
if (this.attachments.length === 0) { | ||
return; | ||
} | ||
logger.info(`⏳ Uploading ${this.attachments.length} attachments...`); | ||
const result_file = this.config.results[0].files[0]; | ||
const result_file_dir = path.dirname(result_file); | ||
try { | ||
let count = 0; | ||
const size = MAX_ATTACHMENTS_PER_REQUEST; | ||
for (let i = 0; i < this.attachments.length; i += size) { | ||
if (count >= MAX_ATTACHMENTS_PER_RUN) { | ||
logger.warn('⚠️ Maximum number of attachments per run reached. Skipping remaining attachments.'); | ||
break; | ||
} | ||
const attachments_subset = this.attachments.slice(i, i + size); | ||
const form = new FormData(); | ||
const file_images = [] | ||
for (const attachment of attachments_subset) { | ||
const attachment_path = path.join(result_file_dir, 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)); | ||
form.append('test_run_id', this.test_run_id); | ||
file_images.push({ | ||
file_name: attachment.name, | ||
file_path: attachment.path, | ||
}); | ||
count += 1; | ||
} | ||
if (file_images.length === 0) { | ||
return; | ||
} | ||
form.append('file_images', JSON.stringify(file_images)); | ||
await this.api.uploadAttachments(form.getHeaders(), form.getBuffer()); | ||
logger.info(`🏞️ Uploaded ${count} attachments`); | ||
} | ||
} catch (error) { | ||
logger.error(`❌ Unable to upload attachments: ${error.message}`, error); | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
module.exports = { BeatsAttachments } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.