feat: send daily repository status to Discord
This commit is contained in:
Executable
+47
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const {
|
||||
buildDiscordPayload,
|
||||
collectRepositoryStatus,
|
||||
postDiscordStatus
|
||||
} = require('../backend/src/services/repositoryStatusDiscordService');
|
||||
|
||||
const repoDir = path.resolve(__dirname, '..');
|
||||
const defaultEnvFile = '/mnt/HC_Volume_103713257/tilbudsgivern-data/secrets/discord.env';
|
||||
|
||||
const loadEnvFile = (filePath) => {
|
||||
if (!filePath || !fs.existsSync(filePath)) return;
|
||||
fs.readFileSync(filePath, 'utf8').split(/\r?\n/).forEach((line) => {
|
||||
const match = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)=(.*)\s*$/);
|
||||
if (!match || process.env[match[1]]) return;
|
||||
let value = match[2].trim();
|
||||
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
||||
value = value.slice(1, -1);
|
||||
}
|
||||
process.env[match[1]] = value;
|
||||
});
|
||||
};
|
||||
|
||||
const main = async () => {
|
||||
const mode = process.argv.includes('--send') ? 'send' : 'dry-run';
|
||||
loadEnvFile(process.env.REPO_STATUS_ENV_FILE || defaultEnvFile);
|
||||
|
||||
const status = await collectRepositoryStatus({ repoDir });
|
||||
const payload = buildDiscordPayload(status);
|
||||
|
||||
if (mode === 'dry-run') {
|
||||
process.stdout.write(`${JSON.stringify({ status, payload }, null, 2)}\n`);
|
||||
return;
|
||||
}
|
||||
|
||||
const webhookUrl = process.env.DISCORD_REPO_STATUS_WEBHOOK_URL || process.env.DISCORD_WEBHOOK_URL;
|
||||
const result = await postDiscordStatus({ webhookUrl, payload });
|
||||
process.stdout.write(`Discord repo-status sendt${result?.id ? ` som besked ${result.id}` : ''}.\n`);
|
||||
};
|
||||
|
||||
main().catch((error) => {
|
||||
process.stderr.write(`Repo-status fejlede: ${error.message}\n`);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user