def post(self, request, *args, **kwargs): self._verify_request(request) github_user, github_repo = self._get_repo_info(request) owner = self._get_owner(github_user) payload = json.loads(request.POST['payload']) request_branch = payload['branch'] travis_status_url = TRAVIS_STATUS_URL.format(user=github_user, repo=github_repo, branch=request_branch) committed_at = payload.get('committed_at') if committed_at: committed_at = parse_datetime(committed_at) else: committed_at = timezone.now() try: provider_ns = models.ProviderNamespace.objects.get( provider__name=constants.PROVIDER_GITHUB, name=github_user, ) except models.ProviderNamespace.DoesNotExist: return ValidationError( 'Prodiver namespace "{}" not found'.format(github_user)) notification = models.Notification( owner=owner, source='travis', github_branch=request_branch, travis_build_url=payload.get('build_url'), travis_status=payload.get('status_message'), commit_message=payload.get('message'), committed_at=committed_at, commit=payload['commit']) repository, _ = models.Repository.objects.get_or_create( provider_namespace=provider_ns, original_name=github_repo, defaults={ 'name': github_repo, 'is_enabled': False, 'travis_status_url': travis_status_url, 'travis_build_url': payload.get('build_url'), }) notification.repository = repository task = tasks.create_import_task( repository, owner, travis_status_url=travis_status_url, travis_build_url=payload.get('build_url')) notification.import_task = task notification.save() serializer = self.get_serializer(instance=notification) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def post(self, request, *args, **kwargs): self._verify_request(request) github_user, github_repo = self._get_repo_info(request) owner = self._get_owner(github_user) payload = json.loads(request.POST['payload']) request_branch = payload['branch'] author_branch = request.query_params.get('branch', None) is_author_prevent_import = (author_branch and author_branch != request_branch and not payload.get('tag')) if is_author_prevent_import: msg = ('Travis request_branch does not match branch author set ' 'in .travis.yml notification webhook url. ' 'Will not import.') logger.info(msg) raise APIException(msg) travis_url = urlparse.urlparse(payload.get('build_url')) travis_status_url = TRAVIS_STATUS_URL.format( protocol=travis_url.scheme, url=travis_url.netloc, user=github_user, repo=github_repo, branch=request_branch) committed_at = payload.get('committed_at') if committed_at: committed_at = parse_datetime(committed_at) else: committed_at = timezone.now() try: provider_ns = models.ProviderNamespace.objects.get( provider__name=constants.PROVIDER_GITHUB, name__iexact=github_user, ) except models.ProviderNamespace.DoesNotExist: raise ValidationError( 'Prodiver namespace "{}" not found'.format(github_user)) notification = models.Notification( owner=owner, source='travis', github_branch=request_branch, travis_build_url=payload.get('build_url'), travis_status=payload.get('status_message'), commit_message=payload.get('message'), committed_at=committed_at, commit=payload['commit']) repository, _ = models.Repository.objects.get_or_create( provider_namespace=provider_ns, original_name=github_repo, defaults={ 'name': github_repo, 'is_enabled': False, 'travis_status_url': travis_status_url, 'travis_build_url': payload.get('build_url'), }) notification.repository = repository is_branch_mismatch = ( request_branch != repository.import_branch and repository.import_branch and # repo is not new not payload.get('tag') # not a tag push ) if is_branch_mismatch: msg = ('Travis request_branch does not match repo import_branch. ' 'Will not import.') logger.info(msg) raise APIException(msg) task = tasks.create_import_task( repository, owner, travis_status_url=travis_status_url, travis_build_url=payload.get('build_url')) notification.import_task = task notification.save() serializer = self.get_serializer(instance=notification) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)