Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
abeyuya committed Aug 16, 2023
1 parent bc55829 commit c33e872
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as core from "@actions/core";
import { debug, warning, getInput, setFailed } from "@actions/core";
import { context } from "@actions/github";
import { WebhookPayload } from "@actions/github/lib/interfaces";

Expand Down Expand Up @@ -35,13 +35,13 @@ export const convertToSlackUsername = (
githubUsernames: string[],
mapping: MappingFile
): string[] => {
core.debug(JSON.stringify({ githubUsernames }, null, 2));
debug(JSON.stringify({ githubUsernames }, null, 2));

const slackIds = githubUsernames
.map((githubUsername) => mapping[githubUsername])
.filter((slackId) => slackId !== undefined) as string[];

core.debug(JSON.stringify({ slackIds }, null, 2));
debug(JSON.stringify({ slackIds }, null, 2));

return slackIds;
};
Expand Down Expand Up @@ -80,7 +80,7 @@ export const execPrReviewRequestedMention = async (
);

if (slackUserIds.length === 0 && slackUserGroupIds.length === 0) {
core.debug(
debug(
"finish execPrReviewRequestedMention because slackUserIds and slackUserGroupIds length === 0"
);
return;
Expand All @@ -107,21 +107,21 @@ export const execNormalMention = async (
const info = pickupInfoFromGithubPayload(payload);

if (info.body === null) {
core.debug("finish execNormalMention because info.body === null");
debug("finish execNormalMention because info.body === null");
return;
}

const githubUsernames = pickupUsername(info.body);
if (githubUsernames.length === 0) {
core.debug("finish execNormalMention because githubUsernames.length === 0");
debug("finish execNormalMention because githubUsernames.length === 0");
return;
}

const slackIds = convertToSlackUsername(githubUsernames, mapping);
const slackIdsWithoutIgnore = arrayDiff(slackIds, ignoreSlackIds);

if (slackIdsWithoutIgnore.length === 0) {
core.debug("finish execNormalMention because slackIds.length === 0");
debug("finish execNormalMention because slackIds.length === 0");
return;
}

Expand All @@ -140,9 +140,7 @@ export const execNormalMention = async (
botName,
});

core.debug(
["postToSlack result", JSON.stringify({ result }, null, 2)].join("\n")
);
debug(["postToSlack result", JSON.stringify({ result }, null, 2)].join("\n"));
};

export const execApproveMention = async (
Expand All @@ -164,7 +162,7 @@ export const execApproveMention = async (
const slackIds = convertToSlackUsername([prOwnerGithubUsername], mapping);

if (slackIds.length === 0) {
core.debug("finish execApproveMention because slackIds.length === 0");
debug("finish execApproveMention because slackIds.length === 0");
return null;
}

Expand All @@ -188,7 +186,7 @@ export const execApproveMention = async (
{ iconUrl, botName }
);

core.debug(
debug(
["postToSlack result", JSON.stringify({ postSlackResult }, null, 2)].join(
"\n"
)
Expand All @@ -211,33 +209,33 @@ export const execPostError = async (
const currentJobUrl = runId ? buildCurrentJobUrl(runId) : undefined;
const message = buildSlackErrorMessage(error, currentJobUrl);

core.warning(message);
warning(message);

const { slackWebhookUrl, iconUrl, botName } = allInputs;

await slackClient.postToSlack(slackWebhookUrl, message, { iconUrl, botName });
};

const getAllInputs = (): AllInputs => {
const slackWebhookUrl = core.getInput("slack-webhook-url", {
const slackWebhookUrl = getInput("slack-webhook-url", {
required: true,
});

if (!slackWebhookUrl) {
core.setFailed("Error! Need to set `slack-webhook-url`.");
setFailed("Error! Need to set `slack-webhook-url`.");
}

const repoToken = core.getInput("repo-token", { required: true });
const repoToken = getInput("repo-token", { required: true });
if (!repoToken) {
core.setFailed("Error! Need to set `repo-token`.");
setFailed("Error! Need to set `repo-token`.");
}

const iconUrl = core.getInput("icon-url", { required: false });
const botName = core.getInput("bot-name", { required: false });
const configurationPath = core.getInput("configuration-path", {
const iconUrl = getInput("icon-url", { required: false });
const botName = getInput("bot-name", { required: false });
const configurationPath = getInput("configuration-path", {
required: true,
});
const runId = core.getInput("run-id", { required: false });
const runId = getInput("run-id", { required: false });

return {
repoToken,
Expand All @@ -250,13 +248,13 @@ const getAllInputs = (): AllInputs => {
};

export const main = async (): Promise<void> => {
core.debug("start main()");
debug("start main()");

const { payload } = context;
core.debug(JSON.stringify({ payload }, null, 2));
debug(JSON.stringify({ payload }, null, 2));

const allInputs = getAllInputs();
core.debug(JSON.stringify({ allInputs }, null, 2));
debug(JSON.stringify({ allInputs }, null, 2));

const { repoToken, configurationPath } = allInputs;

Expand All @@ -275,7 +273,7 @@ export const main = async (): Promise<void> => {
);
})();

core.debug(JSON.stringify({ mapping }, null, 2));
debug(JSON.stringify({ mapping }, null, 2));

if (payload.action === "review_requested") {
await execPrReviewRequestedMention(
Expand All @@ -284,7 +282,7 @@ export const main = async (): Promise<void> => {
mapping,
SlackRepositoryImpl
);
core.debug("finish execPrReviewRequestedMention()");
debug("finish execPrReviewRequestedMention()");
return;
}

Expand All @@ -302,7 +300,7 @@ export const main = async (): Promise<void> => {
ignoreSlackIds.push(sentSlackUserId);
}

core.debug(
debug(
[
"execApproveMention()",
JSON.stringify({ sentSlackUserId }, null, 2),
Expand All @@ -317,9 +315,9 @@ export const main = async (): Promise<void> => {
SlackRepositoryImpl,
ignoreSlackIds
);
core.debug("finish execNormalMention()");
debug("finish execNormalMention()");
} catch (error: unknown) {
await execPostError(error as Error, allInputs, SlackRepositoryImpl);
core.warning(JSON.stringify({ payload }, null, 2));
warning(JSON.stringify({ payload }, null, 2));
}
};

0 comments on commit c33e872

Please sign in to comment.