From 56ba635c20fb9eae0ba0c9ad847b52b4cee7d57d Mon Sep 17 00:00:00 2001 From: Eamonn Faherty Date: Thu, 31 Oct 2019 16:28:54 +0000 Subject: [PATCH 1/3] adding support for bitbucket server --- .gitignore | 1 + functions/source/ZipDl/lambda_function.py | 40 +++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/functions/source/ZipDl/lambda_function.py b/functions/source/ZipDl/lambda_function.py index 96bd8a9..f88cd6f 100644 --- a/functions/source/ZipDl/lambda_function.py +++ b/functions/source/ZipDl/lambda_function.py @@ -13,6 +13,9 @@ import shutil from zipfile import ZipFile from cStringIO import StringIO +from urlparse import urlparse +import json +import base64 # Set to False to allow self-signed/invalid ssl certificates verify = False @@ -52,7 +55,7 @@ def get_members(zip): def lambda_handler(event, context): - + print json.dumps(event) params = None logger.info('Event %s', event) OAUTH_token = event['context']['git-token'] @@ -69,9 +72,11 @@ def lambda_handler(event, context): hostflavour = 'bitbucket' elif event['params']['header']['User-Agent'].startswith('GitHub-Hookshot'): hostflavour = 'github' + elif 'Bitbucket-' in event['params']['header']['User-Agent']: + hostflavour = 'bitbucket-server' elif event['body-json']['publisherId'] == 'tfs': hostflavour='tfs' - + headers = {} branch = 'master' if hostflavour == 'githubent': @@ -120,8 +125,36 @@ def lambda_handler(event, context): headers['Authorization'] = 'Basic %s' % pat_in_base64 headers['Authorization'] = headers['Authorization'].replace('\n','') headers['Accept'] = 'application/zip' + elif hostflavour == 'bitbucket-server': + clone_urls = event['body-json']['repository']['links']['clone'] + http_clone_url = None + for clone_url in clone_urls: + if clone_url.get('name') == "http": + http_clone_url = clone_url.get('href') + if http_clone_url is None: + raise Exception("Could not find http clone url from the webhook payload") + + if len(event['body-json']['changes']) != 1: + raise Exception("Could not handle the number of changes") + change = event['body-json']['changes'][0] + + print "http_clone_url is " + http_clone_url + url_parts = urlparse(http_clone_url) + owner = event['body-json']['repository']['project']['name'] + name = event['body-json']['repository']['name'] + archive_url = "{scheme}://{netloc}/rest/api/latest/projects/{project}/repos/{repo}/archive?at={hash}&format=zip".format( + scheme=url_parts.scheme, + netloc=url_parts.netloc, + project=owner, + repo=name, + hash=change['toHash'], + ) + branch = change['refId'].replace('refs/heads/', '') + secret = base64.b64encode( + ":".join([event['context']['oauth-key'], event['context']['oauth-secret']]) + ) + headers['Authorization'] = 'Basic ' + secret - s3_archive_file = "%s/%s/%s/%s.zip" % (owner, name, branch, name) # download the code archive via archive url logger.info('Downloading archive from %s' % archive_url) r = requests.get(archive_url, verify=verify, headers=headers, params=params) @@ -140,6 +173,7 @@ def lambda_handler(event, context): zip.extractall(path, get_members(zip)) # Create zip from /tmp dir without any common preffixes + s3_archive_file = "%s/%s/%s/%s.zip" % (owner, name, branch, name) shutil.make_archive(zipped_code, 'zip', path) logger.info("Uploading zip to S3://%s/%s" % (OutputBucket, s3_archive_file)) s3_client.upload_file(zipped_code + '.zip', OutputBucket, s3_archive_file) From 635e59bdbf34d15d06d79f531b52a7f9825348ba Mon Sep 17 00:00:00 2001 From: Eamonn Faherty Date: Thu, 31 Oct 2019 16:30:46 +0000 Subject: [PATCH 2/3] removed debugging --- functions/source/ZipDl/lambda_function.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/functions/source/ZipDl/lambda_function.py b/functions/source/ZipDl/lambda_function.py index f88cd6f..1751948 100644 --- a/functions/source/ZipDl/lambda_function.py +++ b/functions/source/ZipDl/lambda_function.py @@ -14,7 +14,6 @@ from zipfile import ZipFile from cStringIO import StringIO from urlparse import urlparse -import json import base64 # Set to False to allow self-signed/invalid ssl certificates @@ -55,7 +54,6 @@ def get_members(zip): def lambda_handler(event, context): - print json.dumps(event) params = None logger.info('Event %s', event) OAUTH_token = event['context']['git-token'] From b47ed82470a938a396262b735f0c440814659028 Mon Sep 17 00:00:00 2001 From: Eamonn Faherty Date: Thu, 31 Oct 2019 16:31:26 +0000 Subject: [PATCH 3/3] removed debugging --- functions/source/ZipDl/lambda_function.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/functions/source/ZipDl/lambda_function.py b/functions/source/ZipDl/lambda_function.py index 1751948..c978813 100644 --- a/functions/source/ZipDl/lambda_function.py +++ b/functions/source/ZipDl/lambda_function.py @@ -135,8 +135,6 @@ def lambda_handler(event, context): if len(event['body-json']['changes']) != 1: raise Exception("Could not handle the number of changes") change = event['body-json']['changes'][0] - - print "http_clone_url is " + http_clone_url url_parts = urlparse(http_clone_url) owner = event['body-json']['repository']['project']['name'] name = event['body-json']['repository']['name']