def post_receive_hook_close_submitted(request, local_site_name=None, repository_id=None, hosting_service_id=None, hooks_uuid=None): """Closes review requests as submitted automatically after a push.""" repository = get_repository_for_hook(repository_id, hosting_service_id, local_site_name, hooks_uuid) if 'payload' not in request.POST: return HttpResponseBadRequest('Missing payload') try: payload = json.loads(request.POST['payload']) except ValueError as e: logging.error('The payload is not in JSON format: %s', e) return HttpResponseBadRequest('Invalid payload format') server_url = get_server_url(request=request) review_request_id_to_commits = \ _get_review_request_id_to_commits_map(payload, server_url, repository) if review_request_id_to_commits: close_all_review_requests(review_request_id_to_commits, local_site_name, repository, hosting_service_id) return HttpResponse()
def post_receive_hook_close_submitted(request, local_site_name=None, repository_id=None, hosting_service_id=None, hooks_uuid=None): """Close review requests as submitted automatically after a push. Args: request (django.http.HttpRequest): The request from the Bitbucket webhook. local_site_name (unicode, optional): The local site name, if available. repository_id (int, optional): The pk of the repository, if available. hosting_service_id (unicode, optional): The name of the hosting service. hooks_uuid (unicode, optional): The UUID of the configured webhook. Returns: django.http.HttpResponse: A response for the request. """ repository = get_repository_for_hook( repository_id=repository_id, hosting_service_id=hosting_service_id, local_site_name=local_site_name, hooks_uuid=hooks_uuid) try: payload = json.loads(request.body) except ValueError as e: logging.error('The payload is not in JSON format: %s', e) return HttpResponseBadRequest('Invalid payload format') server_url = get_server_url(request=request) review_request_id_to_commits = \ BitbucketHookViews._get_review_request_id_to_commits_map( payload, server_url, repository) if review_request_id_to_commits: close_all_review_requests(review_request_id_to_commits, local_site_name, repository, hosting_service_id) return HttpResponse()
def post_receive_hook_close_submitted(request, local_site_name=None, repository_id=None, hosting_service_id=None, hooks_uuid=None): """Close review requests as submitted automatically after a push. Args: request (django.http.HttpRequest): The request from the Bitbucket webhook. local_site_name (unicode): The local site name, if available. repository_id (int): The pk of the repository, if available. hosting_service_id (unicode): The name of the hosting service. hooks_uuid (unicode): The UUID of the configured webhook. Returns: django.http.HttpResponse: A response for the request. """ repository = get_repository_for_hook(repository_id, hosting_service_id, local_site_name, hooks_uuid) if 'payload' not in request.POST: return HttpResponseBadRequest('Missing payload') try: payload = json.loads(request.POST['payload']) except ValueError as e: logging.error('The payload is not in JSON format: %s', e) return HttpResponseBadRequest('Invalid payload format') server_url = get_server_url(request=request) review_request_id_to_commits = \ BitbucketHookViews._get_review_request_id_to_commits_map( payload, server_url, repository) if review_request_id_to_commits: close_all_review_requests(review_request_id_to_commits, local_site_name, repository, hosting_service_id) return HttpResponse()
def post_receive_hook_close_submitted(request, local_site_name=None, repository_id=None, hosting_service_id=None): """Closes review requests as submitted automatically after a push.""" hook_event = request.META.get('HTTP_X_GITHUB_EVENT') if hook_event == 'ping': # GitHub is checking that this hook is valid, so accept the request # and return. return HttpResponse() elif hook_event != 'push': return HttpResponseBadRequest( 'Only "ping" and "push" events are supported.') repository = get_repository_for_hook(repository_id, hosting_service_id, local_site_name) # Validate the hook against the stored UUID. m = hmac.new(bytes(repository.get_or_create_hooks_uuid()), request.body, hashlib.sha1) sig_parts = request.META.get('HTTP_X_HUB_SIGNATURE').split('=') if sig_parts[0] != 'sha1' or len(sig_parts) != 2: # We don't know what this is. return HttpResponseBadRequest('Unsupported HTTP_X_HUB_SIGNATURE') if m.hexdigest() != sig_parts[1]: return HttpResponseBadRequest('Bad signature.') try: payload = json.loads(request.body) except ValueError as e: logging.error('The payload is not in JSON format: %s', e) return HttpResponseBadRequest('Invalid payload format') server_url = get_server_url(request=request) review_request_id_to_commits = \ _get_review_request_id_to_commits_map(payload, server_url) if review_request_id_to_commits: close_all_review_requests(review_request_id_to_commits, local_site_name, repository, hosting_service_id) return HttpResponse()
def post_receive_hook_close_submitted(request, local_site_name=None, repository_id=None, hosting_service_id=None): """Closes review requests as submitted automatically after a push.""" hook_event = request.META.get('HTTP_X_GITHUB_EVENT') if hook_event == 'ping': # GitHub is checking that this hook is valid, so accept the request # and return. return HttpResponse() elif hook_event != 'push': return HttpResponseBadRequest( 'Only "ping" and "push" events are supported.') repository = get_repository_for_hook(repository_id, hosting_service_id, local_site_name) # Validate the hook against the stored UUID. m = hmac.new(bytes(repository.get_or_create_hooks_uuid()), request.body, hashlib.sha1) sig_parts = request.META.get('HTTP_X_HUB_SIGNATURE').split('=') if sig_parts[0] != 'sha1' or len(sig_parts) != 2: # We don't know what this is. return HttpResponseBadRequest('Unsupported HTTP_X_HUB_SIGNATURE') if m.hexdigest() != sig_parts[1]: return HttpResponseBadRequest('Bad signature.') try: payload = json.loads(request.body) except ValueError as e: logging.error('The payload is not in JSON format: %s', e) return HttpResponseBadRequest('Invalid payload format') server_url = get_server_url(request=request) review_request_id_to_commits = \ _get_review_request_id_to_commits_map(payload, server_url, repository) if review_request_id_to_commits: close_all_review_requests(review_request_id_to_commits, local_site_name, repository, hosting_service_id) return HttpResponse()
def hook_close_submitted(request, local_site_name=None, repository_id=None, hosting_service_id=None): """Close review requests as submitted after a push. Args: request (django.http.HttpRequest): The request from the RB Gateway webhook. local_site_name (unicode, optional): The local site name, if available. repository_id (int, optional): The ID of the repository, if available. hosting_service_id (unicode, optional): The ID of the hosting service. Returns: django.http.HttpResponse; A response for the request. """ hook_event = request.META.get('HTTP_X_RBG_EVENT') if hook_event == 'ping': return HttpResponse() elif hook_event != 'push': return HttpResponseBadRequest( 'Only "ping" and "push" events are supported.') repository = get_repository_for_hook(repository_id, hosting_service_id, local_site_name) sig = request.META.get('HTTP_X_RBG_SIGNATURE', '') m = hmac.new(repository.get_or_create_hooks_uuid().encode('utf-8'), request.body, hashlib.sha1) if not hmac.compare_digest(m.hexdigest(), sig): return HttpResponseBadRequest('Bad signature.') try: payload = json.loads(request.body.decode('utf-8')) except ValueError as e: logging.error('The payload is not in JSON format: %s', e) return HttpResponseBadRequest('Invalid payload format.') if 'commits' not in payload: return HttpResponseBadRequest('Invalid payload; expected "commits".') server_url = get_server_url(request=request) review_request_ids_to_commits = defaultdict(list) for commit in payload['commits']: commit_id = commit.get('id') commit_message = commit.get('message') review_request_id = get_review_request_id(commit_message, server_url, commit_id, repository) targets = commit['target'] if 'tags' in targets and targets['tags']: target = targets['tags'][0] elif 'bookmarks' in targets and targets['bookmarks']: target = targets['bookmarks'][0] elif 'branch' in targets: target = targets['branch'] else: target = '' if target: target_str = '%s (%s)' % (target, commit_id[:7]) else: target_str = commit_id[:7] review_request_ids_to_commits[review_request_id].append(target_str) if review_request_ids_to_commits: close_all_review_requests(review_request_ids_to_commits, local_site_name, repository, hosting_service_id) return HttpResponse()
def post_receive_hook_close_submitted(request, local_site_name=None, repository_id=None, hosting_service_id=None): """Close review requests as submitted automatically after a push. Args: request (django.http.HttpRequest): The request from the Bitbucket webhook. local_site_name (unicode): The local site name, if available. repository_id (int): The pk of the repository, if available. hosting_service_id (unicode): The name of the hosting service. Returns: django.http.HttpResponse: A response for the request. """ hook_event = request.META.get('HTTP_X_GITHUB_EVENT') if hook_event == 'ping': # GitHub is checking that this hook is valid, so accept the request # and return. return HttpResponse() elif hook_event != 'push': return HttpResponseBadRequest( 'Only "ping" and "push" events are supported.') repository = get_repository_for_hook(repository_id, hosting_service_id, local_site_name) # Validate the hook against the stored UUID. m = hmac.new(repository.get_or_create_hooks_uuid().encode('utf-8'), request.body, hashlib.sha1) sig_parts = request.META.get('HTTP_X_HUB_SIGNATURE').split('=') if sig_parts[0] != 'sha1' or len(sig_parts) != 2: # We don't know what this is. return HttpResponseBadRequest('Unsupported HTTP_X_HUB_SIGNATURE') if m.hexdigest() != sig_parts[1]: return HttpResponseBadRequest('Bad signature.') try: payload = json.loads(request.body.decode('utf-8')) except ValueError as e: logger.error('The payload is not in JSON format: %s', e) return HttpResponseBadRequest('Invalid payload format') server_url = get_server_url(request=request) review_request_id_to_commits = \ GitHubHookViews._get_review_request_id_to_commits_map( payload, server_url, repository) if review_request_id_to_commits: close_all_review_requests(review_request_id_to_commits, local_site_name, repository, hosting_service_id) return HttpResponse()
def hook_close_submitted(request, local_site_name=None, repository_id=None, hosting_service_id=None): """Close review requests as submitted after a push. Args: request (django.http.HttpRequest): The request from the RB Gateway webhook. local_site_name (unicode, optional): The local site name, if available. repository_id (int, optional): The ID of the repository, if available. hosting_service_id (unicode, optional): The ID of the hosting service. Returns: django.http.HttpResponse; A response for the request. """ hook_event = request.META.get('HTTP_X_RBG_EVENT') if hook_event == 'ping': return HttpResponse() elif hook_event != 'push': return HttpResponseBadRequest( 'Only "ping" and "push" events are supported.') repository = get_repository_for_hook(repository_id, hosting_service_id, local_site_name) sig = request.META.get('HTTP_X_RBG_SIGNATURE', '') m = hmac.new(repository.get_or_create_hooks_uuid().encode('utf-8'), request.body, hashlib.sha1) if not hmac.compare_digest(m.hexdigest(), sig): return HttpResponseBadRequest('Bad signature.') try: payload = json.loads(request.body) except ValueError as e: logging.error('The payload is not in JSON format: %s', e) return HttpResponseBadRequest('Invalid payload format.') if 'commits' not in payload: return HttpResponseBadRequest('Invalid payload; expected "commits".') server_url = get_server_url(request=request) review_request_ids_to_commits = defaultdict(list) for commit in payload['commits']: commit_id = commit.get('id') commit_message = commit.get('message') review_request_id = get_review_request_id( commit_message, server_url, commit_id, repository) targets = commit['target'] if 'tags' in targets and targets['tags']: target = targets['tags'][0] elif 'bookmarks' in targets and targets['bookmarks']: target = targets['bookmarks'][0] elif 'branch' in targets: target = targets['branch'] else: target = '' if target: target_str = '%s (%s)' % (target, commit_id[:7]) else: target_str = commit_id[:7] review_request_ids_to_commits[review_request_id].append(target_str) if review_request_ids_to_commits: close_all_review_requests(review_request_ids_to_commits, local_site_name, repository, hosting_service_id) return HttpResponse()