From 99923d9d24a8155b30fe2b284e12e6ce2462ec4b Mon Sep 17 00:00:00 2001 From: Adrien Lescourt <adrien@lescourt.net> Date: Thu, 10 Apr 2025 10:42:54 +0200 Subject: [PATCH] Split repo creation and user managment for the repo --- README.md | 2 +- pwm/pwm.py | 108 ++++++++++++++++++++++++++--------------------------- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 1b3304f..ecd2314 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # WIP REFACTOR STATUS -- [ ] Create group repo +- [x] Create group repo - [ ] Clone repo - [x] List groups - [x] List projects diff --git a/pwm/pwm.py b/pwm/pwm.py index 0e36065..5e08b93 100755 --- a/pwm/pwm.py +++ b/pwm/pwm.py @@ -23,7 +23,7 @@ TOKEN_URL: str = BASE_URL + "/-/user_settings/personal_access_tokens" @dataclass class User: - id: str + user_login_aai: str @dataclass @@ -104,43 +104,25 @@ class Gitlab: def get_user_id(self, user: User) -> str | None: headers = {"PRIVATE-TOKEN": self.token} res = requests.get( - BASE_API_URL + "/users", params={"search": user.id}, headers=headers + BASE_API_URL + "/users", + params={"search": user.user_login_aai}, + headers=headers, ) if res.status_code != 200: - print(f"Error searching user {user.id}") + print(f"Error searching user {user.user_login_aai}") print(res.text) else: - return res.json()[0]["id"] + return str(res.json()[0]["id"]) - def create_repository( + def add_users_to_repository( self, - group_id: str, + project_id: str, + project_name: str, users: list[User], - name: str, - import_url: str | None, expires_at: str | None, ): - """ - Create repository in group_id, with members from user_ids, a name, and - optional import_url and expiration date. - """ + """Add users to a repo, and set their permissions""" headers = {"PRIVATE-TOKEN": self.token} - - # Create project from name, import_url (if given) and group_id - params = {"name": name, "namespace_id": group_id, "visibility": "private"} - if import_url: - params["import_url"] = import_url - - project = requests.post( - BASE_API_URL + "/projects", params=params, headers=headers - ).json() - if "message" in project: - print("Error in creating project: %s" % project) - exit(1) - print( - "Project '" + project["name"] + "' at '" + project["web_url"] + "' created" - ) - # Allow users with developer access level to push and merge on master access_level = 40 params = { @@ -148,42 +130,58 @@ class Gitlab: "push_access_level": str(access_level), "merge_access_level": str(access_level), } - requests.post( - BASE_API_URL + "/projects/" + str(project["id"]) + "/protected_branches", + res = requests.post( + BASE_API_URL + "/projects/" + str(project_id) + "/protected_branches", params=params, headers=headers, - ).json() + ) + if res.status_code != 201: + raise Exception( + f"Error setting project ({project_name}) premissions", res.text + ) - # Add each student as maintainer (level 40) for user in users: user_id = self.get_user_id(user) if user_id is None: - print(f"ERROR: User {user.id} will not be added to project: {name}") + print( + f"ERROR: User {user.user_login_aai} will not be added to project: {project_name}" + ) params = {"user_id": user_id, "access_level": access_level} if expires_at: params["expires_at"] = expires_at res = requests.post( - BASE_API_URL + "/projects/" + str(project["id"]) + "/members", + BASE_API_URL + "/projects/" + project_id + "/members", params=params, headers=headers, ) if res.status_code != 201: - print(res.text) - else: - new_user = res.json() - out = ( - "Adding '" - + new_user["name"] - + "' (" - + new_user["username"] - + ") in '" - + project["name"] - + "' with access level: " - + str(new_user["access_level"]) + print( + f"ERROR: User {user.user_login_aai} will not be added to project: {project_name}" ) - if expires_at: - out += ", expires at: " + new_user["expires_at"] - print(out) + print(res.text) + + def create_repository( + self, + group_id: str, + name: str, + import_url: str | None, + ) -> str: + """ + Create repository in group_id, with a name and optional import_url + """ + headers = {"PRIVATE-TOKEN": self.token} + + # Create project from name, import_url (if given) and group_id + params = {"name": name, "namespace_id": group_id, "visibility": "private"} + if import_url: + params["import_url"] = import_url + + res = requests.post(BASE_API_URL + "/projects", params=params, headers=headers) + + if res.status_code != 201: + raise Exception(f"Error creating project {name}", res.text) + + return str(res.json()["id"]) @staticmethod def _paginate_responses( @@ -325,15 +323,17 @@ def command_create_group_repos(args): ds = DataSource.from_yaml(args.repos_file) group_id = gl.create_group(args.group_name, args.visibility) for group in ds.groups: - gl.create_repository( + project_id = gl.create_repository( group_id, - group.user_ids, group.name, args.import_url, - args.expires_at, ) - print(f"Created repo: {group.name} with the users: {group.user_ids}") - print() + 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]}" + ) def command_clone_all(args): -- GitLab