Esempio n. 1
0
 def repository_exists(owner_name, project_name, credentials):
     endpoint = f'repos/{owner_name}/{project_name}'
     token = credentials.extra_data['access_token']
     response = github_request(endpoint, 'get', token, {})
     if response.status_code == status.HTTP_200_OK:
         return True
     return False
Esempio n. 2
0
def auth_user_github_data(request, *args, **kwargs):
    credentials = request.user.github_credentials()
    if credentials:
        token = credentials.extra_data['access_token']
        response = github_request('user', 'get', token, {})
        if response.status_code == status.HTTP_200_OK:
            github_data = response.json()
            data = {
                'avatar': github_data['avatar_url'],
                'login': github_data['login']
            }
            serializer = GitHubUserSerializer(data=data)
            serializer.is_valid(raise_exception=True)
            return Response(data=serializer.data)
    raise NotFound(detail='O usuário não possui credenciais do GitHub.')
Esempio n. 3
0
    def _get_past_month_commits(self, data):
        user = self.request.user
        repo = self.get_object()

        params = {}
        since = datetime.datetime.utcnow() - datetime.timedelta(
            days=data['days'])
        params['since'] = since.isoformat()

        credentials = user.github_credentials()
        token = credentials.extra_data['access_token']
        # repo.name already contains {user_name}/{project_name}
        endpoint = f'repos/{repo.name}/commits'
        response = github_request(endpoint, 'get', token, params)
        # If the response is anything different from OK, return an empty array
        if response.status_code != status.HTTP_200_OK:
            return []
        return response.json()
Esempio n. 4
0
 def add_webhook(self, webhook_url):
     if not self.has_webhook:
         credentials = self.owner.github_credentials()
         if credentials:
             token = credentials.extra_data['access_token']
             repo_name = self.name
             webhook_data = {
                 'config': {
                     'url': webhook_url,
                     'content_type': 'json'
                 }
             }
             # repo.name already contains {user_name}/{project_name}
             endpoint = f'repos/{repo_name}/hooks'
             response = github_request(endpoint, 'post', token,
                                       webhook_data)
             if response.status_code == status.HTTP_201_CREATED:
                 webhook = response.json()
                 self.webhook_id = webhook['id']
                 self.save()
                 return True
     return False
Esempio n. 5
0
 def test_not_supported_method_raises_exception(self):
     with self.assertRaisesMessage(
             Exception, 'Não foi possível identificar o método HTTP.'):
         github_request('https://somestuff.com/api/', 'clearlywrong',
                        '4yy9u40442e', {})