Coverage for tools / sel_tools / gitlab_api / add_user.py: 81%

21 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-02 18:55 +0000

1"""Add users to projects.""" 

2 

3from pathlib import Path 

4 

5import gitlab 

6 

7from sel_tools.file_parsing.student_group_parser import ( 

8 Student, 

9 get_student_groups_from_file, 

10) 

11 

12 

13def add_users(student_repos: list[dict], student_group_file: Path, gitlab_instance: gitlab.Gitlab) -> None: 

14 """Add all students to repositories.""" 

15 students = get_student_groups_from_file(student_group_file) 

16 students_found = find_gitlab_users_of_students(gitlab_instance, students) 

17 repo_from_group_id = { 

18 int(repo["name"].split("_")[-1]): gitlab_instance.projects.get(repo["id"]) for repo in student_repos 

19 } 

20 add_students_to_repos(students_found, repo_from_group_id) 

21 

22 

23def find_gitlab_users_of_students(gitlab_instance: gitlab.Gitlab, students: list[Student]) -> list[Student]: 

24 """Return list of students with their Gitlab users.""" 

25 for student in students: 

26 try: 

27 student.gitlab_user = gitlab_instance.users.list(search=student.mail_addr)[0] 

28 except IndexError: 

29 student.gitlab_user = None 

30 print(f"Student {student.name} with {student.mail_addr} not found!") 

31 return [student for student in students if student.gitlab_user is not None] 

32 

33 

34def add_students_to_repos(students: list[Student], repo_from_group_id: dict) -> None: 

35 """Add students to repositories.""" 

36 print(f"Adding {len(students)} to their projects!") 

37 for student in students: 

38 repo = repo_from_group_id[student.group_id] 

39 repo.members.create( 

40 { 

41 "user_id": student.gitlab_user.id, 

42 "access_level": gitlab.const.DEVELOPER_ACCESS, 

43 } 

44 )