[PATCH 13 of 14] controllers: forward pullrequests.comment to changeset

Thomas De Schampheleire patrickdepinguin at gmail.com
Tue Nov 20 20:32:24 UTC 2018


# HG changeset patch
# User Thomas De Schampheleire <thomas.de_schampheleire at nokia.com>
# Date 1542403232 -3600
#      Fri Nov 16 22:20:32 2018 +0100
# Node ID 0cb01b80b3ae90daaa3e72870239e27d97758ef9
# Parent  4e6bfd3650ddfaf9d2bfbb166eaaec15f5194135
controllers: forward pullrequests.comment to changeset

Similar to the way delete_comment is handled.

diff --git a/kallithea/controllers/changeset.py b/kallithea/controllers/changeset.py
--- a/kallithea/controllers/changeset.py
+++ b/kallithea/controllers/changeset.py
@@ -187,6 +187,91 @@ def create_comment(text, status, f_path,
 
     return comment
 
+def create_cs_pr_comment(repo_name, revision=None, pull_request=None, allowed_to_change_status=True):
+    assert request.environ.get('HTTP_X_PARTIAL_XHR')
+    if pull_request:
+        pull_request_id = pull_request.pull_request_id
+    else:
+        pull_request_id = None
+
+    status = request.POST.get('changeset_status')
+    close_pr = request.POST.get('save_close')
+    delete = request.POST.get('save_delete')
+    f_path = request.POST.get('f_path')
+    line_no = request.POST.get('line')
+
+    if (status or close_pr or delete) and (f_path or line_no):
+        # status votes and closing is only possible in general comments
+        raise HTTPBadRequest()
+
+    if not allowed_to_change_status:
+        if status or close_pr:
+            h.flash(_('No permission to change status'), 'error')
+            raise HTTPForbidden()
+
+    if pull_request and delete == "delete":
+        if (pull_request.owner_id == request.authuser.user_id or
+            h.HasPermissionAny('hg.admin')() or
+            h.HasRepoPermissionLevel('admin')(pull_request.org_repo.repo_name) or
+            h.HasRepoPermissionLevel('admin')(pull_request.other_repo.repo_name)
+            ) and not pull_request.is_closed():
+            PullRequestModel().delete(pull_request)
+            Session().commit()
+            h.flash(_('Successfully deleted pull request %s') % pull_request_id,
+                    category='success')
+            return {
+               'location': url('my_pullrequests'), # or repo pr list?
+            }
+            raise HTTPFound(location=url('my_pullrequests')) # or repo pr list?
+        raise HTTPForbidden()
+
+    text = request.POST.get('text', '').strip()
+
+    comment = create_comment(
+        text,
+        status,
+        revision=revision,
+        pull_request_id=pull_request_id,
+        f_path=f_path,
+        line_no=line_no,
+        closing_pr=close_pr,
+    )
+
+    if status:
+        ChangesetStatusModel().set_status(
+            c.db_repo.repo_id,
+            status,
+            request.authuser.user_id,
+            comment,
+            revision=revision,
+            pull_request=pull_request_id,
+        )
+
+    if pull_request:
+        action = 'user_closed_pull_request:%s' % pull_request_id
+    else:
+        action = 'user_commented_revision:%s' % revision
+    action_logger(request.authuser, action, c.db_repo, request.ip_addr)
+
+    if pull_request and close_pr:
+        PullRequestModel().close_pull_request(pull_request_id)
+        action_logger(request.authuser,
+                      'user_closed_pull_request:%s' % pull_request_id,
+                      c.db_repo, request.ip_addr)
+
+    Session().commit()
+
+    data = {
+       'target_id': h.safeid(h.safe_unicode(request.POST.get('f_path'))),
+    }
+    if comment is not None:
+        c.comment = comment
+        data.update(comment.get_dict())
+        data.update({'rendered_text':
+                     render('changeset/changeset_comment_block.html')})
+
+    return data
+
 def delete_cs_pr_comment(repo_name, comment_id, pr_comment=False):
     co = ChangesetComment.get_or_404(comment_id)
     if co.repo.repo_name != repo_name:
@@ -365,91 +450,8 @@ class ChangesetController(BaseRepoContro
     @LoginRequired()
     @HasRepoPermissionLevelDecorator('read')
     @jsonify
-    def comment(self, repo_name, revision, pull_request_id=None, allowed_to_change_status=True):
-        assert request.environ.get('HTTP_X_PARTIAL_XHR')
-
-        if pull_request_id is not None:
-            pull_request = PullRequest.get_or_404(pull_request_id)
-        else:
-            pull_request = None
-
-        status = request.POST.get('changeset_status')
-        close_pr = request.POST.get('save_close')
-        delete = request.POST.get('save_delete')
-        f_path = request.POST.get('f_path')
-        line_no = request.POST.get('line')
-
-        if (status or close_pr or delete) and (f_path or line_no):
-            # status votes and closing is only possible in general comments
-            raise HTTPBadRequest()
-
-        if not allowed_to_change_status:
-            if status or close_pr:
-                h.flash(_('No permission to change status'), 'error')
-                raise HTTPForbidden()
-
-        if pull_request and delete == "delete":
-            if (pull_request.owner_id == request.authuser.user_id or
-                h.HasPermissionAny('hg.admin')() or
-                h.HasRepoPermissionLevel('admin')(pull_request.org_repo.repo_name) or
-                h.HasRepoPermissionLevel('admin')(pull_request.other_repo.repo_name)
-                ) and not pull_request.is_closed():
-                PullRequestModel().delete(pull_request)
-                Session().commit()
-                h.flash(_('Successfully deleted pull request %s') % pull_request_id,
-                        category='success')
-                return {
-                   'location': url('my_pullrequests'), # or repo pr list?
-                }
-                raise HTTPFound(location=url('my_pullrequests')) # or repo pr list?
-            raise HTTPForbidden()
-
-        text = request.POST.get('text', '').strip()
-
-        comment = create_comment(
-            text,
-            status,
-            revision=revision,
-            pull_request_id=pull_request_id,
-            f_path=f_path,
-            line_no=line_no,
-            closing_pr=close_pr,
-        )
-
-        if status:
-            ChangesetStatusModel().set_status(
-                c.db_repo.repo_id,
-                status,
-                request.authuser.user_id,
-                comment,
-                revision=revision,
-                pull_request=pull_request_id,
-            )
-
-        if pull_request:
-            action = 'user_closed_pull_request:%s' % pull_request_id
-        else:
-            action = 'user_commented_revision:%s' % revision
-        action_logger(request.authuser, action, c.db_repo, request.ip_addr)
-
-        if pull_request and close_pr:
-            PullRequestModel().close_pull_request(pull_request_id)
-            action_logger(request.authuser,
-                          'user_closed_pull_request:%s' % pull_request_id,
-                          c.db_repo, request.ip_addr)
-
-        Session().commit()
-
-        data = {
-           'target_id': h.safeid(h.safe_unicode(request.POST.get('f_path'))),
-        }
-        if comment is not None:
-            c.comment = comment
-            data.update(comment.get_dict())
-            data.update({'rendered_text':
-                         render('changeset/changeset_comment_block.html')})
-
-        return data
+    def comment(self, repo_name, revision):
+        return create_cs_pr_comment(repo_name, revision=revision)
 
     @LoginRequired()
     @HasRepoPermissionLevelDecorator('read')
diff --git a/kallithea/controllers/pullrequests.py b/kallithea/controllers/pullrequests.py
--- a/kallithea/controllers/pullrequests.py
+++ b/kallithea/controllers/pullrequests.py
@@ -53,7 +53,7 @@ from kallithea.model.changeset_status im
 from kallithea.model.forms import PullRequestForm, PullRequestPostForm
 from kallithea.lib.utils2 import safe_int
 from kallithea.controllers.changeset import _ignorews_url, _context_url, \
-    create_comment, delete_cs_pr_comment
+    create_cs_pr_comment, delete_cs_pr_comment
 from kallithea.controllers.compare import CompareController
 from kallithea.lib.graphmod import graph_data
 
@@ -632,91 +632,10 @@ class PullrequestsController(BaseRepoCon
     @HasRepoPermissionLevelDecorator('read')
     @jsonify
     def comment(self, repo_name, pull_request_id, revision=None):
-        assert request.environ.get('HTTP_X_PARTIAL_XHR')
-
-        if pull_request_id is not None:
-            pull_request = PullRequest.get_or_404(pull_request_id)
-        else:
-            pull_request = None
-
-        status = request.POST.get('changeset_status')
-        close_pr = request.POST.get('save_close')
-        delete = request.POST.get('save_delete')
-        f_path = request.POST.get('f_path')
-        line_no = request.POST.get('line')
-
-        if (status or close_pr or delete) and (f_path or line_no):
-            # status votes and closing is only possible in general comments
-            raise HTTPBadRequest()
-
+        pull_request = PullRequest.get_or_404(pull_request_id)
         allowed_to_change_status = self._is_allowed_to_change_status(pull_request)
-        if not allowed_to_change_status:
-            if status or close_pr:
-                h.flash(_('No permission to change status'), 'error')
-                raise HTTPForbidden()
-
-        if pull_request and delete == "delete":
-            if (pull_request.owner_id == request.authuser.user_id or
-                h.HasPermissionAny('hg.admin')() or
-                h.HasRepoPermissionLevel('admin')(pull_request.org_repo.repo_name) or
-                h.HasRepoPermissionLevel('admin')(pull_request.other_repo.repo_name)
-                ) and not pull_request.is_closed():
-                PullRequestModel().delete(pull_request)
-                Session().commit()
-                h.flash(_('Successfully deleted pull request %s') % pull_request_id,
-                        category='success')
-                return {
-                   'location': url('my_pullrequests'), # or repo pr list?
-                }
-                raise HTTPFound(location=url('my_pullrequests')) # or repo pr list?
-            raise HTTPForbidden()
-
-        text = request.POST.get('text', '').strip()
-
-        comment = create_comment(
-            text,
-            status,
-            revision=revision,
-            pull_request_id=pull_request_id,
-            f_path=f_path,
-            line_no=line_no,
-            closing_pr=close_pr,
-        )
-
-        if status:
-            ChangesetStatusModel().set_status(
-                c.db_repo.repo_id,
-                status,
-                request.authuser.user_id,
-                comment,
-                revision=revision,
-                pull_request=pull_request_id,
-            )
-
-        if pull_request:
-            action = 'user_closed_pull_request:%s' % pull_request_id
-        else:
-            action = 'user_commented_revision:%s' % revision
-        action_logger(request.authuser, action, c.db_repo, request.ip_addr)
-
-        if pull_request and close_pr:
-            PullRequestModel().close_pull_request(pull_request_id)
-            action_logger(request.authuser,
-                          'user_closed_pull_request:%s' % pull_request_id,
-                          c.db_repo, request.ip_addr)
-
-        Session().commit()
-
-        data = {
-           'target_id': h.safeid(h.safe_unicode(request.POST.get('f_path'))),
-        }
-        if comment is not None:
-            c.comment = comment
-            data.update(comment.get_dict())
-            data.update({'rendered_text':
-                         render('changeset/changeset_comment_block.html')})
-
-        return data
+        return create_cs_pr_comment(repo_name, pull_request=pull_request,
+                allowed_to_change_status=allowed_to_change_status)
 
     @LoginRequired()
     @HasRepoPermissionLevelDecorator('read')


More information about the kallithea-general mailing list