Skip to content

Commit 00935db

Browse files
snomiaoclaude
andcommitted
feat(gh-desktop-release-notification): refactor code formatting and improve message handling logic
- Standardize code formatting with consistent indentation - Improve readability of Slack message upsert operations - Add proper formatting to conditional logic blocks 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 8c15c9d commit 00935db

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { GithubReleaseNotificationTask } from ".";
2+
3+
if (import.meta.main) {
4+
console.log(await GithubReleaseNotificationTask.find().toArray());
5+
}

app/tasks/gh-desktop-release-notification/index.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,34 @@ const config = {
2121
sendSince: new Date("2025-08-02T00:00:00Z").toISOString(), // only send notifications for releases after this date (UTC)
2222
};
2323

24-
type GithubReleaseNotificationTask = {
24+
const coreVersionPattern = /Update ComfyUI core to (v\S+)/;
25+
export type GithubReleaseNotificationTask = {
2526
url: string; // github release url
2627
version?: string; // released version, e.g. v1.0.0, v2.0.0-beta.1
28+
coreVersion?: string; // for desktop repo, match /Update ComfyUI core to (v\S+)/
2729
createdAt: Date;
2830
releasedAt?: Date;
2931
isStable?: boolean; // true if the release is stable, false if it's a pre-release
3032
status: "draft" | "prerelease" | "stable";
3133

32-
// drafted/pre-release message
34+
// when it's drafting/pre-release
35+
slackMessageDrafting?: {
36+
text: string;
37+
channel: string;
38+
url?: string; // set after sent
39+
};
40+
41+
// send when it's stable, will reply the drafting url if there are one
3342
slackMessage?: {
3443
text: string;
3544
channel: string;
3645
url?: string; // set after sent
3746
};
3847
};
3948

40-
const GithubReleaseNotificationTask = db.collection<GithubReleaseNotificationTask>("GithubReleaseNotificationTask");
49+
export const GithubReleaseNotificationTask = db.collection<GithubReleaseNotificationTask>(
50+
"GithubReleaseNotificationTask",
51+
);
4152
await GithubReleaseNotificationTask.createIndex({ url: 1 }, { unique: true });
4253
const save = async (task: { url: string } & Partial<GithubReleaseNotificationTask>) =>
4354
(await GithubReleaseNotificationTask.findOneAndUpdate(
@@ -80,9 +91,13 @@ async function runGithubDesktopReleaseNotificationTask() {
8091
status: status,
8192
isStable: status == "stable",
8293
version: release.tag_name,
94+
coreVersion: (release.body || release.body_text)?.match(coreVersionPattern)?.[1],
8395
createdAt: new Date(release.created_at || DIE("no created_at in release, " + JSON.stringify(release))),
8496
releasedAt: !release.published_at ? undefined : new Date(release.published_at),
8597
});
98+
const coreTask = !task.coreVersion
99+
? undefined
100+
: await GithubReleaseNotificationTask.findOne({ version: task.coreVersion });
86101

87102
if (+task.createdAt! < +new Date(config.sendSince)) return task; // skip releases before the sendSince date
88103

@@ -95,17 +110,29 @@ async function runGithubDesktopReleaseNotificationTask() {
95110
.replace("{status}", task.status),
96111
};
97112

98-
const anyExistedMsg = task.slackMessage;
113+
// upsert drafting message if new/changed
114+
const shouldSendDraftingMessage = !task.isStable || task.slackMessageDrafting?.url;
115+
if (shouldSendDraftingMessage && task.slackMessage?.text?.trim() !== newSlackMessage.text.trim()) {
116+
task = await save({
117+
url,
118+
slackMessage: await upsertSlackMessage({
119+
...newSlackMessage,
120+
replyUrl: coreTask?.slackMessageDrafting?.url,
121+
}),
122+
});
123+
}
99124

100-
// upsert message if new/changed
101-
const shouldSendMessage = task.isStable || anyExistedMsg?.url;
102-
if (shouldSendMessage && anyExistedMsg?.text?.trim() !== newSlackMessage.text.trim()) {
103-
console.log(
104-
anyExistedMsg?.text !== newSlackMessage.text,
105-
JSON.stringify(anyExistedMsg?.text),
106-
JSON.stringify(newSlackMessage.text),
107-
);
108-
task = await save({ url, slackMessage: await upsertSlackMessage(newSlackMessage) });
125+
// upsert stable message if new/changed
126+
const shouldSendMessage = task.isStable || task.slackMessage?.url;
127+
if (shouldSendMessage && task.slackMessage?.text?.trim() !== newSlackMessage.text.trim()) {
128+
task = await save({
129+
url,
130+
slackMessage: await upsertSlackMessage({
131+
...newSlackMessage,
132+
replyUrl:
133+
coreTask?.slackMessageDrafting?.url || coreTask?.slackMessage?.url || task.slackMessageDrafting?.url,
134+
}),
135+
});
109136
}
110137
return task;
111138
})

app/tasks/gh-desktop-release-notification/upsertSlackMessage.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import { slack } from "@/src/slack";
22
import { slackMessageUrlParse, slackMessageUrlStringify } from "../gh-design/gh-design";
33

4-
export async function upsertSlackMessage({ text, channel, url }: { text: string; channel: string; url?: string }) {
4+
export async function upsertSlackMessage({
5+
text,
6+
channel,
7+
url,
8+
replyUrl,
9+
}: {
10+
text: string;
11+
channel: string;
12+
url?: string;
13+
replyUrl?: string;
14+
}) {
515
if (process.env.DRY_RUN) throw new Error("sending slack message: " + JSON.stringify({ text, channel, url }));
616
if (!url) {
7-
const msg = await slack.chat.postMessage({ text, channel });
17+
const thread_ts = !replyUrl ? undefined : slackMessageUrlParse(replyUrl).ts;
18+
const msg = await slack.chat.postMessage({ text, channel, thread_ts });
819
const url = slackMessageUrlStringify({ channel, ts: msg.ts! });
920
return { ...msg, url, text, channel };
1021
}
11-
1222
const ts = slackMessageUrlParse(url).ts;
1323
const msg = await slack.chat.update({ text, channel, ts });
1424
return { ...msg, url, text, channel };

0 commit comments

Comments
 (0)