swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters Β· 2/15/2026
719a1cb
291 lines Β· 8.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
 * Discord Integration
 * Send notifications to Discord channels via Webhooks
 */

import { logger } from "../logger";

export interface DiscordEmbed {
    title?: string;
    description?: string;
    url?: string;
    timestamp?: string;
    color?: number;
    footer?: { text: string; icon_url?: string };
    author?: { name: string; url?: string; icon_url?: string };
    fields?: { name: string; value: string; inline?: boolean }[];
    thumbnail?: { url: string };
}

export interface DiscordWebhookPayload {
    content?: string;
    username?: string;
    avatar_url?: string;
    embeds?: DiscordEmbed[];
}

// Discord colors (decimal format)
const COLORS = {
    success: 0x28a745,
    warning: 0xffc107,
    error: 0xdc3545,
    info: 0x5865f2,
    neutral: 0x6c757d,
    purple: 0x9b59b6,
};

/**
 * Send a message to Discord via webhook
 */
export async function sendDiscordMessage(
    webhookUrl: string,
    payload: DiscordWebhookPayload
): Promise<boolean> {
    try {
        const response = await fetch(webhookUrl, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(payload),
        });

        if (!response.ok) {
            const error = await response.text();
            logger.error({ status: response.status, error }, "Discord webhook failed");
            return false;
        }

        return true;
    } catch (error) {
        logger.error({ error }, "Failed to send Discord message");
        return false;
    }
}

/**
 * Create a PR notification embed
 */
export function createPREmbed(options: {
    action: "opened" | "merged" | "closed" | "review_requested";
    prTitle: string;
    prNumber: number;
    prUrl: string;
    author: string;
    authorAvatar?: string;
    repository: string;
    description?: string;
    additions?: number;
    deletions?: number;
}): DiscordEmbed {
    const colors: Record<string, number> = {
        opened: COLORS.info,
        merged: COLORS.success,
        closed: COLORS.neutral,
        review_requested: COLORS.warning,
    };

    const titles: Record<string, string> = {
        opened: "πŸ†• Pull Request Opened",
        merged: "βœ… Pull Request Merged",
        closed: "❌ Pull Request Closed",
        review_requested: "πŸ‘€ Review Requested",
    };

    const fields: DiscordEmbed["fields"] = [];

    if (options.additions !== undefined && options.deletions !== undefined) {
        fields.push({
            name: "Changes",
            value: `+${options.additions} / -${options.deletions}`,
            inline: true,
        });
    }

    return {
        title: `${titles[options.action]}`,
        description: `**[#${options.prNumber} ${options.prTitle}](${options.prUrl})**\n\n${options.description || ""}`,
        url: options.prUrl,
        color: colors[options.action],
        author: {
            name: options.author,
            icon_url: options.authorAvatar,
        },
        footer: {
            text: options.repository,
        },
        timestamp: new Date().toISOString(),
        fields,
    };
}

/**
 * Create an issue notification embed
 */
export function createIssueEmbed(options: {
    action: "opened" | "closed" | "commented" | "labeled";
    issueTitle: string;
    issueNumber: number;
    issueUrl: string;
    author: string;
    authorAvatar?: string;
    repository: string;
    labels?: string[];
    body?: string;
}): DiscordEmbed {
    const colors: Record<string, number> = {
        opened: COLORS.info,
        closed: COLORS.success,
        commented: COLORS.neutral,
        labeled: COLORS.purple,
    };

    const titles: Record<string, string> = {
        opened: "πŸ› Issue Opened",
        closed: "βœ… Issue Closed",
        commented: "πŸ’¬ New Comment",
        labeled: "🏷️ Issue Labeled",
    };

    const fields: DiscordEmbed["fields"] = [];

    if (options.labels?.length) {
        fields.push({
            name: "Labels",
            value: options.labels.map(l => `\`${l}\``).join(" "),
            inline: true,
        });
    }

    return {
        title: titles[options.action],
        description: `**[#${options.issueNumber} ${options.issueTitle}](${options.issueUrl})**\n\n${options.body?.slice(0, 200) || ""}`,
        url: options.issueUrl,
        color: colors[options.action],
        author: {
            name: options.author,
            icon_url: options.authorAvatar,
        },
        footer: {
            text: options.repository,
        },
        timestamp: new Date().toISOString(),
        fields,
    };
}

