You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by je...@apache.org on 2015/02/16 12:45:08 UTC

[37/37] allura git commit: [#4542] ticket:728 Add more test + refactoring

[#4542] ticket:728 Add more test + refactoring


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

Branch: refs/heads/ib/4542
Commit: b967bc5cc4de9d98cd94fba8dfe86986559b08d9
Parents: a433fa9
Author: Igor Bondarenko <je...@gmail.com>
Authored: Mon Feb 16 11:03:17 2015 +0000
Committer: Igor Bondarenko <je...@gmail.com>
Committed: Mon Feb 16 11:03:17 2015 +0000

----------------------------------------------------------------------
 Allura/allura/model/repo_refresh.py   | 50 +++++++++++++++++-------------
 Allura/allura/tests/test_webhooks.py  | 30 ++++++++++++++++++
 Allura/allura/tests/unit/test_repo.py | 42 ++++++++++++++++++++++++-
 Allura/allura/webhooks.py             |  4 +--
 4 files changed, 101 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/b967bc5c/Allura/allura/model/repo_refresh.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/repo_refresh.py b/Allura/allura/model/repo_refresh.py
index 20bd365..d8ef050 100644
--- a/Allura/allura/model/repo_refresh.py
+++ b/Allura/allura/model/repo_refresh.py
@@ -134,12 +134,6 @@ def refresh_repo(repo, all_commits=False, notify=True, new_clone=False):
                 log.info('Compute last commit info %d: %s', (i + 1), ci._id)
 
     if not all_commits and not new_clone:
-        commits_by_branches = {}
-        commits_by_tags = {}
-        # svn has no branches, so we need __default__ as a fallback to collect
-        # all commits into
-        current_branches = ['__default__']
-        current_tags = []
         for commit in commit_ids:
             new = repo.commit(commit)
             user = User.by_email_address(new.committed.email)
@@ -153,26 +147,13 @@ def refresh_repo(repo, all_commits=False, notify=True, new_clone=False):
                                        related_nodes=[repo.app_config.project],
                                        tags=['commit', repo.tool.lower()])
 
-            branches, tags = repo.symbolics_for_commit(new)
-            if branches:
-                current_branches = branches
-            if tags:
-                current_tags = tags
-            for b in current_branches:
-                if b not in commits_by_branches.keys():
-                    commits_by_branches[b] = []
-                commits_by_branches[b].append(commit)
-            for t in current_tags:
-                if t not in commits_by_tags.keys():
-                    commits_by_tags[t] = []
-                commits_by_tags[t].append(commit)
-
         from allura.webhooks import RepoPushWebhookSender
+        by_branches, by_tags = _group_commits(repo, commit_ids)
         params = []
-        for b, commits in commits_by_branches.iteritems():
+        for b, commits in by_branches.iteritems():
             ref = u'refs/heads/{}'.format(b) if b != '__default__' else None
             params.append(dict(commit_ids=commits, ref=ref))
-        for t, commits in commits_by_tags.iteritems():
+        for t, commits in by_tags.iteritems():
             ref = u'refs/tags/{}'.format(t)
             params.append(dict(commit_ids=commits, ref=ref))
         if params:
@@ -646,3 +627,28 @@ def _update_tree_cache(tree_ids, cache):
     cached_ids = set(cache.instance_ids(Tree))
     new_ids = current_ids - cached_ids
     cache.batch_load(Tree, {'_id': {'$in': list(new_ids)}})
+
+
+def _group_commits(repo, commit_ids):
+    by_branches = {}
+    by_tags = {}
+    # svn has no branches, so we need __default__ as a fallback to collect
+    # all commits into
+    current_branches = ['__default__']
+    current_tags = []
+    for commit in commit_ids:
+        ci = repo.commit(commit)
+        branches, tags = repo.symbolics_for_commit(ci)
+        if branches:
+            current_branches = branches
+        if tags:
+            current_tags = tags
+        for b in current_branches:
+            if b not in by_branches.keys():
+                by_branches[b] = []
+            by_branches[b].append(commit)
+        for t in current_tags:
+            if t not in by_tags.keys():
+                by_tags[t] = []
+            by_tags[t].append(commit)
+    return by_branches, by_tags

http://git-wip-us.apache.org/repos/asf/allura/blob/b967bc5c/Allura/allura/tests/test_webhooks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_webhooks.py b/Allura/allura/tests/test_webhooks.py
index 629b322..c0f9140 100644
--- a/Allura/allura/tests/test_webhooks.py
+++ b/Allura/allura/tests/test_webhooks.py
@@ -485,6 +485,18 @@ class TestRepoPushWebhookSender(TestWebhookBase):
             self.wh._id,
             sender.get_payload.return_value)
 
+    @patch('allura.webhooks.send_webhook', autospec=True)
+    def test_send_with_list(self, send_webhook):
+        sender = RepoPushWebhookSender()
+        sender.get_payload = Mock(side_effect=[1, 2])
+        self.wh.enforce_limit = Mock(return_value=True)
+        with h.push_config(c, app=self.git):
+            sender.send([dict(arg1=1, arg2=2), dict(arg1=3, arg2=4)])
+        assert_equal(send_webhook.post.call_count, 2)
+        assert_equal(send_webhook.post.call_args_list,
+                     [call(self.wh._id, 1), call(self.wh._id, 2)])
+        assert_equal(self.wh.enforce_limit.call_count, 1)
+
     @patch('allura.webhooks.log', autospec=True)
     @patch('allura.webhooks.send_webhook', autospec=True)
     def test_send_limit_reached(self, send_webhook, log):
