Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
普通のコメントも通知を送る
Browse files Browse the repository at this point in the history
  • Loading branch information
shikajiro committed Sep 14, 2023
1 parent bb7b8dd commit 528a76c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 13 deletions.
67 changes: 55 additions & 12 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<typeof ChatworkRepositoryImpl, "postToChatwork">
): Promise<void> => {
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,
Expand Down Expand Up @@ -168,7 +198,7 @@ export const execNormalMention = async (
continue;
}

const message = buildChatworkPostMessage(
const message = buildChatworkPostMentionMessage(
[account.account_id],
info.title,
info.url,
Expand Down Expand Up @@ -341,15 +371,28 @@ export const main = async (): Promise<void> => {
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));
Expand Down
15 changes: 14 additions & 1 deletion src/modules/chatwork.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/modules/github.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(arr: T[]): T[] => [...new Set(arr)];

Expand Down Expand Up @@ -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[]
};
Expand Down

0 comments on commit 528a76c

Please sign in to comment.