From 90a9ce23fd9043f9204afd0baa03e883cb367f32 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <git@minelli.me>
Date: Tue, 19 Mar 2024 12:46:28 +0100
Subject: [PATCH] GitlabManager => Move instance to singleton

---
 .../assignment/subcommands/AssignmentCreateCommand.ts     | 6 +++---
 NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts | 8 ++++----
 .../exercise/subcommands/ExerciseCreateCommand.ts         | 6 +++---
 NodeApp/src/helpers/AccessesHelper.ts                     | 6 +++---
 NodeApp/src/helpers/GlobalHelper.ts                       | 5 +----
 NodeApp/src/managers/GitlabManager.ts                     | 7 ++++++-
 NodeApp/src/managers/SessionManager.ts                    | 8 ++++----
 NodeApp/src/shared                                        | 2 +-
 8 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts b/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts
index 25d8f79..5124242 100644
--- a/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts
+++ b/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts
@@ -5,8 +5,8 @@ import Assignment         from '../../../sharedByClients/models/Assignment';
 import DojoBackendManager from '../../../managers/DojoBackendManager';
 import Toolbox            from '../../../shared/helpers/Toolbox';
 import * as Gitlab        from '@gitbeaker/rest';
-import GlobalHelper       from '../../../helpers/GlobalHelper';
 import TextStyle          from '../../../types/TextStyle';
+import GitlabManager      from '../../../managers/GitlabManager';
 
 
 type CommandOptions = { name: string, template?: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean }
@@ -37,7 +37,7 @@ class AssignmentCreateCommand extends CommanderCommand {
             throw new Error();
         }
 
-        this.members = await GlobalHelper.gitlabManager.fetchMembers(options);
+        this.members = await GitlabManager.fetchMembers(options);
         if ( !this.members ) {
             throw new Error();
         }
@@ -84,7 +84,7 @@ class AssignmentCreateCommand extends CommanderCommand {
         if ( options.clone ) {
             console.log(TextStyle.BLOCK('Please wait while we are cloning the repository...'));
 
-            await GlobalHelper.gitlabManager.cloneRepository(options.clone, this.assignment.gitlabCreationInfo.ssh_url_to_repo, undefined, true, 0);
+            await GitlabManager.cloneRepository(options.clone, this.assignment.gitlabCreationInfo.ssh_url_to_repo, undefined, true, 0);
         }
     }
 
diff --git a/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts b/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts
index 163678b..e2d18f8 100644
--- a/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts
+++ b/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts
@@ -1,6 +1,6 @@
 import CommanderCommand from '../../CommanderCommand';
 import SessionManager   from '../../../managers/SessionManager';
