mirror of
https://github.com/andwati/dokploy.git
synced 2026-10-24 14:02:19 +00:00
183 lines
4.2 KiB
TypeScript
183 lines
4.2 KiB
TypeScript
import { db } from "@dokploy/server/db";
|
|
import { notifications } from "@dokploy/server/db/schema";
|
|
import BuildSuccessEmail from "@dokploy/server/emails/emails/build-success";
|
|
import { Domain } from "@dokploy/server/services/domain";
|
|
import { renderAsync } from "@react-email/components";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { format } from "date-fns";
|
|
import {
|
|
sendDiscordNotification,
|
|
sendEmailNotification,
|
|
sendSlackNotification,
|
|
sendTelegramNotification,
|
|
} from "./utils";
|
|
|
|
interface Props {
|
|
projectName: string;
|
|
applicationName: string;
|
|
applicationType: string;
|
|
buildLink: string;
|
|
adminId: string;
|
|
domains: Domain[];
|
|
}
|
|
|
|
export const sendBuildSuccessNotifications = async ({
|
|
projectName,
|
|
applicationName,
|
|
applicationType,
|
|
buildLink,
|
|
adminId,
|
|
domains
|
|
}: Props) => {
|
|
const date = new Date();
|
|
const unixDate = ~~(Number(date) / 1000);
|
|
const notificationList = await db.query.notifications.findMany({
|
|
where: and(
|
|
eq(notifications.appDeploy, true),
|
|
eq(notifications.adminId, adminId),
|
|
),
|
|
with: {
|
|
email: true,
|
|
discord: true,
|
|
telegram: true,
|
|
slack: true,
|
|
},
|
|
});
|
|
|
|
for (const notification of notificationList) {
|
|
const { email, discord, telegram, slack } = notification;
|
|
|
|
if (email) {
|
|
const template = await renderAsync(
|
|
BuildSuccessEmail({
|
|
projectName,
|
|
applicationName,
|
|
applicationType,
|
|
buildLink,
|
|
date: date.toLocaleString(),
|
|
}),
|
|
).catch();
|
|
await sendEmailNotification(email, "Build success for dokploy", template);
|
|
}
|
|
|
|
if (discord) {
|
|
const decorate = (decoration: string, text: string) =>
|
|
`${discord.decoration ? decoration : ""} ${text}`.trim();
|
|
|
|
await sendDiscordNotification(discord, {
|
|
title: decorate(">", "`✅` Build Success"),
|
|
color: 0x57f287,
|
|
fields: [
|
|
{
|
|
name: decorate("`🛠️`", "Project"),
|
|
value: projectName,
|
|
inline: true,
|
|
},
|
|
{
|
|
name: decorate("`⚙️`", "Application"),
|
|
value: applicationName,
|
|
inline: true,
|
|
},
|
|
{
|
|
name: decorate("`❔`", "Type"),
|
|
value: applicationType,
|
|
inline: true,
|
|
},
|
|
{
|
|
name: decorate("`📅`", "Date"),
|
|
value: `<t:${unixDate}:D>`,
|
|
inline: true,
|
|
},
|
|
{
|
|
name: decorate("`⌚`", "Time"),
|
|
value: `<t:${unixDate}:t>`,
|
|
inline: true,
|
|
},
|
|
{
|
|
name: decorate("`❓`", "Type"),
|
|
value: "Successful",
|
|
inline: true,
|
|
},
|
|
{
|
|
name: decorate("`🧷`", "Build Link"),
|
|
value: `[Click here to access build link](${buildLink})`,
|
|
},
|
|
],
|
|
timestamp: date.toISOString(),
|
|
footer: {
|
|
text: "Dokploy Build Notification",
|
|
},
|
|
});
|
|
}
|
|
|
|
if (telegram) {
|
|
const chunkArray = <T>(array: T[], chunkSize: number): T[][] =>
|
|
Array.from({ length: Math.ceil(array.length / chunkSize) }, (_, i) => array.slice(i * chunkSize, i * chunkSize + chunkSize)
|
|
);
|
|
|
|
const inlineButton = [
|
|
[
|
|
{
|
|
text: "Deployment Logs",
|
|
url: buildLink,
|
|
},
|
|
],
|
|
...chunkArray(domains, 2).map((chunk) =>
|
|
chunk.map((data) => ({
|
|
text: data.host,
|
|
url: `${data.https ? "https" : "http"}://${data.host}`,
|
|
}))
|
|
),
|
|
];
|
|
|
|
await sendTelegramNotification(
|
|
telegram,
|
|
`<b>✅ Build Success</b>\n\n<b>Project:</b> ${projectName}\n<b>Application:</b> ${applicationName}\n<b>Type:</b> ${applicationType}\n<b>Date:</b> ${format(date, "PP")}\n<b>Time:</b> ${format(date, "pp")}`,
|
|
inlineButton
|
|
);
|
|
}
|
|
|
|
if (slack) {
|
|
const { channel } = slack;
|
|
await sendSlackNotification(slack, {
|
|
channel: channel,
|
|
attachments: [
|
|
{
|
|
color: "#00FF00",
|
|
pretext: ":white_check_mark: *Build Success*",
|
|
fields: [
|
|
{
|
|
title: "Project",
|
|
value: projectName,
|
|
short: true,
|
|
},
|
|
{
|
|
title: "Application",
|
|
value: applicationName,
|
|
short: true,
|
|
},
|
|
{
|
|
title: "Type",
|
|
value: applicationType,
|
|
short: true,
|
|
},
|
|
{
|
|
title: "Time",
|
|
value: date.toLocaleString(),
|
|
short: true,
|
|
},
|
|
],
|
|
actions: [
|
|
{
|
|
type: "button",
|
|
text: "View Build Details",
|
|
url: buildLink,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|
|
}
|
|
}
|
|
};
|