Skip to content

Commit ad68360

Browse files
committed
Remote repository: Keep in sync when repos are moved to another org/user.
We were doing some checks to avoid removing the remote organization from a remote repo when this is listed from the /users/repos/ endpoint or changing the organization when the project was moved. But, since we are hitting the GitHub API directly, we can always trust the result from there, which includes the type of owner of the repository, we can use that to decide if the repository belongs to an organization or not. Fixes #8715
1 parent eaef4b7 commit ad68360

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

readthedocs/oauth/services/github.py

+16-26
Original file line numberDiff line numberDiff line change
@@ -103,34 +103,19 @@ def create_repository(self, fields, privacy=None, organization=None):
103103
remote_id=fields['id'],
104104
vcs_provider=self.vcs_provider_slug
105105
)
106-
remote_repository_relation = repo.get_remote_repository_relation(
107-
self.user, self.account
108-
)
109106

110-
# It's possible that a user has access to a repository from within
111-
# an organization without being member of that organization
112-
# (external contributor). In this case, the repository will be
113-
# listed under the ``/repos`` endpoint but not under ``/orgs``
114-
# endpoint. Then, when calling this method (``create_repository``)
115-
# we will have ``organization=None`` but we don't have to skip the
116-
# creation of the ``RemoteRepositoryRelation``.
117-
if repo.organization and organization and repo.organization != organization:
118-
log.debug(
119-
'Not importing repository because mismatched orgs.',
120-
repository=fields['name'],
121-
)
122-
return None
123-
124-
if any([
125-
# There is an organization associated with this repository:
126-
# attach the organization to the repository
127-
organization is not None,
128-
# There is no organization and the repository belongs to a
129-
# user: removes the organization linked to the repository
130-
not organization and fields['owner']['type'] == 'User',
131-
]):
107+
owner_type = fields["owner"]["type"]
108+
109+
# If there is an organization associated with this repository,
110+
# attach the organization to the repository.
111+
if organization and owner_type == "Organization":
132112
repo.organization = organization
133113

114+
# If there is no organization and the repository belongs to a user,
115+
# remove the organization linked to the repository.
116+
if not organization and owner_type == "User":
117+
repo.organization = None
118+
134119
repo.name = fields['name']
135120
repo.full_name = fields['full_name']
136121
repo.description = fields['description']
@@ -151,7 +136,12 @@ def create_repository(self, fields, privacy=None, organization=None):
151136

152137
repo.save()
153138

154-
remote_repository_relation.admin = fields.get('permissions', {}).get('admin', False)
139+
remote_repository_relation = repo.get_remote_repository_relation(
140+
self.user, self.account
141+
)
142+
remote_repository_relation.admin = fields.get("permissions", {}).get(
143+
"admin", False
144+
)
155145
remote_repository_relation.save()
156146

157147
return repo

0 commit comments

Comments
 (0)