1313import shutil
1414from zipfile import ZipFile
1515from cStringIO import StringIO
16+ from urlparse import urlparse
17+ import base64
1618
1719# Set to False to allow self-signed/invalid ssl certificates
1820verify = False
@@ -52,7 +54,6 @@ def get_members(zip):
5254
5355
5456def lambda_handler (event , context ):
55-
5657 params = None
5758 logger .info ('Event %s' , event )
5859 OAUTH_token = event ['context' ]['git-token' ]
@@ -69,9 +70,11 @@ def lambda_handler(event, context):
6970 hostflavour = 'bitbucket'
7071 elif event ['params' ]['header' ]['User-Agent' ].startswith ('GitHub-Hookshot' ):
7172 hostflavour = 'github'
73+ elif 'Bitbucket-' in event ['params' ]['header' ]['User-Agent' ]:
74+ hostflavour = 'bitbucket-server'
7275 elif event ['body-json' ]['publisherId' ] == 'tfs' :
7376 hostflavour = 'tfs'
74-
77+
7578 headers = {}
7679 branch = 'master'
7780 if hostflavour == 'githubent' :
@@ -120,8 +123,34 @@ def lambda_handler(event, context):
120123 headers ['Authorization' ] = 'Basic %s' % pat_in_base64
121124 headers ['Authorization' ] = headers ['Authorization' ].replace ('\n ' ,'' )
122125 headers ['Accept' ] = 'application/zip'
126+ elif hostflavour == 'bitbucket-server' :
127+ clone_urls = event ['body-json' ]['repository' ]['links' ]['clone' ]
128+ http_clone_url = None
129+ for clone_url in clone_urls :
130+ if clone_url .get ('name' ) == "http" :
131+ http_clone_url = clone_url .get ('href' )
132+ if http_clone_url is None :
133+ raise Exception ("Could not find http clone url from the webhook payload" )
134+
135+ if len (event ['body-json' ]['changes' ]) != 1 :
136+ raise Exception ("Could not handle the number of changes" )
137+ change = event ['body-json' ]['changes' ][0 ]
138+ url_parts = urlparse (http_clone_url )
139+ owner = event ['body-json' ]['repository' ]['project' ]['name' ]
140+ name = event ['body-json' ]['repository' ]['name' ]
141+ archive_url = "{scheme}://{netloc}/rest/api/latest/projects/{project}/repos/{repo}/archive?at={hash}&format=zip" .format (
142+ scheme = url_parts .scheme ,
143+ netloc = url_parts .netloc ,
144+ project = owner ,
145+ repo = name ,
146+ hash = change ['toHash' ],
147+ )
148+ branch = change ['refId' ].replace ('refs/heads/' , '' )
149+ secret = base64 .b64encode (
150+ ":" .join ([event ['context' ]['oauth-key' ], event ['context' ]['oauth-secret' ]])
151+ )
152+ headers ['Authorization' ] = 'Basic ' + secret
123153
124- s3_archive_file = "%s/%s/%s/%s.zip" % (owner , name , branch , name )
125154 # download the code archive via archive url
126155 logger .info ('Downloading archive from %s' % archive_url )
127156 r = requests .get (archive_url , verify = verify , headers = headers , params = params )
@@ -140,6 +169,7 @@ def lambda_handler(event, context):
140169 zip .extractall (path , get_members (zip ))
141170
142171 # Create zip from /tmp dir without any common preffixes
172+ s3_archive_file = "%s/%s/%s/%s.zip" % (owner , name , branch , name )
143173 shutil .make_archive (zipped_code , 'zip' , path )
144174 logger .info ("Uploading zip to S3://%s/%s" % (OutputBucket , s3_archive_file ))
145175 s3_client .upload_file (zipped_code + '.zip' , OutputBucket , s3_archive_file )
0 commit comments