@@ -145,8 +145,12 @@ def lambda_handler(event, context):
145145 secure = True
146146 if 'X-Hub-Signature' in event ['params' ]['header' ].keys ():
147147 for k in apikeys :
148- k1 = hmac .new (str (k ), str (event ['context' ]['raw-body' ]), hashlib .sha1 ).hexdigest ()
149- k2 = str (event ['params' ]['header' ]['X-Hub-Signature' ].replace ('sha1=' , '' ))
148+ if 'use-sha256' in event ['context' ]:
149+ k1 = hmac .new (str (k ), str (event ['context' ]['raw-body' ]), hashlib .sha256 ).hexdigest ()
150+ k2 = str (event ['params' ]['header' ]['X-Hub-Signature' ].replace ('sha256=' , '' ))
151+ else :
152+ k1 = hmac .new (str (k ), str (event ['context' ]['raw-body' ]), hashlib .sha1 ).hexdigest ()
153+ k2 = str (event ['params' ]['header' ]['X-Hub-Signature' ].replace ('sha1=' , '' ))
150154 if k1 == k2 :
151155 secure = True
152156 # TODO: Add the ability to clone TFS repo using SSH keys
@@ -162,8 +166,16 @@ def lambda_handler(event, context):
162166 # GitLab
163167 full_name = event ['body-json' ]['repository' ]['path_with_namespace' ]
164168 except KeyError :
165- # GitLab 8.5+
166- full_name = event ['body-json' ]['project' ]['path_with_namespace' ]
169+ try :
170+ # GitLab 8.5+
171+ full_name = event ['body-json' ]['project' ]['path_with_namespace' ]
172+ except KeyError :
173+ try :
174+ # BitBucket server
175+ full_name = event ['body-json' ]['repository' ]['name' ]
176+ except KeyError :
177+ # BitBucket pull-request
178+ full_name = event ['body-json' ]['pullRequest' ]['fromRef' ]['repository' ]['name' ]
167179 if not secure :
168180 logger .error ('Source IP %s is not allowed' % event ['context' ]['source-ip' ])
169181 raise Exception ('Source IP %s is not allowed' % event ['context' ]['source-ip' ])
@@ -186,8 +198,23 @@ def lambda_handler(event, context):
186198 try :
187199 remote_url = 'git@' + event ['body-json' ]['repository' ]['links' ]['html' ]['href' ].replace ('https://' , '' ).replace ('/' , ':' , 1 )+ '.git'
188200 except :
189- # GitHub
190- remote_url = event ['body-json' ]['repository' ]['ssh_url' ]
201+ try :
202+ # GitHub
203+ remote_url = event ['body-json' ]['repository' ]['ssh_url' ]
204+ except :
205+ # Bitbucket
206+ try :
207+ for i , url in enumerate (event ['body-json' ]['repository' ]['links' ]['clone' ]):
208+ if url ['name' ] == 'ssh' :
209+ ssh_index = i
210+ remote_url = event ['body-json' ]['repository' ]['links' ]['clone' ][ssh_index ]['href' ]
211+ except :
212+ # BitBucket pull-request
213+ for i , url in enumerate (event ['body-json' ]['pullRequest' ]['fromRef' ]['repository' ]['links' ]['clone' ]):
214+ if url ['name' ] == 'ssh' :
215+ ssh_index = i
216+
217+ remote_url = event ['body-json' ]['pullRequest' ]['fromRef' ]['repository' ]['links' ]['clone' ][ssh_index ]['href' ]
191218 repo_path = '/tmp/%s' % repo_name
192219 creds = RemoteCallbacks (credentials = get_keys (keybucket , pubkey ), )
193220 try :
0 commit comments