Skip to content

Commit

Permalink
update build 182
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Jun 15, 2023
2 parents 177d988 + f644063 commit 50f3856
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 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
26 changes: 17 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1587,22 +1587,30 @@ const convertToSlackUsername = (githubUsernames, mapping) => {
return slackIds;
};
exports.convertToSlackUsername = convertToSlackUsername;
const getSlackMention = (requestedSlackUserId, requestedSlackUserGroupId) => {
if (requestedSlackUserId) {
return `<@${requestedSlackUserId}>`;
}
return `<!subteam^${requestedSlackUserGroupId}>`;
};
const execPrReviewRequestedMention = async (payload, allInputs, mapping, slackClient) => {
var _a, _b, _c, _d, _e;
const requestedGithubUsername = ((_a = payload.requested_reviewer) === null || _a === void 0 ? void 0 : _a.login) || ((_b = payload.requested_team) === null || _b === void 0 ? void 0 : _b.name);
if (!requestedGithubUsername) {
throw new Error("Can not find review requested user.");
}
const slackIds = (0, exports.convertToSlackUsername)([requestedGithubUsername], mapping);
if (slackIds.length === 0) {
core.debug("finish execPrReviewRequestedMention because slackIds.length === 0");
const requestedGithubUsername = (_a = payload.requested_reviewer) === null || _a === void 0 ? void 0 : _a.login;
const requestedGithubTeam = (_b = payload.requested_team) === null || _b === void 0 ? void 0 : _b.name;
if (!requestedGithubUsername && !requestedGithubTeam) {
throw new Error("Can not find review requested user or team.");
}
const slackUserIds = (0, exports.convertToSlackUsername)([requestedGithubUsername], mapping);
const slackUserGroupIds = (0, exports.convertToSlackUsername)([requestedGithubTeam], mapping);
if (slackUserIds.length === 0 && slackUserGroupIds.length === 0) {
core.debug("finish execPrReviewRequestedMention because slackUserIds and slackUserGroupIds length === 0");
return;
}
const title = (_c = payload.pull_request) === null || _c === void 0 ? void 0 : _c.title;
const url = (_d = payload.pull_request) === null || _d === void 0 ? void 0 : _d.html_url;
const requestedSlackUserId = slackIds[0];
const requestUsername = (_e = payload.sender) === null || _e === void 0 ? void 0 : _e.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
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

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 50f3856

Please sign in to comment.