Skip to content

Commit c955766

Browse files
authored
GitHub: show repos from old or new GH integration only (#12113)
This is a better solution other than always excluding repos from the old integration or the new one. This can be safely be deployed.
1 parent bfb15ca commit c955766

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

readthedocs/oauth/querysets.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
from django.db import models
44

5+
from readthedocs.allauth.providers.githubapp.provider import GitHubAppProvider
56
from readthedocs.core.querysets import NoReprQuerySet
7+
from readthedocs.oauth.constants import GITHUB
68
from readthedocs.oauth.constants import GITHUB_APP
79

810

11+
def _has_account_connected_to_github_app(user):
12+
if not user:
13+
return False
14+
return user.socialaccount_set.filter(
15+
provider=GitHubAppProvider.id,
16+
).exists()
17+
18+
919
class RelatedUserQuerySet(NoReprQuerySet, models.QuerySet):
1020
"""For models with relations through :py:class:`User`."""
1121

@@ -14,9 +24,19 @@ def api(self, user=None):
1424
if not user.is_authenticated:
1525
return self.none()
1626
queryset = self.filter(users=user)
17-
# TODO: Once we are migrated into GitHub App we should include these repositories/organizations.
18-
# Exclude repositories/organizations from the GitHub App for now to avoid duplicated entries.
19-
queryset = queryset.exclude(vcs_provider=GITHUB_APP)
27+
28+
# Exclude repositories/organizations from the old or new GitHub App to avoid duplicated entries.
29+
# If the user has already started using the GitHub App,
30+
# we shouldn't show repositories from the old GitHub integration.
31+
# Otherwise, we should show the repositories from the old GitHub integration only,
32+
# this is done to avoid confusion for users that haven't migrated their accounts yet,
33+
# but still have access to some repositories from the new GitHub App integration.
34+
using_github_app = _has_account_connected_to_github_app(user)
35+
if using_github_app and queryset.filter(vcs_provider=GITHUB_APP).exists():
36+
queryset = queryset.exclude(vcs_provider=GITHUB)
37+
else:
38+
queryset = queryset.exclude(vcs_provider=GITHUB_APP)
39+
2040
return queryset
2141

2242
def api_v2(self, *args, **kwargs):
@@ -37,9 +57,19 @@ def for_project_linking(self, user):
3757
remote_repository_relations__user=user,
3858
remote_repository_relations__admin=True,
3959
)
40-
# TODO: Once we are migrated into GitHub App we should include these repositories/organizations.
41-
# Exclude repositories/organizations from the GitHub App for now to avoid duplicated entries.
42-
queryset = queryset.exclude(vcs_provider=GITHUB_APP)
60+
61+
# Exclude repositories/organizations from the old or new GitHub App to avoid duplicated entries.
62+
# If the user has already started using the GitHub App,
63+
# we shouldn't show repositories from the old GitHub integration.
64+
# Otherwise, we should show the repositories from the old GitHub integration only,
65+
# this is done to avoid confusion for users that haven't migrated their accounts yet,
66+
# but still have access to some repositories from the new GitHub App integration.
67+
using_github_app = _has_account_connected_to_github_app(user)
68+
if using_github_app and queryset.filter(vcs_provider=GITHUB_APP).exists():
69+
queryset = queryset.exclude(vcs_provider=GITHUB)
70+
else:
71+
queryset = queryset.exclude(vcs_provider=GITHUB_APP)
72+
4373
return queryset.distinct()
4474

4575

0 commit comments

Comments
 (0)