Changeset - 533ec10dfd59
[Not reviewed]
default
0 2 0
Søren Løvborg - 9 years ago 2016-10-10 23:15:05
sorenl@unity3d.com
pullrequests: refactor PullRequestModel().create

The database lookups of the user and repositories are moved outside,
into the callers, which have already looked up the relevant objects,
thus saving two database lookups when creating a PR.
2 files changed with 26 insertions and 33 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/pullrequests.py
Show inline comments
 
@@ -378,16 +378,16 @@ class PullrequestsController(BaseRepoCon
 
                                      h.short_ref(other_ref_type, other_ref_name))
 
            else:
 
                title = '%s#%s to %s#%s' % (org_repo_name, h.short_ref(org_ref_type, org_ref_name),
 
                                            other_repo_name, h.short_ref(other_ref_type, other_ref_name))
 
        description = _form['pullrequest_desc'].strip() or _('No description')
 
        try:
 
            created_by = User.get(self.authuser.user_id)
 
            pull_request = PullRequestModel().create(
 
                self.authuser.user_id, org_repo_name, org_ref, other_repo_name,
 
                other_ref, revisions, reviewer_ids, title, description
 
            )
 
                created_by, org_repo, org_ref, other_repo, other_ref, revisions,
 
                title, description, reviewer_ids)
 
            Session().commit()
 
            h.flash(_('Successfully opened new pull request'),
 
                    category='success')
 
        except UserInvalidException as u:
 
            h.flash(_('Invalid reviewer "%s" specified') % u, category='error')
 
            raise HTTPBadRequest()
 
@@ -480,18 +480,16 @@ class PullrequestsController(BaseRepoCon
 
        descriptions = description.replace('\r\n', '\n').split('\n-- \n', 1)
 
        description = descriptions[0].strip() + '\n\n-- \n' + '\n'.join(infos)
 
        if len(descriptions) > 1:
 
            description += '\n\n' + descriptions[1].strip()
 

	
 
        try:
 
            created_by = User.get(self.authuser.user_id)
 
            pull_request = PullRequestModel().create(
 
                self.authuser.user_id,
 
                old_pull_request.org_repo.repo_name, new_org_ref,
 
                old_pull_request.other_repo.repo_name, new_other_ref,
 
                revisions, reviewer_ids, title, description
 
            )
 
                created_by, org_repo, new_org_ref, other_repo, new_other_ref, revisions,
 
                title, description, reviewer_ids)
 
        except UserInvalidException as u:
 
            h.flash(_('Invalid reviewer "%s" specified') % u, category='error')
 
            raise HTTPBadRequest()
 
        except Exception:
 
            h.flash(_('Error occurred while creating pull request'),
 
                    category='error')
kallithea/model/pull_request.py
Show inline comments
 
@@ -56,54 +56,49 @@ class PullRequestModel(BaseModel):
 
            user = self._get_user(user_spec)
 
            if user is None or user.username == User.DEFAULT_USER:
 
                raise UserInvalidException(user_spec)
 
            yield user
 

	
 
    def create(self, created_by, org_repo, org_ref, other_repo, other_ref,
 
               revisions, reviewers, title, description=None):
 
        from kallithea.model.changeset_status import ChangesetStatusModel
 

	
 
        created_by_user = self._get_user(created_by)
 
        org_repo = self._get_repo(org_repo)
 
        other_repo = self._get_repo(other_repo)
 

	
 
        new = PullRequest()
 
        new.org_repo = org_repo
 
        new.org_ref = org_ref
 
        new.other_repo = other_repo
 
        new.other_ref = other_ref
 
        new.revisions = revisions
 
        new.title = title
 
        new.description = description
 
        new.owner = created_by_user
 
        Session().add(new)
 
        Session().flush()
 
               revisions, title, description, reviewers):
 
        pr = PullRequest()
 
        pr.org_repo = org_repo
 
        pr.org_ref = org_ref
 
        pr.other_repo = other_repo
 
        pr.other_ref = other_ref
 
        pr.revisions = revisions
 
        pr.title = title
 
        pr.description = description
 
        pr.owner = created_by
 
        Session().add(pr)
 
        Session().flush() # make database assign pull_request_id
 

	
 
        #reset state to under-review
 
        from kallithea.model.changeset_status import ChangesetStatusModel
 
        from kallithea.model.comment import ChangesetCommentsModel
 
        comment = ChangesetCommentsModel().create(
 
            text=u'',
 
            repo=org_repo,
 
            author=new.owner,
 
            pull_request=new,
 
            author=created_by,
 
            pull_request=pr,
 
            send_email=False,
 
            status_change=ChangesetStatus.STATUS_UNDER_REVIEW,
 
        )
 
        ChangesetStatusModel().set_status(
 
            org_repo,
 
            ChangesetStatus.STATUS_UNDER_REVIEW,
 
            new.owner,
 
            created_by,
 
            comment,
 
            pull_request=new
 
            pull_request=pr,
 
        )
 

	
 
        reviewers = set(self._get_valid_reviewers(reviewers))
 
        mention_recipients = extract_mentioned_users(new.description)
 
        self.add_reviewers(created_by_user, new, reviewers, mention_recipients)
 
        mention_recipients = extract_mentioned_users(description)
 
        self.add_reviewers(created_by, pr, reviewers, mention_recipients)
 

	
 
        return new
 
        return pr
 

	
 
    def add_reviewers(self, user, pr, reviewers, mention_recipients=None):
 
        """Add reviewer and send notification to them.
 
        """
 
        reviewer_users = set(self._get_valid_reviewers(reviewers))
 
        #members
0 comments (0 inline, 0 general)