Skip to content

Commit c5a73e6

Browse files
arkid15rmrkeshav-05
authored andcommitted
Update code
1 parent 28c226b commit c5a73e6

File tree

11 files changed

+124
-107
lines changed

11 files changed

+124
-107
lines changed

backend/apps/owasp/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ owasp-aggregate-projects:
44

55
owasp-aggregate-contributions:
66
@echo "Aggregating OWASP contributions"
7-
@CMD="python manage.py owasp_aggregate_contributions" $(MAKE) exec-backend-command
7+
@CMD="python manage.py owasp_aggregate_contributions --entity-type chapter" $(MAKE) exec-backend-command
8+
@CMD="python manage.py owasp_aggregate_contributions --entity-type project" $(MAKE) exec-backend-command
89

910
owasp-create-project-metadata-file:
1011
@echo "Generating metadata"

backend/apps/owasp/management/commands/owasp_aggregate_contributions.py

Lines changed: 60 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@ def add_arguments(self, parser):
2222
"""Add command arguments."""
2323
parser.add_argument(
2424
"--entity-type",
25+
choices=["chapter", "project"],
26+
help="Entity type to aggregate: chapter, project",
27+
required=True,
2528
type=str,
26-
choices=["chapter", "project", "both"],
27-
default="both",
28-
help="Entity type to aggregate: chapter, project, or both",
2929
)
3030
parser.add_argument(
3131
"--days",
32-
type=int,
3332
default=365,
3433
help="Number of days to look back for contributions (default: 365)",
34+
type=int,
3535
)
3636
parser.add_argument(
3737
"--key",
38-
type=str,
3938
help="Specific chapter or project key to aggregate",
39+
type=str,
4040
)
4141
parser.add_argument(
4242
"--offset",
43-
type=int,
4443
default=0,
4544
help="Skip the first N entities",
45+
type=int,
4646
)
4747

