From 9d2917d53b2f0d01ef2c690dd0fa8782a5a087ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Thu, 10 Aug 2023 17:45:08 +0200
Subject: [PATCH] ExerciceRoutes => Add the ci/cd file on exercice creation

---
 ExpressAPI/assets/exercice_gitlab_ci.yml | 30 ++++++++++++++++++++++++
 ExpressAPI/src/managers/GitlabManager.ts | 11 +++++++++
 ExpressAPI/src/routes/ExerciceRoutes.ts  | 15 +++++++++++-
 3 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 ExpressAPI/assets/exercice_gitlab_ci.yml

diff --git a/ExpressAPI/assets/exercice_gitlab_ci.yml b/ExpressAPI/assets/exercice_gitlab_ci.yml
new file mode 100644
index 0000000..9a9877f
--- /dev/null
+++ b/ExpressAPI/assets/exercice_gitlab_ci.yml
@@ -0,0 +1,30 @@
+###################################################################################################################
+# DO NOT MODIFY THIS FILE
+# This file is the ci/cd pipeline that will be used to test your exercice
+###################################################################################################################
+
+variables:
+    GIT_SUBMODULE_STRATEGY: recursive
+    GIT_SUBMODULE_FORCE_HTTPS: "true"
+    DOCKER_HOST: tcp://docker:2375
+    DOCKER_TLS_CERTDIR:
+    DOCKER_DRIVER: overlay2
+
+stages:
+    - dojo
+
+dojo:
+    stage: dojo
+    tags:
+        - dojo_exercice
+    services:
+        - docker:dind
+    image:
+        name: dojohesso/dojo_exercice_checker:latest
+    script:
+        - dojo_exercice_checker
+    artifacts:
+        when: always
+        paths:
+            - $DOJO_RESULTS_FOLDER/*
+    allow_failure: false
\ No newline at end of file
diff --git a/ExpressAPI/src/managers/GitlabManager.ts b/ExpressAPI/src/managers/GitlabManager.ts
index 6aa86c5..0b33bd5 100644
--- a/ExpressAPI/src/managers/GitlabManager.ts
+++ b/ExpressAPI/src/managers/GitlabManager.ts
@@ -186,6 +186,17 @@ class GitlabManager {
 
         return response.data;
     }
+
+    async createFile(repoId: number, filePath: string, fileBase64: string, commitMessage: string, branch: string = 'main', authorName: string = 'Dojo', authorMail: string | undefined = undefined) {
+        await axios.post(this.getApiUrl(GitlabRoutes.REPOSITORY_FILE).replace('{{id}}', String(repoId)).replace('{{filePath}}', encodeURIComponent(filePath)), {
+            encoding      : 'base64',
+            branch        : branch,
+            commit_message: commitMessage,
+            content       : fileBase64,
+            author_name   : authorName,
+            author_email  : authorMail
+        });
+    }
 }
 
 
diff --git a/ExpressAPI/src/routes/ExerciceRoutes.ts b/ExpressAPI/src/routes/ExerciceRoutes.ts
index 5fde126..f4a2e38 100644
--- a/ExpressAPI/src/routes/ExerciceRoutes.ts
+++ b/ExpressAPI/src/routes/ExerciceRoutes.ts
@@ -120,6 +120,18 @@ class ExerciceRoutes implements RoutesManager {
             return res.status(StatusCodes.INSUFFICIENT_SPACE_ON_RESOURCE).send();
         }
 
+        try {
+            await GitlabManager.createFile(repository.id, '.gitlab-ci.yml', fs.readFileSync(path.join(__dirname, '../../assets/exercice_gitlab_ci.yml'), 'base64'), 'Add .gitlab-ci.yml (DO NOT MODIFY THIS FILE)');
+        } catch ( error ) {
+            logger.error(error);
+
+            if ( error instanceof AxiosError ) {
+                return res.status(error.response?.status ?? HttpStatusCode.InternalServerError).send();
+            }
+
+            return res.status(StatusCodes.INTERNAL_SERVER_ERROR).send();
+        }
+
         try {
             await Promise.all([ ...new Set([ ...enonce.staff.map(user => user.gitlabId), ...params.members.map(member => member.id) ]) ].map(async (memberId: number): Promise<GitlabMember | false> => {
                 try {
@@ -158,11 +170,12 @@ class ExerciceRoutes implements RoutesManager {
 
             return req.session.sendResponse(res, StatusCodes.OK, exercice);
         } catch ( error ) {
+            logger.error(error);
+            
             if ( error instanceof AxiosError ) {
                 return res.status(error.response?.status ?? HttpStatusCode.InternalServerError).send();
             }
 
-            logger.error(error);
             return res.status(StatusCodes.INTERNAL_SERVER_ERROR).send();
         }
     }
-- 
GitLab