Skip to content

Commit

Permalink
Merge pull request #102 from test-results-reporter/30-extension-test-…
Browse files Browse the repository at this point in the history
…context

30 extension metadata
  • Loading branch information
ASaiAnudeep authored Oct 29, 2022
2 parents c424cab + eb9cf53 commit b8d563c
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 22 deletions.
2 changes: 1 addition & 1 deletion 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": "test-results-reporter",
"version": "1.0.14",
"version": "1.0.15",
"description": "Publish test results to Microsoft Teams, Google Chat and Slack",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const rp_history = require('./report-portal-history');
const qc_test_summary = require('./quick-chart-test-summary');
const percy_analysis = require('./percy-analysis');
const custom = require('./custom');
const metadata = require('./metadata');
const { EXTENSION } = require('../helpers/constants');
const { checkCondition } = require('../helpers/helper');

Expand Down Expand Up @@ -47,6 +48,8 @@ function getExtensionRunner(extension) {
return percy_analysis;
case EXTENSION.CUSTOM:
return custom;
case EXTENSION.METADATA:
return metadata;
default:
return require(extension.name);
}
Expand Down
119 changes: 119 additions & 0 deletions src/extensions/metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
const { HOOK, STATUS } = require('../helpers/constants');
const { addExtension } = require('../helpers/teams');
const { addTextBlock } = require('../helpers/slack');
const { addTextSection } = require('../helpers/chat');
const { checkCondition } = require('../helpers/helper');

/**
* @param {object} param0
* @param {import('..').Target} param0.target
* @param {import('..').MetadataExtension} param0.extension
*/
async function run({ target, extension, result, payload, root_payload }) {
if (target.name === 'teams') {
extension.inputs = Object.assign({}, default_inputs_teams, extension.inputs);
await attachForTeams({ extension, payload, result });
} else if (target.name === 'slack') {
extension.inputs = Object.assign({}, default_inputs_slack, extension.inputs);
await attachForSlack({ extension, payload, result });
} else if (target.name === 'chat') {
extension.inputs = Object.assign({}, default_inputs_chat, extension.inputs);
await attachForChat({ extension, payload, result });
}
}

/**
* @param {object} param0
* @param {import('..').MetadataExtension} param0.extension
*/
async function attachForTeams({ extension, payload, result }) {
const valid_data = await getValidData({ extension, result });
if (valid_data.length > 0) {
const data = [];
for (const current of valid_data) {
if (current.type === 'hyperlink') {
data.push(`[${current.key}](${current.value})`);
} else if (current.key) {
data.push(`**${current.key}:** ${current.value}`);
} else {
data.push(current.value);
}
}
addExtension({ payload, extension, text: data.join(' | ') });
}
}

async function attachForSlack({ extension, payload, result }) {
const valid_data = await getValidData({ extension, result });
if (valid_data.length > 0) {
const data = [];
for (const current of valid_data) {
if (current.type === 'hyperlink') {
data.push(`<${current.value}|${current.key}>`);
} else if (current.key) {
data.push(`*${current.key}:* ${current.value}`);
} else {
data.push(current.value);
}
}
addTextBlock({ payload, extension, text: data.join(' | ') });
}
}

async function attachForChat({ extension, payload, result }) {
const valid_data = await getValidData({ extension, result });
if (valid_data.length > 0) {
const data = [];
for (const current of valid_data) {
if (current.type === 'hyperlink') {
data.push(`<a href="${current.value}">${current.key}</a>`);
} else if (current.key) {
data.push(`<b>${current.key}:</b> ${current.value}`);
} else {
data.push(current.value);
}
}
addTextSection({ payload, extension, text: data.join(' | ') });
}
}

/**
* @param {object} param0
* @param {import('..').MetadataExtension} param0.extension
*/
async function getValidData({ extension, result }) {
const valid_data = [];
for (const current of extension.inputs.data) {
const condition = current.condition || default_options.condition;
if (await checkCondition({ condition, result })) {
valid_data.push(current);
}
}
return valid_data;
}


const default_options = {
hook: HOOK.END,
condition: STATUS.PASS_OR_FAIL
}

const default_inputs_teams = {
title: '',
separator: true
}

const default_inputs_slack = {
title: '',
separator: false
}

const default_inputs_chat = {
title: '',
separator: true
}

module.exports = {
run,
default_options
}
3 changes: 2 additions & 1 deletion src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const EXTENSION = Object.freeze({
REPORT_PORTAL_HISTORY: 'report-portal-history',
QUICK_CHART_TEST_SUMMARY: 'quick-chart-test-summary',
PERCY_ANALYSIS: 'percy-analysis',
CUSTOM: 'custom'
CUSTOM: 'custom',
METADATA: 'metadata'
});

