Coverage for sel_tools/gitlab_api/fetch_repo.py: 95%

20 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-04 21:22 +0000

1"""Clone or pull repos into a local workspace.""" 

2 

3import json 

4import os 

5from pathlib import Path 

6 

7import gitlab 

8from gitlab.v4.objects.projects import Project 

9from tqdm import tqdm 

10 

11from sel_tools.config import GIT_MAIN_BRANCH, GITLAB_SERVER_URL 

12from sel_tools.utils.repo import GitlabProject, GitRepo 

13 

14 

15def fetch_repos(workspace: Path, student_repos_file: Path, gitlab_token: str) -> list[GitlabProject]: 

16 """Fetch the student repositories into the workspace.""" 

17 workspace.mkdir(parents=True, exist_ok=True) 

18 

19 student_repos = json.loads(student_repos_file.read_text()) 

20 gitlab_instance = gitlab.Gitlab(GITLAB_SERVER_URL, private_token=gitlab_token) 

21 return [ 

22 fetch_repo( 

23 GitRepo(workspace / student_repo["name"], student_repo.get("branch", GIT_MAIN_BRANCH)), 

24 gitlab_instance.projects.get(student_repo["id"]), 

25 ) 

26 for student_repo in tqdm(student_repos, desc="Fetching Repos") 

27 ] 

28 

29 

30def fetch_repo(repo: GitRepo, gitlab_project: Project) -> GitlabProject: 

31 """Clone or pull student repo.""" 

32 if os.environ.get("SEL_CI_MODE"): 

33 repo.fetch_from(gitlab_project.http_url_to_repo) 

34 elif repo.is_repo(): 

35 repo.pull() 

36 else: 

37 repo.clone(gitlab_project.ssh_url_to_repo) 

38 return GitlabProject(repo.path, gitlab_project)