/**
 * Create a CI/CD status embed
 */
export function createCIEmbed(options: {
    status: "success" | "failure" | "pending" | "running" | "cancelled";
    workflowName: string;
    repository: string;
    branch: string;
    commitSha: string;
    commitMessage?: string;
    runUrl: string;
    duration?: string;
    triggeredBy?: string;
}): DiscordEmbed {
    const colors: Record<string, number> = {
        success: COLORS.success,
        failure: COLORS.error,
        pending: COLORS.warning,
        running: COLORS.info,
        cancelled: COLORS.neutral,
    };

    const icons: Record<string, string> = {
        success: "βœ…",
        failure: "❌",
        pending: "⏳",
        running: "πŸ”„",
        cancelled: "⏹️",
    };

    return {
        title: `${icons[options.status]} Workflow: ${options.status.charAt(0).toUpperCase() + options.status.slice(1)}`,
        description: `**${options.workflowName}**`,
        url: options.runUrl,
        color: colors[options.status],
        fields: [
            { name: "Branch", value: `\`${options.branch}\``, inline: true },
            { name: "Commit", value: `\`${options.commitSha.slice(0, 7)}\``, inline: true },
            ...(options.duration ? [{ name: "Duration", value: options.duration, inline: true }] : []),
            ...(options.commitMessage ? [{ name: "Message", value: options.commitMessage.slice(0, 100) }] : []),
        ],
        footer: {
            text: options.repository,
        },
        timestamp: new Date().toISOString(),
    };
}

/**
 * Create a push notification embed
 */
export function createPushEmbed(options: {
    repository: string;
    branch: string;
    pusher: string;
    pusherAvatar?: string;
    commits: { sha: string; message: string; url: string }[];
    compareUrl: string;
}): DiscordEmbed {
    const commitList = options.commits
        .slice(0, 5)
        .map(c => `[\`${c.sha.slice(0, 7)}\`](${c.url}) ${c.message.split("\n")[0].slice(0, 50)}`)
        .join("\n");

    const moreCommits = options.commits.length > 5
        ? `\n... and ${options.commits.length - 5} more`
        : "";

    return {
        title: `πŸ“€ ${options.commits.length} commit${options.commits.length > 1 ? "s" : ""} pushed to \`${options.branch}\``,
        description: commitList + moreCommits,
        url: options.compareUrl,
        color: COLORS.info,
        author: {
            name: options.pusher,
            icon_url: options.pusherAvatar,
        },
        footer: {
            text: options.repository,
        },
        timestamp: new Date().toISOString(),
    };
}

/**
 * Create a release notification embed
 */
export function createReleaseEmbed(options: {
    action: "published" | "prereleased" | "released";
    name: string;
    tagName: string;
    repository: string;
    releaseUrl: string;
    body?: string;
    author: string;
    authorAvatar?: string;
    isPrerelease: boolean;
}): DiscordEmbed {
    return {
        title: `πŸš€ Release ${options.isPrerelease ? "(Pre-release)" : ""}: ${options.name}`,
        description: options.body?.slice(0, 500) || "No release notes provided.",
        url: options.releaseUrl,
        color: options.isPrerelease ? COLORS.warning : COLORS.success,
        author: {
            name: options.author,
            icon_url: options.authorAvatar,
        },
        fields: [
            { name: "Tag", value: `\`${options.tagName}\``, inline: true },
        ],
        footer: {
            text: options.repository,
        },
        timestamp: new Date().toISOString(),
    };
}