From 3cdab89eba94b88def3a28834304255e4f6ce315 Mon Sep 17 00:00:00 2001
From: Adrien Lescourt <adrien@lescourt.net>
Date: Thu, 10 Apr 2025 11:05:45 +0200
Subject: [PATCH] Improve stdout messages for repo creations

---
 pwm/pwm.py | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/pwm/pwm.py b/pwm/pwm.py
index b41582b..3807fc5 100755
--- a/pwm/pwm.py
+++ b/pwm/pwm.py
@@ -112,7 +112,13 @@ class Gitlab:
             print(f"Error searching user {user.user_login_aai}")
             print(res.text)
         else:
-            return str(res.json()[0]["id"])
+            res_json = res.json()
+            if len(res_json) == 1:
+                return str(res.json()[0]["id"])
+            elif len(res_json) > 1:
+                print(f"ERROR: More than one users found for {user.user_login_aai}")
+            else:
+                print(f"ERROR: No user found for {user.user_login_aai}")
 
     def add_users_to_repository(
         self,
@@ -120,8 +126,8 @@ class Gitlab:
         project_name: str,
         users: list[User],
         expires_at: str | None,
-    ):
-        """Add users to a repo, and set their permissions"""
+    ) -> list[User]:
+        """Add users to a repo, and set their permissions, returns all user added"""
         headers = {"PRIVATE-TOKEN": self.token}
         # Allow users with developer access level to push and merge on master
         access_level = 40
@@ -140,12 +146,14 @@ class Gitlab:
                 f"Error setting project ({project_name}) premissions", res.text
             )
 
+        added_users = []
         for user in users:
             user_id = self.get_user_id(user)
             if user_id is None:
                 print(
                     f"ERROR: User {user.user_login_aai} will not be added to project: {project_name}"
                 )
+                continue
             params = {"user_id": user_id, "access_level": access_level}
             if expires_at:
                 params["expires_at"] = expires_at
@@ -159,6 +167,8 @@ class Gitlab:
                     f"ERROR: User {user.user_login_aai} will not be added to project: {project_name}"
                 )
                 print(res.text)
+            added_users.append(user)
+        return added_users
 
     def create_repository(
         self,
@@ -328,11 +338,11 @@ def command_create_group_repos(args):
             group.name,
             args.import_url,
         )
-        gl.add_users_to_repository(
+        added_users = gl.add_users_to_repository(
             project_id, group.name, group.user_ids, args.expires_at
         )
         print(
-            f"Created repo: {group.name} with the users: {[u.user_login_aai for u in group.user_ids]}"
+            f"Created repo: {group.name} with the users: {[u.user_login_aai for u in added_users]}"
         )
 
 
-- 
GitLab