-
-
Notifications
You must be signed in to change notification settings - Fork 270
feature/Implementation of management command. #2073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
divyanshu-vr
wants to merge
27
commits into
OWASP:main
from
divyanshu-vr:feature/ProjectLevelComplianceDetection
+1,889
−31
Closed
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
949afda
WIP: Implementation of management command.
SUPERGAMERDIV e28ebc5
fix project_levels_url
divyanshu-vr 038c71d
avoid string literal in raise
divyanshu-vr ff4df3b
wrap db updates in transaction
divyanshu-vr d2fea99
use logger.exception
divyanshu-vr b652e64
Apply suggestions from code review
divyanshu-vr 50127c3
Apply suggestions from code review
divyanshu-vr 4e11688
Apply suggestions from code review
divyanshu-vr 75dffda
coderabbit suggestions fixed
SUPERGAMERDIV df2f113
Merge branch 'feature/ProjectLevelComplianceDetection' of https://git…
SUPERGAMERDIV d85e0d5
conflict fixes
SUPERGAMERDIV d3bd32b
pre-commit fixes
SUPERGAMERDIV 1365beb
fixes and added CronJob Scheduling
SUPERGAMERDIV 41598c9
coderabbit fixes
SUPERGAMERDIV 9199bd2
Merge branch 'OWASP:main' into feature/ProjectLevelComplianceDetection
divyanshu-vr 64ee6bb
made changes asked.
SUPERGAMERDIV e1c923b
Merge branch 'feature/ProjectLevelComplianceDetection' of https://git…
SUPERGAMERDIV 8f4e3a4
Update backend/apps/owasp/management/commands/owasp_update_project_he…
divyanshu-vr 021ec86
fixed sonarqube issues
SUPERGAMERDIV b7a7eac
Update __init__.py
divyanshu-vr 79acaec
Update __init__.py
divyanshu-vr 2748680
sonar fixes
SUPERGAMERDIV d5aed68
Merge branch 'feature/ProjectLevelComplianceDetection' of https://git…
SUPERGAMERDIV b886714
fixed make check errors
SUPERGAMERDIV 55bcf9d
fixed sonarqube and precommit errors
SUPERGAMERDIV 1e20a06
made coderabbit suggestions
SUPERGAMERDIV 9b3205d
test fixes
SUPERGAMERDIV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
backend/apps/owasp/management/commands/owasp_detect_project_level_compliance.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """A command to detect and report project level compliance status.""" | ||
|
|
||
| import logging | ||
| from io import StringIO | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from apps.owasp.models.project import Project | ||
| from apps.owasp.models.project_health_metrics import ProjectHealthMetrics | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| """Command to detect and report project level compliance status.""" | ||
|
|
||
| help = "Detect and report projects with non-compliant level assignments" | ||
|
|
||
| def add_arguments(self, parser): | ||
| """Add command line arguments.""" | ||
| parser.add_argument( | ||
| "--verbose", | ||
| action="store_true", | ||
| help="Enable verbose output showing all projects", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| """Execute compliance detection and reporting.""" | ||
| verbose = options["verbose"] | ||
|
|
||
| self.stdout.write("Analyzing project level compliance status...") | ||
|
|
||
| # Get all active projects | ||
| active_projects = Project.objects.filter(is_active=True).select_related() | ||
|
|
||
| compliant_projects = [] | ||
| non_compliant_projects = [] | ||
|
|
||
| for project in active_projects: | ||
| if project.is_level_compliant: | ||
| compliant_projects.append(project) | ||
| if verbose: | ||
| self.stdout.write( | ||
| f"✓ {project.name}: {project.level} (matches official)" | ||
| ) | ||
| else: | ||
| non_compliant_projects.append(project) | ||
| self.stdout.write( | ||
| self.style.WARNING( | ||
| f"✗ {project.name}: Local={project.level}, Official={project.project_level_official}" | ||
| ) | ||
| ) | ||
|
|
||
| # Summary statistics | ||
| total_projects = len(active_projects) | ||
| compliant_count = len(compliant_projects) | ||
| non_compliant_count = len(non_compliant_projects) | ||
| compliance_rate = (compliant_count / total_projects * 100) if total_projects else 0.0 | ||
|
|
||
| self.stdout.write("\n" + "="*60) | ||
| self.stdout.write("PROJECT LEVEL COMPLIANCE SUMMARY") | ||
| self.stdout.write("="*60) | ||
| self.stdout.write(f"Total active projects: {total_projects}") | ||
| self.stdout.write(f"Compliant projects: {compliant_count}") | ||
| self.stdout.write(f"Non-compliant projects: {non_compliant_count}") | ||
| self.stdout.write(f"Compliance rate: {compliance_rate:.1f}%") | ||
|
|
||
| if non_compliant_count > 0: | ||
| self.stdout.write(f"\n{self.style.WARNING('⚠ WARNING: Found ' + str(non_compliant_count) + ' non-compliant projects')}") | ||
| self.stdout.write("These projects will receive score penalties in the next health score update.") | ||
| else: | ||
| self.stdout.write(f"\n{self.style.SUCCESS('✓ All projects are level compliant!')}") | ||
|
|
||
| # Log summary for monitoring | ||
| logger.info( | ||
| "Project level compliance analysis completed", | ||
| extra={ | ||
| "total_projects": total_projects, | ||
| "compliant_projects": compliant_count, | ||
| "non_compliant_projects": non_compliant_count, | ||
| "compliance_rate": f"{compliance_rate:.1f}%", | ||
| }, | ||
| ) | ||
|
|
||
| # Check if official levels are populated | ||
| default_level = Project._meta.get_field('project_level_official').default | ||
| projects_without_official_level = sum( | ||
| 1 for project in active_projects | ||
| if project.project_level_official == default_level | ||
| ) | ||
|
|
||
| if projects_without_official_level > 0: | ||
| self.stdout.write( | ||
| f"\n{self.style.NOTICE('ℹ INFO: ' + str(projects_without_official_level) + ' projects have default official levels')}" | ||
| ) | ||
| self.stdout.write("Run 'owasp_update_project_health_metrics' to sync official levels from OWASP GitHub.") |
131 changes: 131 additions & 0 deletions
131
backend/apps/owasp/management/commands/owasp_update_project_health_metrics.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
backend/apps/owasp/migrations/0047_add_is_level_compliant_field.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Generated by Django 5.2.5 on 2025-08-12 21:00 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('owasp', '0046_merge_0045_badge_0045_project_audience'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='projecthealthmetrics', | ||
| name='is_level_compliant', | ||
| field=models.BooleanField(default=True, help_text="Whether the project's local level matches the official OWASP level", verbose_name='Is project level compliant'), | ||
| ), | ||
| ] |
29 changes: 29 additions & 0 deletions
29
backend/apps/owasp/migrations/0048_add_compliance_penalty_weight.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Generated by Django 5.2.5 on 2025-08-14 15:17 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("owasp", "0047_add_is_level_compliant_field"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="projecthealthrequirements", | ||
| name="compliance_penalty_weight", | ||
| field=models.FloatField( | ||
| default=10.0, | ||
| help_text="Percentage penalty applied to non-compliant projects (0-100)", | ||
| verbose_name="Compliance penalty weight (%)", | ||
| ), | ||
| ), | ||
| migrations.AddConstraint( | ||
| model_name="projecthealthrequirements", | ||
| constraint=models.CheckConstraint( | ||
| name="owasp_compliance_penalty_weight_0_100", | ||
| check=models.Q(compliance_penalty_weight__gte=0.0) | ||
| & models.Q(compliance_penalty_weight__lte=100.0), | ||
| ), | ||
| ), | ||
| ] |
19 changes: 19 additions & 0 deletions
19
...s/0049_remove_projecthealthrequirements_owasp_compliance_penalty_weight_0_100_and_more.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Generated by Django 5.2.5 on 2025-08-18 12:29 | ||
|
|
||
| import django.core.validators | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('owasp', '0048_add_compliance_penalty_weight'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name='projecthealthrequirements', | ||
| name='compliance_penalty_weight', | ||
| field=models.FloatField(default=10.0, help_text='Percentage penalty applied to non-compliant projects (0-100)', validators=[django.core.validators.MinValueValidator(0.0), django.core.validators.MaxValueValidator(100.0)], verbose_name='Compliance penalty weight (%)'), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.