Skip to content

Commit

Permalink
feat: added ceat info (#155)
Browse files Browse the repository at this point in the history
* feat: added ceat info

* bump version
  • Loading branch information
ASaiAnudeep authored Mar 17, 2024
1 parent 1db9fdc commit 6b6e461
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 11 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": "test-results-reporter",
"version": "1.1.1",
"version": "1.1.2",
"description": "Publish test results to Microsoft Teams, Google Chat, Slack and InfluxDB",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down
126 changes: 126 additions & 0 deletions src/extensions/ci-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const { STATUS, HOOK } = require("../helpers/constants");
const { getCIInformation } = require('../helpers/ci');
const { addTeamsExtension, addSlackExtension, addChatExtension } = require('../helpers/extension.helper');
const { getTeamsMetaDataText, getSlackMetaDataText, getChatMetaDataText } = require('../helpers/metadata.helper');

/**
*
* @param {object} param0 - the payload object
* @param {import('..').Extension} param0.extension - The result object
*
*/
async function run({ target, extension, payload, result }) {
extension.inputs = Object.assign({}, default_inputs, extension.inputs);
if (target.name === 'teams') {
extension.inputs = Object.assign({}, default_inputs_teams, extension.inputs);
const text = await get_text({ target, extension, result });
if (text) {
addTeamsExtension({ payload, extension, text });
}
} else if (target.name === 'slack') {
extension.inputs = Object.assign({}, default_inputs_slack, extension.inputs);
const text = await get_text({ target, extension, result });
if (text) {
addSlackExtension({ payload, extension, text });
}
} else if (target.name === 'chat') {
extension.inputs = Object.assign({}, default_inputs_chat, extension.inputs);
const text = await get_text({ target, extension, result });
if (text) {
addChatExtension({ payload, extension, text });
}
}
}

/**
*
* @param {import('..').CIInfoInputs} inputs
*/
function get_repository_elements(inputs) {
const elements = [];
const ci = getCIInformation();
if (inputs.show_repository && ci && ci.repository_url && ci.repository_name) {
elements.push({ label: 'Repository', key: ci.repository_name, value: ci.repository_url, type: 'hyperlink' });
}
if (inputs.show_repository_branch && ci && ci.repository_ref) {
elements.push({ key: 'Branch', value: ci.repository_ref.replace('refs/heads/', '') });
}
return elements;
}

/**
*
* @param {import('..').CIInfoInputs} inputs
*/
function get_build_elements(inputs) {
let elements = [];
const ci = getCIInformation();
if (inputs.show_build && ci && ci.build_url) {
const name = (ci.build_name || 'Build') + (ci.build_number ? ` #${ci.build_number}` : '');
elements.push({ key: name, value: ci.build_url, type: 'hyperlink' });
}
if (inputs.data) {
elements = elements.concat(inputs.data);
}
return elements;
}

async function get_text({ target, extension, result }) {
const repository_elements = get_repository_elements(extension.inputs);
const build_elements = get_build_elements(extension.inputs);
if (target.name === 'teams') {
const repository_text = await getTeamsMetaDataText({ elements: repository_elements, target, extension, result, default_condition: default_options.condition });
const build_text = await getTeamsMetaDataText({ elements: build_elements, target, extension, result, default_condition: default_options.condition });
if (build_text) {
return `${repository_text ? `${repository_text}\n\n` : '' }${build_text}`;
} else {
return repository_text;
}
} else if (target.name === 'slack') {
const repository_text = await getSlackMetaDataText({ elements: repository_elements, target, extension, result, default_condition: default_options.condition });
const build_text = await getSlackMetaDataText({ elements: build_elements, target, extension, result, default_condition: default_options.condition });
if (build_text) {
return `${repository_text ? `${repository_text}\n` : '' }${build_text}`;
} else {
return repository_text;
}
} else if (target.name === 'chat') {
const repository_text = await getChatMetaDataText({ elements: repository_elements, target, extension, result, default_condition: default_options.condition });
const build_text = await getChatMetaDataText({ elements: build_elements, target, extension, result, default_condition: default_options.condition });
if (build_text) {
return `${repository_text ? `${repository_text}<br>` : '' }${build_text}`;
} else {
return repository_text;
}
}
}

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

const default_inputs = {
title: '',
show_repository: true,
show_repository_branch: true,
show_build: true,
}

const default_inputs_teams = {

separator: true
}

const default_inputs_slack = {
separator: false
}

const default_inputs_chat = {
separator: true
}

module.exports = {
run,
default_options
}
3 changes: 3 additions & 0 deletions src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const qc_test_summary = require('./quick-chart-test-summary');
const percy_analysis = require('./percy-analysis');
const custom = require('./custom');
const metadata = require('./metadata');
const ci_info = require('./ci-info');
const { EXTENSION } = require('../helpers/constants');
const { checkCondition } = require('../helpers/helper');

Expand Down Expand Up @@ -50,6 +51,8 @@ function getExtensionRunner(extension) {
return custom;
case EXTENSION.METADATA:
return metadata;
case EXTENSION.CI_INFO:
return ci_info;
default:
return require(extension.name);
}
Expand Down
18 changes: 15 additions & 3 deletions src/helpers/metadata.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ async function getSlackMetaDataText({ elements, target, extension, result, defau
if (await is_valid({ element, result, default_condition })) {
if (element.type === 'hyperlink') {
const url = await get_url({ url: element.value, target, result, extension });
items.push(`<${url}|${element.key}>`);
if (element.label) {
items.push(`*${element.label}:* <${url}|${element.key}>`);
} else {
items.push(`<${url}|${element.key}>`);
}
} else if (element.key) {
items.push(`*${element.key}:* ${element.value}`);
} else {
Expand Down Expand Up @@ -45,7 +49,11 @@ async function getTeamsMetaDataText({ elements, target, extension, result, defau
if (await is_valid({ element, result, default_condition })) {
if (element.type === 'hyperlink') {
const url = await get_url({ url: element.value, target, result, extension });
items.push(`[${element.key}](${url})`);
if (element.label) {
items.push(`**${element.label}:** [${element.key}](${url})`);
} else {
items.push(`[${element.key}](${url})`);
}
} else if (element.key) {
items.push(`**${element.key}:** ${element.value}`);
} else {
Expand Down Expand Up @@ -73,7 +81,11 @@ async function getChatMetaDataText({ elements, target, extension, result, defaul
if (await is_valid({ element, result, default_condition })) {
if (element.type === 'hyperlink') {
const url = await get_url({ url: element.value, target, result, extension });
items.push(`<a href="${url}">${element.key}</a>`);
if (element.label) {
items.push(`<b>${element.label}:</b> <a href="${url}">${element.key}</a>`);
} else {
items.push(`<a href="${url}">${element.key}</a>`);
}
} else if (element.key) {
items.push(`<b>${element.key}:</b> ${element.value}`);
} else {
Expand Down
18 changes: 13 additions & 5 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { User, Schedule } from 'rosters';
import TestResult from 'test-results-parser/src/models/TestResult';
import { PerformanceParseOptions } from 'performance-results-parser';
import { ParseOptions } from 'test-results-parser';
import PerformanceTestResult from 'performance-results-parser/src/models/PerformanceTestResult';
import { Schedule, User } from 'rosters';
import { ParseOptions } from 'test-results-parser';
import TestResult from 'test-results-parser/src/models/TestResult';

export type ExtensionName = 'report-portal-analysis' | 'hyperlinks' | 'mentions' | 'report-portal-history' | 'quick-chart-test-summary' | 'metadata' | 'custom';
export type ExtensionName = 'report-portal-analysis' | 'hyperlinks' | 'mentions' | 'report-portal-history' | 'quick-chart-test-summary' | 'metadata' | 'ci-info' | 'custom';
export type Hook = 'start' | 'end';
export type TargetName = 'slack' | 'teams' | 'chat' | 'custom' | 'delay';
export type PublishReportType = 'test-summary' | 'test-summary-slim' | 'failure-details';
Expand Down Expand Up @@ -54,11 +54,18 @@ export interface MentionInputs extends ExtensionInputs {
schedule?: Schedule;
}

export interface CIInfoInputs extends ExtensionInputs {
show_repository?: boolean;
show_repository_branch?: boolean;
show_build?: boolean;
data?: Metadata[];
}

export interface Extension {
name: ExtensionName;
condition?: Condition;
hook?: Hook;
inputs?: ReportPortalAnalysisInputs | ReportPortalHistoryInputs | HyperlinkInputs | MentionInputs | QuickChartTestSummaryInputs | PercyAnalysisInputs | CustomExtensionInputs | MetadataInputs;
inputs?: ReportPortalAnalysisInputs | ReportPortalHistoryInputs | HyperlinkInputs | MentionInputs | QuickChartTestSummaryInputs | PercyAnalysisInputs | CustomExtensionInputs | MetadataInputs | CIInfoInputs;
}

export interface PercyAnalysisInputs extends ExtensionInputs {
Expand Down Expand Up @@ -124,6 +131,7 @@ export interface HyperlinksExtension extends Extension {
}

export interface Metadata {
label?: string;
key?: string;
value: string;
type?: string;
Expand Down
138 changes: 138 additions & 0 deletions test/ext-ci-info.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
const { mock } = require('pactum');
const assert = require('assert');
const { publish } = require('../src');

describe('extensions - ci-info', () => {

beforeEach(() => {
process.env.GITHUB_ACTIONS = '';
process.env.GITHUB_SERVER_URL = '';
process.env.GITHUB_REPOSITORY = '';
process.env.GITHUB_REF = '';
process.env.GITHUB_SHA = '';
process.env.GITHUB_RUN_ID = '';
process.env.GITHUB_RUN_NUMBER = '';
process.env.GITHUB_WORKFLOW = '';

process.env.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = '';
process.env.BUILD_REPOSITORY_URI = '';
process.env.BUILD_REPOSITORY_NAME = '';
process.env.BUILD_SOURCEBRANCH = '';
process.env.BUILD_SOURCEVERSION = '';
process.env.BUILD_BUILDID = '';
process.env.BUILD_BUILDNUMBER = '';
process.env.BUILD_DEFINITIONNAME = '';
});

it('should send test-summary with ci-info to teams with no ci information', async () => {
const id = mock.addInteraction('post test-summary to teams');
await publish({
config: {
targets: [
{
name: 'teams',
inputs: {
url: 'http://localhost:9393/message'
},
extensions: [
{
name: 'ci-info'
}
]
}
],
results: [
{
type: 'testng',
files: [
'test/data/testng/single-suite.xml'
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with github ci information to teams', async () => {
process.env.GITHUB_ACTIONS = 'GITHUB_ACTIONS';
process.env.GITHUB_SERVER_URL = 'https://github.com';
process.env.GITHUB_REPOSITORY = 'test/test';
process.env.GITHUB_REF = '/refs/heads/feature-test';
process.env.GITHUB_SHA = 'sha';
process.env.GITHUB_RUN_ID = 'id-123';
process.env.GITHUB_RUN_NUMBER = 'number-123';
process.env.GITHUB_WORKFLOW = 'Build';
const id = mock.addInteraction('post test-summary with ci-info to teams');
await publish({
config: {
targets: [
{
name: 'teams',
inputs: {
url: 'http://localhost:9393/message'
},
extensions: [
{
name: 'ci-info'
}
]
}
],
results: [
{
type: 'testng',
files: [
'test/data/testng/single-suite.xml'
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with azure devops ci information to slack', async () => {
process.env.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = 'https://dev.azure.com/';
process.env.SYSTEM_TEAMPROJECT = 'test';
process.env.BUILD_REPOSITORY_URI = 'https://github.com/test/test';
process.env.BUILD_REPOSITORY_NAME = 'test/test';
process.env.BUILD_SOURCEBRANCH = '/refs/heads/feature-test';
process.env.BUILD_SOURCEVERSION = 'sha';
process.env.BUILD_BUILDID = 'id-123';
process.env.BUILD_BUILDNUMBER = 'number-123';
process.env.BUILD_DEFINITIONNAME = 'Build';
const id = mock.addInteraction('post test-summary with ci-info to slack');
await publish({
config: {
targets: [
{
name: 'slack',
inputs: {
url: 'http://localhost:9393/message'
},
extensions: [
{
name: 'ci-info'
}
]
}
],
results: [
{
type: 'testng',
files: [
'test/data/testng/single-suite.xml'
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

afterEach(() => {
mock.clearInteractions();
});

});
Loading

0 comments on commit 6b6e461

Please sign in to comment.