diff --git a/src/main.ts b/src/main.ts index 71256c73..4be9376d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,12 +3,12 @@ import { context } from "@actions/github"; import { WebhookPayload } from "@actions/github/lib/interfaces"; import { - pickupUsername, - pickupInfoFromGithubPayload, - needToSendApproveMention, latestReviewer, + pickupUsername, + pickupInfoFromGithubPayload, + needToSendApproveMention, latestReviewer, needToMention, } from "./modules/github"; import { - buildChatworkErrorMessage, + buildChatworkErrorMessage, buildChatworkPostMentionMessage, buildChatworkPostMessage, ChatworkRepositoryImpl, } from "./modules/chatwork"; @@ -136,6 +136,36 @@ export const execPrReviewRequestedMention = async ( await chatworkClient.postToChatwork(apiToken, roomId, message); }; +export const execNormalComment = async ( + payload: WebhookPayload, + allInputs: AllInputs, + mapping: MappingFile, + chatworkClient: Pick +): Promise => { + const info = pickupInfoFromGithubPayload(payload); + + if (info.body === null) { + core.debug("finish execNormalMention because info.body === null"); + return; + } + + const message = buildChatworkPostMessage( + info.title, + info.url, + info.body, + info.senderName + ); + + const account = mapping[info.senderName]; + + const result = await chatworkClient.postToChatwork(allInputs.apiToken, account.room_id, message); + + core.debug( + ["postToSlack result", JSON.stringify({result}, null, 2)].join("\n") + ); + +}; + export const execNormalMention = async ( payload: WebhookPayload, allInputs: AllInputs, @@ -168,7 +198,7 @@ export const execNormalMention = async ( continue; } - const message = buildChatworkPostMessage( + const message = buildChatworkPostMentionMessage( [account.account_id], info.title, info.url, @@ -341,15 +371,28 @@ export const main = async (): Promise => { JSON.stringify({ sentSlackUserId }, null, 2), ].join("\n") ); + return; + } + + if (needToMention(payload, mapping)) { + await execNormalMention( + payload, + allInputs, + mapping, + ChatworkRepositoryImpl, + ); + core.debug("finish execNormalMention()"); + return; } - await execNormalMention( - payload, - allInputs, - mapping, - ChatworkRepositoryImpl, - ); - core.debug("finish execNormalMention()"); + await execNormalComment( + payload, + allInputs, + mapping, + ChatworkRepositoryImpl, + ); + core.debug("finish execNormalComment()"); + } catch (error: any) { await execPostError(error, allInputs); core.warning(JSON.stringify({ payload }, null, 2)); diff --git a/src/modules/chatwork.ts b/src/modules/chatwork.ts index f4fd96f5..9a6d2561 100644 --- a/src/modules/chatwork.ts +++ b/src/modules/chatwork.ts @@ -1,7 +1,7 @@ import * as core from "@actions/core"; import axios from "axios"; -export const buildChatworkPostMessage = ( +export const buildChatworkPostMentionMessage = ( chatworkIdsForMention: string[], issueTitle: string, commentLink: string, @@ -20,6 +20,19 @@ export const buildChatworkPostMessage = ( return `${message}\n${body}`; }; +export const buildChatworkPostMessage = ( + issueTitle: string, + commentLink: string, + githubBody: string, + senderName: string +): string => { + const message = [ + `commented at ${commentLink} ${issueTitle} by ${senderName}`, + ].join(" "); + + return `${message}\n${githubBody}`; +}; + export const buildChatworkErrorMessage = ( error: Error, currentJobUrl?: string diff --git a/src/modules/github.ts b/src/modules/github.ts index dda23f1e..2794f0a4 100644 --- a/src/modules/github.ts +++ b/src/modules/github.ts @@ -1,6 +1,8 @@ import { WebhookPayload } from "@actions/github/lib/interfaces"; import axios from "axios"; import * as core from "@actions/core"; +import {convertToChatworkUsername} from "../main"; +import {MappingFile} from "./mappingConfig"; const uniq = (arr: T[]): T[] => [...new Set(arr)]; @@ -37,6 +39,29 @@ export const needToSendApproveMention = (payload: WebhookPayload): boolean => { return false; }; +export const needToMention = (payload: WebhookPayload, mapping: MappingFile,): boolean => { + const info = pickupInfoFromGithubPayload(payload); + + if (info.body === null) { + core.debug("finish execNormalMention because info.body === null"); + return false; + } + + const githubUsernames = pickupUsername(info.body); + if (githubUsernames.length === 0) { + core.debug("finish execNormalMention because githubUsernames.length === 0"); + return false; + } + + const slackIds = convertToChatworkUsername(githubUsernames, mapping); + if (slackIds.length === 0) { + core.debug("finish execNormalMention because slackIds.length === 0"); + return false; + } + + return true; +}; + type GithubGetReviewerResult = { users: GithubGetReviewerNameResult[] };