def get(self, request, provider=None): social_token = get_integration_token(request.user, provider, task=request.GET.get('task')) if not social_token: return Response({'status': 'Unauthorized'}, status.HTTP_401_UNAUTHORIZED) if provider == GitHubProvider.id: r = github.api(endpoint='/user/issues', method='get', params={'filter': 'all'}, access_token=social_token.token) if r.status_code == 200: issues = [] for issue in r.json(): if 'pull_request' in issue: continue # Github returns both issues and pull requests from this endpoint issue_info = {} for key in github.ISSUE_FIELDS: if key == 'repository': issue_info[key] = github.extract_repo_info( issue[key]) else: issue_info[key] = issue[key] issues.append(issue_info) return Response(issues) return Response(r.json(), r.status_code) return Response({'status': 'Not implemented'}, status.HTTP_501_NOT_IMPLEMENTED)
def get(self, request, provider=None): social_token = get_integration_token(request.user, provider, task=request.GET.get('task')) if not social_token: return Response({'status': 'Unauthorized'}, status.HTTP_401_UNAUTHORIZED) if provider == GitHubProvider.id: r = github.api(endpoint='/user/repos', method='get', access_token=social_token.token) if r.status_code == 200: repos = [github.extract_repo_info(repo) for repo in r.json()] return Response(repos) return Response(r.json(), r.status_code) return Response({'status': 'Not implemented'}, status.HTTP_501_NOT_IMPLEMENTED)
def get(self, request, provider=None): social_token = get_social_token(user=request.user, provider=provider) if not social_token: return Response({'status': 'Unauthorized'}, status.HTTP_401_UNAUTHORIZED) if provider == 'github': r = github.api(endpoint='/user/issues', method='get', params={'filter': 'all'}, access_token=social_token.token) if r.status_code == 200: issues = [] for issue in r.json(): if 'pull_request' in issue: continue # Github returns both issues and pull requests from this endpoint issue_info = {} for key in ISSUE_FIELDS: if key == 'repository': issue_info[key] = extract_repo_info(issue[key]) else: issue_info[key] = issue[key] issues.append(issue_info) return Response(issues) return Response(r.json(), r.status_code) return Response({'status': 'Not implemented'}, status.HTTP_501_NOT_IMPLEMENTED)
def integration(self, request, pk=None, provider=None): """ Manage Task Integrations --- serializer: IntegrationSerializer omit_parameters: - query """ get_object_or_404(self.queryset, pk=pk) queryset = Integration.objects.filter(task_id=pk, provider=provider) if request.method == 'GET': instance = get_object_or_404(queryset) self.check_object_permissions(request, instance) serializer = self.get_serializer(instance, context={'request': request}) return Response(serializer.data) elif request.method == 'POST': request_data = dict(request.data) request_data['provider'] = provider request_data['task'] = pk try: instance = queryset.latest('created_at') except Integration.DoesNotExist: instance = None secret = get_random_string() if instance: self.check_object_permissions(request, instance) secret = instance.secret or secret else: self.check_permissions(request) serializer = self.get_serializer(instance, data=request_data, context={'request': request}) serializer.is_valid(raise_exception=True) data = { 'name': 'web', 'config': { 'url': '%s://%s/task/%s/hook/%s/' % (request.scheme, request.get_host(), pk, provider), 'content_type': 'json', 'secret': secret }, 'events': github.transform_to_github_events(request_data['events']), 'active': True } repo_full_name = None repo = request_data.get('repo', None) if repo: repo_full_name = repo.get('full_name', None) if not repo_full_name and instance: repo_full_name = instance.repo_full_name if not repo_full_name: return Response({'status': 'Bad Request'}, status.HTTP_400_BAD_REQUEST) web_hook_endpoint = '/repos/%s/hooks' % repo_full_name hook_method = 'post' if instance and instance.hook_id: web_hook_endpoint += '/%s' % instance.hook_id hook_method = 'patch' social_token = get_social_token(user=request.user, provider=provider) if not social_token: return Response({'status': 'Unauthorized'}, status.HTTP_401_UNAUTHORIZED) r = github.api(endpoint=web_hook_endpoint, method=hook_method, data=data, access_token=social_token.token) if r.status_code in [200, 201]: hook = r.json() integration = serializer.save(secret=secret) if 'id' in hook: IntegrationMeta.objects.update_or_create( integration=integration, meta_key='hook_id', defaults={'meta_value': hook['id']}) return Response(serializer.data) return Response(r.json(), r.status_code) else: return Response({'status': 'Method not allowed'}, status.HTTP_405_METHOD_NOT_ALLOWED)
def integration(self, request, pk=None, provider=None): """ Manage Task Integrations --- serializer: IntegrationSerializer omit_parameters: - query """ get_object_or_404(self.queryset, pk=pk) queryset = Integration.objects.filter(task_id=pk, provider=provider) if request.method == 'GET': instance = get_object_or_404(queryset) self.check_object_permissions(request, instance) serializer = self.get_serializer(instance, context={'request': request}) return Response(serializer.data) elif request.method == 'POST': request_data = dict(request.data) request_data['provider'] = provider request_data['task'] = pk try: instance = queryset.latest('created_at') except Integration.DoesNotExist: instance = None secret = get_random_string() if instance: self.check_object_permissions(request, instance) secret = instance.secret or secret else: self.check_permissions(request) serializer = self.get_serializer(instance, data=request_data, context={'request': request}) serializer.is_valid(raise_exception=True) data = { 'name': 'web', 'config': { 'url': '%s://%s/task/%s/hook/%s/' % (request.scheme, request.get_host(), pk, provider), 'content_type': 'json', 'secret': secret }, 'events': github.transform_to_github_events(request_data['events']), 'active': True } repo_full_name = None repo = request_data.get('repo', None) if repo: repo_full_name = repo.get('full_name', None) if not repo_full_name and instance: repo_full_name = instance.repo_full_name if not repo_full_name: return Response({'status': 'Bad Request'}, status.HTTP_400_BAD_REQUEST) web_hook_endpoint = '/repos/%s/hooks' % repo_full_name hook_method = 'post' if instance and instance.hook_id: web_hook_endpoint += '/%s' % instance.hook_id hook_method = 'patch' social_token = get_social_token(user=request.user, provider=provider) if not social_token: return Response({'status': 'Unauthorized'}, status.HTTP_401_UNAUTHORIZED) r = github.api(endpoint=web_hook_endpoint, method=hook_method, data=data, access_token=social_token.token) if r.status_code in [200, 201]: hook = r.json() integration = serializer.save(secret=secret) if 'id' in hook: IntegrationMeta.objects.update_or_create( integration=integration, meta_key='hook_id', defaults={'meta_value': hook['id']} ) return Response(serializer.data) return Response(r.json(), r.status_code) else: return Response({'status': 'Method not allowed'}, status.HTTP_405_METHOD_NOT_ALLOWED)