Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch link into button on "View Build" #35

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

});
});
Loading