11"""OWASP repository contributor GraphQL queries."""
22
33import graphene
4- from django .db .models import F , OuterRef , Subquery , Window
4+ from django .db .models import F , Window
55from django .db .models .functions import Rank
66
77from apps .common .graphql .queries import BaseQuery
88from apps .github .graphql .nodes .repository_contributor import RepositoryContributorNode
99from apps .github .models .repository_contributor import RepositoryContributor
10- from apps .owasp .models .project import Project
1110
1211
1312class RepositoryContributorQuery (BaseQuery ):
@@ -36,6 +35,14 @@ def resolve_top_contributors(root, info, limit, organization=None):
3635 RepositoryContributor .objects .by_humans ()
3736 .to_community_repositories ()
3837 .filter (repository__project__isnull = False )
38+ .select_related ("repository__project" , "user" )
39+ .annotate (
40+ rank = Window (
41+ expression = Rank (),
42+ order_by = F ("contributions_count" ).desc (),
43+ partition_by = F ("user__login" ),
44+ )
45+ )
3946 )
4047
4148 if organization :
@@ -46,34 +53,18 @@ def resolve_top_contributors(root, info, limit, organization=None):
4653 )
4754
4855 top_contributors = (
49- queryset .annotate (
50- project_id = Subquery (
51- Project .repositories .through .objects .filter (
52- repository = OuterRef ("repository_id" )
53- )
54- .values ("project_id" )
55- .order_by ("project_id" )[:1 ] # Select the first project ID per repository
56- ),
57- project_name = Subquery (
58- Project .objects .filter (id = OuterRef ("project_id" )).values ("name" )[:1 ]
59- ),
60- project_url = Subquery (
61- Project .objects .filter (id = OuterRef ("project_id" )).values ("key" )[:1 ]
62- ),
63- rank = Window (
64- expression = Rank (),
65- order_by = F ("contributions_count" ).desc (),
66- partition_by = F ("user__login" ),
67- ),
56+ queryset .filter (rank = 1 )
57+ .annotate (
58+ project_name = F ("repository__project__name" ),
59+ project_key = F ("repository__project__key" ),
6860 )
69- .filter (rank = 1 ) # Keep only the highest contribution per user
7061 .values (
7162 "contributions_count" ,
7263 "user__avatar_url" ,
7364 "user__login" ,
7465 "user__name" ,
66+ "project_key" ,
7567 "project_name" ,
76- "project_url" ,
7768 )
7869 .order_by ("-contributions_count" )[:limit ]
7970 )
@@ -84,8 +75,8 @@ def resolve_top_contributors(root, info, limit, organization=None):
8475 contributions_count = trc ["contributions_count" ],
8576 login = trc ["user__login" ],
8677 name = trc ["user__name" ],
78+ project_key = trc ["project_key" ].replace ("www-project-" , "" ),
8779 project_name = trc ["project_name" ],
88- project_url = trc ["project_url" ],
8980 )
9081 for trc in top_contributors
9182 ]
0 commit comments