From c66336da9d2114d025e9bbe84c55a2eba5f66c3c Mon Sep 17 00:00:00 2001 From: Maksudul Haque Date: Tue, 15 Dec 2020 21:29:21 +0600 Subject: [PATCH 1/3] Set the right permissions on GitLab OAuth RemoteRepository --- readthedocs/oauth/services/gitlab.py | 34 ++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/readthedocs/oauth/services/gitlab.py b/readthedocs/oauth/services/gitlab.py index ba3a2ca2fdf..5d73fa06d55 100644 --- a/readthedocs/oauth/services/gitlab.py +++ b/readthedocs/oauth/services/gitlab.py @@ -122,11 +122,35 @@ def sync_organizations(self): remote_organizations.append(remote_organization) for repo in org_repos: - remote_repository = self.create_repository( - repo, - organization=remote_organization, - ) - remote_repositories.append(remote_repository) + try: + # Get response for a single repository with permission data + resp = self.get_session().get( + '{url}/api/v4/projects/{id}'.format( + url=self.adapter.provider_base_url, + id=repo['id'] + ) + ) + + if resp.status_code == 200: + repo_details = resp.json() + remote_repository = self.create_repository( + repo_details, + organization=remote_organization + ) + remote_repositories.append(remote_repository) + else: + log.warning( + 'GitLab project does not exist or user does not have ' + 'permissions: project=%s', + repo['name_with_namespace'], + ) + + except Exception: + log.exception( + 'Error creating GitLab repository=%s', + repo['name_with_namespace'], + ) + except (TypeError, ValueError): log.warning('Error syncing GitLab organizations') raise SyncServiceError( From 2ffe6f3a4277a35531a2fca48f140fd2df88fb42 Mon Sep 17 00:00:00 2001 From: Maksudul Haque Date: Tue, 15 Dec 2020 22:17:39 +0600 Subject: [PATCH 2/3] Update comment --- readthedocs/oauth/services/gitlab.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/readthedocs/oauth/services/gitlab.py b/readthedocs/oauth/services/gitlab.py index 5d73fa06d55..a3481fa0ab4 100644 --- a/readthedocs/oauth/services/gitlab.py +++ b/readthedocs/oauth/services/gitlab.py @@ -122,8 +122,13 @@ def sync_organizations(self): remote_organizations.append(remote_organization) for repo in org_repos: + # TODO: Optimize this so that we don't re-fetch project data + # Details: https://github.com/readthedocs/readthedocs.org/issues/7743 try: - # Get response for a single repository with permission data + # The response from /groups/{id}/projects API does not contain + # admin permission fields for GitLab projects. + # So, fetch every single project data from the API + # which contains with admin permission fields resp = self.get_session().get( '{url}/api/v4/projects/{id}'.format( url=self.adapter.provider_base_url, From 5c4773840253af7c8ba55a660ac97cf6c5d0dd62 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 16 Dec 2020 12:14:23 -0500 Subject: [PATCH 3/3] Update readthedocs/oauth/services/gitlab.py --- readthedocs/oauth/services/gitlab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readthedocs/oauth/services/gitlab.py b/readthedocs/oauth/services/gitlab.py index a3481fa0ab4..68a0b42953c 100644 --- a/readthedocs/oauth/services/gitlab.py +++ b/readthedocs/oauth/services/gitlab.py @@ -128,7 +128,7 @@ def sync_organizations(self): # The response from /groups/{id}/projects API does not contain # admin permission fields for GitLab projects. # So, fetch every single project data from the API - # which contains with admin permission fields + # which contains the admin permission fields. resp = self.get_session().get( '{url}/api/v4/projects/{id}'.format( url=self.adapter.provider_base_url,