changeset 6323:533ec10dfd59

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.
author Søren Løvborg <sorenl@unity3d.com>
date Mon, 10 Oct 2016 23:15:05 +0200
parents 02d6a5b63331
children 97619528c270
files kallithea/controllers/pullrequests.py kallithea/model/pull_request.py
diffstat 2 files changed, 26 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/pullrequests.py	Thu Oct 27 18:35:13 2016 +0200
+++ b/kallithea/controllers/pullrequests.py	Mon Oct 10 23:15:05 2016 +0200
@@ -381,10 +381,10 @@
                                             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 @@
             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()
--- a/kallithea/model/pull_request.py	Thu Oct 27 18:35:13 2016 +0200
+++ b/kallithea/model/pull_request.py	Mon Oct 10 23:15:05 2016 +0200
@@ -59,48 +59,43 @@
             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.