changeset 6683:19bc05bd8cf7

api: add get_changesets
author domruf <dominikruf@gmail.com>
date Tue, 06 Jun 2017 19:40:18 +0200
parents d18dfba3c245
children 911652ac162e
files docs/api/api.rst kallithea/controllers/api/api.py kallithea/lib/vcs/backends/base.py kallithea/tests/api/api_base.py
diffstat 4 files changed, 104 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/docs/api/api.rst	Tue Jun 13 22:55:58 2017 +0200
+++ b/docs/api/api.rst	Tue Jun 06 19:40:18 2017 +0200
@@ -1071,6 +1071,48 @@
             }
     error:  null
 
+get_changesets
+^^^^^^^^^^^^^^
+
+Get changesets of a given repository. This command can only be executed using the api_key
+of a user with read permissions to the repository.
+
+INPUT::
+
+    id : <id_for_response>
+    api_key : "<api_key>"
+    method  : "get_changesets"
+    args:     {
+                "repoid" : "<reponame or repo_id>",
+                "start": "<revision number> = Optional(None)",
+                "end": "<revision number> = Optional(None)",
+                "start_date": "<date> = Optional(None)",    # in "%Y-%m-%dT%H:%M:%S" format
+                "end_date": "<date> = Optional(None)",      # in "%Y-%m-%dT%H:%M:%S" format
+                "branch_name": "<branch name filter> = Optional(None)",
+                "reverse": "<bool> = Optional(False)",
+                "with_file_list": "<bool> = Optional(False)"
+              }
+
+OUTPUT::
+
+    id : <id_given_in_input>
+    result: [
+    {
+      "raw_id": "<raw_id>",
+      "short_id": "short_id": "<short_id>",
+      "author": "<full_author>",
+      "date": "<date_time_of_commit>",
+      "message": "<commit_message>",
+      "revision": "<numeric_revision>",
+      <if with_file_list == True>
+      "added": [<list of added files>],
+      "changed": [<list of changed files>],
+      "removed": [<list of removed files>]
+    },
+    ...
+    ]
+    error:  null
+
 get_changeset
 ^^^^^^^^^^^^^
 
--- a/kallithea/controllers/api/api.py	Tue Jun 13 22:55:58 2017 +0200
+++ b/kallithea/controllers/api/api.py	Tue Jun 06 19:40:18 2017 +0200
@@ -28,6 +28,8 @@
 import time
 import traceback
 import logging
+
+from datetime import datetime
 from sqlalchemy import or_
 
 from tg import request
@@ -56,7 +58,7 @@
 from kallithea.lib.compat import json
 from kallithea.lib.exceptions import (
     DefaultUserException, UserGroupsAssignedException)
-from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError
+from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError, EmptyRepositoryError
 from kallithea.lib.vcs.backends.base import EmptyChangeset
 from kallithea.lib.utils import action_logger
 
@@ -2492,6 +2494,25 @@
                                % (gist.gist_access_id,))
 
     # permission check inside
+    def get_changesets(self, repoid, start=None, end=None, start_date=None,
+                       end_date=None, branch_name=None, reverse=False, with_file_list=False):
+        repo = get_repo_or_error(repoid)
+        if not HasRepoPermissionLevel('read')(repo.repo_name):
+            raise JSONRPCError('Access denied to repo %s' % repo.repo_name)
+
+        format = "%Y-%m-%dT%H:%M:%S"
+        try:
+            return [e.__json__(with_file_list) for e in
+                repo.scm_instance.get_changesets(start,
+                                                 end,
+                                                 datetime.strptime(start_date, format) if start_date else None,
+                                                 datetime.strptime(end_date, format) if end_date else None,
+                                                 branch_name,
+                                                 reverse)]
+        except EmptyRepositoryError as e:
+            raise JSONRPCError(e.message)
+
+    # permission check inside
     def get_changeset(self, repoid, raw_id, with_reviews=Optional(False)):
         repo = get_repo_or_error(repoid)
         if not HasRepoPermissionLevel('read')(repo.repo_name):
--- a/kallithea/lib/vcs/backends/base.py	Tue Jun 13 22:55:58 2017 +0200
+++ b/kallithea/lib/vcs/backends/base.py	Tue Jun 06 19:40:18 2017 +0200
@@ -384,15 +384,28 @@
     def __eq__(self, other):
         return self.raw_id == other.raw_id
 
-    def __json__(self):
-        return dict(
-            short_id=self.short_id,
-            raw_id=self.raw_id,
-            revision=self.revision,
-            message=self.message,
-            date=self.date,
-            author=self.author,
-        )
+    def __json__(self, with_file_list=False):
+        if with_file_list:
+            return dict(
+                short_id=self.short_id,
+                raw_id=self.raw_id,
+                revision=self.revision,
+                message=self.message,
+                date=self.date,
+                author=self.author,
+                added=[el.path for el in self.added],
+                changed=[el.path for el in self.changed],
+                removed=[el.path for el in self.removed],
+            )
+        else:
+            return dict(
+                short_id=self.short_id,
+                raw_id=self.raw_id,
+                revision=self.revision,
+                message=self.message,
+                date=self.date,
+                author=self.author,
+            )
 
     @LazyProperty
     def last(self):
--- a/kallithea/tests/api/api_base.py	Tue Jun 13 22:55:58 2017 +0200
+++ b/kallithea/tests/api/api_base.py	Tue Jun 06 19:40:18 2017 +0200
@@ -2461,6 +2461,24 @@
         expected = Setting.get_server_info()
         self._compare_ok(id_, expected, given=response.body)
 
+    def test_api_get_changesets(self):
+        id_, params = _build_data(self.apikey, 'get_changesets',
+                                  repoid=self.REPO, start=0, end=2)
+        response = api_call(self, params)
+        result = json.loads(response.body)["result"]
+        assert len(result) == 3
+        assert result[0].has_key('message')
+        assert not result[0].has_key('added')
+
+    def test_api_get_changesets_with_file_list(self):
+        id_, params = _build_data(self.apikey, 'get_changesets',
+                                  repoid=self.REPO, start_date="2010-04-07T23:30:30", end_date="2010-04-08T00:31:14", with_file_list=True)
+        response = api_call(self, params)
+        result = json.loads(response.body)["result"]
+        assert len(result) == 3
+        assert result[0].has_key('message')
+        assert result[0].has_key('added')
+
     def test_api_get_changeset(self):
         review = fixture.review_changeset(self.REPO, self.TEST_REVISION, "approved")
         id_, params = _build_data(self.apikey, 'get_changeset',