diff --git a/src/commands/publish.command.js b/src/commands/publish.command.js index 8c07297..f9b7b0b 100644 --- a/src/commands/publish.command.js +++ b/src/commands/publish.command.js @@ -158,6 +158,9 @@ class PublishCommand { throw new Error('targets must be an array'); } for (const target of config.targets) { + if (target.enable === false || target.enable === 'false') { + continue; + } if (!target.name) { throw new Error(`'config.targets[*].name' is missing`); } @@ -219,6 +222,9 @@ class PublishCommand { await beats.run(config, result); if (config.targets) { for (const target of config.targets) { + if (target.enable === false || target.enable === 'false') { + continue; + } target.extensions = target.extensions || []; target.extensions = config.extensions.concat(target.extensions); await target_manager.run(target, result); diff --git a/src/extensions/base.extension.js b/src/extensions/base.extension.js index 313b8ad..9fad17d 100644 --- a/src/extensions/base.extension.js +++ b/src/extensions/base.extension.js @@ -5,8 +5,8 @@ class BaseExtension { /** * - * @param {import('..').Target} target - * @param {import('..').Extension} extension + * @param {import('..').ITarget} target + * @param {import('..').IExtension} extension * @param {import('..').TestResult} result * @param {any} payload * @param {any} root_payload diff --git a/src/extensions/index.js b/src/extensions/index.js index 2e813d1..217b298 100644 --- a/src/extensions/index.js +++ b/src/extensions/index.js @@ -17,9 +17,15 @@ const { FailureAnalysisExtension } = require('./failure-analysis.extension'); async function run(options) { const { target, result, hook } = options; + /** + * @type {import("..").IExtension[]} + */ const extensions = target.extensions || []; for (let i = 0; i < extensions.length; i++) { const extension = extensions[i]; + if (extension.enable === false || extension.enable === 'false') { + continue; + } const extension_runner = getExtensionRunner(extension, options); const extension_options = Object.assign({}, extension_runner.default_options, extension); if (extension_options.hook === hook) { diff --git a/src/extensions/metadata.js b/src/extensions/metadata.js index 9ad5dd7..ab7173c 100644 --- a/src/extensions/metadata.js +++ b/src/extensions/metadata.js @@ -4,7 +4,7 @@ const { getTeamsMetaDataText, getSlackMetaDataText, getChatMetaDataText } = requ /** * @param {object} param0 - * @param {import('..').Target} param0.target + * @param {import('..').ITarget} param0.target * @param {import('..').MetadataExtension} param0.extension */ async function run({ target, extension, result, payload, root_payload }) { diff --git a/src/helpers/extension.helper.js b/src/helpers/extension.helper.js index 692c252..c595cff 100644 --- a/src/helpers/extension.helper.js +++ b/src/helpers/extension.helper.js @@ -3,9 +3,9 @@ * * @param {object} param0 - the payload object * @param {object} param0.payload - the payload object - * @param {import("..").Extension} param0.extension - the extension to add + * @param {import("..").IExtension} param0.extension - the extension to add * @param {string} param0.text - the text to include - * @return {void} + * @return {void} */ function addSlackExtension({ payload, extension, text }) { if (extension.inputs.separator) { @@ -44,9 +44,9 @@ function addSlackExtension({ payload, extension, text }) { * * @param {object} param0 - the payload object * @param {object} param0.payload - the payload object - * @param {import("..").Extension} param0.extension - the extension to add + * @param {import("..").IExtension} param0.extension - the extension to add * @param {string} param0.text - the text to include - * @return {void} + * @return {void} */ function addTeamsExtension({ payload, extension, text }) { if (extension.inputs.title) { @@ -79,9 +79,9 @@ function addTeamsExtension({ payload, extension, text }) { * * @param {object} param0 - the payload object * @param {object} param0.payload - the payload object - * @param {import("..").Extension} param0.extension - the extension to add + * @param {import("..").IExtension} param0.extension - the extension to add * @param {string} param0.text - the text to include - * @return {void} + * @return {void} */ function addChatExtension({ payload, extension, text }) { let updated_text = text; diff --git a/src/helpers/metadata.helper.js b/src/helpers/metadata.helper.js index 8d729bf..2c2aa06 100644 --- a/src/helpers/metadata.helper.js +++ b/src/helpers/metadata.helper.js @@ -18,8 +18,8 @@ function getMetaDataText(params) { * * @param {object} param0 - the payload object * @param {Object} param0.elements - The elements to generate metadata text from - * @param {import('..').Target} param0.target - The result object - * @param {import('..').Extension} param0.extension - The result object + * @param {import('..').ITarget} param0.target - The result object + * @param {import('..').IExtension} param0.extension - The result object * @param {Object} param0.result - The result object * @param {string} param0.default_condition - The default condition object * @return {string} The generated metadata text @@ -50,8 +50,8 @@ async function getSlackMetaDataText({ elements, target, extension, result, defau * * @param {object} param0 - the payload object * @param {Object} param0.elements - The elements to generate metadata text from - * @param {import('..').Target} param0.target - The result object - * @param {import('..').Extension} param0.extension - The result object + * @param {import('..').ITarget} param0.target - The result object + * @param {import('..').IExtension} param0.extension - The result object * @param {Object} param0.result - The result object * @param {string} param0.default_condition - The default condition object * @return {string} The generated metadata text @@ -82,8 +82,8 @@ async function getTeamsMetaDataText({ elements, target, extension, result, defau * * @param {object} param0 - the payload object * @param {Object} param0.elements - The elements to generate metadata text from - * @param {import('..').Target} param0.target - The result object - * @param {import('..').Extension} param0.extension - The result object + * @param {import('..').ITarget} param0.target - The result object + * @param {import('..').IExtension} param0.extension - The result object * @param {Object} param0.result - The result object * @param {string} param0.default_condition - The default condition object * @return {string} The generated metadata text diff --git a/src/index.d.ts b/src/index.d.ts index 438ce55..a27ea2a 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -4,14 +4,30 @@ import { Schedule, User } from 'rosters'; import { ParseOptions } from 'test-results-parser'; import TestResult from 'test-results-parser/src/models/TestResult'; +export interface ITarget { + name: TargetName; + enable?: string | boolean; + condition?: Condition; + inputs?: SlackInputs | TeamsInputs | ChatInputs | CustomTargetInputs | InfluxDBTargetInputs; + extensions?: IExtension[]; +} + +export interface IExtension { + name: ExtensionName; + enable?: string | boolean; + condition?: Condition; + hook?: Hook; + inputs?: ReportPortalAnalysisInputs | ReportPortalHistoryInputs | HyperlinkInputs | MentionInputs | QuickChartTestSummaryInputs | PercyAnalysisInputs | CustomExtensionInputs | MetadataInputs | CIInfoInputs | AIFailureSummaryInputs; +} + export type ExtensionName = 'report-portal-analysis' | 'hyperlinks' | 'mentions' | 'report-portal-history' | 'quick-chart-test-summary' | 'metadata' | 'ci-info' | 'custom' | 'ai-failure-summary'; export type Hook = 'start' | 'end' | 'after-summary'; export type TargetName = 'slack' | 'teams' | 'chat' | 'custom' | 'delay'; export type PublishReportType = 'test-summary' | 'test-summary-slim' | 'failure-details'; export interface ConditionFunctionContext { - target: Target; - extension?: Extension, + target: ITarget; + extension?: IExtension, result: TestResult; } export type ConditionFunction = (ctx: ConditionFunctionContext) => boolean | Promise; @@ -67,12 +83,7 @@ export interface AIFailureSummaryInputs extends ExtensionInputs { failure_summary: string; } -export interface Extension { - name: ExtensionName; - condition?: Condition; - hook?: Hook; - inputs?: ReportPortalAnalysisInputs | ReportPortalHistoryInputs | HyperlinkInputs | MentionInputs | QuickChartTestSummaryInputs | PercyAnalysisInputs | CustomExtensionInputs | MetadataInputs | CIInfoInputs | AIFailureSummaryInputs; -} + export interface PercyAnalysisInputs extends ExtensionInputs { url?: string; @@ -90,14 +101,14 @@ export interface PercyAnalysisOutputs { project?: object; } -export interface PercyAnalysisExtension extends Extension { +export interface PercyAnalysisExtension extends IExtension { inputs?: PercyAnalysisInputs; outputs?: PercyAnalysisOutputs; } export interface CustomExtensionFunctionContext { - target: Target; - extension: Extension, + target: ITarget; + extension: IExtension, result: TestResult; payload: any; root_payload: any; @@ -109,13 +120,13 @@ export interface CustomExtensionInputs extends ExtensionInputs { load: string | CustomExtensionFunction; } -export interface CustomExtension extends Extension { +export interface CustomExtension extends IExtension { inputs?: CustomExtensionInputs; outputs?: any; } export interface LinkUrlFunctionContext { - target: Target; + target: ITarget; extension: HyperlinksExtension, result: TestResult; } @@ -132,7 +143,7 @@ export interface HyperlinkInputs extends ExtensionInputs { links: Link[]; } -export interface HyperlinksExtension extends Extension { +export interface HyperlinksExtension extends IExtension { inputs?: HyperlinkInputs; } @@ -148,7 +159,7 @@ export interface MetadataInputs extends ExtensionInputs { data?: Metadata[]; } -export interface MetadataExtension extends Extension { +export interface MetadataExtension extends IExtension { inputs?: MetadataInputs; } @@ -202,7 +213,7 @@ export interface InfluxDBTargetInputs { } export interface CustomTargetFunctionContext { - target: Target; + target: ITarget; result: TestResult; } @@ -212,12 +223,7 @@ export interface CustomTargetInputs { load: string | CustomTargetFunction; } -export interface Target { - name: TargetName; - condition?: Condition; - inputs?: SlackInputs | TeamsInputs | ChatInputs | CustomTargetInputs | InfluxDBTargetInputs; - extensions?: Extension[]; -} + export interface CustomResultOptions { type: string; @@ -232,8 +238,8 @@ export interface PublishReport { show_failure_analysis?: boolean; show_smart_analysis?: boolean; show_error_clusters?: boolean; - targets?: Target[]; - extensions?: Extension[]; + targets?: ITarget[]; + extensions?: IExtension[]; results?: ParseOptions[] | PerformanceParseOptions[] | CustomResultOptions[]; } @@ -241,8 +247,8 @@ export interface PublishConfig { api_key?: string; project?: string; run?: string; - targets?: Target[]; - extensions?: Extension[]; + targets?: ITarget[]; + extensions?: IExtension[]; results?: ParseOptions[] | PerformanceParseOptions[] | CustomResultOptions[]; } diff --git a/src/platforms/base.platform.js b/src/platforms/base.platform.js index 2696f37..b58b724 100644 --- a/src/platforms/base.platform.js +++ b/src/platforms/base.platform.js @@ -15,7 +15,7 @@ class BasePlatform { /** * - * @param {import('..').Target} target + * @param {import('..').ITarget} target * @param {import('test-results-parser').ITestSuite} suite */ getSuiteSummaryText(target, suite) { @@ -74,7 +74,7 @@ class BasePlatform { /** * - * @param {import('..').Target} target + * @param {import('..').ITarget} target * @param {import('test-results-parser').ITestSuite} suite */ #getSuiteDurationText(target, suite) { @@ -84,7 +84,7 @@ class BasePlatform { /** * - * @param {import('..').Target} target + * @param {import('..').ITarget} target * @param {import('test-results-parser').ITestSuite} suite */ getSuiteDuration(target, suite) { diff --git a/src/targets/custom.js b/src/targets/custom.js index 6b023c9..2413a4a 100644 --- a/src/targets/custom.js +++ b/src/targets/custom.js @@ -2,9 +2,9 @@ const path = require('path'); const { STATUS } = require('../helpers/constants'); /** - * - * @param {object} param0 - * @param {import('../index').Target} param0.target + * + * @param {object} param0 + * @param {import('../index').ITarget} param0.target */ async function run({result, target}) { if (typeof target.inputs.load === 'string') { diff --git a/src/targets/influx.js b/src/targets/influx.js index 45fb2b6..43352aa 100644 --- a/src/targets/influx.js +++ b/src/targets/influx.js @@ -10,10 +10,10 @@ const TestSuite = require('test-results-parser/src/models/TestSuite'); const { STATUS } = require('../helpers/constants'); /** - * - * @param {object} param0 + * + * @param {object} param0 * @param {PerformanceTestResult | TestResult} param0.result - * @param {import('..').Target} param0.target + * @param {import('..').ITarget} param0.target */ async function run({ result, target }) { target.inputs = Object.assign({}, default_inputs, target.inputs); @@ -34,10 +34,10 @@ async function run({ result, target }) { } /** - * - * @param {object} param0 + * + * @param {object} param0 * @param {PerformanceTestResult | TestResult} param0.result - * @param {import('..').Target} param0.target + * @param {import('..').ITarget} param0.target */ function getMetrics({ result, target }) { const influx_metrics = []; @@ -60,10 +60,10 @@ function getMetrics({ result, target }) { } /** - * @param {object} param0 + * @param {object} param0 * @param {PerformanceTestResult} param0.result - * @param {import('..').Target} param0.target - * @returns + * @param {import('..').ITarget} param0.target + * @returns */ function getPerfRunInfluxMetric({ result, target }) { const tags = Object.assign({}, target.inputs.tags); @@ -88,8 +88,8 @@ function getPerfRunInfluxMetric({ result, target }) { } /** - * - * @param {Metric} metric + * + * @param {Metric} metric */ function setPerfInfluxMetricFields(metric, fields) { let name = metric.name; @@ -110,8 +110,8 @@ function setPerfInfluxMetricFields(metric, fields) { } /** - * - * @param {Transaction} transaction + * + * @param {Transaction} transaction */ function getTransactionInfluxMetric(transaction, target) { const tags = Object.assign({}, target.inputs.tags); @@ -133,10 +133,10 @@ function getTransactionInfluxMetric(transaction, target) { } /** - * @param {object} param0 + * @param {object} param0 * @param {TestResult | TestSuite} param0.result - * @param {import('..').Target} param0.target - * @returns + * @param {import('..').ITarget} param0.target + * @returns */ function getTestInfluxMetric({ result, target }, measurement) { const tags = Object.assign({}, target.inputs.tags); @@ -158,10 +158,10 @@ function getTestInfluxMetric({ result, target }, measurement) { } /** - * @param {object} param0 + * @param {object} param0 * @param {TestCase} param0.result - * @param {import('..').Target} param0.target - * @returns + * @param {import('..').ITarget} param0.target + * @returns */ function getTestCaseInfluxMetric({ result, target }) { const tags = Object.assign({}, target.inputs.tags); diff --git a/test/targets.spec.js b/test/targets.spec.js index e69de29..c260f33 100644 --- a/test/targets.spec.js +++ b/test/targets.spec.js @@ -0,0 +1,66 @@ +const { mock } = require('pactum'); +const assert = require('assert'); +const { publish } = require('../src'); + +describe('Targets', () => { + + it('disable target', async () => { + const id = mock.addInteraction('post test-summary to slack'); + await publish({ + config: { + "targets": [ + { + "name": "slack", + "inputs": { + "url": "http://localhost:9393/message" + } + }, + { + name: 'teams', + enable: false + } + ], + "results": [ + { + "type": "testng", + "files": [ + "test/data/testng/single-suite.xml" + ] + } + ] + } + }); + assert.equal(mock.getInteraction(id).exercised, true); + }); + + it('disable extension', async () => { + const id = mock.addInteraction('post test-summary to slack'); + await publish({ + config: { + "targets": [ + { + "name": "slack", + "inputs": { + "url": "http://localhost:9393/message" + } + } + ], + "extensions": [ + { + name: 'ci-info', + enable: false + } + ], + "results": [ + { + "type": "testng", + "files": [ + "test/data/testng/single-suite.xml" + ] + } + ] + } + }); + assert.equal(mock.getInteraction(id).exercised, true); + }); +}); \ No newline at end of file