2
2
3
3
from django .db import models
4
4
5
+ from readthedocs .allauth .providers .githubapp .provider import GitHubAppProvider
5
6
from readthedocs .core .querysets import NoReprQuerySet
7
+ from readthedocs .oauth .constants import GITHUB
6
8
from readthedocs .oauth .constants import GITHUB_APP
7
9
8
10
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
+
9
19
class RelatedUserQuerySet (NoReprQuerySet , models .QuerySet ):
10
20
"""For models with relations through :py:class:`User`."""
11
21
@@ -14,9 +24,19 @@ def api(self, user=None):
14
24
if not user .is_authenticated :
15
25
return self .none ()
16
26
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
+
20
40
return queryset
21
41
22
42
def api_v2 (self , * args , ** kwargs ):
@@ -37,9 +57,19 @@ def for_project_linking(self, user):
37
57
remote_repository_relations__user = user ,
38
58
remote_repository_relations__admin = True ,
39
59
)
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
+
43
73
return queryset .distinct ()
44
74
45
75
0 commit comments