Skip to content

Commit

Permalink
Support slack group name mentions (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
leelaprasadv committed Apr 7, 2024
1 parent 981211e commit e85c6da
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/extensions/mentions.js
Expand Up @@ -34,9 +34,20 @@ function attachForTeam({ extension, payload }) {
}
}

function formatSlackMentions({slack_uid, slack_gid}) {
if (slack_gid && slack_uid) {
throw new Error(`Error in slack extension configuration. Either slack user or group Id is allowed`);
}
if (slack_uid) {
return `<@${slack_uid}>`
}
const tagPrefix = ["here", "everyone", "channel"].includes(slack_gid.toLowerCase()) ? "" : "subteam^";
return `<!${tagPrefix}${slack_gid}>`
}

function attachForSlack({ extension, payload }) {
const users = getUsers(extension);
const user_ids = users.map(user => `<@${user.slack_uid}>`);
const user_ids = users.map(formatSlackMentions);
if (users.length > 0) {
addSlackExtension({ payload, extension, text: user_ids.join(' | ') });
}
Expand Down
84 changes: 84 additions & 0 deletions test/ext.mentions.spec.js
Expand Up @@ -148,6 +148,90 @@ describe('extensions - mentions', () => {
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should mention group name in slack', async () => {
const id = mock.addInteraction('post test-summary with mentions group name to slack');
await publish({
config: {
"reports": [
{
"targets": [
{
"name": "slack",
"inputs": {
"url": "http://localhost:9393/message"
},
"extensions": [
{
"name": "mentions",
"inputs": {
"users": [
{
"name": "mom",
"slack_gid": "ULA15K66M"
}
]
}
}
]
}
],
"results": [
{
"type": "testng",
"files": [
"test/data/testng/single-suite-failures.xml"
]
}
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should mention special group name in slack', async () => {
const id = mock.addInteraction('post test-summary with mentions special group name to slack');
await publish({
config: {
"reports": [
{
"targets": [
{
"name": "slack",
"inputs": {
"url": "http://localhost:9393/message"
},
"extensions": [
{
"name": "mentions",
"inputs": {
"users": [
{
"name": "mom",
"slack_gid": "here"
}
]
}
}
]
}
],
"results": [
{
"type": "testng",
"files": [
"test/data/testng/single-suite-failures.xml"
]
}
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should mention users with schedule rotation in slack', async () => {
const id = mock.addInteraction('post test-summary with mentions to slack');
await publish({
Expand Down
63 changes: 63 additions & 0 deletions test/mocks/slack.mock.js
Expand Up @@ -385,6 +385,69 @@ addInteractionHandler('post test-summary with mentions to slack', () => {
}
});


addInteractionHandler('post test-summary with mentions group name to slack', () => {
return {
request: {
method: 'POST',
path: '/message',
body: {
"attachments": [
{
"color": "#DC143C",
"blocks": [
{
"@DATA:TEMPLATE@": "SLACK_ROOT_SINGLE_SUITE_FAILURE"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<!subteam^ULA15K66M>"
}
}
]
}
]
}
},
response: {
status: 200
}
}
});

addInteractionHandler('post test-summary with mentions special group name to slack', () => {
return {
request: {
method: 'POST',
path: '/message',
body: {
"attachments": [
{
"color": "#DC143C",
"blocks": [
{
"@DATA:TEMPLATE@": "SLACK_ROOT_SINGLE_SUITE_FAILURE"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<!here>"
}
}
]
}
]
}
},
response: {
status: 200
}
}
});

addInteractionHandler('post test-summary to slack with qc-test-summary', (ctx) => {
return {
request: {
Expand Down

0 comments on commit e85c6da

Please sign in to comment.