Coverage for tools / sel_tools / gitlab_api / create_repo.py: 100%

25 statements  

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

1"""Create gitlab repos with contents of source folder.""" 

2 

3from pathlib import Path 

4 

5import gitlab 

6from gitlab.v4.objects import Project, ProjectProtectedBranch 

7from tqdm import tqdm 

8 

9from sel_tools.config import GIT_MAIN_BRANCH, REPO_DIR, RUNNER_ID 

10from sel_tools.gitlab_api.create_commit import create_commit 

11 

12AVATAR_PATH = REPO_DIR / "assets" / "repo-avatar.png" 

13 

14 

15def create_repos( 

16 source_folder: Path, 

17 repo_base_name: str, 

18 group_id: int, 

19 number_of_repos: int, 

20 gitlab_instance: gitlab.Gitlab, 

21) -> tuple[list[dict], str]: 

22 """Create gitlab repos with contents of source folder.""" 

23 student_repos = [] 

24 for repo_number in tqdm(range(1, number_of_repos + 1), desc="Creating Student Repos"): 

25 project = gitlab_instance.projects.create(get_repo_settings(group_id, repo_base_name, repo_number)) 

26 configure_project(project) 

27 main_branch = configure_main_branch(project) 

28 create_commit(source_folder, "Initial commit", main_branch.name, project) 

29 student_repos.append({"name": project.name, "id": project.id, "branch": main_branch.name}) 

30 

31 group = gitlab_instance.groups.get(group_id) 

32 return student_repos, group.path 

33 

34 

35def configure_project(gitlab_project: Project) -> None: 

36 """Configure gitlab project.""" 

37 gitlab_project.runners.create({"runner_id": RUNNER_ID}) 

38 gitlab_project.avatar = AVATAR_PATH.read_bytes() 

39 gitlab_project.save() 

40 

41 

42def configure_main_branch(gitlab_project: Project) -> ProjectProtectedBranch: 

43 """Configure main branch.""" 

44 return gitlab_project.protectedbranches.create( 

45 { 

46 "name": GIT_MAIN_BRANCH, 

47 "merge_access_level": gitlab.const.DEVELOPER_ACCESS, 

48 "push_access_level": gitlab.const.MAINTAINER_ACCESS, 

49 } 

50 ) 

51 

52 

53def get_repo_settings(group_id: int, repo_base_name: str, repo_number: int) -> dict: 

54 """Get gitlab repo settings dictionary.""" 

55 return { 

56 "name": f"{repo_base_name}_{repo_number}", 

57 "description": f"Software Engineering Lab Homework Group {repo_number}", 

58 "namespace_id": group_id, 

59 "jobs_enabled": True, 

60 }