-
-
Notifications
You must be signed in to change notification settings - Fork 529
Implement command to detect non-compliant project levels and update score calculation #3340
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
Open
OM-JADHAV25
wants to merge
11
commits into
OWASP:main
Choose a base branch
from
OM-JADHAV25:feature/non-compliant-project-levels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+331
−1
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
95e397d
Detect non-compliant project levels
OM-JADHAV25 54bdd8d
Fix mypy and tests
OM-JADHAV25 214acd7
Merge branch 'OWASP:main' into feature/non-compliant-project-levels
OM-JADHAV25 0869553
add project level compliance job
OM-JADHAV25 271a055
Merge branch 'OWASP:main' into feature/non-compliant-project-levels
OM-JADHAV25 83db2a4
fix sonar float comparison warning
OM-JADHAV25 bbb1424
Fix formatting of warning message for Missing Spaces
OM-JADHAV25 1afb6c7
Fix bulk_save method argument from update_fields to fields
OM-JADHAV25 7961462
Merge branch 'main' into feature/non-compliant-project-levels
OM-JADHAV25 ccf2d9c
Merge branch 'main' into feature/non-compliant-project-levels
OM-JADHAV25 59863eb
Merge branch 'main' into feature/non-compliant-project-levels
OM-JADHAV25 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
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_update_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 non-compliant OWASP project levels.""" | ||
|
|
||
| import logging | ||
| import re | ||
| from decimal import Decimal, InvalidOperation | ||
|
|
||
| import requests | ||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from apps.owasp.models import ProjectHealthMetrics | ||
| from apps.owasp.utils.project_level import map_level | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| LEVELS_URL = ( | ||
| "https://raw.githubusercontent.com/OWASP/owasp.github.io/main/_data/project_levels.json" | ||
| ) | ||
|
|
||
|
|
||
| def clean_name(name: str) -> str: | ||
| """Normalize project names for matching with OWASP official data.""" | ||
| name = name.lower().replace("owasp", "") | ||
| return re.sub(r"[^a-z0-9]+", "", name) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Update project level compliance." | ||
|
|
||
| def handle(self, *args, **options): | ||
| self.stdout.write("Updating project level compliance...") | ||
|
|
||
| response = requests.get(LEVELS_URL, timeout=15) | ||
| response.raise_for_status() | ||
|
|
||
| official = response.json() | ||
|
|
||
| by_repo: dict[str, Decimal] = {} | ||
| by_name: dict[str, Decimal] = {} | ||
|
|
||
| for item in official: | ||
| raw_level = item.get("level") | ||
| try: | ||
| level = Decimal(str(raw_level)) | ||
| except (InvalidOperation, TypeError, ValueError): | ||
| logger.debug("Skipping invalid project level: %s", raw_level) | ||
| continue | ||
|
|
||
| repo = item.get("repo") | ||
| if repo: | ||
| by_repo[repo.lower()] = level | ||
|
|
||
| name = item.get("name") | ||
| if name: | ||
| by_name[clean_name(name)] = level | ||
|
|
||
| metrics = ProjectHealthMetrics.objects.select_related("project") | ||
|
|
||
| updated_metrics = [] | ||
|
|
||
| for metric in metrics: | ||
| project = metric.project | ||
|
|
||
| official_level = None | ||
| if project.repo_url: | ||
| slug = project.repo_url.rstrip("/").split("/")[-1].lower() | ||
| official_level = by_repo.get(slug) | ||
|
|
||
| if official_level is None: | ||
| official_level = by_name.get(clean_name(project.name)) | ||
|
|
||
| if official_level is None: | ||
| continue | ||
|
|
||
| expected_level = map_level(official_level) | ||
| if expected_level is None: | ||
| continue | ||
|
|
||
| metric.level_non_compliant = project.level != expected_level | ||
| if metric.level_non_compliant: | ||
| self.stdout.write( | ||
| self.style.WARNING( | ||
| f"Level mismatch: {project.name} " | ||
| f"local={project.level} " | ||
| f"official={expected_level}" | ||
| ) | ||
| ) | ||
|
|
||
| updated_metrics.append(metric) | ||
|
|
||
| if updated_metrics: | ||
| ProjectHealthMetrics.bulk_save( | ||
| updated_metrics, | ||
| fields=["level_non_compliant"], | ||
| ) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| self.stdout.write(f"Project level compliance updated for {len(updated_metrics)} metrics.") | ||
17 changes: 17 additions & 0 deletions
17
backend/apps/owasp/migrations/0070_projecthealthmetrics_level_non_compliant.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,17 @@ | ||
| # Generated by Django 6.0.1 on 2026-01-11 19:03 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("owasp", "0069_alter_project_contribution_data_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="projecthealthmetrics", | ||
| name="level_non_compliant", | ||
| field=models.BooleanField(default=False, verbose_name="Is level non-compliant"), | ||
| ), | ||
| ] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Utilities for mapping OWASP official project levels to Nest ProjectLevel enum.""" | ||
|
|
||
| from decimal import Decimal, InvalidOperation | ||
| from typing import cast | ||
|
|
||
| from apps.owasp.models.enums.project import ProjectLevel | ||
|
|
||
| LEVEL_MAP = { | ||
| Decimal(4): ProjectLevel.FLAGSHIP, | ||
| Decimal("3.5"): ProjectLevel.FLAGSHIP, | ||
| Decimal(3): ProjectLevel.PRODUCTION, | ||
| Decimal(2): ProjectLevel.INCUBATOR, | ||
| Decimal(1): ProjectLevel.LAB, | ||
| Decimal(0): ProjectLevel.OTHER, | ||
| } | ||
|
|
||
|
|
||
| def map_level(level: Decimal) -> ProjectLevel | None: | ||
| """Map OWASP official numeric level to Nest ProjectLevel.""" | ||
| try: | ||
| parsed_level = Decimal(str(level)) | ||
| except (InvalidOperation, TypeError, ValueError): | ||
| return None | ||
|
|
||
| if parsed_level < 0: | ||
| return None | ||
|
|
||
| return cast("ProjectLevel | None", LEVEL_MAP.get(parsed_level)) |
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
89 changes: 89 additions & 0 deletions
89
backend/tests/apps/owasp/management/commands/owasp_update_project_level_compliance_test.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,89 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from django.core.management import call_command | ||
| from django.test import SimpleTestCase | ||
|
|
||
| from apps.owasp.models.enums.project import ProjectLevel | ||
|
|
||
|
|
||
| class TestProjectLevelCompliance(SimpleTestCase): | ||
| @patch( | ||
| "apps.owasp.management.commands.owasp_update_project_level_compliance." | ||
| "ProjectHealthMetrics.bulk_save" | ||
| ) | ||
| @patch( | ||
| "apps.owasp.management.commands.owasp_update_project_level_compliance." | ||
| "ProjectHealthMetrics.objects" | ||
| ) | ||
| @patch("apps.owasp.management.commands.owasp_update_project_level_compliance.requests.get") | ||
| def test_parses_official_level_data(self, mock_get, mock_metrics, mock_bulk_save): | ||
| mock_get.return_value.status_code = 200 | ||
| mock_get.return_value.json.return_value = [ | ||
| {"name": "Parsed Project", "repo": "parsed-project", "level": "3.5"} | ||
| ] | ||
|
|
||
| metric = MagicMock() | ||
| metric.project.name = "Parsed Project" | ||
| metric.project.level = ProjectLevel.FLAGSHIP | ||
| metric.level_non_compliant = False | ||
|
|
||
| mock_metrics.select_related.return_value = [metric] | ||
|
|
||
| call_command("owasp_update_project_level_compliance") | ||
|
|
||
| assert metric.level_non_compliant is False | ||
| mock_bulk_save.assert_called_once() | ||
|
|
||
| @patch( | ||
| "apps.owasp.management.commands.owasp_update_project_level_compliance." | ||
| "ProjectHealthMetrics.bulk_save" | ||
| ) | ||
| @patch( | ||
| "apps.owasp.management.commands.owasp_update_project_level_compliance." | ||
| "ProjectHealthMetrics.objects" | ||
| ) | ||
| @patch("apps.owasp.management.commands.owasp_update_project_level_compliance.requests.get") | ||
| def test_marks_non_compliant_when_levels_differ(self, mock_get, mock_metrics, mock_bulk_save): | ||
| mock_get.return_value.status_code = 200 | ||
| mock_get.return_value.json.return_value = [ | ||
| {"name": "Test Project", "repo": "test-project", "level": "4"} | ||
| ] | ||
|
|
||
| metric = MagicMock() | ||
| metric.project.name = "Test Project" | ||
| metric.project.level = ProjectLevel.PRODUCTION | ||
| metric.level_non_compliant = False | ||
|
|
||
| mock_metrics.select_related.return_value = [metric] | ||
|
|
||
| call_command("owasp_update_project_level_compliance") | ||
|
|
||
| assert metric.level_non_compliant is True | ||
| mock_bulk_save.assert_called_once() | ||
|
|
||
| @patch( | ||
| "apps.owasp.management.commands.owasp_update_project_level_compliance." | ||
| "ProjectHealthMetrics.bulk_save" | ||
| ) | ||
| @patch( | ||
| "apps.owasp.management.commands.owasp_update_project_level_compliance." | ||
| "ProjectHealthMetrics.objects" | ||
| ) | ||
| @patch("apps.owasp.management.commands.owasp_update_project_level_compliance.requests.get") | ||
| def test_remains_compliant_when_levels_match(self, mock_get, mock_metrics, mock_bulk_save): | ||
| mock_get.return_value.status_code = 200 | ||
| mock_get.return_value.json.return_value = [ | ||
| {"name": "Test Project", "repo": "test-project", "level": "3"} | ||
| ] | ||
|
|
||
| metric = MagicMock() | ||
| metric.project.name = "Test Project" | ||
| metric.project.level = ProjectLevel.PRODUCTION | ||
| metric.level_non_compliant = False | ||
|
|
||
| mock_metrics.select_related.return_value = [metric] | ||
|
|
||
| call_command("owasp_update_project_level_compliance") | ||
|
|
||
| assert metric.level_non_compliant is False | ||
| mock_bulk_save.assert_called_once() |
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,27 @@ | ||
| from decimal import Decimal | ||
|
|
||
| from django.test import SimpleTestCase | ||
|
|
||
| from apps.owasp.models.enums.project import ProjectLevel | ||
| from apps.owasp.utils.project_level import map_level | ||
|
|
||
|
|
||
| class ProjectLevelMappingTest(SimpleTestCase): | ||
| def test_flagship_levels(self): | ||
| assert map_level(Decimal(4)) == ProjectLevel.FLAGSHIP | ||
| assert map_level(Decimal("3.5")) == ProjectLevel.FLAGSHIP | ||
|
|
||
| def test_production_level(self): | ||
| assert map_level(Decimal(3)) == ProjectLevel.PRODUCTION | ||
|
|
||
| def test_incubator_level(self): | ||
| assert map_level(Decimal(2)) == ProjectLevel.INCUBATOR | ||
|
|
||
| def test_lab_level(self): | ||
| assert map_level(Decimal(1)) == ProjectLevel.LAB | ||
|
|
||
| def test_other_level(self): | ||
| assert map_level(Decimal(0)) == ProjectLevel.OTHER | ||
|
|
||
| def test_negative_level_returns_none(self): | ||
| assert map_level(Decimal(-1)) is None |
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.