Skip to content

Commit

Permalink
Switch link into button on "View Build" (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
franklychilled authored Feb 21, 2024
1 parent 65645ff commit d31a8ae
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 31 deletions.
27 changes: 21 additions & 6 deletions src/slackNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export const getTextSummaryLine = (result: JunitResult): string => {
return "No tests data generated!";
};

export const getText = (result: JunitResult): string => {
return `${getEmoji(result)} *${result.buildkite_pipeline} (${result.git_branch_name}) #${result.build_id}*\n${result.git_comment} - ${result.git_username} (${result.git_log})\n*${getTextSummaryLine(result)}*\n<${result.build_url}|View build>`;
export const getCommitText = (result: JunitResult): string => {
return `${getEmoji(result)} *${result.buildkite_pipeline} (${result.git_branch_name}) #${result.build_id}*\n${result.git_comment} - ${result.git_username} (${result.git_log})`;
};

export const getSlackMessageAttachments = (result: JunitResult): any => {
export const getSlackMessageAttachments = (result: JunitResult): unknown => {
return [
{
"color": getColor(result),
Expand All @@ -58,15 +58,30 @@ export const getSlackMessageAttachments = (result: JunitResult): any => {
"type": "section",
"text": {
"type": "mrkdwn",
"text": getText(result)
"text": getCommitText(result)
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "View build"
},
"url": result.build_url
}
},
{
"type": "section",
"text": {
"text": `*${getTextSummaryLine(result)}*`,
"type": "mrkdwn"
}
}
]
}
];
};

export const sendResultToSlack = async (slackToken: string, channel: string, junitResult: JunitResult): Promise<any> => {
export const sendResultToSlack = async (slackToken: string, channel: string, junitResult: JunitResult): Promise<unknown> => {
let goodToken = "";
for (let i = 0; i < slackToken.length; i++) {
if (checkChar(slackToken[i])) {
Expand All @@ -76,7 +91,7 @@ export const sendResultToSlack = async (slackToken: string, channel: string, jun
console.warn(`Invalid character in token - code: ${slackToken.charCodeAt(i)} => '${slackToken[i]}'`);
}
}
return sendSlackMessage(goodToken, channel, getSlackMessageAttachments(junitResult));
return sendSlackMessage(goodToken, channel, getSlackMessageAttachments(junitResult) as any[]);
};

const checkChar = (c: string) => {
Expand Down
109 changes: 84 additions & 25 deletions test/slackNotification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,31 @@ describe("Failed test", () => {

expect(actual).toStrictEqual([
{
"color": "#B94A48",
"blocks": [
{
"type": "section",
"accessory": {
"text": {
"text": "View build",
"type": "plain_text"
},
"type": "button",
"url": "https://www.iress.com/mybuild"
},
"text": {
"type": "mrkdwn",
"text": ":-1: :-1: *My Build pipeline (hac-483_branch) #123*\nInitial commit - F T (a1b2c3)\n*Tests failed: 3, passed: 1, ignored: 2*\n<https://www.iress.com/mybuild|View build>"
}
"text": ":-1: :-1: *My Build pipeline (hac-483_branch) #123*\nInitial commit - F T (a1b2c3)",
"type": "mrkdwn"
},
"type": "section"
},
{
"text": {
"text": "*Tests failed: 3, passed: 1, ignored: 2*",
"type": "mrkdwn"
},
"type": "section"
}
]
],
"color": "#B94A48"
}
]);

Expand Down Expand Up @@ -110,8 +125,23 @@ describe("Passed test", () => {
{
"blocks": [
{
"accessory": {
"text": {
"text": "View build",
"type": "plain_text"
},
"type": "button",
"url": "https://www.iress.com/myotherbuild"
},
"text": {
"text": ":+1: *My Build other pipeline (hac-483_other_branch) #456*\nSecond commit - Frankly Chilled (a1b2c3d4)\n*Tests passed: 1*\n<https://www.iress.com/myotherbuild|View build>",
"text": ":+1: *My Build other pipeline (hac-483_other_branch) #456*\nSecond commit - Frankly Chilled (a1b2c3d4)",
"type": "mrkdwn"
},
"type": "section"
},
{
"text": {
"text": "*Tests passed: 1*",
"type": "mrkdwn"
},
"type": "section"
Expand Down Expand Up @@ -154,11 +184,26 @@ describe("No tests", () => {
{
"blocks": [
{
"type": "section",
"text": {
"text": ":-1: *My Build other pipeline (hac-483_other_branch) #789*\nSecond commit - Frankly Chilled (a1b2c3d4)\n*No tests data generated!*\n<https://www.iress.com/myotherbuild|View build>",
"type": "mrkdwn"
"type": "mrkdwn",
"text": ":-1: *My Build other pipeline (hac-483_other_branch) #789*\nSecond commit - Frankly Chilled (a1b2c3d4)"
},
"type": "section"
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "View build"
},
"url": "https://www.iress.com/myotherbuild"
}
},
{
"type": "section",
"text": {
"text": "*No tests data generated!*",
"type": "mrkdwn"
}
}
],
"color": "#B94A48"
Expand All @@ -174,23 +219,37 @@ describe("No tests", () => {
await sendResultToSlack(SLACK_TOKEN, SLACK_CHANNEL, result);

// verify call to chat
const attachments = [{
"blocks": [
{
"text": {
"text": ":-1: *My Build other pipeline (hac-483_other_branch) #789*\n" +
"Second commit - Frankly Chilled (a1b2c3d4)\n" +
"*No tests data generated!*\n" +
"<https://www.iress.com/myotherbuild|View build>",
"type": "mrkdwn",
const attachments = [
{
"color": "#B94A48",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ":-1: *My Build other pipeline (hac-483_other_branch) #789*\nSecond commit - Frankly Chilled (a1b2c3d4)"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "View build"
},
"url": "https://www.iress.com/myotherbuild"
}
},
"type": "section",
},
],
"color": "#B94A48",
}
{
"type": "section",
"text": {
"text": "*No tests data generated!*",
"type": "mrkdwn"
}
}
]
}
];
expect(_postMessageMock).toHaveBeenCalledWith({channel: SLACK_CHANNEL, attachments, text: ""});
const expected = {channel: SLACK_CHANNEL, attachments, text: ""};
expect(_postMessageMock).toHaveBeenCalledWith(expected);

});
});

0 comments on commit d31a8ae

Please sign in to comment.