def project_import_github(request, sync=False): """ Integrate with GitHub to pull repos from there. """ repo_type = getattr(settings, 'GITHUB_PRIVACY', 'public') tokens = SocialToken.objects.filter( account__user__username=request.user.username, app__provider='github') github_connected = False if tokens.exists(): github_connected = True if sync: repos = [] token = tokens[0] session = OAuth2Session( client_id=token.app.client_id, token={ 'access_token': str(token.token), 'token_type': 'bearer' } ) # Get user repos owner_resp = github_paginate(session, 'https://api.github.com/user/repos?per_page=100') for repo in owner_resp.json(): log.info('Trying %s' % repo['full_name']) oauth_utils.make_github_project(user=request.user, org=None, privacy=repo_type, repo_json=repo) # Get org repos resp = session.get('https://api.github.com/user/orgs') for org_json in resp.json(): org_resp = github_paginate(session, 'https://api.github.com/orgs/%s' % org_json['login']) org_obj = oauth_utils.make_github_organization(user=request.user, org_json=org_resp.json()) # Add repos org_repos_resp = github_paginate(session, 'https://api.github.com/orgs/%s/repos?type=%s' % (org_json['login'], repo_type)) for repo in org_repos_resp.json(): oauth_utils.make_github_project(user=request.user, org=org_obj, privacy=repo_type, repo_json=repo) repos = GithubProject.objects.filter(users__in=[request.user]) for repo in repos: ghetto_repo = repo.git_url.replace('git://', '').replace('.git', '') projects = Project.objects.filter(repo__endswith=ghetto_repo) | Project.objects.filter(repo__endswith=ghetto_repo + '.git') if projects: repo.matches = [project.slug for project in projects] else: repo.matches = [] return render_to_response( 'projects/project_import_github.html', { 'repos': repos, 'github_connected': github_connected, 'sync': sync, }, context_instance=RequestContext(request) )
def test_make_github_project_fail(self): repo_json = { "name": "", "full_name": "", "description": "", "git_url": "", "private": True, "ssh_url": "", "html_url": "", } github_project = make_github_project(user=self.user, org=self.org, privacy=self.privacy, repo_json=repo_json) self.assertIsNone(github_project)
def test_make_github_project_pass(self): repo_json = { "name": "", "full_name": "", "description": "", "git_url": "", "private": False, "ssh_url": "", "html_url": "", } github_project = make_github_project(user=self.user, org=self.org, privacy=self.privacy, repo_json=repo_json) self.assertIsInstance(github_project, GithubProject)
def test_multiple_users_same_repo(self): repo_json = { "name": "", "full_name": "testrepo/multiple", "description": "", "git_url": "", "private": False, "ssh_url": "", "html_url": "", } github_project = make_github_project(user=self.user, org=self.org, privacy=self.privacy, repo_json=repo_json) github_project_2 = make_github_project(user=User.objects.get(pk=2), org=self.org, privacy=self.privacy, repo_json=repo_json) self.assertIsInstance(github_project, GithubProject) self.assertIsInstance(github_project_2, GithubProject) self.assertNotEqual(github_project_2, github_project) github_project_3 = make_github_project(user=self.user, org=self.org, privacy=self.privacy, repo_json=repo_json) github_project_4 = make_github_project(user=User.objects.get(pk=2), org=self.org, privacy=self.privacy, repo_json=repo_json) self.assertIsInstance(github_project_3, GithubProject) self.assertIsInstance(github_project_4, GithubProject) self.assertEqual(github_project, github_project_3) self.assertEqual(github_project_2, github_project_4) github_project_5 = make_github_project(user=self.user, org=self.org, privacy=self.privacy, repo_json=repo_json) github_project_6 = make_github_project(user=User.objects.get(pk=2), org=self.org, privacy=self.privacy, repo_json=repo_json) self.assertEqual(github_project, github_project_5) self.assertEqual(github_project_2, github_project_6)
def project_import_github(request, sync=False): """ Integrate with GitHub to pull repos from there. """ repo_type = getattr(settings, 'GITHUB_PRIVACY', 'public') tokens = SocialToken.objects.filter( account__user__username=request.user.username, app__provider='github') github_connected = False if tokens.exists(): github_connected = True if sync: repos = [] token = tokens[0] session = OAuth2Session(client_id=token.app.client_id, token={ 'access_token': str(token.token), 'token_type': 'bearer' }) # Get user repos owner_resp = github_paginate( session, 'https://api.github.com/user/repos?per_page=100') for repo in owner_resp.json(): log.info('Trying %s' % repo['full_name']) oauth_utils.make_github_project(user=request.user, org=None, privacy=repo_type, repo_json=repo) # Get org repos resp = session.get('https://api.github.com/user/orgs') for org_json in resp.json(): org_resp = github_paginate( session, 'https://api.github.com/orgs/%s' % org_json['login']) org_obj = oauth_utils.make_github_organization( user=request.user, org_json=org_resp.json()) # Add repos org_repos_resp = github_paginate( session, 'https://api.github.com/orgs/%s/repos?type=%s' % (org_json['login'], repo_type)) for repo in org_repos_resp.json(): oauth_utils.make_github_project(user=request.user, org=org_obj, privacy=repo_type, repo_json=repo) repos = GithubProject.objects.filter(users__in=[request.user]) for repo in repos: ghetto_repo = repo.git_url.replace('git://', '').replace('.git', '') projects = Project.objects.filter( repo__endswith=ghetto_repo) | Project.objects.filter( repo__endswith=ghetto_repo + '.git') if projects: repo.matches = [project.slug for project in projects] else: repo.matches = [] return render_to_response('projects/project_import_github.html', { 'repos': repos, 'github_connected': github_connected, 'sync': sync, }, context_instance=RequestContext(request))