1313import shutil
1414from zipfile import ZipFile
1515from cStringIO import StringIO
16+ from urlparse import urlparse
17+ import json
18+ import base64
1619
1720# Set to False to allow self-signed/invalid ssl certificates
1821verify = False
@@ -52,7 +55,7 @@ def get_members(zip):
5255
5356
5457def lambda_handler (event , context ):
55-
58+ print json . dumps ( event )
5659 params = None
5760 logger .info ('Event %s' , event )
5861 OAUTH_token = event ['context' ]['git-token' ]
@@ -69,9 +72,11 @@ def lambda_handler(event, context):
6972 hostflavour = 'bitbucket'
7073 elif event ['params' ]['header' ]['User-Agent' ].startswith ('GitHub-Hookshot' ):
7174 hostflavour = 'github'
75+ elif 'Bitbucket-' in event ['params' ]['header' ]['User-Agent' ]:
76+ hostflavour = 'bitbucket-server'
7277 elif event ['body-json' ]['publisherId' ] == 'tfs' :
7378 hostflavour = 'tfs'
74-
79+
7580 headers = {}
7681 branch = 'master'
7782 if hostflavour == 'githubent' :
@@ -120,8 +125,36 @@ def lambda_handler(event, context):
120125 headers ['Authorization' ] = 'Basic %s' % pat_in_base64
121126 headers ['Authorization' ] = headers ['Authorization' ].replace ('\n ' ,'' )
122127 headers ['Accept' ] = 'application/zip'
128+ elif hostflavour == 'bitbucket-server' :
129+ clone_urls = event ['body-json' ]['repository' ]['links' ]['clone' ]
130+ http_clone_url = None
131+ for clone_url in clone_urls :
132+ if clone_url .get ('name' ) == "http" :
133+ http_clone_url = clone_url .get ('href' )
134+ if http_clone_url is None :
135+ raise Exception ("Could not find http clone url from the webhook payload" )
136+
137+ if len (event ['body-json' ]['changes' ]) != 1 :
138+ raise Exception ("Could not handle the number of changes" )
139+ change = event ['body-json' ]['changes' ][0 ]
140+
141+ print "http_clone_url is " + http_clone_url
142+ url_parts = urlparse (http_clone_url )
143+ owner = event ['body-json' ]['repository' ]['project' ]['name' ]
144+ name = event ['body-json' ]['repository' ]['name' ]
145+ archive_url = "{scheme}://{netloc}/rest/api/latest/projects/{project}/repos/{repo}/archive?at={hash}&format=zip" .format (
146+ scheme = url_parts .scheme ,
147+ netloc = url_parts .netloc ,
148+ project = owner ,
149+ repo = name ,
150+ hash = change ['toHash' ],
151+ )
152+ branch = change ['refId' ].replace ('refs/heads/' , '' )
153+ secret = base64 .b64encode (
154+ ":" .join ([event ['context' ]['oauth-key' ], event ['context' ]['oauth-secret' ]])
155+ )
156+ headers ['Authorization' ] = 'Basic ' + secret
123157
124- s3_archive_file = "%s/%s/%s/%s.zip" % (owner , name , branch , name )
125158 # download the code archive via archive url
126159 logger .info ('Downloading archive from %s' % archive_url )
127160 r = requests .get (archive_url , verify = verify , headers = headers , params = params )
@@ -140,6 +173,7 @@ def lambda_handler(event, context):
140173 zip .extractall (path , get_members (zip ))
141174
142175 # Create zip from /tmp dir without any common preffixes
176+ s3_archive_file = "%s/%s/%s/%s.zip" % (owner , name , branch , name )
143177 shutil .make_archive (zipped_code , 'zip' , path )
144178 logger .info ("Uploading zip to S3://%s/%s" % (OutputBucket , s3_archive_file ))
145179 s3_client .upload_file (zipped_code + '.zip' , OutputBucket , s3_archive_file )
0 commit comments