4848
def _aggregate_contribution_dates(
@@ -59,11 +59,12 @@ def _aggregate_contribution_dates(
5959
contribution_map: Dictionary to update with counts
6060
6161
"""
62-
dates = queryset.values_list(date_field, flat=True)
63-
for date_value in dates:
64-
if date_value:
65-
date_key = date_value.date().isoformat()
66-
contribution_map[date_key] = contribution_map.get(date_key, 0) + 1
62+
for date_value in queryset.values_list(date_field, flat=True):
63+
if not date_value:
64+
continue
65+
66+
date_key = date_value.date().isoformat()
67+
contribution_map[date_key] = contribution_map.get(date_key, 0) + 1
6768

6869
def aggregate_chapter_contributions(
6970
self,
@@ -87,7 +88,7 @@ def aggregate_chapter_contributions(
8788

8889
repository = chapter.owasp_repository
8990

90-
# Aggregate commits
91+
# Aggregate commits.
9192
self._aggregate_contribution_dates(
9293
Commit.objects.filter(
9394
repository=repository,
@@ -97,32 +98,32 @@ def aggregate_chapter_contributions(
9798
contribution_map,
9899
)
99100

100-
# Aggregate issues
101+
# Aggregate issues.
101102
self._aggregate_contribution_dates(
102103
Issue.objects.filter(
103-
repository=repository,
104104
created_at__gte=start_date,
105+
repository=repository,
105106
),
106107
"created_at",
107108
contribution_map,
108109
)
109110

110-
# Aggregate pull requests
111+
# Aggregate pull requests.
111112
self._aggregate_contribution_dates(
112113
PullRequest.objects.filter(
113-
repository=repository,
114114
created_at__gte=start_date,
115+
repository=repository,
115116
),
116117
"created_at",
117118
contribution_map,
118119
)
119120

120-
# Aggregate releases (exclude drafts)
121+
# Aggregate published releases.
121122
self._aggregate_contribution_dates(
122123
Release.objects.filter(
123-
repository=repository,
124-
published_at__gte=start_date,
125124
is_draft=False,
125+
published_at__gte=start_date,
126+
repository=repository,
126127
),
127128
"published_at",
128129
contribution_map,
@@ -147,26 +148,24 @@ def aggregate_project_contributions(
147148
"""
148149
contribution_map: dict[str, int] = {}
149150

150-
repositories = list(project.repositories.all())
151+
repository_ids = [r.id for r in project.repositories.all()]
151152
if project.owasp_repository:
152-
repositories.append(project.owasp_repository)
153-
154-
repository_ids = [repo.id for repo in repositories if repo]
153+
repository_ids.append(project.owasp_repository.id)
155154

156155
if not repository_ids:
157156
return contribution_map
158157

159-
# Aggregate commits
158+
# Aggregate commits.
160159
self._aggregate_contribution_dates(
161160
Commit.objects.filter(
162-
repository_id__in=repository_ids,
163161
created_at__gte=start_date,
162+
repository_id__in=repository_ids,
164163
),
165164
"created_at",
166165
contribution_map,
167166
)
168167

169-
# Aggregate issues
168+
# Aggregate issues.
170169
self._aggregate_contribution_dates(
171170
Issue.objects.filter(
172171
repository_id__in=repository_ids,
@@ -176,7 +175,7 @@ def aggregate_project_contributions(
176175
contribution_map,
177176
)
178177

179-
# Aggregate pull requests
178+
# Aggregate pull requests.
180179
self._aggregate_contribution_dates(
181180
PullRequest.objects.filter(
182181
repository_id__in=repository_ids,
@@ -186,7 +185,7 @@ def aggregate_project_contributions(
186185
contribution_map,
187186
)
188187

189-
# Aggregate releases (exclude drafts)
188+
# Aggregate published releases.
190189
self._aggregate_contribution_dates(
191190
Release.objects.filter(
192191
repository_id__in=repository_ids,
@@ -211,13 +210,13 @@ def calculate_chapter_contribution_stats(
211210
start_date: Start date for calculation
212211
213212
Returns:
214-
Dictionary with commits, issues, pullRequests, releases counts
213+
Dictionary with commits, issues, pull requests, releases counts
215214
216215
"""
217216
stats = {
218217
"commits": 0,
219218
"issues": 0,
220-
"pullRequests": 0,
219+
"pull_requests": 0,
221220
"releases": 0,
222221
"total": 0,
223222
}
@@ -240,7 +239,7 @@ def calculate_chapter_contribution_stats(
240239
).count()
241240

242241
# Count pull requests
243-
stats["pullRequests"] = PullRequest.objects.filter(
242+
stats["pull_requests"] = PullRequest.objects.filter(
244243
repository=repository,
245244
created_at__gte=start_date,
246245
).count()
@@ -254,7 +253,7 @@ def calculate_chapter_contribution_stats(
254253

255254
# Calculate total
256255
stats["total"] = (
257-
stats["commits"] + stats["issues"] + stats["pullRequests"] + stats["releases"]
256+
stats["commits"] + stats["issues"] + stats["pull_requests"] + stats["releases"]
258257
)
259258

260259
return stats
@@ -271,13 +270,13 @@ def calculate_project_contribution_stats(
271270
start_date: Start date for calculation
272271
273272
Returns:
274-
Dictionary with commits, issues, pullRequests, releases counts
273+
Dictionary with commits, issues, pull requests, releases counts
275274
276275
"""
277276
stats = {
278277
"commits": 0,
279278
"issues": 0,
280-
"pullRequests": 0,
279+
"pull_requests": 0,
281280
"releases": 0,
282281
"total": 0,
283282
}
@@ -304,7 +303,7 @@ def calculate_project_contribution_stats(
304303
).count()
305304

306305
# Count pull requests
307-
stats["pullRequests"] = PullRequest.objects.filter(
306+
stats["pull_requests"] = PullRequest.objects.filter(
308307
repository_id__in=repository_ids,
309308
created_at__gte=start_date,
310309
).count()
@@ -318,7 +317,7 @@ def calculate_project_contribution_stats(
318317

319318
# Calculate total
320319
stats["total"] = (
321-
stats["commits"] + stats["issues"] + stats["pullRequests"] + stats["releases"]
320+
stats["commits"] + stats["issues"] + stats["pull_requests"] + stats["releases"]
322321
)
323322

324323
return stats
@@ -338,81 +337,66 @@ def handle(self, *args, **options):
338337
),
339338
)
340339

341-
# Process chapters
342-
if entity_type in ["chapter", "both"]:
340+
if entity_type == "chapter":
343341
self._process_chapters(start_date, key, offset)
344-
345-
# Process projects
346-
if entity_type in ["project", "both"]:
342+
elif entity_type == "project":
347343
self._process_projects(start_date, key, offset)
348344

349345
self.stdout.write(self.style.SUCCESS("Done!"))
350346

351347
def _process_chapters(self, start_date, key, offset):
352348
"""Process chapters for contribution aggregation."""
353-
chapter_queryset = Chapter.objects.filter(is_active=True).order_by("id")
349+
active_chapters = Chapter.objects.filter(is_active=True).order_by("id")
354350

355351
if key:
356-
chapter_queryset = chapter_queryset.filter(key=key)
352+
active_chapters = active_chapters.filter(key=key)
357353

358-
chapter_queryset = chapter_queryset.select_related("owasp_repository")
354+
active_chapters = active_chapters.select_related("owasp_repository")
359355

360356
if offset:
361-
chapter_queryset = chapter_queryset[offset:]
357+
active_chapters = active_chapters[offset:]
362358

363-
chapters = list(chapter_queryset)
364-
self.stdout.write(f"Processing {len(chapters)} chapters...")
359+
self.stdout.write(f"Processing {active_chapters.count()} chapters...")
365360

366-
for chapter in chapters:
367-
contribution_data = self.aggregate_chapter_contributions(
368-
chapter,
369-
start_date,
370-
)
371-
contribution_stats = self.calculate_chapter_contribution_stats(
372-
chapter,
373-
start_date,
361+
chapters = []
362+
for chapter in active_chapters:
363+
chapter.contribution_data = self.aggregate_chapter_contributions(chapter, start_date)
364+
chapter.contribution_stats = self.calculate_chapter_contribution_stats(
365+
chapter, start_date
374366
)
375-
chapter.contribution_data = contribution_data
376-
chapter.contribution_stats = contribution_stats
367+
chapters.append(chapter)
377368

378369
if chapters:
379370
Chapter.bulk_save(chapters, fields=("contribution_data", "contribution_stats"))
380371
self.stdout.write(
381-
self.style.SUCCESS(f"Updated {len(chapters)} chapters"),
372+
self.style.SUCCESS(f"Updated {active_chapters.count()} chapters"),
382373
)
383374

384375
def _process_projects(self, start_date, key, offset):
385376
"""Process projects for contribution aggregation."""
386-
project_queryset = (
377+
active_projects = (
387378
Project.objects.filter(is_active=True)
388379
.order_by("id")
389380
.select_related("owasp_repository")
390381
.prefetch_related("repositories")
391382
)
392383

393384
if key:
394-
project_queryset = project_queryset.filter(key=key)
385+
active_projects = active_projects.filter(key=key)
395386

396387
if offset:
397-
project_queryset = project_queryset[offset:]
388+
active_projects = active_projects[offset:]
398389

399-
projects = list(project_queryset)
400-
self.stdout.write(f"Processing {len(projects)} projects...")
390+
self.stdout.write(f"Processing {active_projects.count()} projects...")
401391

402-
for project in projects:
403-
contribution_data = self.aggregate_project_contributions(
404-
project,
405-
start_date,
392+
projects = []
393+
for project in active_projects:
394+
project.contribution_data = self.aggregate_project_contributions(project, start_date)
395+
project.contribution_stats = self.calculate_project_contribution_stats(
396+
project, start_date
406397
)
407-
contribution_stats = self.calculate_project_contribution_stats(
408-
project,
409-
start_date,
410-
)
411-
project.contribution_data = contribution_data
412-
project.contribution_stats = contribution_stats
398+
projects.append(project)
413399

414400
if projects:
415401
Project.bulk_save(projects, fields=("contribution_data", "contribution_stats"))
416-
self.stdout.write(
417-
self.style.SUCCESS(f"✓ Updated {len(projects)} projects"),
418-
)
402+
self.stdout.write(self.style.SUCCESS(f"Updated {active_projects.count()} projects"))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Generated by Django 6.0 on 2025-12-10 04:11
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("owasp", "0067_chapter_contribution_stats_and_more"),
9+
]
10+
11+
operations = [
12+
migrations.AlterField(
13+
model_name="chapter",
14+
name="contribution_stats",
15+
field=models.JSONField(
16+
blank=True,
17+
default=dict,
18+
help_text="Detailed contribution breakdown (commits, issues, pull requests, releases)",
19+
verbose_name="Contribution Statistics",
20+
),
21+
),
22+
migrations.AlterField(
23+
model_name="project",
24+
name="contribution_stats",
25+
field=models.JSONField(
26+
blank=True,
27+
default=dict,
28+
help_text="Detailed contribution breakdown (commits, issues, pull requests, releases)",
29+
verbose_name="Contribution Statistics",
30+
),
31+
),
32+
]

backend/apps/owasp/models/chapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Meta:
7474
verbose_name="Contribution Statistics",
7575
default=dict,
7676
blank=True,
77-
help_text="Detailed contribution breakdown (commits, issues, pullRequests, releases)",
77+
help_text="Detailed contribution breakdown (commits, issues, pull requests, releases)",
7878
)
7979

8080
# GRs.

backend/apps/owasp/models/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Meta:
107107
verbose_name="Contribution Statistics",
108108
default=dict,
109109
blank=True,
110-
help_text="Detailed contribution breakdown (commits, issues, pullRequests, releases)",
110+
help_text="Detailed contribution breakdown (commits, issues, pull requests, releases)",
111111
)
112112

113113
# GKs.

0 commit comments

Comments
 (0)