|
| 1 | +"""GitHub app comment model.""" |
| 2 | + |
| 3 | +from django.contrib.contenttypes.fields import GenericForeignKey |
| 4 | +from django.contrib.contenttypes.models import ContentType |
| 5 | +from django.db import models |
| 6 | + |
| 7 | +from apps.common.models import BulkSaveModel, TimestampedModel |
| 8 | +from apps.common.utils import truncate |
| 9 | + |
| 10 | + |
| 11 | +class Comment(BulkSaveModel, TimestampedModel): |
| 12 | + """Represents a comment on a GitHub Issue.""" |
| 13 | + |
| 14 | + class Meta: |
| 15 | + db_table = "github_comments" |
| 16 | + verbose_name = "Comment" |
| 17 | + verbose_name_plural = "Comments" |
| 18 | + ordering = ("-nest_created_at",) |
| 19 | + |
| 20 | + github_id = models.BigIntegerField(unique=True, verbose_name="Github ID") |
| 21 | + created_at = models.DateTimeField(verbose_name="Created at", null=True, blank=True) |
| 22 | + updated_at = models.DateTimeField( |
| 23 | + verbose_name="Updated at", null=True, blank=True, db_index=True |
| 24 | + ) |
| 25 | + author = models.ForeignKey( |
| 26 | + "github.User", on_delete=models.SET_NULL, null=True, related_name="comments" |
| 27 | + ) |
| 28 | + body = models.TextField(verbose_name="Body") |
| 29 | + |
| 30 | + content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) |
| 31 | + object_id = models.PositiveIntegerField() |
| 32 | + content_object = GenericForeignKey("content_type", "object_id") |
| 33 | + |
| 34 | + def __str__(self): |
| 35 | + """Return a string representation of the comment.""" |
| 36 | + return f"{self.author} - {truncate(self.body, 50)}" |
| 37 | + |
| 38 | + def from_github(self, gh_comment, author=None): |
| 39 | + """Populate fields from a GitHub API comment object.""" |
| 40 | + field_mapping = { |
| 41 | + "body": "body", |
| 42 | + "created_at": "created_at", |
| 43 | + "updated_at": "updated_at", |
| 44 | + } |
| 45 | + |
| 46 | + for model_field, gh_field in field_mapping.items(): |
| 47 | + value = getattr(gh_comment, gh_field, None) |
| 48 | + if value is not None: |
| 49 | + setattr(self, model_field, value) |
| 50 | + |
| 51 | + self.author = author |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def bulk_save(comments, fields=None): # type: ignore[override] |
| 55 | + """Bulk save comments.""" |
| 56 | + BulkSaveModel.bulk_save(Comment, comments, fields=fields) |
| 57 | + |
| 58 | + @staticmethod |
| 59 | + def update_data(gh_comment, *, author=None, content_object=None, save: bool = True): |
| 60 | + """Update or create a Comment instance from a GitHub comment object. |
| 61 | +
|
| 62 | + Args: |
| 63 | + gh_comment (github.IssueComment.IssueComment): GitHub comment object. |
| 64 | + author (User, optional): Comment author. Defaults to None. |
| 65 | + content_object (GenericForeignKey, optional): Content object. Defaults to None. |
| 66 | + save (bool, optional): Whether to save the instance immediately. Defaults to True. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + Comment: The updated or newly created Comment instance. |
| 70 | +
|
| 71 | + """ |
| 72 | + try: |
| 73 | + comment = Comment.objects.get(github_id=gh_comment.id) |
| 74 | + except Comment.DoesNotExist: |
| 75 | + comment = Comment(github_id=gh_comment.id) |
| 76 | + |
| 77 | + comment.from_github(gh_comment, author=author) |
| 78 | + |
| 79 | + if content_object is not None: |
| 80 | + comment.content_object = content_object |
| 81 | + |
| 82 | + if save: |
| 83 | + comment.save() |
| 84 | + |
| 85 | + return comment |
0 commit comments