allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jetm...@apache.org
Subject [4/7] allura git commit: [#7866] ticket:754 Cache can_merge results
Date Mon, 20 Apr 2015 10:45:42 GMT
[#7866] ticket:754 Cache can_merge results


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/b6c4d6b2
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/b6c4d6b2
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/b6c4d6b2

Branch: refs/heads/ib/7866
Commit: b6c4d6b245d8eca7ba5cdf99996c11533291c4c5
Parents: 9e95883
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Sat Apr 18 11:34:27 2015 +0000
Committer: Igor Bondarenko <jetmind2@gmail.com>
Committed: Sat Apr 18 11:34:27 2015 +0000

----------------------------------------------------------------------
 Allura/allura/controllers/repository.py         |  5 ++++
 Allura/allura/model/repository.py               | 28 ++++++++++++++++----
 Allura/allura/tasks/repo_tasks.py               |  4 ++-
 Allura/allura/templates/repo/merge_request.html | 15 ++++++++---
 4 files changed, 42 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/b6c4d6b2/Allura/allura/controllers/repository.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/repository.py b/Allura/allura/controllers/repository.py
index 07a522b..ce38b2b 100644
--- a/Allura/allura/controllers/repository.py
+++ b/Allura/allura/controllers/repository.py
@@ -460,6 +460,11 @@ class MergeRequestController(object):
     def can_merge_task_status(self):
         return {'status': self.req.can_merge_task_status()}
 
+    @expose('json:')
+    def can_merge_result(self):
+        """Return result from the cache. Used by js, after task was completed."""
+        return {'can_merge': self.req.can_merge()}
+
 
 class RefsController(object):
 

http://git-wip-us.apache.org/repos/asf/allura/blob/b6c4d6b2/Allura/allura/model/repository.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/repository.py b/Allura/allura/model/repository.py
index 35d2870..a9b07a0 100644
--- a/Allura/allura/model/repository.py
+++ b/Allura/allura/model/repository.py
@@ -737,6 +737,7 @@ class MergeRequest(VersionedArtifact, ActivityObject):
     created = FieldProperty(datetime, if_missing=datetime.utcnow)
     summary = FieldProperty(str)
     description = FieldProperty(str)
+    can_merge_cache = FieldProperty({str: bool})
 
     @property
     def activity_name(self):
@@ -838,17 +839,34 @@ class MergeRequest(VersionedArtifact, ActivityObject):
             return False
         return True
 
-    def can_merge_cache(self, source_hash, target_hash):
+    def can_merge_cache_key(self):
         """
-        Returns True/False or None in case of cache miss.
+        Returns key for can_merge_cache constructed from current
+        source & target branch commits.
         """
-        return None
+        source_hash = self.downstream.commit_id
+        target_hash = self.app.repo.commit(self.target_branch)._id
+        key = '{}-{}'.format(source_hash, target_hash)
+        return key
+
+    def get_can_merge_cache(self):
+        """Returns True/False or None in case of cache miss."""
+        key = self.can_merge_cache_key()
+        return self.can_merge_cache.get(key)
+
+    def set_can_merge_cache(self, val):
+        key = self.can_merge_cache_key()
+        self.can_merge_cache[key] = val
 
     def can_merge(self):
         """
-        Returns true if you can merge cleanly (no conflicts)
+        Returns boolean indicating if automatic merge is possible (no
+        conflicts). If result is unknown yet, returns None and fires a task to
+        get the result. Caches result for later reuse.
         """
-        cached = self.can_merge_cache(None, None)
+        if self.status == 'merged':
+            return True
+        cached = self.get_can_merge_cache()
         if cached is not None:
             return cached
         in_progress = self.can_merge_task_status() in ['ready', 'busy']

http://git-wip-us.apache.org/repos/asf/allura/blob/b6c4d6b2/Allura/allura/tasks/repo_tasks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tasks/repo_tasks.py b/Allura/allura/tasks/repo_tasks.py
index 8191b54..614b4b0 100644
--- a/Allura/allura/tasks/repo_tasks.py
+++ b/Allura/allura/tasks/repo_tasks.py
@@ -169,4 +169,6 @@ def can_merge(merge_request_id):
     from allura import model as M
     mr = M.MergeRequest.query.get(_id=merge_request_id)
     result = mr.app.repo.can_merge(mr)
-    # TODO: set cache (or inside repo's can_merge?)
+    source_hash = mr.downstream.commit_id
+    target_hash = mr.app.repo.commit(mr.target_branch)._id
+    mr.set_can_merge_cache(result)

http://git-wip-us.apache.org/repos/asf/allura/blob/b6c4d6b2/Allura/allura/templates/repo/merge_request.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/repo/merge_request.html b/Allura/allura/templates/repo/merge_request.html
index 37830b8..f87a894 100644
--- a/Allura/allura/templates/repo/merge_request.html
+++ b/Allura/allura/templates/repo/merge_request.html
@@ -201,12 +201,19 @@ $(function() {
     {% elif can_merge_status in ('ready', 'busy') %}
       var spinner = '#can_merge_task_status > .spinner';
       var url = "{{request.path.rstrip('/') + '/can_merge_task_status'}}";
+      var can_merge_result_url = "{{request.path.rstrip('/') + '/can_merge_result'}}";
       function on_complete() {
         $('#can_merge_task_status h2').hide();
-        // TODO: check if actually can merge and show appropriate message
-        $('.merge-help-text').hide();
-        $('.merge-ok').show();
-        $('#merge-btn').prop('disabled', false);
+        $.get(can_merge_result_url, function(data) {
+          $('.merge-help-text').hide();
+          if (data.can_merge) {
+            $('.merge-ok').show();
+            $('#merge-btn').prop('disabled', false);
+          } else {
+            $('.merge-conflicts').show();
+            $('#merge-btn').prop('disabled', true);
+          }
+        });
       }
       function on_progress() {
         $('#can_merge_task_status h2').hide();


Mime
View raw message