Skip to content

Use RemoteRepository releation to match already imported projects #8024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions readthedocs/oauth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,16 @@ class Meta:
def __str__(self):
return 'Remote repository: {}'.format(self.html_url)

@property
def clone_fuzzy_url(self):
"""Try to match against several permutations of project URL."""

def matches(self, user):
"""Projects that exist with repository URL already."""
# Support Git scheme GitHub url format that may exist in database
truncated_url = self.clone_url.replace('.git', '')
http_url = self.clone_url.replace('git://', 'https://').replace('.git', '')
"""Existing projects connected to this RemoteRepository."""

# TODO: remove this method and refactor the API response in ``/api/v2/repos/``
# (or v3) to just return the linked Project (slug, url) if the ``RemoteRepository``
# connection exists. Note the frontend needs to be simplified as well in
# ``import.js`` and ``project_import.html``.

projects = Project.objects.public(user).filter(
Q(repo=self.clone_url) |
Q(repo=truncated_url) |
Q(repo=truncated_url + '.git') |
Q(repo=http_url) |
Q(repo=http_url + '.git')
remote_repository=self,
).values('slug')
Comment on lines 211 to 213
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use self.project.slug here as it's an OneToOne relationship and it will reduce the number of queries if we use select_related() in the RemoteRepository Queryset?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think you are right! We will need to check for user permissions on that project, tho. Note that we are using .public queryset in the query here.

Can you write an example? I'm confused 😞

Also, I think eventually we will want to allow multiple projects connected to the same RemoteRepository (because of translations)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At First, I thought we could do something similar to this

return [{
    'id': self.project.slug,
    'url': reverse(
        'projects_detail',
        kwargs={
            'project_slug': self.project.slug,
        },
    ),
}] if self.project else []

But as you mentioned there are some permissions related here with .public() :(


return [{
Expand Down