-import GlobalHelper     from '../../../helpers/GlobalHelper';
+import GitlabManager    from '../../../managers/GitlabManager';
 
 
 class AuthTestCommand extends CommanderCommand {
@@ -8,13 +8,13 @@ class AuthTestCommand extends CommanderCommand {
 
     protected defineCommand() {
         this.command
-        .description('test availability of registered Dojo API and Gitlab API sessions')
-        .action(this.commandAction.bind(this));
+            .description('test availability of registered Dojo API and Gitlab API sessions')
+            .action(this.commandAction.bind(this));
     }
 
     protected async commandAction(): Promise<void> {
         await SessionManager.testSession();
-        await GlobalHelper.gitlabManager.testToken();
+        await GitlabManager.testToken();
     }
 }
 
diff --git a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts
index 335f44f..8a0d7ea 100644
--- a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts
+++ b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts
@@ -5,8 +5,8 @@ import AccessesHelper     from '../../../helpers/AccessesHelper';
 import Assignment         from '../../../sharedByClients/models/Assignment';
 import Exercise           from '../../../sharedByClients/models/Exercise';
 import * as Gitlab        from '@gitbeaker/rest';
-import GlobalHelper       from '../../../helpers/GlobalHelper';
 import TextStyle          from '../../../types/TextStyle';
+import GitlabManager      from '../../../managers/GitlabManager';
 
 
 type CommandOptions = { assignment: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean }
@@ -36,7 +36,7 @@ class ExerciseCreateCommand extends CommanderCommand {
             throw new Error();
         }
 
-        this.members = await GlobalHelper.gitlabManager.fetchMembers(options);
+        this.members = await GitlabManager.fetchMembers(options);
         if ( !this.members ) {
             throw new Error();
         }
@@ -87,7 +87,7 @@ class ExerciseCreateCommand extends CommanderCommand {
         if ( options.clone ) {
             console.log(TextStyle.BLOCK('Please wait while we are cloning the repository...'));
 
-            await GlobalHelper.gitlabManager.cloneRepository(options.clone, this.exercise.gitlabCreationInfo.ssh_url_to_repo, `DojoExercise - ${ this.exercise.assignmentName }`, true, 0);
+            await GitlabManager.cloneRepository(options.clone, this.exercise.gitlabCreationInfo.ssh_url_to_repo, `DojoExercise - ${ this.exercise.assignmentName }`, true, 0);
         }
     }
 
diff --git a/NodeApp/src/helpers/AccessesHelper.ts b/NodeApp/src/helpers/AccessesHelper.ts
index 9b868cf..c9ff5e1 100644
--- a/NodeApp/src/helpers/AccessesHelper.ts
+++ b/NodeApp/src/helpers/AccessesHelper.ts
@@ -1,5 +1,5 @@
 import SessionManager from '../managers/SessionManager';
-import GlobalHelper   from './GlobalHelper';
+import GitlabManager  from '../managers/GitlabManager';
 
 
 class AccessesHelper {
@@ -11,7 +11,7 @@ class AccessesHelper {
         }
 
         if ( testGitlab ) {
-            return (await GlobalHelper.gitlabManager.testToken(true)).every(result => result);
+            return (await GitlabManager.testToken(true)).every(result => result);
         } else {
             return true;
         }
@@ -25,7 +25,7 @@ class AccessesHelper {
         }
 
         if ( testGitlab ) {
-            return (await GlobalHelper.gitlabManager.testToken(true)).every(result => result);
+            return (await GitlabManager.testToken(true)).every(result => result);
         } else {
             return true;
         }
diff --git a/NodeApp/src/helpers/GlobalHelper.ts b/NodeApp/src/helpers/GlobalHelper.ts
index 50192cb..8ecac86 100644
--- a/NodeApp/src/helpers/GlobalHelper.ts
+++ b/NodeApp/src/helpers/GlobalHelper.ts
@@ -1,7 +1,6 @@
 import { Command, Option } from 'commander';
 import Config              from '../config/Config';
 import SessionManager      from '../managers/SessionManager';
-import GitlabManager       from '../managers/GitlabManager';
 
 
 class GlobalHelper {
@@ -16,13 +15,11 @@ class GlobalHelper {
     }
 
 
-    private readonly refreshGitlabTokenFunction = async () => {
+    public readonly refreshGitlabTokenFunction = async () => {
         await SessionManager.refreshTokens();
 
         return SessionManager.gitlabCredentials.accessToken ?? '';
     };
-
-    readonly gitlabManager = new GitlabManager('', this.refreshGitlabTokenFunction.bind(this));
 }
 
 
diff --git a/NodeApp/src/managers/GitlabManager.ts b/NodeApp/src/managers/GitlabManager.ts
index b24ab00..9291be4 100644
--- a/NodeApp/src/managers/GitlabManager.ts
+++ b/NodeApp/src/managers/GitlabManager.ts
@@ -4,12 +4,17 @@ import { spawn }                                 from 'child_process';
 import { NotificationSettingSchema, UserSchema } from '@gitbeaker/rest';
 import * as GitlabCore                           from '@gitbeaker/core';
 import SharedGitlabManager                       from '../shared/managers/SharedGitlabManager';
+import GlobalHelper                              from '../helpers/GlobalHelper';
 
 
 type getGitlabUser = (param: number | string) => Promise<UserSchema | undefined>
 
 
 class GitlabManager extends SharedGitlabManager {
+    constructor() {
+        super('', GlobalHelper.refreshGitlabTokenFunction.bind(GlobalHelper));
+    }
+
     public async testToken(verbose: boolean = true): Promise<[ boolean, boolean ]> {
         if ( verbose ) {
             ora('Checking Gitlab token: ').start().info();
@@ -208,4 +213,4 @@ class GitlabManager extends SharedGitlabManager {
 }
 
 
-export default GitlabManager;
+export default new GitlabManager();
diff --git a/NodeApp/src/managers/SessionManager.ts b/NodeApp/src/managers/SessionManager.ts
index 27264fc..9e8638e 100644
--- a/NodeApp/src/managers/SessionManager.ts
+++ b/NodeApp/src/managers/SessionManager.ts
@@ -18,9 +18,9 @@ import inquirer                  from 'inquirer';
 import GitlabToken               from '../shared/types/Gitlab/GitlabToken';
 import open                      from 'open';
 import { sessionConfigFile }     from '../config/ConfigFiles';
-import GlobalHelper              from '../helpers/GlobalHelper';
 import TextStyle                 from '../types/TextStyle';
 import DojoBackendHelper         from '../sharedByClients/helpers/Dojo/DojoBackendHelper';
+import GitlabManager             from './GitlabManager';
 
 
 class LoginServer {
@@ -117,7 +117,7 @@ class SessionManager {
         sessionConfigFile.setParam(LocalConfigKeys.GITLAB, credentials);
 
         if ( credentials.accessToken ) {
-            GlobalHelper.gitlabManager.setToken(credentials.accessToken);
+            GitlabManager.setToken(credentials.accessToken);
         }
     }
 
@@ -208,7 +208,7 @@ class SessionManager {
                                         }).start();
         let gitlabTokens: GitlabToken;
         try {
-            gitlabTokens = await GlobalHelper.gitlabManager.getTokens(gitlabCode);
+            gitlabTokens = await GitlabManager.getTokens(gitlabCode);
             this.gitlabCredentials = {
                 refreshToken: gitlabTokens.refresh_token,
                 accessToken : gitlabTokens.access_token
@@ -219,7 +219,7 @@ class SessionManager {
             throw error;
         }
 
-        const isGitlabTokensValid = (await GlobalHelper.gitlabManager.testToken()).every(value => value);
+        const isGitlabTokensValid = (await GitlabManager.testToken()).every(value => value);
         if ( !isGitlabTokensValid ) {
             throw new Error('Gitlab tokens are invalid');
         }
diff --git a/NodeApp/src/shared b/NodeApp/src/shared
index 87bb71b..e1ffeb5 160000
--- a/NodeApp/src/shared
+++ b/NodeApp/src/shared
@@ -1 +1 @@
-Subproject commit 87bb71b1c38a784550d12efe7626e62323355813
+Subproject commit e1ffeb584f5c7805befd951cb82ca179eabcaabb
-- 
GitLab