Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions locales/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,11 @@ e_project_item_status_changed =
👉 status: <b>{ $status }</b>

{ $repoHashtag } #project

e_comment_created =
💬 <a href="{ $ghProfileUrl }">{ $commentAuthor }</a> { $telegramStatus } commented on <a href="{ $commentLink }">{ $repoName }#{ $number }</a>:

<b>{ $title }</b>
<blockquote>{ $commentPreview }</blockquote>

{ $repoHashtag } #{ $type }
55 changes: 55 additions & 0 deletions src/lib/github/webhooks/handlers/comment-created.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { HandlerFunction } from "@octokit/webhooks/types";

import { bot } from "#bot";

import { escapeHtml } from "../../../escape-html.ts";
import { botText, getRepoHashtag, getUser } from "./_utils.ts";

export const commentCreatedCallback: HandlerFunction<
"issue_comment.created" | "pull_request_review_comment.created",
unknown
> = async (event) => {
const comment = event.payload.comment;
const repository = event.payload.repository;
const sender = event.payload.sender;

const user = await getUser(sender);
const repoHashtag = getRepoHashtag(repository.name);

let title: string;
let number: number;
let type: "issue" | "pull_request";

if ("issue" in event.payload) {
const issue = event.payload.issue;
title = issue.title;
number = issue.number;
type = "issue";
} else if ("pull_request" in event.payload) {
const pr = event.payload.pull_request;
title = pr.title;
number = pr.number;
type = "pull_request";
} else {
return;
}

const commentLink = comment.html_url;
const commentPreview = comment.body.length > 100 ? `${comment.body.substring(0, 100)}...` : comment.body;

await bot.announce(
botText("e_comment_created", {
commentAuthor: escapeHtml(user.ghDisplayname),
telegramStatus: user.telegramStatus,
repoName: escapeHtml(repository.full_name),
repoHashtag: escapeHtml(repoHashtag),
title: escapeHtml(title),
number: number.toString(),
commentPreview: escapeHtml(commentPreview),
commentLink: escapeHtml(commentLink),
ghProfileUrl: escapeHtml(user.ghProfileUrl),
type,
}),
{ link_preview_options: { prefer_small_media: true, url: commentLink } },
);
};
5 changes: 4 additions & 1 deletion src/lib/github/webhooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Webhooks } from "@octokit/webhooks";
import { config } from "#config";

import { commentCreatedCallback } from "./handlers/comment-created.ts";
import { issuesAssignedCallback } from "./handlers/issues-assigned.ts";
import { issuesOpenedCallback } from "./handlers/issues-opened.ts";
import { projectItemEditedCallback } from "./handlers/projects-item-edited.ts";
Expand All @@ -20,4 +21,6 @@ webhooks.on("pull_request.opened", withGuards(pullRequestOpenedCallback));
webhooks.on("release.created", withGuards(releaseCreatedCallback));
webhooks.on("repository.created", withGuards(repositoryCreatedCallback));
webhooks.on("star.created", withGuards(starCreatedCallback));
webhooks.on("projects_v2_item.edited", withGuards(projectItemEditedCallback, { skipRepositoryCheck: true }));
webhooks.on("issue_comment.created", withGuards(commentCreatedCallback));
webhooks.on("pull_request_review_comment.created", withGuards(commentCreatedCallback));
webhooks.on("projects_v2_item.edited", withGuards(projectItemEditedCallback));
Loading