@@ -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