From 07d2a1abd5bda0b706244027b732d674b4c4fbcb Mon Sep 17 00:00:00 2001
From: Joel von der Weid <joel.von-der-weid@hesge.ch>
Date: Tue, 2 Apr 2024 14:26:15 +0200
Subject: [PATCH] Add sonar types

---
 config/SharedConfig.ts              |  7 ++++++-
 managers/SharedSonarManager.ts      | 27 +++++++++++++++++++++++++++
 types/Sonar/SonarProjectCreation.ts | 10 ++++++++++
 types/Sonar/SonarRoute.ts           |  6 ++++++
 4 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 managers/SharedSonarManager.ts
 create mode 100644 types/Sonar/SonarProjectCreation.ts
 create mode 100644 types/Sonar/SonarRoute.ts

diff --git a/config/SharedConfig.ts b/config/SharedConfig.ts
index 5e4121f..4cbfe89 100644
--- a/config/SharedConfig.ts
+++ b/config/SharedConfig.ts
@@ -1,3 +1,6 @@
+import * as process from 'process';
+
+
 class SharedConfig {
     public readonly production: boolean;
     public debug: boolean = false;
@@ -7,13 +10,15 @@ class SharedConfig {
     public sonar: {
         enabled: boolean
         url: string
+        token: string
     }
 
     constructor() {
         this.production = process.env.NODE_ENV === 'production';
         this.sonar = {
             enabled: ['yes', 'true', '1', 'on'].includes(process.env.SONAR_ENABLED?.trim()?.toLowerCase() ?? ''),
-            url: process.env.SONAR_URL || ''
+            url: process.env.SONAR_URL || '',
+            token: process.env.SONAR_TOKEN || ''
         };
 
         this.logsFolder = process.env.LOGS_FOLDER ?? '';
diff --git a/managers/SharedSonarManager.ts b/managers/SharedSonarManager.ts
new file mode 100644
index 0000000..5169202
--- /dev/null
+++ b/managers/SharedSonarManager.ts
@@ -0,0 +1,27 @@
+import axios        from 'axios';
+import https        from 'https';
+import SharedConfig from '../config/SharedConfig';
+
+
+class SharedSonarManager {
+
+    // Use custom instance to allow self-signed certificates
+    private instance = axios.create({
+        httpsAgent: new https.Agent({
+            rejectUnauthorized: false
+        })
+    });
+    async isSonarSupported(): Promise<boolean> {
+        if (!SharedConfig.sonar.enabled) {
+            return false;
+        }
+        try {
+            const sonar = await this.instance.get(SharedConfig.sonar.url);
+            return sonar.status == 200;
+        } catch ( error ) {
+            return false;
+        }
+    }
+}
+
+export default new SharedSonarManager();
\ No newline at end of file
diff --git a/types/Sonar/SonarProjectCreation.ts b/types/Sonar/SonarProjectCreation.ts
new file mode 100644
index 0000000..9c1bde9
--- /dev/null
+++ b/types/Sonar/SonarProjectCreation.ts
@@ -0,0 +1,10 @@
+interface SonarProjectCreation {
+    project: {
+        key: string,
+        name: string,
+        qualifier: string,
+        visibility: string
+    }
+}
+
+export default SonarProjectCreation;
\ No newline at end of file
diff --git a/types/Sonar/SonarRoute.ts b/types/Sonar/SonarRoute.ts
new file mode 100644
index 0000000..ecfab74
--- /dev/null
+++ b/types/Sonar/SonarRoute.ts
@@ -0,0 +1,6 @@
+enum SonarRoute {
+    SET_PAT               = '/api/alm_integrations/set_pat',
+    PROJECT_CREATE_GITLAB = '/api/alm_integrations/import_gitlab_project'
+}
+
+export default SonarRoute;
\ No newline at end of file
-- 
GitLab