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

18 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-21 08:53 +0000

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

2 

3import gitlab 

4from gitlab.v4.objects import Project 

5from tqdm import tqdm 

6 

7from sel_tools.gitlab_api.attachments import ( 

8 replace_file_paths_with_urls, 

9 upload_attachments, 

10) 

11from sel_tools.utils.comment import Comment, ProjectCommentParser 

12 

13 

14def comment_issues(comment: Comment, student_repos: list[dict], gitlab_instance: gitlab.Gitlab) -> None: 

15 """Comment to all issues from comment to student repos.""" 

16 project_comment_parser = ProjectCommentParser(comment, [student_repo["id"] for student_repo in student_repos]) 

17 for student_repo in tqdm( 

18 student_repos, 

19 desc="Commenting same message to all issues" 

20 if project_comment_parser.is_same_comment_for_all_projects 

21 else "Commenting specific message to individual projects", 

22 ): 

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

24 create_comment(project_comment_parser.get_comment_for_project(student_repo["id"]), student_homework_project) 

25 

26 

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

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

29 uploaded_files = upload_attachments(comment.attachments, gitlab_project) 

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

31 

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

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

34 if comment.state_event is not None: 

35 issue.state_event = comment.state_event 

36 issue.save()