swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters Β· 2/15/2026
719a1cb
236 lines Β· 6.7 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
/**
 * Microsoft Teams Integration
 * Send notifications to Teams channels via Incoming Webhooks
 */

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

export interface TeamsMessageCard {
    "@type": "MessageCard";
    "@context": "http://schema.org/extensions";
    themeColor: string;
    summary: string;
    sections: TeamsSection[];
    potentialAction?: TeamsAction[];
}

interface TeamsSection {
    activityTitle?: string;
    activitySubtitle?: string;
    activityImage?: string;
    facts?: { name: string; value: string }[];
    markdown?: boolean;
    text?: string;
}

interface TeamsAction {
    "@type": "OpenUri" | "ActionCard";
    name: string;
    targets?: { os: string; uri: string }[];
}

// Theme colors for different event types
const THEME_COLORS = {
    success: "28a745",
    warning: "ffc107",
    error: "dc3545",
    info: "0078d4",
    neutral: "6c757d",
};

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

        if (!response.ok) {
            logger.error({ status: response.status }, "Teams webhook failed");
            return false;
        }

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

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

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

    return {
        "@type": "MessageCard",
        "@context": "http://schema.org/extensions",
        themeColor: colors[options.action] || THEME_COLORS.info,
        summary: `${titles[options.action]}: ${options.prTitle}`,
        sections: [
            {
                activityTitle: `**${titles[options.action]}**`,
                activitySubtitle: `${options.repository} #${options.prNumber}`,
                activityImage: options.authorAvatar,
                facts: [
                    { name: "Title", value: options.prTitle },
                    { name: "Author", value: options.author },
                ],
                markdown: true,
                text: options.description,
            },
        ],
        potentialAction: [
            {
                "@type": "OpenUri",
                name: "View Pull Request",
                targets: [{ os: "default", uri: options.prUrl }],
            },
        ],
    };
}

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

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

    return {
        "@type": "MessageCard",
        "@context": "http://schema.org/extensions",
        themeColor: colors[options.action] || THEME_COLORS.info,
        summary: `${titles[options.action]}: ${options.issueTitle}`,
        sections: [
            {
                activityTitle: `**${titles[options.action]}**`,
                activitySubtitle: `${options.repository} #${options.issueNumber}`,
                activityImage: options.authorAvatar,
                facts: [
                    { name: "Title", value: options.issueTitle },
                    { name: "Author", value: options.author },
                    ...(options.labels?.length
                        ? [{ name: "Labels", value: options.labels.join(", ") }]
                        : []),
                ],
                markdown: true,
            },
        ],
        potentialAction: [
            {
                "@type": "OpenUri",
                name: "View Issue",
                targets: [{ os: "default", uri: options.issueUrl }],
            },
        ],
    };
}

/**
 * Create a CI/CD status card
 */
export function createCICard(options: {
    status: "success" | "failure" | "pending" | "running";
    workflowName: string;
    repository: string;
    branch: string;
    commitSha: string;
    commitUrl: string;
    runUrl: string;
    duration?: string;
}): TeamsMessageCard {
    const colors: Record<string, string> = {
        success: THEME_COLORS.success,
        failure: THEME_COLORS.error,
        pending: THEME_COLORS.warning,
        running: THEME_COLORS.info,
    };

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

    return {
        "@type": "MessageCard",
        "@context": "http://schema.org/extensions",
        themeColor: colors[options.status],
        summary: `${icons[options.status]} ${options.workflowName}: ${options.status}`,
        sections: [
            {
                activityTitle: `**${icons[options.status]} Workflow ${options.status.charAt(0).toUpperCase() + options.status.slice(1)}**`,
                activitySubtitle: options.repository,
                facts: [
                    { name: "Workflow", value: options.workflowName },
                    { name: "Branch", value: options.branch },
                    { name: "Commit", value: options.commitSha.slice(0, 7) },
                    ...(options.duration ? [{ name: "Duration", value: options.duration }] : []),
                ],
                markdown: true,
            },
        ],
        potentialAction: [
            {
                "@type": "OpenUri",
                name: "View Run",
                targets: [{ os: "default", uri: options.runUrl }],
            },
            {
                "@type": "OpenUri",
                name: "View Commit",
                targets: [{ os: "default", uri: options.commitUrl }],
            },
        ],
    };
}