Coverage for sel_tools/gitlab_api/create_issue.py: 100%

22 statements  

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

1"""Create gitlab issues from tasks.""" 

2 

3import json 

4from copy import deepcopy 

5from pathlib import Path 

6 

7import gitlab 

8from gitlab.v4.objects.projects import Project 

9from tqdm import tqdm 

10 

11from sel_tools.config import GITLAB_SERVER_URL 

12from sel_tools.gitlab_api.attachments import ( 

13 replace_file_paths_with_urls, 

14 upload_attachments, 

15) 

16from sel_tools.utils.task import Task 

17 

18 

19def create_issues(tasks: list[Task], student_repos_file: Path, gitlab_token: str) -> None: 

20 """Create gitlab issues from tasks for all student repos.""" 

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

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

23 for student_repo in tqdm(student_repos, desc="Creating homework issues"): 

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

25 for task in tasks: 

26 create_issue(deepcopy(task), student_homework_project) 

27 

28 

29def create_issue(task: Task, gitlab_project: Project) -> None: 

30 """Create issue for gitlab project from task.""" 

31 uploaded_files = upload_attachments(task.attachments, gitlab_project) 

32 task.description = replace_file_paths_with_urls(task.description, uploaded_files, task.attachments) 

33 

34 gitlab_project.issues.create(get_issue_dict(task)) 

35 

36 

37def get_issue_dict(task: Task) -> dict: 

38 """Get a dict that contains the fields to create an issue from a task.""" 

39 return { 

40 "title": task.title, 

41 "description": task.description + "\n## Documentation\n" + task.documentation, 

42 "due_date": str(task.due_date) if task.due_date else "", 

43 "labels": [task.label] if task.label else [], 

44 }