From fc947bdbbe633b27d966961e5adb50d0984b1758 Mon Sep 17 00:00:00 2001 From: Luciano Lima Date: Tue, 13 Jun 2023 10:58:53 -0300 Subject: [PATCH] Allow user group slack mention when requesting github team --- __tests__/main.test.ts | 8 +++++++- src/main.ts | 29 +++++++++++++++++++---------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 9816bb23..0b2923ee 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -68,7 +68,7 @@ describe("src/main", () => { const dummyMapping = { github_user_1: "slack_user_1", - github_team_1: "slack_user_2", + github_team_1: "slack_usergroup_1", }; it("should call postToSlack if requested_user is listed in mapping", async () => { @@ -159,6 +159,12 @@ describe("src/main", () => { ); expect(slackMock.postToSlack).toHaveBeenCalledTimes(1); + + const call = slackMock.postToSlack.mock.calls[0]; + expect(call[0]).toEqual("dummy_url"); + expect(call[1].includes("")).toEqual(true); + expect(call[1].includes("")).toEqual(true); + expect(call[1].includes("by sender_github_username")).toEqual(true); }); }); diff --git a/src/main.ts b/src/main.ts index 78a1e4fe..8e39c9ec 100644 --- a/src/main.ts +++ b/src/main.ts @@ -46,34 +46,43 @@ export const convertToSlackUsername = ( return slackIds; }; +const getSlackMention = (requestedSlackUserId: string, requestedSlackUserGroupId: string): string => { + if (requestedSlackUserId) { + return `<@${requestedSlackUserId}>`; + } + + return `` +} + export const execPrReviewRequestedMention = async ( payload: WebhookPayload, allInputs: AllInputs, mapping: MappingFile, slackClient: Pick ): Promise => { - const requestedGithubUsername = - payload.requested_reviewer?.login || payload.requested_team?.name; + const requestedGithubUsername = payload.requested_reviewer?.login; + const requestedGithubTeam = payload.requested_team?.name; - if (!requestedGithubUsername) { - throw new Error("Can not find review requested user."); + if (!requestedGithubUsername && !requestedGithubTeam) { + throw new Error("Can not find review requested user or team."); } + + const slackUserIds = convertToSlackUsername([requestedGithubUsername], mapping); + const slackUserGroupIds = convertToSlackUsername([requestedGithubTeam], mapping); - const slackIds = convertToSlackUsername([requestedGithubUsername], mapping); - - if (slackIds.length === 0) { + if (slackUserIds.length === 0 && slackUserGroupIds.length === 0) { core.debug( - "finish execPrReviewRequestedMention because slackIds.length === 0" + "finish execPrReviewRequestedMention because slackUserIds and slackUserGroupIds length === 0" ); return; } const title = payload.pull_request?.title; const url = payload.pull_request?.html_url; - const requestedSlackUserId = slackIds[0]; const requestUsername = payload.sender?.login; - const message = `<@${requestedSlackUserId}> has been requested to review <${url}|${title}> by ${requestUsername}.`; + const slackMention = getSlackMention(slackUserIds[0], slackUserGroupIds[0]); + const message = `${slackMention} has been requested to review <${url}|${title}> by ${requestUsername}.` const { slackWebhookUrl, iconUrl, botName } = allInputs; await slackClient.postToSlack(slackWebhookUrl, message, { iconUrl, botName });