const URLS = Object.freeze({
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function getResultText({ result }) {
*/
async function checkCondition({ condition, result, target, extension }) {
if (typeof condition === 'function') {
return await condition({target, result, extension });
return await condition({ target, result, extension });
} else {
const lower_condition = condition.toLowerCase();
if (ALLOWED_CONDITIONS.has(lower_condition)) {
Expand Down
34 changes: 25 additions & 9 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { PerformanceParseOptions } from 'performance-results-parser';
import { ParseOptions } from 'test-results-parser';
import PerformanceTestResult from 'performance-results-parser/src/models/PerformanceTestResult';

export type ExtensionName = 'report-portal-analysis' | 'hyperlinks' | 'mentions' | 'report-portal-history' | 'quick-chart-test-summary' | 'custom';
export type ExtensionName = 'report-portal-analysis' | 'hyperlinks' | 'mentions' | 'report-portal-history' | 'quick-chart-test-summary' | 'metadata' | 'custom';
export type Hook = 'start' | 'end';
export type TargetName = 'slack' | 'teams' | 'chat' | 'custom';
export type TargetName = 'slack' | 'teams' | 'chat' | 'custom' | 'delay';
export type PublishReportType = 'test-summary' | 'test-summary-slim' | 'failure-details';

export interface ConditionFunctionContext {
Expand All @@ -31,17 +31,18 @@ export interface ReportPortalAnalysisInputs extends ExtensionInputs {
url: string;
api_key: string;
project: string;
launch_id: string;
launch_name: string;
launch_id?: string;
launch_name?: string;
}

export interface ReportPortalHistoryInputs extends ExtensionInputs {
url: string;
api_key: string;
project: string;
launch_id: string;
launch_name: string;
history_depth: number;
launch_id?: string;
launch_name?: string;
history_depth?: number;
link_history_via?: string;
}

export interface QuickChartTestSummaryInputs {
Expand All @@ -57,7 +58,7 @@ export interface Extension {
name: ExtensionName;
condition?: Condition;
hook?: Hook;
inputs?: ReportPortalAnalysisInputs | ReportPortalHistoryInputs | HyperlinkInputs | MentionInputs | QuickChartTestSummaryInputs | PercyAnalysisInputs | CustomExtensionInputs;
inputs?: ReportPortalAnalysisInputs | ReportPortalHistoryInputs | HyperlinkInputs | MentionInputs | QuickChartTestSummaryInputs | PercyAnalysisInputs | CustomExtensionInputs | MetadataInputs;
}

export interface PercyAnalysisInputs extends ExtensionInputs {
Expand All @@ -68,7 +69,7 @@ export interface PercyAnalysisInputs extends ExtensionInputs {
project_id?: string;
project_name?: string;
organization_uid?: string;
title_link_to_build: boolean;
title_link_to_build?: boolean;
}

export interface PercyAnalysisOutputs {
Expand Down Expand Up @@ -122,6 +123,21 @@ export interface HyperlinksExtension extends Extension {
inputs?: HyperlinkInputs;
}

export interface Metadata {
key?: string;
value: string;
type?: string;
condition?: Condition;
}

export interface MetadataInputs extends ExtensionInputs {
data?: Metadata[];
}

export interface MetadataExtension extends Extension {
inputs?: MetadataInputs;
}

/**
* Targets
*/
Expand Down
6 changes: 3 additions & 3 deletions src/targets/chat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const request = require('phin-retry');
const { getTitleText, getResultText, getPercentage, truncate, getPrettyDuration } = require('../helpers/helper');
const { getTitleText, getResultText, truncate, getPrettyDuration } = require('../helpers/helper');
const extension_manager = require('../extensions');
const { HOOK } = require('../helpers/constants');
const { HOOK, STATUS } = require('../helpers/constants');
const PerformanceTestResult = require('performance-results-parser/src/models/PerformanceTestResult');
const { getValidMetrics, getMetricValuesText } = require('../helpers/performance');

Expand Down Expand Up @@ -213,7 +213,7 @@ async function getTransactionSummary({ target, transaction }) {


const default_options = {
condition: 'passOrFail'
condition: STATUS.PASS_OR_FAIL
};

const default_inputs = {
Expand Down
3 changes: 2 additions & 1 deletion src/targets/custom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const { STATUS } = require('../helpers/constants');

/**
*
Expand All @@ -18,7 +19,7 @@ async function run({result, target}) {
}

const default_options = {
condition: 'passOrFail'
condition: STATUS.PASS_OR_FAIL
}

module.exports = {
Expand Down
4 changes: 3 additions & 1 deletion src/targets/delay.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { STATUS } = require("../helpers/constants");

async function run({ target }) {
target.inputs = Object.assign({}, default_inputs, target.inputs);
await new Promise(resolve => setTimeout(resolve, target.inputs.seconds * 1000));
}

const default_options = {
condition: 'passOrFail'
condition: STATUS.PASS_OR_FAIL
}

const default_inputs = {
Expand Down
4 changes: 2 additions & 2 deletions src/targets/slack.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const request = require('phin-retry');
const { getPercentage, truncate, getPrettyDuration } = require('../helpers/helper');
const extension_manager = require('../extensions');
const { HOOK } = require('../helpers/constants');
const { HOOK, STATUS } = require('../helpers/constants');

const PerformanceTestResult = require('performance-results-parser/src/models/PerformanceTestResult');
const { getValidMetrics, getMetricValuesText } = require('../helpers/performance');
Expand Down Expand Up @@ -236,7 +236,7 @@ async function setTransactionBlock({ result, target, payload }) {


const default_options = {
condition: 'passOrFail'
condition: STATUS.PASS_OR_FAIL
}

const default_inputs = {
Expand Down
4 changes: 2 additions & 2 deletions src/targets/teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const request = require('phin-retry');
const { getPercentage, truncate, getPrettyDuration } = require('../helpers/helper');
const { getValidMetrics, getMetricValuesText } = require('../helpers/performance');
const extension_manager = require('../extensions');
const { HOOK } = require('../helpers/constants');
const { HOOK, STATUS } = require('../helpers/constants');

const TestResult = require('test-results-parser/src/models/TestResult');
const PerformanceTestResult = require('performance-results-parser/src/models/PerformanceTestResult');
Expand Down Expand Up @@ -273,7 +273,7 @@ async function setTransactionBlock({ result, target, payload }) {
}

const default_options = {
condition: 'passOrFail'
condition: STATUS.PASS_OR_FAIL
}

const default_inputs = {
Expand Down
Loading

0 comments on commit b8d563c

Please sign in to comment.