From b2881f02416e7bc5db0e0bf68cfcfdd1864ec040 Mon Sep 17 00:00:00 2001 From: Anudeep Date: Sat, 19 Oct 2024 19:13:05 +0530 Subject: [PATCH] chore: use failure analysis endpoint --- src/beats/beats.api.js | 14 ++++++++++ src/beats/beats.js | 3 +- src/beats/beats.types.d.ts | 12 ++++---- src/extensions/failure-analysis.extension.js | 29 ++++++++++---------- test/beats.spec.js | 4 ++- test/mocks/beats.mock.js | 24 +++++++++++++++- 6 files changed, 63 insertions(+), 23 deletions(-) diff --git a/src/beats/beats.api.js b/src/beats/beats.api.js index 6338b56..aa1f281 100644 --- a/src/beats/beats.api.js +++ b/src/beats/beats.api.js @@ -61,6 +61,20 @@ class BeatsApi { } }); } + + /** + * + * @param {string} run_id + * @returns {import('./beats.types').IFailureAnalysisMetric[]} + */ + getFailureAnalysis(run_id) { + return request.get({ + url: `${this.getBaseUrl()}/api/core/v1/test-runs/${run_id}/failure-analysis`, + headers: { + 'x-api-key': this.config.api_key + } + }); + } } module.exports = { BeatsApi } \ No newline at end of file diff --git a/src/beats/beats.js b/src/beats/beats.js index 1b97545..150c55d 100644 --- a/src/beats/beats.js +++ b/src/beats/beats.js @@ -147,11 +147,12 @@ class Beats { try { logger.info('🪄 Fetching Failure Analysis...'); await this.#setTestRun('Failure Analysis Status', 'failure_analysis_status'); + const metrics = await this.api.getFailureAnalysis(this.test_run_id); this.config.extensions.push({ name: 'failure-analysis', hook: HOOK.AFTER_SUMMARY, inputs: { - data: this.test_run + data: metrics } }); } catch (error) { diff --git a/src/beats/beats.types.d.ts b/src/beats/beats.types.d.ts index ea6ab37..7cbf0e4 100644 --- a/src/beats/beats.types.d.ts +++ b/src/beats/beats.types.d.ts @@ -9,12 +9,6 @@ export type IBeatExecutionMetric = { added: number removed: number flaky: number - product_bugs: number - environment_issues: number - automation_bugs: number - not_a_defects: number - to_investigate: number - auto_analysed: number failure_summary: any failure_summary_provider: any failure_summary_model: any @@ -38,3 +32,9 @@ export type IErrorCluster = { failure: string count: number } + +export type IFailureAnalysisMetric = { + id: string + name: string + count: number +} diff --git a/src/extensions/failure-analysis.extension.js b/src/extensions/failure-analysis.extension.js index 9ed097a..d3adb82 100644 --- a/src/extensions/failure-analysis.extension.js +++ b/src/extensions/failure-analysis.extension.js @@ -26,28 +26,29 @@ class FailureAnalysisExtension extends BaseExtension { } #setText() { - const data = this.extension.inputs.data; - if (!data) { - return; - } - /** - * @type {import('../beats/beats.types').IBeatExecutionMetric} + * @type {import('../beats/beats.types').IFailureAnalysisMetric[]} */ - const execution_metrics = data.execution_metrics[0]; - - if (!execution_metrics) { - logger.warn('⚠️ No execution metrics found. Skipping.'); + const metrics = this.extension.inputs.data; + if (!metrics || metrics.length === 0) { + logger.warn('⚠️ No failure analysis metrics found. Skipping.'); return; } + const to_investigate = metrics.find(metric => metric.name === 'To Investigate'); + const auto_analysed = metrics.find(metric => metric.name === 'Auto Analysed'); + const failure_analysis = []; - if (execution_metrics.to_investigate) { - failure_analysis.push(`🔎 To Investigate: ${execution_metrics.to_investigate}`); + if (to_investigate && to_investigate.count > 0) { + failure_analysis.push(`🔎 To Investigate: ${to_investigate.count}`); + } + if (auto_analysed && auto_analysed.count > 0) { + failure_analysis.push(`🪄 Auto Analysed: ${auto_analysed.count}`); } - if (execution_metrics.auto_analysed) { - failure_analysis.push(`🪄 Auto Analysed: ${execution_metrics.auto_analysed}`); + + if (failure_analysis.length === 0) { + return; } this.text = failure_analysis.join('    '); diff --git a/test/beats.spec.js b/test/beats.spec.js index 4341ff7..0812381 100644 --- a/test/beats.spec.js +++ b/test/beats.spec.js @@ -234,7 +234,8 @@ describe('TestBeats', () => { const id1 = mock.addInteraction('post test results to beats'); const id2 = mock.addInteraction('get test results with failure analysis from beats'); const id3 = mock.addInteraction('get empty error clusters from beats'); - const id4 = mock.addInteraction('post test-summary with beats to teams with ai failure summary and smart analysis and failure analysis'); + const id4 = mock.addInteraction('get failure analysis from beats'); + const id5 = mock.addInteraction('post test-summary with beats to teams with ai failure summary and smart analysis and failure analysis'); await publish({ config: { api_key: 'api-key', @@ -262,6 +263,7 @@ describe('TestBeats', () => { assert.equal(mock.getInteraction(id2).exercised, true); assert.equal(mock.getInteraction(id3).exercised, true); assert.equal(mock.getInteraction(id4).exercised, true); + assert.equal(mock.getInteraction(id5).exercised, true); }); }); \ No newline at end of file diff --git a/test/mocks/beats.mock.js b/test/mocks/beats.mock.js index 64313c7..795afe4 100644 --- a/test/mocks/beats.mock.js +++ b/test/mocks/beats.mock.js @@ -1,5 +1,4 @@ const { addInteractionHandler } = require('pactum').handler; -const { like, includes } = require('pactum-matchers'); addInteractionHandler('post test results to beats', () => { return { @@ -155,6 +154,29 @@ addInteractionHandler('get empty error clusters from beats', () => { } }); +addInteractionHandler('get failure analysis from beats', () => { + return { + strict: false, + request: { + method: 'GET', + path: '/api/core/v1/test-runs/test-run-id/failure-analysis' + }, + response: { + status: 200, + body: [ + { + name: 'To Investigate', + count: 1 + }, + { + name: 'Auto Analysed', + count: 1 + } + ] + } + } +}); + addInteractionHandler('upload attachments', () => { return { strict: false,