Skip to content

Commit dc7bed2

Browse files
committed
Use RemoteRepository releation to match already imported projects
Currently production code consider a project already imported if the `Project.repo` fuzzy matches `RemoteRepository.clone_url` even if that `Project` is not linked to any `RemoteRepository`. This allow anyone to import a repository manually using the real GitHub URL to "take over" that repository and avoiding the real owner of that repository to import it from "Import Project" page because it says: This repository has already been imported and linking to the project that someone's else has imported. With the changes on this PR, we are showing that message only if the `Project` is connected to a `RemoteRepository` which is only possible to be done by owners of the GitHub repository.
1 parent afcc41f commit dc7bed2

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

readthedocs/oauth/models.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,15 @@ class Meta:
200200
def __str__(self):
201201
return 'Remote repository: {}'.format(self.html_url)
202202

203-
@property
204-
def clone_fuzzy_url(self):
205-
"""Try to match against several permutations of project URL."""
206-
207203
def matches(self, user):
208-
"""Projects that exist with repository URL already."""
209-
# Support Git scheme GitHub url format that may exist in database
210-
truncated_url = self.clone_url.replace('.git', '')
211-
http_url = self.clone_url.replace('git://', 'https://').replace('.git', '')
204+
"""Existing projects connected to this RemoteRepository"""
205+
206+
# TODO: remove this method and refactor the API response in ``/api/v2/repos/`` (or v3)
207+
# to just return the linked Project (slug, url) if the ``RemoteRepository`` connection exists.
208+
# Note the frontend needs to be simplified as well in ``import.js`` and ``project_import.html``.
212209

213210
projects = Project.objects.public(user).filter(
214-
Q(repo=self.clone_url) |
215-
Q(repo=truncated_url) |
216-
Q(repo=truncated_url + '.git') |
217-
Q(repo=http_url) |
218-
Q(repo=http_url + '.git')
211+
remote_repository=self,
219212
).values('slug')
220213

221214
return [{

0 commit comments

Comments
 (0)