From 4c7fb3d67111d02d08e272f837d5d955de6d5d52 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Wed, 9 Aug 2023 19:46:29 +0200
Subject: [PATCH] Config => Move result folder name to pipeline result and add
 result folder

---
 ExpressAPI/src/config/Config.ts         | 20 ++++++++++++++++----
 ExpressAPI/src/routes/ExerciceRoutes.ts |  2 +-
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/ExpressAPI/src/config/Config.ts b/ExpressAPI/src/config/Config.ts
index acf4e9d..83edf27 100644
--- a/ExpressAPI/src/config/Config.ts
+++ b/ExpressAPI/src/config/Config.ts
@@ -1,4 +1,7 @@
 import GitlabVisibility from '../shared/types/Gitlab/GitlabVisibility';
+import { Exercice }     from '../types/DatabaseTypes';
+import path             from 'path';
+import fs               from 'fs';
 
 
 class Config {
@@ -23,7 +26,7 @@ class Config {
     };
 
     public exercice: {
-        maxSameName: number; resultsFolder: string; default: { description: string; visibility: string; };
+        maxSameName: number; resultsFolder: string, pipelineResultsFolder: string; default: { description: string; visibility: string; };
     };
 
     public readonly userPasswordLength: number;
@@ -73,9 +76,10 @@ class Config {
         };
 
         this.exercice = {
-            maxSameName  : Number(process.env.EXERCICE_MAX_SAME_NAME || 0),
-            resultsFolder: process.env.EXERCICE_RESULTS_FOLDER ?? '', //Do not use convertWithEnvVars() because it is used in the exercice creation and muste be interpreted at exercice runtime
-            default      : {
+            maxSameName          : Number(process.env.EXERCICE_MAX_SAME_NAME || 0),
+            resultsFolder        : process.env.EXERCICE_RESULTS_FOLDER?.convertWithEnvVars() ?? '',
+            pipelineResultsFolder: process.env.EXERCICE_PIPELINE_RESULTS_FOLDER ?? '', //Do not use convertWithEnvVars() because it is used in the exercice creation and muste be interpreted at exercice runtime
+            default              : {
                 description: process.env.EXERCICE_DEFAULT_DESCRIPTION?.convertWithEnvVars() ?? '',
                 visibility : process.env.EXERCICE_DEFAULT_VISIBILITY || GitlabVisibility.PRIVATE
             }
@@ -84,6 +88,14 @@ class Config {
         this.userPasswordLength = Number(process.env.USER_PASSWORD_LENGTH || 0);
         this.userPasswordSaltRounds = Number(process.env.USER_PASSWORD_SALT_ROUNDS || 10);
     }
+
+    public getResultsFolder(exercice: Exercice): string {
+        const folderPath = path.join(this.exercice.resultsFolder, exercice.enonceName, exercice.id);
+
+        fs.mkdirSync(folderPath, { recursive: true });
+
+        return folderPath;
+    }
 }
 
 
diff --git a/ExpressAPI/src/routes/ExerciceRoutes.ts b/ExpressAPI/src/routes/ExerciceRoutes.ts
index 42cbff9..265212d 100644
--- a/ExpressAPI/src/routes/ExerciceRoutes.ts
+++ b/ExpressAPI/src/routes/ExerciceRoutes.ts
@@ -68,7 +68,7 @@ class ExerciceRoutes implements RoutesManager {
 
                 await GitlabManager.addRepositoryVariable(repository.id, 'DOJO_EXERCICE_ID', exerciceId, false, true);
                 await GitlabManager.addRepositoryVariable(repository.id, 'DOJO_SECRET', secret, false, true);
-                await GitlabManager.addRepositoryVariable(repository.id, 'DOJO_RESULTS_FOLDER', Config.exercice.resultsFolder, false, false);
+                await GitlabManager.addRepositoryVariable(repository.id, 'DOJO_RESULTS_FOLDER', Config.exercice.pipelineResultsFolder, false, false);
 
                 break;
             } catch ( error ) {
-- 
GitLab