Skip to content

Commit 5d04c78

Browse files
authored
Merge pull request #42 from test-results-reporter/40-configurable-titles-dividers-for-all-extensions
40 configurable titles and dividers for all extensions
2 parents 12ab73c + c8c0439 commit 5d04c78

19 files changed

+516
-138
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "test-results-reporter",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "Publish test results to Microsoft Teams and Slack",
55
"main": "src/index.js",
66
"types": "./src/index.d.ts",

src/extensions/hyperlinks.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
const { STATUS } = require("../helpers/constants");
1+
const { STATUS, HOOK } = require("../helpers/constants");
2+
const { addExtension } = require('../helpers/teams');
3+
const { addContextText } = require('../helpers/slack');
24

35
async function run({ target, extension, payload, result }) {
46
if (target.name === 'teams') {
7+
extension.inputs = Object.assign({}, default_inputs_teams, extension.inputs);
58
attachTeamLinks({ extension, payload, result });
69
} else if (target.name === 'slack') {
10+
extension.inputs = Object.assign({}, default_inputs_slack, extension.inputs);
711
attachSlackLinks({ extension, payload, result });
812
}
913
}
@@ -17,11 +21,7 @@ function attachTeamLinks({ extension, payload, result }) {
1721
}
1822
}
1923
if (links.length) {
20-
payload.body.push({
21-
"type": "TextBlock",
22-
"text": links.join(' | '),
23-
"separator": true
24-
});
24+
addExtension({ payload, extension, text: links.join(' | ') });
2525
}
2626
}
2727

@@ -34,21 +34,23 @@ function attachSlackLinks({ extension, payload, result }) {
3434
}
3535
}
3636
if (links.length) {
37-
payload.blocks.push({
38-
"type": "context",
39-
"elements": [
40-
{
41-
"type": "mrkdwn",
42-
"text": links.join(' | ')
43-
}
44-
]
45-
});
37+
addContextText({ payload, extension, text: links.join(' | ') });
4638
}
4739
}
4840

4941
const default_options = {
50-
hook: 'end',
51-
condition: STATUS.PASS_OR_FAIL
42+
hook: HOOK.END,
43+
condition: STATUS.PASS_OR_FAIL,
44+
}
45+
46+
const default_inputs_teams = {
47+
title: '',
48+
separator: true
49+
}
50+
51+
const default_inputs_slack = {
52+
title: '',
53+
separator: false
5254
}
5355