@@ -549,6 +561,24 @@ class TestRepoPushWebhookSender(TestWebhookBase):
             add_webhooks('two', 3)
             assert_equal(sender.enforce_limit(self.git), False)
 
+    def test_before(self):
+        sender = RepoPushWebhookSender()
+        with patch.object(self.git.repo, 'commit', autospec=True) as _ci:
+            assert_equal(sender._before(self.git.repo, ['3', '2', '1']), '')
+            _ci.return_value.parent_ids = ['0']
+            assert_equal(sender._before(self.git.repo, ['3', '2', '1']), '0')
+
+    def test_after(self):
+        sender = RepoPushWebhookSender()
+        assert_equal(sender._after([]), '')
+        assert_equal(sender._after(['3', '2', '1']), '3')
+
+    def test_convert_id(self):
+        sender = RepoPushWebhookSender()
+        assert_equal(sender._convert_id(''), '')
+        assert_equal(sender._convert_id('a433fa9'), 'a433fa9')
+        assert_equal(sender._convert_id('a433fa9:13'), 'r13')
+
 
 class TestModels(TestWebhookBase):
 

http://git-wip-us.apache.org/repos/asf/allura/blob/b967bc5c/Allura/allura/tests/unit/test_repo.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/test_repo.py b/Allura/allura/tests/unit/test_repo.py
index f2554fb..7411db5 100644
--- a/Allura/allura/tests/unit/test_repo.py
+++ b/Allura/allura/tests/unit/test_repo.py
@@ -19,13 +19,18 @@ import datetime
 import unittest
 from mock import patch, Mock, MagicMock
 from nose.tools import assert_equal
+from datadiff import tools as dd
 
 from pylons import tmpl_context as c
 
 from allura import model as M
 from allura.controllers.repository import topo_sort
 from allura.model.repository import zipdir, prefix_paths_union
-from allura.model.repo_refresh import CommitRunDoc, CommitRunBuilder
+from allura.model.repo_refresh import (
+    CommitRunDoc,
+    CommitRunBuilder,
+    _group_commits,
+)
 from alluratest.controller import setup_unit_test
 
 
@@ -315,3 +320,38 @@ class TestPrefixPathsUnion(unittest.TestCase):
         a = set(['a1', 'a2', 'a3'])
         b = set(['b1', 'a2/foo', 'b3/foo'])
         self.assertItemsEqual(prefix_paths_union(a, b), ['a2'])
+
+
+class TestGroupCommits(object):
+
+    def setUp(self):
+        self.repo = Mock()
+        self.repo.symbolics_for_commit.return_value = ([], [])
+
+    def test_no_branches(self):
+        b, t = _group_commits(self.repo, ['3', '2', '1'])
+        dd.assert_equal(b, {'__default__': ['3', '2', '1']})
+        dd.assert_equal(t, {})
+
+    def test_branches_and_tags(self):
+        self.repo.symbolics_for_commit.side_effect = [
+            (['master'], ['v1.1']),
+            ([], []),
+            ([], []),
+        ]
+        b, t = _group_commits(self.repo, ['3', '2', '1'])
+        dd.assert_equal(b, {'master': ['3', '2', '1']})
+        dd.assert_equal(t, {'v1.1': ['3', '2', '1']})
+
+    def test_multiple_branches(self):
+        self.repo.symbolics_for_commit.side_effect = [
+            (['master'], ['v1.1']),
+            ([], ['v1.0']),
+            (['test1', 'test2'], []),
+        ]
+        b, t = _group_commits(self.repo, ['3', '2', '1'])
+        dd.assert_equal(b, {'master': ['3', '2'],
+                            'test1': ['1'],
+                            'test2': ['1']})
+        dd.assert_equal(t, {'v1.1': ['3'],
+                            'v1.0': ['2', '1']})

http://git-wip-us.apache.org/repos/asf/allura/blob/b967bc5c/Allura/allura/webhooks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/webhooks.py b/Allura/allura/webhooks.py
index 03a909d..ba55a89 100644
--- a/Allura/allura/webhooks.py
+++ b/Allura/allura/webhooks.py
@@ -337,7 +337,7 @@ class RepoPushWebhookSender(WebhookSender):
                 return self._convert_id(parents[-1])
         return u''
 
-    def _after(self, repo, commit_ids):
+    def _after(self, commit_ids):
         if len(commit_ids) > 0:
             return self._convert_id(commit_ids[0])
         return u''
@@ -353,7 +353,7 @@ class RepoPushWebhookSender(WebhookSender):
         for ci in commits:
             ci['id'] = self._convert_id(ci['id'])
         before = self._before(app.repo, commit_ids)
-        after = self._after(app.repo, commit_ids)
+        after = self._after(commit_ids)
         payload = {
             'size': len(commits),
             'commits': commits,