Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit d987ef7

Browse files
Try new approach
1 parent 3763fb7 commit d987ef7

File tree

5 files changed

+37
-106
lines changed

5 files changed

+37
-106
lines changed

graphql_api/actions/repository.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33

44
import sentry_sdk
55
from django.db.models import QuerySet
6-
from shared.django_apps.codecov_auth.models import Owner
6+
from shared.django_apps.codecov_auth.models import GithubAppInstallation, Owner
77
from shared.django_apps.core.models import Repository
88

9+
from graphql_api.types.owner.owner import AI_FEATURES_GH_APP_ID
10+
911
log = logging.getLogger(__name__)
1012

1113

1214
def apply_filters_to_queryset(
13-
queryset: QuerySet, filters: dict[str, Any] | None
15+
queryset: QuerySet, filters: dict[str, Any] | None, owner: Owner
1416
) -> QuerySet:
1517
filters = filters or {}
1618
term = filters.get("term")
1719
active = filters.get("active")
1820
activated = filters.get("activated")
1921
repo_names = filters.get("repo_names")
2022
is_public = filters.get("is_public")
23+
ai_enabled = filters.get("ai_enabled")
2124

2225
if repo_names:
2326
queryset = queryset.filter(name__in=repo_names)
@@ -29,6 +32,25 @@ def apply_filters_to_queryset(
2932
queryset = queryset.filter(active=active)
3033
if is_public is not None:
3134
queryset = queryset.filter(private=not is_public)
35+
if is_public is not None:
36+
queryset = queryset.filter(private=not is_public)
37+
if ai_enabled is not None:
38+
queryset = filter_queryset_by_ai_enabled_repos(queryset, owner)
39+
return queryset
40+
41+
42+
def filter_queryset_by_ai_enabled_repos(queryset: QuerySet, owner: Owner) -> QuerySet:
43+
ai_features_app_install = GithubAppInstallation.objects.filter(
44+
app_id=AI_FEATURES_GH_APP_ID, owner=owner
45+
).first()
46+
47+
if not ai_features_app_install:
48+
return Repository.objects.none()
49+
50+
if ai_features_app_install.repository_service_ids:
51+
queryset = queryset.filter(
52+
service_id__in=ai_features_app_install.repository_service_ids
53+
)
3254

3355
return queryset
3456

@@ -43,14 +65,20 @@ def list_repository_for_owner(
4365
) -> QuerySet:
4466
queryset = Repository.objects.viewable_repos(current_owner)
4567

68+
ai_enabled_filter = filters.get("ai_enabled")
69+
70+
if ai_enabled_filter:
71+
return filter_queryset_by_ai_enabled_repos(queryset, owner)
72+
4673
if exclude_okta_enforced_repos:
4774
queryset = queryset.exclude_accounts_enforced_okta(okta_account_auths)
4875

49-
queryset = (
50-
queryset.with_recent_coverage().with_latest_commit_at().filter(author=owner)
51-
)
76+
if not ai_enabled_filter:
77+
queryset = (
78+
queryset.with_recent_coverage().with_latest_commit_at().filter(author=owner)
79+
)
5280

53-
queryset = apply_filters_to_queryset(queryset, filters)
81+
queryset = apply_filters_to_queryset(queryset, filters, owner)
5482
return queryset
5583

5684

graphql_api/tests/test_owner.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,65 +1194,6 @@ def test_fetch_available_plans_is_enterprise_plan(self):
11941194
}
11951195
}
11961196

