|
| 1 | +import logging |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import TypeVar, Callable |
| 4 | + |
| 5 | +from github.CommitComment import CommitComment |
| 6 | +from github.CommitStatus import CommitStatus |
| 7 | +from github.Reaction import Reaction |
| 8 | +from github.IssueComment import IssueComment |
| 9 | +from github.IssueEvent import IssueEvent |
| 10 | +from github.TimelineEvent import TimelineEvent |
| 11 | +from github.PullRequestComment import PullRequestComment |
| 12 | +from github.PullRequestReview import PullRequestReview |
| 13 | + |
| 14 | +from gitlab2prov.domain.objects import Annotation, User |
| 15 | +from gitlab2prov.domain.constants import ProvRole |
| 16 | + |
| 17 | +A = TypeVar("A") |
| 18 | + |
| 19 | +log = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class GithubAnnotationParser: |
| 24 | + @staticmethod |
| 25 | + def sort_by_date(annotations: list[Annotation]) -> list[Annotation]: |
| 26 | + return list(sorted(annotations, key=lambda a: a.start)) |
| 27 | + |
| 28 | + def choose_parser(self, raw_annotation: A) -> Callable[[A], Annotation]: |
| 29 | + match raw_annotation: |
| 30 | + case CommitComment(): |
| 31 | + return self.parse_commit_comment |
| 32 | + case CommitStatus(): |
| 33 | + return self.parse_commit_status |
| 34 | + case Reaction(): |
| 35 | + return self.parse_reaction |
| 36 | + case IssueComment(): |
| 37 | + return self.parse_issue_comment |
| 38 | + case IssueEvent(): |
| 39 | + return self.parse_issue_event |
| 40 | + case TimelineEvent(): |
| 41 | + return self.parse_timeline_event |
| 42 | + case PullRequestReview(): |
| 43 | + return self.parse_pull_request_review |
| 44 | + case PullRequestComment(): |
| 45 | + return self.parse_pull_request_comment |
| 46 | + case _: |
| 47 | + log.warning(f"no parser found for {raw_annotation=}") |
| 48 | + |
| 49 | + def parse(self, annotations: list[A]) -> list[Annotation]: |
| 50 | + parsed_annotations = [] |
| 51 | + for annotation in annotations: |
| 52 | + if parser := self.choose_parser(annotation): |
| 53 | + parsed_annotations.append(parser(annotation)) |
| 54 | + return self.sort_by_date(parsed_annotations) |
| 55 | + |
| 56 | + def parse_commit_comment(self, comment: CommitComment) -> Annotation: |
| 57 | + annotator = User( |
| 58 | + name=comment.user.name, |
| 59 | + email=comment.user.email, |
| 60 | + github_username=comment.user.login, |
| 61 | + github_id=comment.user.id, |
| 62 | + prov_role=ProvRole.ANNOTATOR, |
| 63 | + ) |
| 64 | + return Annotation( |
| 65 | + uid=comment.id, |
| 66 | + name="add_comment", |
| 67 | + body=comment.body, |
| 68 | + start=comment.created_at, |
| 69 | + end=comment.created_at, |
| 70 | + annotator=annotator, |
| 71 | + ) |
| 72 | + |
| 73 | + def parse_commit_status(self, status: CommitStatus) -> Annotation: |
| 74 | + annotator = User( |
| 75 | + name=status.creator.name, |
| 76 | + email=status.creator.email, |
| 77 | + github_username=status.creator.login, |
| 78 | + github_id=status.creator.id, |
| 79 | + prov_role=ProvRole.ANNOTATOR, |
| 80 | + ) |
| 81 | + return Annotation( |
| 82 | + uid=status.id, |
| 83 | + name="add_commit_status", |
| 84 | + body=status.description, |
| 85 | + start=status.created_at, |
| 86 | + end=status.created_at, |
| 87 | + annotator=annotator, |
| 88 | + ) |
| 89 | + |
| 90 | + def parse_reaction(self, reaction: Reaction) -> Annotation: |
| 91 | + annotator = User( |
| 92 | + name=reaction.user.name, |
| 93 | + email=reaction.user.email, |
| 94 | + github_username=reaction.user.login, |
| 95 | + github_id=reaction.user.id, |
| 96 | + prov_role=ProvRole.ANNOTATOR, |
| 97 | + ) |
| 98 | + return Annotation( |
| 99 | + uid=reaction.id, |
| 100 | + name="add_award", |
| 101 | + body=reaction.content, |
| 102 | + start=reaction.created_at, |
| 103 | + end=reaction.created_at, |
| 104 | + annotator=annotator, |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | + def parse_issue_comment(self, comment: IssueComment) -> Annotation: |
| 109 | + annotator = User( |
| 110 | + name=comment.user.name, |
| 111 | + email=comment.user.email, |
| 112 | + github_username=comment.user.login, |
| 113 | + github_id=comment.user.id, |
| 114 | + prov_role=ProvRole.ANNOTATOR, |
| 115 | + ) |
| 116 | + return Annotation( |
| 117 | + uid=comment.id, |
| 118 | + name="add_comment", |
| 119 | + body=comment.body, |
| 120 | + start=comment.created_at, |
| 121 | + end=comment.created_at, |
| 122 | + annotator=annotator, |
| 123 | + ) |
| 124 | + |
| 125 | + def parse_issue_event(self, event: IssueEvent) -> Annotation: |
| 126 | + annotator = User( |
| 127 | + name=event.actor.name, |
| 128 | + email=event.actor.email, |
| 129 | + github_username=event.actor.login, |
| 130 | + github_id=event.actor.id, |
| 131 | + prov_role=ProvRole.ANNOTATOR, |
| 132 | + ) |
| 133 | + return Annotation( |
| 134 | + uid=event.id, |
| 135 | + name=event.event, |
| 136 | + body=event.event, |
| 137 | + start=event.created_at, |
| 138 | + end=event.created_at, |
| 139 | + annotator=annotator, |
| 140 | + ) |
| 141 | + |
| 142 | + def parse_timeline_event(self, event: TimelineEvent) -> Annotation: |
| 143 | + annotator = User( |
| 144 | + name=event.actor.name, |
| 145 | + email=event.actor.email, |
| 146 | + github_username=event.actor.login, |
| 147 | + github_id=event.actor.id, |
| 148 | + prov_role=ProvRole.ANNOTATOR, |
| 149 | + ) |
| 150 | + return Annotation( |
| 151 | + uid=event.id, |
| 152 | + name=event.event, |
| 153 | + body=event.event, |
| 154 | + start=event.created_at, |
| 155 | + end=event.created_at, |
| 156 | + annotator=annotator, |
| 157 | + ) |
| 158 | + |
| 159 | + def parse_pull_request_review(self, review: PullRequestReview) -> Annotation: |
| 160 | + annotator = User( |
| 161 | + name=review.user.name, |
| 162 | + email=review.user.email, |
| 163 | + github_username=review.user.login, |
| 164 | + github_id=review.user.id, |
| 165 | + prov_role=ProvRole.ANNOTATOR, |
| 166 | + ) |
| 167 | + return Annotation( |
| 168 | + uid=review.id, |
| 169 | + name="add_review", |
| 170 | + body=review.body, |
| 171 | + start=review.submitted_at, |
| 172 | + end=review.submitted_at, |
| 173 | + annotator=annotator, |
| 174 | + ) |
| 175 | + |
| 176 | + def parse_pull_request_comment(self, comment: PullRequestComment) -> Annotation: |
| 177 | + annotator = User( |
| 178 | + name=comment.user.name, |
| 179 | + email=comment.user.email, |
| 180 | + github_username=comment.user.login, |
| 181 | + github_id=comment.user.id, |
| 182 | + prov_role=ProvRole.ANNOTATOR, |
| 183 | + ) |
| 184 | + return Annotation( |
| 185 | + uid=comment.id, |
| 186 | + name="add_comment", |
| 187 | + body=comment.body, |
| 188 | + start=comment.created_at, |
| 189 | + end=comment.created_at, |
| 190 | + annotator=annotator, |
| 191 | + ) |
0 commit comments