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

23 statements  

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

1"""Comment to gitlab issues.""" 

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.comment import Comment 

17 

18 

19def comment_issues(comment: Comment, student_repos_file: Path, gitlab_token: str) -> None: 

20 """Comment to all issues from comment to 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="Commenting to issues"): 

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

25 create_comment(deepcopy(comment), student_homework_project) 

26 

27 

28def create_comment(comment: Comment, gitlab_project: Project) -> None: 

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

30 uploaded_files = upload_attachments(comment.attachments, gitlab_project) 

31 comment.message = replace_file_paths_with_urls(comment.message, uploaded_files, comment.attachments) 

32 

33 issue = gitlab_project.issues.get(comment.issue_id) 

34 issue.notes.create({"body": comment.message}) 

35 if comment.state_event is not None: 

36 issue.state_event = comment.state_event 

37 issue.save()