Skip to content

Commit 712ac03

Browse files
committed
fix: restore features lost in rebase
1 parent 731f942 commit 712ac03

5 files changed

Lines changed: 73 additions & 8 deletions

File tree

src/config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ export async function getRepoConfig(
1616
): Promise<RepoConfig | null> {
1717
const [owner] = fullName.split("/");
1818

19-
const exact = await kv.get(configKey(fullName));
19+
const exactKey = configKey(fullName);
20+
const exact = await kv.get(exactKey);
2021
if (exact) {
2122
return JSON.parse(exact) as RepoConfig;
2223
}
2324

24-
const wildcard = await kv.get(wildcardKey(owner));
25+
const wildKey = wildcardKey(owner);
26+
const wildcard = await kv.get(wildKey);
2527
if (wildcard) {
2628
return JSON.parse(wildcard) as RepoConfig;
2729
}
@@ -33,7 +35,8 @@ export async function getOrgWildcardConfig(
3335
kv: KVNamespace,
3436
orgName: string,
3537
): Promise<RepoConfig | null> {
36-
const raw = await kv.get(wildcardKey(orgName));
38+
const key = wildcardKey(orgName);
39+
const raw = await kv.get(key);
3740
if (!raw) return null;
3841
return JSON.parse(raw) as RepoConfig;
3942
}

src/formatters/issues.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ export function formatIssues(event: GitHubEvent): string {
2727
const lines: string[] = [];
2828
lines.push(headerLine("Event: ", `${emoji} issue`));
2929
lines.push(headerLine("Repo: ", `<a href="${repoUrl}">${escapeHtml(repo)}</a>`));
30+
lines.push(headerLine("Number: ", `#${number}`));
31+
lines.push(headerLine("Title: ", `<a href="${url}">${escapeHtml(title)}</a>`));
3032
lines.push(headerLine("Action: ", actionText));
3133
lines.push(headerLine("By: ", `<a href="${senderUrl}">${escapeHtml(sender)}</a>`));
3234
if (labels.length > 0) {
3335
lines.push(headerLine("Labels: ", labels.map((l) => escapeHtml(l.name)).join(", ")));
3436
}
35-
lines.push("");
36-
lines.push(`<a href="${url}">#${number} ${escapeHtml(title)}</a>`);
3737

3838
return lines.join("\n");
3939
}

src/formatters/pull_request.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ export function formatPullRequest(event: GitHubEvent): string {
3333
else if (action === "reopened") actionText = "reopened";
3434
else if (action === "ready_for_review") actionText = "ready for review";
3535
else if (action === "converted_to_draft") actionText = "converted to draft";
36+
else if (action === "review_requested") actionText = "review requested";
3637
else if (action === "synchronize") actionText = "synchronized";
3738
else actionText = action;
3839

40+
const requestedReviewers = action === "review_requested"
41+
? (event.requested_reviewer as { login: string } | undefined) ?? (pr?.requested_reviewers as Array<{ login: string }> | undefined)
42+
: undefined;
43+
3944
const lines: string[] = [];
4045
lines.push(headerLine("Event: ", `${emoji} pull_request`));
4146
lines.push(headerLine("Repo: ", `<a href="${repoUrl}">${escapeHtml(repo)}</a>`));
47+
lines.push(headerLine("Number: ", `#${number}`));
48+
lines.push(headerLine("Title: ", `<a href="${url}">${escapeHtml(title)}</a>`));
4249
lines.push(headerLine("Action: ", actionText));
4350
lines.push(headerLine("By: ", `<a href="${senderUrl}">${escapeHtml(sender)}</a>`));
4451
if (headLabel && baseLabel) {
@@ -53,13 +60,24 @@ export function formatPullRequest(event: GitHubEvent): string {
5360
if (labels.length > 0) {
5461
lines.push(headerLine("Labels: ", labels.map((l) => escapeHtml(l.name)).join(", ")));
5562
}
56-
lines.push("");
57-
lines.push(`<a href="${url}">#${number} ${escapeHtml(title)}</a>`);
63+
if (action === "review_requested" && requestedReviewers) {
64+
const reviewers = Array.isArray(requestedReviewers)
65+
? requestedReviewers.map((r) => escapeHtml(r.login)).join(", ")
66+
: escapeHtml(requestedReviewers.login);
67+
lines.push(headerLine("Reviewer: ", reviewers));
68+
}
69+
const body = (pr?.body as string) ?? "";
70+
if (body) {
71+
lines.push("");
72+
const truncated = body.length > 300 ? body.substring(0, 300) + "..." : body;
73+
lines.push(escapeHtml(truncated));
74+
}
5875
if (action === "synchronize") {
5976
const before = (event.before as string) ?? "";
6077
const after = (event.after as string) ?? "";
6178
if (before && after) {
6279
const compareUrl = `${repoUrl}/compare/${before.substring(0, 7)}...${after.substring(0, 7)}`;
80+
lines.push("");
6381
lines.push(`<a href="${compareUrl}">Compare changes</a>`);
6482
}
6583
}

src/index.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ import { sendMessage, sendAlert } from "./telegram";
66

77
export interface Env {
88
TG_GH_KV: KVNamespace;
9+
DEBUG?: string;
10+
}
11+
12+
function isDebug(env: Env): boolean {
13+
return env.DEBUG === "1" || env.DEBUG === "true";
914
}
1015

1116
export default {
1217
async fetch(request: Request, env: Env): Promise<Response> {
1318
if (request.method !== "POST") {
19+
console.log("rejected: non-POST method");
1420
return new Response("Method not allowed", { status: 405 });
1521
}
1622

1723
const eventType = request.headers.get("X-GitHub-Event") ?? "";
24+
console.log(`[event:${eventType}] received`);
1825

1926
if (eventType === "ping") {
2027
return new Response("OK", { status: 200 });
@@ -27,37 +34,66 @@ export default {
2734
try {
2835
payload = JSON.parse(body);
2936
} catch {
37+
console.log(`[event:${eventType}] invalid JSON`);
3038
return new Response("Invalid JSON", { status: 400 });
3139
}
3240

3341
const fullName = (payload.repository as { full_name?: string } | undefined)?.full_name;
3442
const orgName = (payload.organization as { login?: string } | undefined)?.login;
3543

44+
if (isDebug(env)) {
45+
console.log(`[event:${eventType}] fullName=${fullName} orgName=${orgName}`);
46+
console.log(`[event:${eventType}] env.TG_GH_KV type: ${typeof env.TG_GH_KV}, get: ${typeof env.TG_GH_KV?.get}`);
47+
48+
try {
49+
const listResult = await env.TG_GH_KV.list({ prefix: "config:" });
50+
console.log(`[event:${eventType}] KV list config:* returned ${listResult.keys.length} keys`);
51+
for (const k of listResult.keys.slice(0, 5)) {
52+
console.log(`[event:${eventType}] KV key: ${k.name}`);
53+
}
54+
if (listResult.keys.length === 0) {
55+
await env.TG_GH_KV.put("config:__debug__", "hello");
56+
const debugGet = await env.TG_GH_KV.get("config:__debug__");
57+
console.log(`[event:${eventType}] KV put/get debug: ${debugGet}`);
58+
}
59+
} catch (e) {
60+
console.error(`[event:${eventType}] KV list failed:`, e);
61+
}
62+
}
63+
3664
if (!fullName && !orgName) {
65+
console.log(`[event:${eventType}] no fullName or orgName, returning 200`);
3766
return new Response("OK", { status: 200 });
3867
}
3968

4069
let adminConfig;
4170
try {
4271
adminConfig = await getAdminConfig(env.TG_GH_KV);
72+
console.log(`[event:${eventType}] adminConfig loaded: ${adminConfig ? "yes" : "no"}`);
4373
} catch (e) {
44-
console.error("Failed to read admin config:", e);
74+
console.error(`[event:${eventType}] failed to read admin config:`, e);
4575
return new Response("Internal error", { status: 500 });
4676
}
4777

4878
let config = null;
4979
if (fullName) {
5080
config = await getRepoConfig(env.TG_GH_KV, fullName);
81+
console.log(`[event:${eventType}] getRepoConfig(${fullName}): ${config ? "found" : "null"}`);
5182
}
5283
if (!config && orgName) {
5384
config = await getOrgWildcardConfig(env.TG_GH_KV, orgName);
85+
console.log(`[event:${eventType}] getOrgWildcardConfig(${orgName}): ${config ? "found" : "null"}`);
5486
}
5587
if (!config) {
88+
console.log(`[event:${eventType}] no config matched, returning 204`);
5689
return new Response(null, { status: 204 });
5790
}
5891

92+
console.log(`[event:${eventType}] config found, verifying signature`);
93+
5994
const valid = await verifySignature(body, signature, config.secret);
6095
if (!valid) {
96+
console.log(`[event:${eventType}] signature verification FAILED`);
6197
if (adminConfig) {
6298
const context = fullName ?? orgName ?? "unknown";
6399
try {
@@ -69,8 +105,11 @@ export default {
69105
return new Response("Unauthorized", { status: 401 });
70106
}
71107

108+
console.log(`[event:${eventType}] signature OK`);
109+
72110
const isPrivate = (payload.repository as { private?: boolean } | undefined)?.private ?? false;
73111
const targets = matchTargets(config, eventType, isPrivate);
112+
console.log(`[event:${eventType}] matched ${targets.length} target(s)`);
74113

75114
const formatter = getFormatter(eventType);
76115
const message = formatter(payload as import("./types").GitHubEvent);
@@ -91,8 +130,10 @@ export default {
91130
chat_id: target.chat_id,
92131
text: message,
93132
});
133+
console.log(`[event:${eventType}] sent to target (${target.public ? "public" : "private"})`);
94134
} catch (e) {
95135
const errMsg = e instanceof Error ? e.message : String(e);
136+
console.error(`[event:${eventType}] failed to send to target:`, errMsg);
96137
errors.push(`Failed to send to ${target.chat_id}: ${errMsg}`);
97138
}
98139
}

wrangler.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ name = "telegram-github-worker"
22
main = "src/index.ts"
33
compatibility_date = "2025-04-21"
44

5+
[vars]
6+
DEBUG = ""
7+
58
[[kv_namespaces]]
69
binding = "TG_GH_KV"
710
id = "KV_ID_PLACEHOLDER"

0 commit comments

Comments
 (0)