# HG changeset patch # User Søren Løvborg # Date 2016-10-10 23:15:05 # Node ID 533ec10dfd59a032a5240f93868a8e889fba4611 # Parent 02d6a5b63331a078787fe7c2d9b93a27949da309 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. diff --git a/kallithea/controllers/pullrequests.py b/kallithea/controllers/pullrequests.py --- a/kallithea/controllers/pullrequests.py +++ b/kallithea/controllers/pullrequests.py @@ -381,10 +381,10 @@ class PullrequestsController(BaseRepoCon 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') @@ -483,12 +483,10 @@ class PullrequestsController(BaseRepoCon 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() diff --git a/kallithea/model/pull_request.py b/kallithea/model/pull_request.py --- a/kallithea/model/pull_request.py +++ b/kallithea/model/pull_request.py @@ -59,48 +59,43 @@ class PullRequestModel(BaseModel): 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.