Coverage for tools/sel_tools/gitlab_api/add_user.py: 82%
22 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-10-06 08:15 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-10-06 08:15 +0000
1"""Add users to projects."""
3from pathlib import Path
5import gitlab
6import gitlab.const
8from sel_tools.file_parsing.student_group_parser import (
9 Student,
10 get_student_groups_from_file,
11)
14def add_users(student_repos: list[dict], student_group_file: Path, gitlab_instance: gitlab.Gitlab) -> None:
15 """Add all students to repositories."""
16 students = get_student_groups_from_file(student_group_file)
17 students_found = find_gitlab_users_of_students(gitlab_instance, students)
18 repo_from_group_id = {
19 int(repo["name"].split("_")[-1]): gitlab_instance.projects.get(repo["id"]) for repo in student_repos
20 }
21 add_students_to_repos(students_found, repo_from_group_id)
24def find_gitlab_users_of_students(gitlab_instance: gitlab.Gitlab, students: list[Student]) -> list[Student]:
25 """Return list of students with their Gitlab users."""
26 for student in students:
27 try:
28 student.gitlab_user = gitlab_instance.users.list(search=student.mail_addr)[0]
29 except IndexError:
30 student.gitlab_user = None
31 print(f"Student {student.name} with {student.mail_addr} not found!")
32 return [student for student in students if student.gitlab_user is not None]
35def add_students_to_repos(students: list[Student], repo_from_group_id: dict) -> None:
36 """Add students to repositories."""
37 print(f"Adding {len(students)} to their projects!")
38 for student in students:
39 repo = repo_from_group_id[student.group_id]
40 repo.members.create(
41 {
42 # Students had to provide their ID and sign up so their account should exist
43 "user_id": student.gitlab_user.id, # ty: ignore[unresolved-attribute]
44 "access_level": gitlab.const.DEVELOPER_ACCESS,
45 }
46 )