103 lines
3.1 KiB
JavaScript
103 lines
3.1 KiB
JavaScript
require('dotenv').config();
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
/**
|
|
* Configuration centralisée de l'application
|
|
*/
|
|
const config = {
|
|
// Configuration Discord
|
|
discord: {
|
|
token: process.env.DISCORD_TOKEN,
|
|
guildId: process.env.DISCORD_GUILD_ID,
|
|
},
|
|
|
|
// Configuration GitLab
|
|
gitlab: {
|
|
url: process.env.GITLAB_URL,
|
|
token: process.env.GITLAB_TOKEN,
|
|
},
|
|
|
|
// Configuration Bot
|
|
bot: {
|
|
syncIntervalMinutes: parseInt(process.env.SYNC_INTERVAL_MINUTES) || 5,
|
|
logLevel: process.env.LOG_LEVEL || 'info',
|
|
},
|
|
|
|
// Configuration des projets et boards (format JSON)
|
|
projects: [],
|
|
|
|
// Initialise la configuration des projets
|
|
init() {
|
|
try {
|
|
// Méthode 1: Fichier JSON dédié
|
|
const projectsJsonPath = path.join(__dirname, '../../config/projects.json');
|
|
if (fs.existsSync(projectsJsonPath)) {
|
|
console.log('Chargement depuis config/projects.json...');
|
|
const projectsData = fs.readFileSync(projectsJsonPath, 'utf8');
|
|
this.projects = JSON.parse(projectsData);
|
|
console.log('Projets chargés depuis le fichier:', this.projects);
|
|
}
|
|
// Méthode 2: Configuration legacy (rétrocompatibilité)
|
|
else if (process.env.GITLAB_PROJECT_ID && process.env.DISCORD_CATEGORY_ID) {
|
|
console.log('Utilisation de la configuration legacy...');
|
|
this.projects = [{
|
|
name: "Default Project",
|
|
projectId: process.env.GITLAB_PROJECT_ID,
|
|
categoryId: process.env.DISCORD_CATEGORY_ID,
|
|
boardIds: []
|
|
}];
|
|
console.log('Configuration legacy créée:', this.projects);
|
|
}
|
|
// Aucune configuration trouvée
|
|
else {
|
|
throw new Error('Aucune configuration trouvée. Créez le fichier config/projects.json ou définissez les variables legacy (GITLAB_PROJECT_ID + DISCORD_CATEGORY_ID)');
|
|
}
|
|
|
|
// Validation des projets avant encoding
|
|
if (!this.projects || this.projects.length === 0) {
|
|
throw new Error('Aucun projet configuré');
|
|
}
|
|
|
|
// Encode les project IDs
|
|
this.projects = this.projects.map(project => ({
|
|
...project,
|
|
projectId: encodeURIComponent(project.projectId)
|
|
}));
|
|
|
|
} catch (error) {
|
|
throw new Error(`Erreur lors de l'initialisation des projets: ${error.message}`);
|
|
}
|
|
},
|
|
|
|
// Validation de la configuration
|
|
validate() {
|
|
const required = [
|
|
'DISCORD_TOKEN',
|
|
'DISCORD_GUILD_ID',
|
|
'GITLAB_URL',
|
|
'GITLAB_TOKEN'
|
|
];
|
|
|
|
const missing = required.filter(key => !process.env[key]);
|
|
|
|
if (missing.length > 0) {
|
|
throw new Error(`Variables d'environnement manquantes: ${missing.join(', ')}`);
|
|
}
|
|
|
|
// Validation des projets
|
|
if (this.projects.length === 0) {
|
|
throw new Error('Aucun projet configuré. Définissez PROJECTS_CONFIG ou les variables legacy.');
|
|
}
|
|
|
|
// Validation de chaque projet
|
|
for (const project of this.projects) {
|
|
if (!project.name || !project.projectId || !project.categoryId) {
|
|
throw new Error(`Configuration projet invalide: ${JSON.stringify(project)}`);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = config;
|