Commits

Wes Thomas committed 48fbfbb

Initial commit with beta broker service.

  • Participants

Comments (0)

Files changed (5)

+http://www.opensource.org/licenses/mit-license
+
+The MIT License (MIT)
+Copyright (c) 2012 Edge Case Labs, LLC
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+# DeployHQ Bitbucket.org Service Broker
+
+
+The custom service broker was created to enable deployment via deployhq.com's deployment service using your git or mercurial repository.
+
+When invoked, it will parse a payload from the bitbucket service broker manager and then POST a request to the specified URL to invoke a deployhq deployment.
+import urllib
+from brokers import BaseBroker
+from django.utils import simplejson as sj
+ 
+class URLOpener(urllib.FancyURLopener):
+    version = 'bitbucket.org'
+ 
+class DeployHQBroker(BaseBroker):
+    
+    """
+    Three parameters are expected within the payload['service']:
+    url (required) - the DeployHQ URL to which this broker will post
+    notification_email (required) - the email to which DeployHQ will send deployment messages
+    deploy_branch (optional) - the branch from which DeployHQ will deploy - defaults to 'master'
+
+    At least one parameter must be in the payload['commits']:
+    raw_node or node if raw_node not available (required) - the commit ID for git or hg which DeployHQ will checkout and deploy
+    """
+    def handle(self, payload):
+
+        import pdb; pdb.set_trace()
+
+
+        #the DeployHQ URL to which this broker will post
+        url = payload['service']['url']
+
+        #the email to which DeployHQ will send deployment message
+        notification_email = payload['service']['notification_email']
+
+        #the branch from which DeployHQ will deploy - defaults to 'master'
+        deploy_branch = payload['service']['deploy_branch'] if 'deploy_branch' in payload['service'] else 'master'
+        
+        #commit ID - try the full raw_node value if it exists, else use the abreviated node value
+        commit = payload['commits']
+        #if we're dealing with a list, always select last item in list
+        if isinstance(commit, list):
+            commit = commit[len(commit) - 1]
+
+        commit_id = commit['raw_node'] if 'raw_node' in commit else commit['node']
+
+        #prepay payload for POST
+        deployhq_payload = {
+                "branch": deploy_branch,
+                "clone_url": url,
+                "email": notification_email,
+                "new_ref": commit_id,
+        }
+        post_load = { 'payload': sj.dumps(deployhq_payload) }
+ 
+        #post payload to our URL
+        opener = self.get_local('opener', URLOpener)
+        opener.open(url, urllib.urlencode(post_load))
+
+"""
+Stand-in class for bitbucket's BaseBroker.
+"""
+class BaseBroker():
+	def get_local(self, arg, theclass):
+		return theclass()
+import unittest
+import json
+from broker import DeployHQBroker
+
+"""
+Execute tests:
+$ python tests.py
+
+
+For debugging:
+$ python -m pdb tests.py
+
+
+Pre-reqs:
+If you don't want the broker to fail to post to the service url, setup a POST
+handler at http://localhost:8000/post_dump so you can see the output.
+
+
+Ipython copy paste for interactive testing (enable alternate class statement):
+In [1]: from tests import TestDeployHQ
+In [2]: import json
+In [3]: t = TestDeployHQ()
+In [4]: t.setUp()
+In [5]: j = json.loads(t.deployhq_test_post1)
+In [6]: j2 = json.loads(t.deployhq_test_post2)
+In [7]: j3 = json.loads(t.deployhq_test_post3)
+"""
+#class TestDeployHQ(): #Alternate class statement for interactive command line testing
+class TestDeployHQ(unittest.TestCase):
+
+    def setUp(self):
+
+        #Test with all required params and optional branch params
+        self.deployhq_test_post1 = """
+        {
+            "service": {"url": "http://localhost:8000/post_dump", "notification_email": "testemail@gmail.com", "deploy_branch": "master" },
+            "canon_url": "https://bitbucket.org", 
+            "commits": [
+                {
+                    "author": "testuser", 
+                    "branch": "master", 
+                    "files": [
+                        {
+                            "file": "webapp/settings.py", 
+                            "type": "modified"
+                        }
+                    ], 
+                    "message": "test commit 2\\n", 
+                    "node": "cd4fbbdce7f4", 
+                    "parents": [
+                        "afa01ed6c229"
+                    ], 
+                    "raw_author": "Test User <testemail@gmail.com>", 
+                    "raw_node": "cd4fbbdce7f4e95284d552ad5868acc1cf42252c", 
+                    "revision": null, 
+                    "size": -1, 
+                    "timestamp": "2012-03-20 06:01:44", 
+                    "utctimestamp": "2012-03-20 05:01:44+00:00"
+                }
+            ], 
+            "repository": {
+                "absolute_url": "/testuser/testrepo/", 
+                "fork": false, 
+                "is_private": true, 
+                "name": "testrepo", 
+                "owner": "testuser", 
+                "scm": "git", 
+                "slug": "testrepo", 
+                "website": ""
+            }, 
+            "user": "testuser"
+        }
+        """
+
+        # No branch param
+        self.deployhq_test_post2 = """
+        {
+            "service": {"url": "http://localhost:8000/post_dump", "notification_email": "testemail@gmail.com" },
+            "canon_url": "https://bitbucket.org", 
+            "commits": [
+                {
+                    "author": "testuser", 
+                    "branch": "master", 
+                    "files": [
+                        {
+                            "file": "webapp/settings.py", 
+                            "type": "modified"
+                        }
+                    ], 
+                    "message": "test commit 2\\n", 
+                    "node": "cd4fbbdce7f4", 
+                    "parents": [
+                        "afa01ed6c229"
+                    ], 
+                    "raw_author": "Test User <testemail@gmail.com>", 
+                    "raw_node": "cd4fbbdce7f4e95284d552ad5868acc1cf42252c", 
+                    "revision": null, 
+                    "size": -1, 
+                    "timestamp": "2012-03-20 06:01:44", 
+                    "utctimestamp": "2012-03-20 05:01:44+00:00"
+                },
+                {
+                    "author": "testuser", 
+                    "branch": "master", 
+                    "files": [
+                        {
+                            "file": "webapp/test.py", 
+                            "type": "modified"
+                        }
+                    ], 
+                    "message": "test commit 2\\n", 
+                    "node": "123123123", 
+                    "parents": [
+                        "afa01ed6c229"
+                    ], 
+                    "raw_author": "Test User <testemail@gmail.com>", 
+                    "raw_node": "123123123", 
+                    "revision": null, 
+                    "size": -1, 
+                    "timestamp": "2012-03-21 06:01:44", 
+                    "utctimestamp": "2012-03-21 05:01:44+00:00"
+                }
+            ], 
+            "repository": {
+                "absolute_url": "/testuser/testrepo/", 
+                "fork": false, 
+                "is_private": true, 
+                "name": "testrepo", 
+                "owner": "testuser", 
+                "scm": "git", 
+                "slug": "testrepo", 
+                "website": ""
+            }, 
+            "user": "testuser"
+        }
+        """
+
+        #Commits is a dict instead of list
+        self.deployhq_test_post3 = """
+        {
+            "service": {"url": "http://localhost:8000/post_dump", "notification_email": "testemail@gmail.com", "deploy_branch": "master" },
+            "canon_url": "https://bitbucket.org", 
+            "commits": 
+                {
+                    "author": "testuser", 
+                    "branch": "master", 
+                    "files": [
+                        {
+                            "file": "webapp/settings.py", 
+                            "type": "modified"
+                        }
+                    ], 
+                    "message": "test commit 2\\n", 
+                    "node": "cd4fbbdce7f4", 
+                    "parents": [
+                        "afa01ed6c229"
+                    ], 
+                    "raw_author": "Test User <testemail@gmail.com>", 
+                    "raw_node": "cd4fbbdce7f4e95284d552ad5868acc1cf42252c", 
+                    "revision": null, 
+                    "size": -1, 
+                    "timestamp": "2012-03-20 06:01:44", 
+                    "utctimestamp": "2012-03-20 05:01:44+00:00"
+                }
+            , 
+            "repository": {
+                "absolute_url": "/testuser/testrepo/", 
+                "fork": false, 
+                "is_private": true, 
+                "name": "testrepo", 
+                "owner": "testuser", 
+                "scm": "git", 
+                "slug": "testrepo", 
+                "website": ""
+            }, 
+            "user": "testuser"
+        }
+        """
+
+
+    def test_posts(self):
+
+        broker = DeployHQBroker()
+        
+        broker.handle(json.loads(self.deployhq_test_post1))
+        
+        broker.handle(json.loads(self.deployhq_test_post2))
+
+        broker.handle(json.loads(self.deployhq_test_post3))
+
+        self.assertTrue(True)
+
+if __name__ == '__main__':
+    unittest.main()