From 3af7942b692688330149a8b64d45f348fe39e2a7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Sat, 24 Feb 2024 00:13:47 +0100
Subject: [PATCH] SharedGitlabManager => Add getUserById and ByUsername
 functions

---
 managers/SharedGitlabManager.ts | 43 +++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 7 deletions(-)

diff --git a/managers/SharedGitlabManager.ts b/managers/SharedGitlabManager.ts
index 42039c2..1633ec0 100644
--- a/managers/SharedGitlabManager.ts
+++ b/managers/SharedGitlabManager.ts
@@ -1,10 +1,10 @@
-import axios                      from 'axios';
-import SharedConfig               from '../config/SharedConfig';
-import * as GitlabCore            from '@gitbeaker/core';
-import { GitbeakerRequestError }  from '@gitbeaker/requester-utils';
-import { Gitlab, PipelineSchema } from '@gitbeaker/rest';
-import GitlabToken                from '../types/Gitlab/GitlabToken';
-import { StatusCodes }            from 'http-status-codes';
+import axios                                  from 'axios';
+import SharedConfig                           from '../config/SharedConfig';
+import * as GitlabCore                        from '@gitbeaker/core';
+import { GitbeakerRequestError }              from '@gitbeaker/requester-utils';
+import { Gitlab, PipelineSchema, UserSchema } from '@gitbeaker/rest';
+import GitlabToken                            from '../types/Gitlab/GitlabToken';
+import { StatusCodes }                        from 'http-status-codes';
 
 
 class SharedGitlabManager {
@@ -50,6 +50,35 @@ class SharedGitlabManager {
         return response.data;
     }
 
+    public async getUserById(id: number): Promise<UserSchema | undefined> {
+        try {
+            return await this.executeGitlabRequest(async () => {
+                const user = await this.api.Users.show(id);
+
+                return user.id === id ? user : undefined;
+            });
+        } catch ( e ) {
+            return undefined;
+        }
+    }
+
+    public async getUserByUsername(username: string): Promise<UserSchema | undefined> {
+        try {
+            return await this.executeGitlabRequest(async () => {
+                const user = await this.api.Users.all({
+                                                          username: username,
+                                                          maxPages: 1,
+                                                          perPage : 1
+                                                      });
+
+
+                return user.length > 0 && user[0].username === username ? user[0] : undefined;
+            });
+        } catch ( e ) {
+            return undefined;
+        }
+    }
+
     async getRepositoryPipelines(repoId: number, branch: string = 'main'): Promise<Array<PipelineSchema>> {
         return await this.executeGitlabRequest(async () => {
             return await this.api.Pipelines.all(repoId, {
-- 
GitLab