Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.

Commit 56ba635

Browse files
committed
adding support for bitbucket server
1 parent a405f42 commit 56ba635

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

functions/source/ZipDl/lambda_function.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import shutil
1414
from zipfile import ZipFile
1515
from 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
1821
verify = False
@@ -52,7 +55,7 @@ def get_members(zip):
5255

5356

5457
def 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

Comments
 (0)