Skip to content

Commit

Permalink
Merge pull request #252 from lucianomlima/feat/slack-usergroup-mention
Browse files Browse the repository at this point in the history
feature(mentions): allow user group slack mention when requesting github team
  • Loading branch information
abeyuya authored Jun 15, 2023
2 parents 0610b20 + fc947bd commit f644063
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
8 changes: 7 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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("<!subteam^slack_usergroup_1>")).toEqual(true);
expect(call[1].includes("<pr_url|pr_title>")).toEqual(true);
expect(call[1].includes("by sender_github_username")).toEqual(true);
});
});

Expand Down
29 changes: 19 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,43 @@ export const convertToSlackUsername = (
return slackIds;
};

const getSlackMention = (requestedSlackUserId: string, requestedSlackUserGroupId: string): string => {
if (requestedSlackUserId) {
return `<@${requestedSlackUserId}>`;
}

return `<!subteam^${requestedSlackUserGroupId}>`
}

export const execPrReviewRequestedMention = async (
payload: WebhookPayload,
allInputs: AllInputs,
mapping: MappingFile,
slackClient: Pick<typeof SlackRepositoryImpl, "postToSlack">
): Promise<void> => {
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 });
Expand Down

0 comments on commit f644063

Please sign in to comment.