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/03/06 13:35:11 UTC

[04/26] allura git commit: [#7832] ticket:731 List webhooks API

[#7832] ticket:731 List webhooks API


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

Branch: refs/heads/ib/7830
Commit: 804d86d6472d16591faf9fb6e6898dce1d6a636f
Parents: 8c604ce
Author: Igor Bondarenko <je...@gmail.com>
Authored: Fri Feb 20 11:07:16 2015 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Fri Feb 27 22:40:51 2015 +0000

----------------------------------------------------------------------
 Allura/allura/lib/repository.py      |  6 ++-
 Allura/allura/model/webhook.py       | 10 +++++
 Allura/allura/tests/test_webhooks.py | 72 ++++++++++++++++++++++++++++++-
 3 files changed, 86 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/804d86d6/Allura/allura/lib/repository.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/repository.py b/Allura/allura/lib/repository.py
index d5d467b..41e671d 100644
--- a/Allura/allura/lib/repository.py
+++ b/Allura/allura/lib/repository.py
@@ -293,4 +293,8 @@ class RestWebhooksLookup(BaseController):
         webhooks = self.app._webhooks
         if len(webhooks) == 0:
             raise exc.HTTPNotFound()
-        return {'test': 'works'}
+        configured_hooks = M.Webhook.query.find({
+            'type': {'$in': [wh.type for wh in webhooks]},
+            'app_config_id': self.app.config._id}
+        ).sort('_id', 1).all()
+        return {'webhooks': [hook.__json__() for hook in configured_hooks]}

http://git-wip-us.apache.org/repos/asf/allura/blob/804d86d6/Allura/allura/model/webhook.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/webhook.py b/Allura/allura/model/webhook.py
index d1165ff..0d5140e 100644
--- a/Allura/allura/model/webhook.py
+++ b/Allura/allura/model/webhook.py
@@ -22,6 +22,7 @@ from paste.deploy.converters import asint
 from tg import config
 
 from allura.model import Artifact
+from allura.lib import helpers as h
 
 
 class Webhook(Artifact):
@@ -53,3 +54,12 @@ class Webhook(Artifact):
     def update_limit(self):
         self.last_sent = dt.datetime.utcnow()
         session(self).flush(self)
+
+    def __json__(self):
+        return {
+            '_id': unicode(self._id),
+            'url': h.absurl(u'/rest' + self.url()),
+            'type': unicode(self.type),
+            'hook_url': unicode(self.hook_url),
+            'mod_date': self.mod_date,
+        }

http://git-wip-us.apache.org/repos/asf/allura/blob/804d86d6/Allura/allura/tests/test_webhooks.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_webhooks.py b/Allura/allura/tests/test_webhooks.py
index 330c4bd..d750d9d 100644
--- a/Allura/allura/tests/test_webhooks.py
+++ b/Allura/allura/tests/test_webhooks.py
@@ -27,6 +27,7 @@ from nose.tools import (
     assert_not_in,
     assert_in,
 )
+from datadiff import tools as dd
 from formencode import Invalid
 from ming.odm import session
 from pylons import tmpl_context as c
@@ -42,7 +43,11 @@ from allura.webhooks import (
     SendWebhookHelper,
 )
 from allura.tests import decorators as td
-from alluratest.controller import setup_basic_test, TestController
+from alluratest.controller import (
+    setup_basic_test,
+    TestController,
+    TestRestApiBase,
+)
 
 
 # important to be distinct from 'test' and 'test2' which ForgeGit and
@@ -639,3 +644,68 @@ class TestModels(TestWebhookBase):
         self.wh.update_limit()
         session(self.wh).expunge(self.wh)
         assert_equal(M.Webhook.query.get(_id=self.wh._id).last_sent, _now)
+
+    def test_json(self):
+        expected = {
+            '_id': unicode(self.wh._id),
+            'url': u'http://localhost/rest/adobe/adobe-1/admin'
+                   u'/src/webhooks/repo-push/{}'.format(self.wh._id),
+            'type': u'repo-push',
+            'hook_url': u'http://httpbin.org/post',
+            'mod_date': self.wh.mod_date,
+        }
+        dd.assert_equal(self.wh.__json__(), expected)
+
+
+
+class TestWebhookRestController(TestRestApiBase):
+    def setUp(self):
+        super(TestWebhookRestController, self).setUp()
+        self.patches = self.monkey_patch()
+        for p in self.patches:
+            p.start()
+        self.setup_with_tools()
+        self.project = M.Project.query.get(shortname=test_project_with_repo)
+        self.git = self.project.app_instance('src')
+        self.url = str('/rest' + self.git.admin_url + 'webhooks')
+        self.webhooks = []
+        for i in range(3):
+            webhook = M.Webhook(
+                type='repo-push',
+                app_config_id=self.git.config._id,
+                hook_url='http://httpbin.org/post/{}'.format(i),
+                secret='secret-{}'.format(i))
+            session(webhook).flush(webhook)
+            self.webhooks.append(webhook)
+
+    def tearDown(self):
+        super(TestWebhookRestController, self).tearDown()
+        for p in self.patches:
+            p.stop()
+
+    @with_git
+    def setup_with_tools(self):
+        pass
+
+    def monkey_patch(self):
+        gen_secret = patch.object(
+            WebhookController,
+            'gen_secret',
+            return_value='super-secret',
+            autospec=True)
+        # we don't need actual repo here, and this avoids test conflicts when
+        # running in parallel
+        repo_init = patch.object(M.Repository, 'init', autospec=True)
+        return [gen_secret, repo_init]
+
+    def test_webhooks_list(self):
+        r = self.api_get(self.url)
+        webhooks = [{
+            '_id': unicode(wh._id),
+            'url': 'http://localhost/rest/adobe/adobe-1/admin'
+                   '/src/webhooks/repo-push/{}'.format(wh._id),
+            'type': 'repo-push',
+            'hook_url': 'http://httpbin.org/post/{}'.format(n),
+            'mod_date': unicode(wh.mod_date),
+        } for n, wh in enumerate(self.webhooks)]
+        dd.assert_equal(r.json, {'webhooks': webhooks})