diff --git a/src/main.ts b/src/main.ts index 373c161c..bc6c4a33 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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"; @@ -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; }; @@ -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; @@ -107,13 +107,13 @@ 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; } @@ -121,7 +121,7 @@ export const execNormalMention = async ( 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; } @@ -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 ( @@ -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; } @@ -188,7 +186,7 @@ export const execApproveMention = async ( { iconUrl, botName } ); - core.debug( + debug( ["postToSlack result", JSON.stringify({ postSlackResult }, null, 2)].join( "\n" ) @@ -211,7 +209,7 @@ 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; @@ -219,25 +217,25 @@ export const execPostError = async ( }; 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, @@ -250,13 +248,13 @@ const getAllInputs = (): AllInputs => { }; export const main = async (): Promise => { - 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; @@ -275,7 +273,7 @@ export const main = async (): Promise => { ); })(); - core.debug(JSON.stringify({ mapping }, null, 2)); + debug(JSON.stringify({ mapping }, null, 2)); if (payload.action === "review_requested") { await execPrReviewRequestedMention( @@ -284,7 +282,7 @@ export const main = async (): Promise => { mapping, SlackRepositoryImpl ); - core.debug("finish execPrReviewRequestedMention()"); + debug("finish execPrReviewRequestedMention()"); return; } @@ -302,7 +300,7 @@ export const main = async (): Promise => { ignoreSlackIds.push(sentSlackUserId); } - core.debug( + debug( [ "execApproveMention()", JSON.stringify({ sentSlackUserId }, null, 2), @@ -317,9 +315,9 @@ export const main = async (): Promise => { 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)); } };