Skip to content

Commit cbb6dcf

Browse files
committed
feat: Push event
1 parent 1e2191b commit cbb6dcf

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

events/push.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
type APIMessageTopLevelComponent,
3+
ComponentType,
4+
SeparatorSpacingSize,
5+
} from "@discordjs/core/http-only";
6+
import type { PushEvent } from "@octokit/webhooks-types";
7+
8+
export function pushCreatedComponents(payload: PushEvent): APIMessageTopLevelComponent[] {
9+
const branch = payload.ref.replace("refs/heads/", "");
10+
11+
const commits = payload.commits.map(
12+
(commit) =>
13+
`[\`${commit.id.slice(0, 7)}\`](${commit.url}) ${commit.committer.name}: ${commit.message} <t:${Date.parse(commit.timestamp) / 1000}:R>`,
14+
);
15+
16+
const commitDescription =
17+
commits.length > 1
18+
? `[${payload.before.slice(0, 7)}...${payload.after.slice(0, 7)}](${payload.compare})\n${commits.join("\n")}`
19+
: commits[0]!;
20+
21+
return [
22+
{
23+
type: ComponentType.Container,
24+
components: [
25+
{
26+
type: ComponentType.TextDisplay,
27+
content: `[${payload.sender.name ?? payload.sender.login}](${payload.sender.html_url}) committed to [${payload.repository.name}:${branch}](${payload.repository.html_url}).\n${commitDescription}`,
28+
},
29+
{
30+
type: ComponentType.Separator,
31+
divider: true,
32+
spacing: SeparatorSpacingSize.Small,
33+
},
34+
{
35+
type: ComponentType.TextDisplay,
36+
content: `-# [${payload.repository.full_name}](${payload.repository.html_url})`,
37+
},
38+
],
39+
},
40+
];
41+
}

source/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
} from "@discordjs/core/http-only";
77
import { REST } from "@discordjs/rest";
88
import { Webhooks } from "@octokit/webhooks";
9-
import type { StarEvent, WebhookEvent, WebhookEventName } from "@octokit/webhooks-types";
9+
import type { PushEvent, StarEvent, WebhookEvent, WebhookEventName } from "@octokit/webhooks-types";
1010
import { starCreatedComponents } from "../events/star.js";
11+
import { pushCreatedComponents } from "../events/push.js";
1112

1213
interface Env {
1314
GITHUB_WEBHOOK_SERCET: string;
@@ -47,7 +48,9 @@ export default {
4748
const payload = JSON.parse(text) as WebhookEvent;
4849
let components: APIMessageTopLevelComponent[] | undefined;
4950

50-
if (eventType === "star") {
51+
if (eventType === "push") {
52+
components = pushCreatedComponents(payload as PushEvent);
53+
} else if (eventType === "star") {
5154
const starEvent = payload as StarEvent;
5255

5356
if (starEvent.action === "deleted") {

0 commit comments

Comments
 (0)