changeset 8982:a136383459ef stable

api: add possibility to optionally return comments from get_changeset()
author Manuel Jacob <me@manueljacob.de>
date Wed, 29 Mar 2023 11:59:07 +0200
parents ea1a656702ab
children 6cb54b9ef4de
files kallithea/controllers/api/api.py kallithea/tests/api/api_base.py kallithea/tests/fixture.py
diffstat 3 files changed, 61 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/controllers/api/api.py	Wed Mar 29 11:05:00 2023 +0200
+++ b/kallithea/controllers/api/api.py	Wed Mar 29 11:59:07 2023 +0200
@@ -1847,7 +1847,7 @@
             raise JSONRPCError('Repository is empty')
 
     # permission check inside
-    def get_changeset(self, repoid, raw_id, with_reviews=False):
+    def get_changeset(self, repoid, raw_id, with_reviews=False, with_comments=False, with_inline_comments=False):
         """
         TODO
         """
@@ -1865,6 +1865,16 @@
                                 repo.repo_name, changeset.raw_id)
             info["reviews"] = reviews
 
+        if with_comments:
+            comments = ChangesetCommentsModel().get_comments(
+                                repo.repo_id, changeset.raw_id)
+            info["comments"] = comments
+
+        if with_inline_comments:
+            inline_comments = ChangesetCommentsModel().get_inline_comments(
+                                repo.repo_id, changeset.raw_id)
+            info["inline_comments"] = inline_comments
+
         return info
 
     # permission check inside
--- a/kallithea/tests/api/api_base.py	Wed Mar 29 11:05:00 2023 +0200
+++ b/kallithea/tests/api/api_base.py	Wed Mar 29 11:59:07 2023 +0200
@@ -2478,6 +2478,8 @@
         result = ext_json.loads(response.body)["result"]
         assert result["raw_id"] == self.TEST_REVISION
         assert "reviews" not in result
+        assert "comments" not in result
+        assert "inline_comments" not in result
 
     def test_api_get_changeset_with_reviews(self):
         reviewobjs = fixture.review_changeset(self.REPO, self.TEST_REVISION, "approved")
@@ -2488,6 +2490,8 @@
         result = ext_json.loads(response.body)["result"]
         assert result["raw_id"] == self.TEST_REVISION
         assert "reviews" in result
+        assert "comments" not in result
+        assert "inline_comments" not in result
         assert len(result["reviews"]) == 1
         review = result["reviews"][0]
         expected = {
@@ -2497,6 +2501,47 @@
         }
         assert review == expected
 
+    def test_api_get_changeset_with_comments(self):
+        commentobj = fixture.add_changeset_comment(self.REPO, self.TEST_REVISION, "example changeset comment")
+        id_, params = _build_data(self.apikey, 'get_changeset',
+                                  repoid=self.REPO, raw_id=self.TEST_REVISION,
+                                  with_comments=True)
+        response = api_call(self, params)
+        result = ext_json.loads(response.body)["result"]
+        assert result["raw_id"] == self.TEST_REVISION
+        assert "reviews" not in result
+        assert "comments" in result
+        assert "inline_comments" not in result
+        comment = result["comments"][-1]
+        expected = {
+            'comment_id': commentobj.comment_id,
+            'text': 'example changeset comment',
+            'username': 'test_admin',
+        }
+        assert comment == expected
+
+    def test_api_get_changeset_with_inline_comments(self):
+        commentobj = fixture.add_changeset_comment(self.REPO, self.TEST_REVISION, "example inline comment", f_path='vcs/__init__.py', line_no="n3")
+        id_, params = _build_data(self.apikey, 'get_changeset',
+                                  repoid=self.REPO, raw_id=self.TEST_REVISION,
+                                  with_inline_comments=True)
+        response = api_call(self, params)
+        result = ext_json.loads(response.body)["result"]
+        assert result["raw_id"] == self.TEST_REVISION
+        assert "reviews" not in result
+        assert "comments" not in result
+        assert "inline_comments" in result
+        expected = [
+            ['vcs/__init__.py', {
+                'n3': [{
+                    'comment_id': commentobj.comment_id,
+                    'text': 'example inline comment',
+                    'username': 'test_admin',
+                }]
+            }]
+        ]
+        assert result["inline_comments"] == expected
+
     def test_api_get_changeset_that_does_not_exist(self):
         """ Fetch changeset status for non-existant changeset.
         revision id is the above git hash used in the test above with the
--- a/kallithea/tests/fixture.py	Wed Mar 29 11:05:00 2023 +0200
+++ b/kallithea/tests/fixture.py	Wed Mar 29 11:59:07 2023 +0200
@@ -329,6 +329,11 @@
         meta.Session().commit()
         return csm
 
+    def add_changeset_comment(self, repo, revision, text, author=TEST_USER_ADMIN_LOGIN, f_path=None, line_no=None):
+        comment = ChangesetCommentsModel().create(text, repo, author, revision=revision, f_path=f_path, line_no=line_no, send_email=False)
+        meta.Session().commit()
+        return comment
+
     def create_pullrequest(self, testcontroller, repo_name, pr_src_rev, pr_dst_rev, title='title'):
         org_ref = 'branch:stable:%s' % pr_src_rev
         other_ref = 'branch:default:%s' % pr_dst_rev