1197-
@patch("services.self_hosted.get_config")
1198-
def test_ai_enabled_repositories(self, get_config_mock):
1199-
get_config_mock.return_value = [
1200-
{"service": "github", "ai_features_app_id": 12345},
1201-
]
1202-
1203-
ai_app_installation = GithubAppInstallation(
1204-
name="ai-features",
1205-
owner=self.owner,
1206-
repository_service_ids=[],
1207-
installation_id=12345,
1208-
)
1209-
1210-
ai_app_installation.save()
1211-
1212-
query = """{
1213-
owner(username: "%s") {
1214-
aiEnabledRepositories(first: 20) {
1215-
edges {
1216-
node {
1217-
name
1218-
}
1219-
}
1220-
}
1221-
}
1222-
}
1223-
1224-
""" % (self.owner.username)
1225-
data = self.gql_request(query, owner=self.owner)
1226-
repos = paginate_connection(data["owner"]["aiEnabledRepositories"])
1227-
assert repos == [{"name": "a"}, {"name": "b"}]
1228-
1229-
@patch("services.self_hosted.get_config")
1230-
def test_ai_enabled_repositories_app_not_configured(self, get_config_mock):
1231-
current_org = OwnerFactory(
1232-
username="random-plan-user",
1233-
service="github",
1234-
)
1235-
1236-
get_config_mock.return_value = [
1237-
{"service": "github", "ai_features_app_id": 12345},
1238-
]
1239-
1240-
query = """{
1241-
owner(username: "%s") {
1242-
aiEnabledRepositories {
1243-
edges {
1244-
node {
1245-
name
1246-
}
1247-
}
1248-
}
1249-
}
1250-
}
1251-
1252-
""" % (current_org.username)
1253-
data = self.gql_request(query, owner=current_org)
1254-
assert data["owner"]["aiEnabledRepositories"] is None
1255-
12561197
def test_fetch_owner_with_no_service(self):
12571198
current_org = OwnerFactory(
12581199
username="random-plan-user",

graphql_api/types/inputs/repository_set_filters.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ input RepositorySetFilters {
44
active: Boolean
55
activated: Boolean
66
isPublic: Boolean
7+
aiEnabled: Boolean
78
}

graphql_api/types/owner/owner.graphql

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ type Owner {
4040
yaml: String
4141
aiFeaturesEnabled: Boolean!
4242
aiEnabledRepos: [String]
43-
aiEnabledRepositories(
44-
ordering: RepositoryOrdering
45-
orderingDirection: OrderingDirection
46-
first: Int
47-
after: String
48-
last: Int
49-
before: String
50-
): RepositoryConnection @cost(complexity: 25, multipliers: ["first", "last"])
5143
uploadTokenRequired: Boolean
5244
activatedUserCount: Int
5345
}

graphql_api/types/owner/owner.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
from graphql_api.helpers.connection import (
3232
Connection,
3333
build_connection_graphql,
34-
queryset_to_connection,
3534
queryset_to_connection_sync,
3635
)
3736
from graphql_api.helpers.mutation import (
@@ -55,6 +54,7 @@
5554

5655

5756
@owner_bindable.field("repositories")
57+
@sync_to_async
5858
def resolve_repositories(
5959
owner: Owner,
6060
info: GraphQLResolveInfo,
@@ -77,7 +77,7 @@ def resolve_repositories(
7777
current_owner, owner, filters, okta_account_auths, exclude_okta_enforced_repos
7878
)
7979

80-
return queryset_to_connection(
80+
return queryset_to_connection_sync(
8181
queryset,
8282
ordering=(ordering, RepositoryOrdering.ID),
8383
ordering_direction=ordering_direction,
@@ -406,34 +406,3 @@ def resolve_activated_user_count(owner: Owner, info: GraphQLResolveInfo) -> int:
406406
@require_part_of_org
407407
def resolve_billing(owner: Owner, info: GraphQLResolveInfo) -> dict | None:
408408
return owner
409-
410-
411-
@owner_bindable.field("aiEnabledRepositories")
412-
@sync_to_async
413-
def resolve_ai_enabled_repositories(
414-
owner: Owner,
415-
info: GraphQLResolveInfo,
416-
ordering: Optional[RepositoryOrdering] = RepositoryOrdering.ID,
417-
ordering_direction: Optional[OrderingDirection] = OrderingDirection.ASC,
418-
**kwargs: Any,
419-
) -> Coroutine[Any, Any, Connection] | None:
420-
ai_features_app_install = GithubAppInstallation.objects.filter(
421-
app_id=AI_FEATURES_GH_APP_ID, owner=owner
422-
).first()
423-
424-
if not ai_features_app_install:
425-
return None
426-
427-
current_owner = info.context["request"].current_owner
428-
queryset = Repository.objects.filter(author=owner).viewable_repos(current_owner)
429-
if ai_features_app_install.repository_service_ids:
430-
queryset = queryset.filter(
431-
service_id__in=ai_features_app_install.repository_service_ids
432-
)
433-
434-
return queryset_to_connection_sync(
435-
queryset,
436-
ordering=(ordering, RepositoryOrdering.ID),
437-
ordering_direction=ordering_direction,
438-
**kwargs,
439-
)

0 commit comments

Comments
 (0)