5456
module.exports = {

src/extensions/index.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const mentions = require('./mentions');
33
const rp_analysis = require('./report-portal-analysis');
44
const rp_history = require('./report-portal-history');
55
const qc_test_summary = require('./quick-chart-test-summary');
6+
const { EXTENSION } = require('../helpers/constants');
67

78
async function run(options) {
89
const { target, result, hook } = options;
@@ -22,15 +23,15 @@ async function run(options) {
2223

2324
function getExtensionRunner(extension) {
2425
switch (extension.name) {
25-
case 'hyperlinks':
26+
case EXTENSION.HYPERLINKS:
2627
return hyperlinks;
27-
case 'mentions':
28+
case EXTENSION.MENTIONS:
2829
return mentions;
29-
case 'report-portal-analysis':
30+
case EXTENSION.REPORT_PORTAL_ANALYSIS:
3031
return rp_analysis;
31-
case 'report-portal-history':
32+
case EXTENSION.REPORT_PORTAL_HISTORY:
3233
return rp_history;
33-
case 'quick-chart-test-summary':
34+
case EXTENSION.QUICK_CHART_TEST_SUMMARY:
3435
return qc_test_summary;
3536
default:
3637
return require(extension.name);

src/extensions/mentions.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const { getOnCallPerson } = require('rosters');
2+
const { addExtension } = require('../helpers/teams');
3+
const { addSectionText } = require('../helpers/slack');
24

35
function run({ target, extension, payload }) {
46
if (target.name === 'teams') {
7+
extension.inputs = Object.assign({}, default_inputs_teams, extension.inputs);
58
attachForTeam({ extension, payload });
69
} else if (target.name === 'slack') {
10+
extension.inputs = Object.assign({}, default_inputs_slack, extension.inputs);
711
attachForSlack({ extension, payload });
812
}
913
}
@@ -13,11 +17,7 @@ function attachForTeam({ extension, payload }) {
1317
if (users.length > 0) {
1418
setPayloadWithMSTeamsEntities(payload);
1519
const users_ats = users.map(user => `<at>${user.name}</at>`);
16-
payload.body.push({
17-
"type": "TextBlock",
18-
"text": users_ats.join(' | '),
19-
"separator": true
20-
});
20+
addExtension({ payload, extension, text: users_ats.join(' | ')});
2121
for (const user of users) {
2222
payload.msteams.entities.push({
2323
"type": "mention",
@@ -35,13 +35,7 @@ function attachForSlack({ extension, payload }) {
3535
const users = getUsers(extension);
3636
const user_ids = users.map(user => `<@${user.slack_uid}>`);
3737
if (users.length > 0) {
38-
payload.blocks.push({
39-
"type": "section",
40-
"text": {
41-
"type": "mrkdwn",
42-
"text": user_ids.join(' | ')
43-
}
44-
});
38+
addSectionText({ payload, extension, text: user_ids.join(' | ') });
4539
}
4640
}
4741

@@ -75,6 +69,16 @@ const default_options = {
7569
condition: 'fail'
7670
}
7771

72+
const default_inputs_teams = {
73+
title: '',
74+
separator: true
75+
}
76+
77+
const default_inputs_slack = {
78+
title: '',
79+
separator: false
80+
}
81+
7882
module.exports = {
7983
run,
8084
default_options

src/extensions/report-portal-analysis.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const { getLaunchDetails, getLastLaunchByName } = require('../helpers/report-portal');
2+
const { addExtension } = require('../helpers/teams');
3+
const { addSectionText } = require('../helpers/slack');
24

35
function getReportPortalDefectsSummary(defects, bold = '**') {
46
const results = [];
@@ -30,28 +32,12 @@ function getReportPortalDefectsSummary(defects, bold = '**') {
3032
return results;
3133
}
3234

33-
function attachForTeams(payload, analyses) {
34-
payload.body.push({
35-
"type": "TextBlock",
36-
"text": "Report Portal Analysis",
37-
"isSubtle": true,
38-
"weight": "bolder",
39-
"separator": true
40-
});
41-
payload.body.push({
42-
"type": "TextBlock",
43-
"text": analyses.join(' | ')
44-
});
35+
function attachForTeams({ payload, analyses, extension }) {
36+
addExtension({ payload, extension, text: analyses.join(' | ')});
4537
}
4638

47-
function attachForSlack(payload, analyses) {
48-
payload.blocks.push({
49-
"type": "section",
50-
"text": {
51-
"type": "mrkdwn",
52-
"text": `*Report Portal Analysis*\n\n${analyses.join(' | ')}`
53-
}
54-
});
39+
function attachForSlack({ payload, analyses, extension }) {
40+
addSectionText({ payload, extension, text: analyses.join(' | ')});
5541
}
5642

5743
async function _getLaunchDetails(options) {
@@ -66,11 +52,13 @@ async function run({ extension, payload, target }) {
6652
const { statistics } = await _getLaunchDetails(extension.inputs);
6753
if (statistics && statistics.defects) {
6854
if (target.name === 'teams') {
55+
extension.inputs = Object.assign({}, default_inputs_teams, extension.inputs);
6956
const analyses = getReportPortalDefectsSummary(statistics.defects);
70-
attachForTeams(payload, analyses);
57+
attachForTeams({ payload, analyses, extension });
7158
} else {
59+
extension.inputs = Object.assign({}, default_inputs_slack, extension.inputs);
7260
const analyses = getReportPortalDefectsSummary(statistics.defects, '*');
73-
attachForSlack(payload, analyses);
61+
attachForSlack({ payload, analyses, extension });
7462
}
7563
}
7664
} catch (error) {
@@ -84,6 +72,16 @@ const default_options = {
8472
condition: 'fail'
8573
}
8674

75+
const default_inputs_teams = {
76+
title: 'Report Portal Analysis',
77+
separator: true
78+
}
79+
80+
const default_inputs_slack = {
81+
title: 'Report Portal Analysis',
82+
separator: false
83+
}
84+
8785
module.exports = {
8886
run,
8987
default_options

src/extensions/report-portal-history.js

+26-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const { getSuiteHistory, getLastLaunchByName, getLaunchDetails } = require('../helpers/report-portal');
2+
const { addExtension } = require('../helpers/teams');
3+
const { addSectionText } = require('../helpers/slack');
24

35
async function getLaunchHistory(inputs) {
46
if (!inputs.launch_id && inputs.launch_name) {
@@ -31,28 +33,18 @@ function getSymbols(launches) {
3133
return symbols;
3234
}
3335

34-
function attachForTeams(payload, symbols) {
35-
payload.body.push({
36-
"type": "TextBlock",
37-
"text": `Last ${symbols.length} Runs`,
38-
"isSubtle": true,
39-
"weight": "bolder",
40-
"separator": true
41-
});
42-
payload.body.push({
43-
"type": "TextBlock",
44-
"text": symbols.join(' ')
45-
});
36+
function attachForTeams({ payload, symbols, extension }) {
37+
if (extension.inputs.title === 'Last Runs') {
38+
extension.inputs.title = `Last ${symbols.length} Runs`
39+
}
40+
addExtension({ payload, extension, text: symbols.join(' ') });
4641
}
4742

48-
function attachForSlack(payload, symbols) {
49-
payload.blocks.push({
50-
"type": "section",
51-
"text": {
52-
"type": "mrkdwn",
53-
"text": `*Last ${symbols.length} Runs*\n\n${symbols.join(' ')}`
54-
}
55-
});
43+
function attachForSlack({ payload, symbols, extension }) {
44+
if (extension.inputs.title === 'Last Runs') {
45+
extension.inputs.title = `Last ${symbols.length} Runs`
46+
}
47+
addSectionText({ payload, extension, text: symbols.join(' ') });
5648
}
5749

5850
async function run({ extension, target, payload }) {
@@ -62,9 +54,11 @@ async function run({ extension, target, payload }) {
6254
const symbols = getSymbols(launches);
6355
if (symbols.length > 0) {
6456
if (target.name === 'teams') {
65-
attachForTeams(payload, symbols);
57+
extension.inputs = Object.assign({}, default_inputs_teams, extension.inputs);
58+
attachForTeams({ payload, symbols, extension });
6659
} else {
67-
attachForSlack(payload, symbols);
60+
extension.inputs = Object.assign({}, default_inputs_slack, extension.inputs);
61+
attachForSlack({ payload, symbols, extension });
6862
}
6963
}
7064
} catch (error) {
@@ -74,7 +68,16 @@ async function run({ extension, target, payload }) {
7468
}
7569

7670
const default_inputs = {
77-
history_depth: 5
71+
history_depth: 5,
72+
title: 'Last Runs',
73+
}
74+
75+
const default_inputs_teams = {
76+
separator: true
77+
}
78+
79+
const default_inputs_slack = {
80+
separator: false
7881
}
7982

8083
const default_options = {

src/helpers/constants.js

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1-
// Constants
2-
3-
const status = Object.freeze({
1+
const STATUS = Object.freeze({
42
PASS: "pass",
53
FAIL: "fail",
64
PASS_OR_FAIL: "passOrfail"
7-
})
5+
});
6+
7+
const HOOK = Object.freeze({
8+
START: 'start',
9+
POST_MAIN: 'post-main',
10+
END: "end",
11+
});
12+
13+
const TARGET = Object.freeze({
14+
SLACK: 'slack',
15+
TEAMS: 'teams',
16+
CUSTOM: "custom",
17+
DELAY: 'delay'
18+
});
19+
20+
const EXTENSION = Object.freeze({
21+
HYPERLINKS: 'hyperlinks',
22+
MENTIONS: 'mentions',
23+
REPORT_PORTAL_ANALYSIS: "report-portal-analysis",
24+
REPORT_PORTAL_HISTORY: 'report-portal-history',
25+
QUICK_CHART_TEST_SUMMARY: "quick-chart-test-summary"
26+
});
827

928
module.exports = Object.freeze({
10-
STATUS: status
11-
})
29+
STATUS,
30+
HOOK,
31+
TARGET,
32+
EXTENSION
33+
});

0 commit comments

Comments
 (0)