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

Commit cd73419

Browse files
authored
Merge pull request #34 from slalom/feature/bitbucket-server
update lambda to handle bitbuckt server
2 parents c383098 + 6620ae6 commit cd73419

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

functions/source/GitPullS3/lambda_function.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,12 @@ def lambda_handler(event, context):
144144
secure = True
145145
if 'X-Hub-Signature' in event['params']['header'].keys():
146146
for k in apikeys:
147-
k1 = hmac.new(str(k), str(event['context']['raw-body']), hashlib.sha1).hexdigest()
148-
k2 = str(event['params']['header']['X-Hub-Signature'].replace('sha1=', ''))
147+
if 'use-sha256' in event['context']:
148+
k1 = hmac.new(str(k), str(event['context']['raw-body']), hashlib.sha256).hexdigest()
149+
k2 = str(event['params']['header']['X-Hub-Signature'].replace('sha256=', ''))
150+
else:
151+
k1 = hmac.new(str(k), str(event['context']['raw-body']), hashlib.sha1).hexdigest()
152+
k2 = str(event['params']['header']['X-Hub-Signature'].replace('sha1=', ''))
149153
if k1 == k2:
150154
secure = True
151155
# TODO: Add the ability to clone TFS repo using SSH keys
@@ -161,8 +165,16 @@ def lambda_handler(event, context):
161165
# GitLab
162166
full_name = event['body-json']['repository']['path_with_namespace']
163167
except KeyError:
164-
# GitLab 8.5+
165-
full_name = event['body-json']['project']['path_with_namespace']
168+
try:
169+
# GitLab 8.5+
170+
full_name = event['body-json']['project']['path_with_namespace']
171+
except KeyError:
172+
try:
173+
# BitBucket server
174+
full_name = event['body-json']['repository']['name']
175+
except KeyError:
176+
# BitBucket pull-request
177+
full_name = event['body-json']['pullRequest']['fromRef']['repository']['name']
166178
if not secure:
167179
logger.error('Source IP %s is not allowed' % event['context']['source-ip'])
168180
raise Exception('Source IP %s is not allowed' % event['context']['source-ip'])
@@ -185,8 +197,23 @@ def lambda_handler(event, context):
185197
try:
186198
remote_url = 'git@'+event['body-json']['repository']['links']['html']['href'].replace('https://', '').replace('/', ':', 1)+'.git'
187199
except:
188-
# GitHub
189-
remote_url = event['body-json']['repository']['ssh_url']
200+
try:
201+
# GitHub
202+
remote_url = event['body-json']['repository']['ssh_url']
203+
except:
204+
# Bitbucket
205+
try:
206+
for i, url in enumerate(event['body-json']['repository']['links']['clone']):
207+
if url['name'] == 'ssh':
208+
ssh_index = i
209+
remote_url = event['body-json']['repository']['links']['clone'][ssh_index]['href']
210+
except:
211+
# BitBucket pull-request
212+
for i, url in enumerate(event['body-json']['pullRequest']['fromRef']['repository']['links']['clone']):
213+
if url['name'] == 'ssh':
214+
ssh_index = i
215+
216+
remote_url = event['body-json']['pullRequest']['fromRef']['repository']['links']['clone'][ssh_index]['href']
190217
repo_path = '/tmp/%s' % repo_name
191218
creds = RemoteCallbacks(credentials=get_keys(keybucket, pubkey), )
192219
try:

0 commit comments

Comments
 (0)