Aethel Bot OSS repository!
aethel.xyz
bot
fun
ai
discord
discord-bot
aethel
1import { WebhookClient } from 'discord.js';
2import { execSync } from 'child_process';
3import logger from './logger';
4
5function getGitInfo() {
6 try {
7 try {
8 execSync('git rev-parse --is-inside-work-tree');
9
10 try {
11 execSync('git remote get-url origin');
12 } catch {
13 execSync('git remote add origin https://github.com/aethel/aethel-labs');
14 }
15
16 try {
17 execSync('git fetch --depth=100 origin');
18 } catch (e) {
19 logger.debug('git fetch failed or unnecessary; continuing', {
20 error: (e as Error).message,
21 });
22 }
23 } catch (e) {
24 logger.debug('Not a git repository; initializing temporary repo for metadata', {
25 error: (e as Error).message,
26 });
27 try {
28 execSync('git init');
29 try {
30 execSync('git remote add origin https://github.com/aethel/aethel-labs');
31 } catch (e) {
32 logger.debug('origin remote already exists or cannot be added', {
33 error: (e as Error).message,
34 });
35 }
36 const sourceCommit = process.env.SOURCE_COMMIT;
37 if (sourceCommit) {
38 try {
39 execSync(`git fetch --depth=1 origin ${sourceCommit}`);
40 } catch (err) {
41 logger.debug('Failed to fetch SOURCE_COMMIT from origin', {
42 error: (err as Error).message,
43 });
44 }
45 } else {
46 try {
47 const remoteHead = execSync('git ls-remote origin HEAD').toString().split('\t')[0];
48 if (remoteHead) {
49 execSync(`git fetch --depth=1 origin ${remoteHead}`);
50 process.env.SOURCE_COMMIT = remoteHead;
51 }
52 } catch (err) {
53 logger.debug('Failed to resolve remote HEAD', { error: (err as Error).message });
54 }
55 }
56 } catch (err) {
57 logger.debug('Failed to bootstrap temporary git repo', { error: (err as Error).message });
58 }
59 }
60
61 let commitHash: string | null = null;
62 try {
63 commitHash = process.env.SOURCE_COMMIT || execSync('git rev-parse HEAD').toString().trim();
64 } catch {
65 try {
66 const remoteHead = execSync('git ls-remote origin HEAD').toString().split('\t')[0];
67 commitHash = remoteHead || null;
68 } catch (e) {
69 logger.debug('Failed to resolve remote HEAD for commit hash', {
70 error: (e as Error).message,
71 });
72 }
73 }
74
75 const shortHash = commitHash ? commitHash.substring(0, 7) : 'unknown';
76 let commitMessage = 'No commit message';
77 try {
78 commitMessage = commitHash
79 ? execSync(`git log -1 --pretty=%B ${commitHash}`).toString().trim()
80 : commitMessage;
81 } catch (e) {
82 logger.debug('Failed to resolve commit message', { error: (e as Error).message });
83 }
84 const branch =
85 process.env.GIT_BRANCH ||
86 process.env.VERCEL_GIT_COMMIT_REF ||
87 process.env.COOLIFY_BRANCH ||
88 (() => {
89 try {
90 return execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
91 } catch (e) {
92 logger.debug('Failed to resolve branch', { error: (e as Error).message });
93 return 'unknown';
94 }
95 })();
96
97 return {
98 commitHash: shortHash,
99 commitMessage,
100 branch,
101 };
102 } catch (error) {
103 logger.warn('Failed to get git info:', error);
104 return {
105 commitHash: 'unknown',
106 commitMessage: 'No commit message',
107 branch: 'unknown',
108 };
109 }
110}
111
112export async function sendDeploymentNotification(startTime: number) {
113 const webhookUrl = process.env.DEPLOYMENT_WEBHOOK_URL;
114 if (!webhookUrl) {
115 logger.warn('DEPLOYMENT_WEBHOOK_URL not set, skipping deployment notification');
116 return;
117 }
118
119 try {
120 const webhook = new WebhookClient({ url: webhookUrl });
121 const deploymentTime = Date.now() - startTime;
122 const seconds = (deploymentTime / 1000).toFixed(2);
123
124 const { commitHash, commitMessage, branch } = getGitInfo();
125
126 await webhook.send({
127 embeds: [
128 {
129 title: '<:check:942538737332662282> Aethel was deployed successfully',
130 color: 0xf4f4f4,
131 fields: [
132 {
133 name: '<:development:1269783674782748775> Branch',
134 value: branch,
135 inline: true,
136 },
137 {
138 name: '<:4_:1387343665264853092> Commit',
139 value: `\`${commitHash}\``,
140 inline: true,
141 },
142 {
143 name: '<a:Time:1186795135263051847> Deployment Time',
144 value: `${seconds}s`,
145 inline: true,
146 },
147 {
148 name: '<:github:1371987360044159016> Commit Message',
149 value:
150 commitMessage.length > 100
151 ? `${commitMessage.substring(0, 100)}...`
152 : commitMessage,
153 },
154 ],
155 timestamp: new Date().toISOString(),
156 },
157 ],
158 });
159
160 logger.info('Deployment notification sent successfully');
161 } catch (error) {
162 logger.error('Failed to send deployment notification:', error);
163 }
164}