Example #1
0
    def save(self, *args, **kwargs):
        """
        Check for the related Commit object if the event is a reference to a
        commit. If not found, a job is created to search it later
        """
        from .commits import Commit

        needs_comit = False

        if self.commit_sha and not self.related_object_id:

            try:
                self.related_object = Commit.objects.filter(
                    authored_at__lte=self.created_at,
                    sha=self.commit_sha,
                    author=self.user
                ).order_by('-authored_at')[0]
            except IndexError:
                needs_comit = True

        super(IssueEvent, self).save(*args, **kwargs)

        if needs_comit:
            from gim.core.tasks.commit import FetchCommitBySha
            FetchCommitBySha.add_job('%s#%s' % (self.repository_id, self.commit_sha))
            from gim.core.tasks.event import SearchReferenceCommitForEvent
            SearchReferenceCommitForEvent.add_job(self.id, delayed_for=30)
    def save(self, *args, **kwargs):
        """
        Try to get the commit if not set, using the sha, or ask for it to be
        fetched from github
        If it's a creation, update the comments_count of the commit
        If it's an update, update the starting point of the entry-point
        """
        is_new = not bool(self.pk)

        if not self.commit_id:
            self.commit, _ = self.repository.commits.get_or_create(
                sha=self.sha,
            )
            from gim.core.tasks.commit import FetchCommitBySha
            FetchCommitBySha.add_job('%s#%s' % (self.repository_id, self.sha))

        elif not self.commit_sha:
            self.commit_sha = self.commit.sha

        super(CommitComment, self).save(*args, **kwargs)

        if is_new:
            self.commit.update_comments_count()
        else:
            self.entry_point.update_starting_point(save=True)
    def save(self, *args, **kwargs):
        """
        Try to get the commit if not set, using the sha, or ask for it to be
        fetched from github
        """
        if not self.commit_id:
            self.commit, _ = self.repository.commits.get_or_create(
                sha=self.commit_sha,
            )
            from gim.core.tasks.commit import FetchCommitBySha
            FetchCommitBySha.add_job('%s#%s' % (self.repository_id, self.commit_sha))

        super(CommitCommentEntryPoint, self).save(*args, **kwargs)
Example #4
0
 def after_run(self, gh, obj):
     from gim.core.tasks.commit import FetchCommitBySha
     FetchCommitBySha.add_job('%s#%s' % (obj.repository_id, obj.commit.sha), fetch_comments=1, gh=gh)
    def find_commits(self, jobs_priority=0):
        """
        Check all references to commits in the comment, and link them via the
        `linked_commits` m2m field.
        """
        from .commits import Commit
        from .repositories import Repository

        new_commits = self.RE_COMMITS.findall(self.body_html) if self.body_html and self.body_html.strip() else []
        existing_commits = self.linked_commits.all().select_related('repository__owner')
        existing_commits_dict = {(c.repository.owner.username, c.repository.name, c.sha): c for c in existing_commits}

        # remove removed commits
        to_remove = []
        for e_tuple, e_commit in existing_commits_dict.items():
            found = False
            for new in new_commits:
                if (e_tuple[0], e_tuple[1], e_tuple[2][:len(new[2])]) == new:
                    found = True
                    break
            if not found:
                to_remove.append(e_commit)

        if to_remove:
            self.linked_commits.remove(*to_remove)

        # add new commits if we have them
        to_add, not_found = [], []
        for new in new_commits:

            # check if this new one is already in existing ones
            found = False
            for e_tuple in existing_commits_dict.keys():
                if new == (e_tuple[0], e_tuple[1], e_tuple[2][:len(new[2])]):
                    found = True
                    break
            if found:
                continue

            # try to find the commit to add
            try:
                to_add.append(Commit.objects.get(
                    repository__owner__username=new[0],
                    repository__name=new[1],
                    sha__startswith=new[2]
                ))
            except Commit.DoesNotExist:
                not_found.append(new)

        if to_add:
            self.linked_commits.add(*to_add)

        if not_found:
            from gim.core.tasks.commit import FetchCommitBySha
            if isinstance(self, IssueComment):
                from gim.core.tasks.comment import SearchReferenceCommitForComment as JobModel
            elif isinstance(self, PullRequestComment):
                from gim.core.tasks.comment import SearchReferenceCommitForPRComment as JobModel
            else:
                from gim.core.tasks.comment import SearchReferenceCommitForCommitComment as JobModel

            repos = {}
            for new in not_found:
                repo_tuple = (new[0], new[1])
                if (repo_tuple) not in repos:
                    try:
                        repos[repo_tuple] = Repository.objects.get(
                            owner__username=new[0],
                            name=new[1]
                        )
                    except Repository.DoesNotExist:
                        continue

                FetchCommitBySha.add_job('%s#%s' % (repos[repo_tuple].id, new[2]),
                                                    priority=jobs_priority)
                JobModel.add_job(self.id, delayed_for=30,
                                 repository_id=repos[repo_tuple].id, commit_sha=new[2],
                                 priority=jobs_priority)