Skip to content

Commit

Permalink
Merge pull request #28 from leelaprasadv/feature/conditional-hyperlinks
Browse files Browse the repository at this point in the history
Conditional Hyperlinks support on slack and teams
  • Loading branch information
ASaiAnudeep authored Jun 5, 2022
2 parents 4e50891 + bbb4320 commit 38dc010
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 28 deletions.
56 changes: 34 additions & 22 deletions src/extensions/hyperlinks.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,54 @@
async function run({ target, extension, payload }) {
const { STATUS } = require("../helpers/constants");

async function run({ target, extension, payload, result }) {
if (target.name === 'teams') {
attachTeamLinks({ extension, payload });
attachTeamLinks({ extension, payload, result });
} else if (target.name === 'slack') {
attachSlackLinks({ extension, payload });
attachSlackLinks({ extension, payload, result });
}
}

function attachTeamLinks({ extension, payload }) {
function attachTeamLinks({ extension, payload, result }) {
const links = [];
for (const link of extension.inputs.links) {
links.push(`[${link.text}](${link.url})`);
link["condition"] = link.condition || default_options.condition;
if (link.condition.toLowerCase().includes(result.status.toLowerCase())) {
links.push(`[${link.text}](${link.url})`);
}
}
if (links.length) {
payload.body.push({
"type": "TextBlock",
"text": links.join(' | '),
"separator": true
});
}
payload.body.push({
"type": "TextBlock",
"text": links.join(' | '),
"separator": true
});
}

function attachSlackLinks({ extension, payload }) {
function attachSlackLinks({ extension, payload, result }) {
const links = [];
for (const link of extension.inputs.links) {
links.push(`<${link.url}|${link.text}>`);
link["condition"] = link.condition || default_options.condition;
if (link.condition.toLowerCase().includes(result.status.toLowerCase())) {
links.push(`<${link.url}|${link.text}>`);
}
}
if (links.length) {
payload.blocks.push({
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": links.join(' | ')
}
]
});
}
payload.blocks.push({
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": links.join(' | ')
}
]
});
}

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

module.exports = {
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Constants

const status = Object.freeze({
PASS: "pass",
FAIL: "fail",
PASS_OR_FAIL: "passOrfail"
})

module.exports = Object.freeze({
STATUS: status
})
211 changes: 207 additions & 4 deletions test/ext.hyperlink.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const { publish } = require('../src');

describe('extensions - hyperlinks', () => {

it('should send test-summary with links to teams', async () => {
const id = mock.addInteraction('post test-summary with hyperlinks to teams');
it('should send test-summary with links to teams - default condition', async () => {
const id = mock.addInteraction('post test-summary with hyperlinks to teams - pass status');
await publish({
config: {
"reports": [
Expand Down Expand Up @@ -50,8 +50,107 @@ describe('extensions - hyperlinks', () => {
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with links to slack', async () => {
const id = mock.addInteraction('post test-summary with hyperlinks to slack');
it('should send test-summary with links to teams - conditional hyperlinks - pass', async () => {
const id = mock.addInteraction('post test-summary with hyperlinks to teams - pass status');
await publish({
config: {
"reports": [
{
"targets": [
{
"name": "teams",
"inputs": {
"url": "http://localhost:9393/message"
},
"extensions": [
{
"name": "hyperlinks",
"inputs": {
"links": [
{
"text": "Pipeline",
"url": "some-url"
},
{
"text": "Video",
"url": "some-url",
"condition": "pass"
}
]
}
}
]
}
],
"results": [
{
"type": "testng",
"files": [
"test/data/testng/single-suite.xml"
]
}
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with links to teams - conditional hyperlinks - multiconditions', async () => {
const id = mock.addInteraction('post test-summary with hyperlinks to teams - fail status');
await publish({
config: {
"reports": [
{
"targets": [
{
"name": "teams",
"inputs": {
"url": "http://localhost:9393/message"
},
"extensions": [
{
"name": "hyperlinks",
"inputs": {
"links": [
{
"text": "Pipeline",
"url": "some-url",
},
{
"text": "S3 link",
"url": "some-url",
"condition": "pass"
},
{
"text": "Video",
"url": "some-url",
"condition": "fail"
}
]
}
}
]
}
],
"results": [
{
"type": "testng",
"files": [
"test/data/testng/single-suite-failures.xml"
]
}
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with links to slack - default condition', async () => {
const id = mock.addInteraction('post test-summary with hyperlinks to slack - pass status');
await publish({
config: {
"reports": [
Expand Down Expand Up @@ -96,6 +195,110 @@ describe('extensions - hyperlinks', () => {
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with links to slack - conditional hyperlinks - pass', async () => {
const id = mock.addInteraction('post test-summary with hyperlinks to slack - pass status');
await publish({
config: {
"reports": [
{
"targets": [
{
"name": "slack",
"inputs": {
"url": "http://localhost:9393/message"
},
"extensions": [
{
"name": "hyperlinks",
"inputs": {
"links": [
{
"text": "Pipeline",
"url": "some-url"
},
{
"text": "S3 link",
"url": "some-url",
"condition": "fail"
},
{
"text": "Video",
"url": "some-url",
"condition": "pass"
}
]
}
}
]
}
],
"results": [
{
"type": "testng",
"files": [
"test/data/testng/single-suite.xml"
]
}
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with links to slack - conditional hyperlinks - multiconditions', async () => {
const id = mock.addInteraction('post test-summary with hyperlinks to slack - fail status');
await publish({
config: {
"reports": [
{
"targets": [
{
"name": "slack",
"inputs": {
"url": "http://localhost:9393/message"
},
"extensions": [
{
"name": "hyperlinks",
"inputs": {
"links": [
{
"text": "Pipeline",
"url": "some-url",
},
{
"text": "S3 link",
"url": "some-url",
"condition": "pass"
},
{
"text": "Video",
"url": "some-url",
"condition": "fail"
}
]
}
}
]
}
],
"results": [
{
"type": "testng",
"files": [
"test/data/testng/single-suite-failures.xml"
]
}
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

afterEach(() => {
mock.clearInteractions();
});
Expand Down
39 changes: 38 additions & 1 deletion test/mocks/slack.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ addInteractionHandler('post failure-details to slack with single suite', () => {
}
});

addInteractionHandler('post test-summary with hyperlinks to slack', () => {
addInteractionHandler('post test-summary with hyperlinks to slack - pass status', () => {
return {
request: {
method: 'POST',
Expand Down Expand Up @@ -226,6 +226,43 @@ addInteractionHandler('post test-summary with hyperlinks to slack', () => {
}
});

addInteractionHandler('post test-summary with hyperlinks to slack - fail status', () => {
return {
request: {
method: 'POST',
path: '/message',
body: {
"attachments": [
{
"color": "#DC143C",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Default suite*\n\n*Results*: 3 / 4 Passed (75%)\n*Duration*: 00:02"
}
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "<some-url|Pipeline> | <some-url|Video>"
}
]
}
]
}
]
}
},
response: {
status: 200
}
}
});

addInteractionHandler('post test-summary to slack with report portal analysis', () => {
return {
request: {
Expand Down
Loading

0 comments on commit 38dc010

Please sign in to comment.