-
Notifications
You must be signed in to change notification settings - Fork 69
fix(core): enforce severity contract v1 #308
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
Merged
Merged
Changes from all commits
Commits
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
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
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,100 @@ | ||
| """Enforce severity contract v1 and repair historical scan scores. | ||
|
|
||
| Revision ID: d8e4f6a1b2c3 | ||
| Revises: c7a2e9f1b3d4 | ||
| Create Date: 2026-08-21 00:00:00.000000 | ||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
| # Revision identifiers, used by Alembic. | ||
| revision: str = "d8e4f6a1b2c3" | ||
| down_revision: Union[str, Sequence[str], None] = "c7a2e9f1b3d4" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
| _CONSTRAINT = "ck_findings_severity_v1" | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """Normalize known aliases, reject unknown data, constrain and rescore.""" | ||
| op.execute( | ||
| """ | ||
| DO $$ | ||
| DECLARE invalid_values text; | ||
| BEGIN | ||
| SELECT string_agg(value, ', ' ORDER BY value) | ||
| INTO invalid_values | ||
| FROM ( | ||
| SELECT DISTINCT UPPER(BTRIM(severity)) AS value | ||
| FROM findings | ||
| WHERE UPPER(BTRIM(severity)) NOT IN | ||
| ('CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'INFO', 'INFORMATIONAL') | ||
| ) invalid; | ||
|
|
||
| IF invalid_values IS NOT NULL THEN | ||
| RAISE EXCEPTION 'Cannot apply severity contract v1; unsupported values: %', invalid_values; | ||
| END IF; | ||
| END $$; | ||
| """ | ||
| ) | ||
| op.execute( | ||
| """ | ||
| UPDATE findings | ||
| SET severity = CASE UPPER(BTRIM(severity)) | ||
| WHEN 'INFORMATIONAL' THEN 'INFO' | ||
| ELSE UPPER(BTRIM(severity)) | ||
| END | ||
| WHERE severity <> CASE UPPER(BTRIM(severity)) | ||
| WHEN 'INFORMATIONAL' THEN 'INFO' | ||
| ELSE UPPER(BTRIM(severity)) | ||
| END | ||
| """ | ||
| ) | ||
| op.add_column( | ||
| "scans", | ||
| sa.Column( | ||
| "severity_contract_version", | ||
| sa.Text(), | ||
| nullable=True, | ||
| ), | ||
| ) | ||
| op.execute( | ||
| """ | ||
| ALTER TABLE findings | ||
| ADD CONSTRAINT ck_findings_severity_v1 | ||
| CHECK (severity IN ('CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'INFO')) | ||
| NOT VALID | ||
| """ | ||
| ) | ||
| op.execute("ALTER TABLE findings VALIDATE CONSTRAINT ck_findings_severity_v1") | ||
| op.execute( | ||
| """ | ||
| UPDATE scans AS scan | ||
| SET score = GREATEST( | ||
| 0, | ||
| 100 - COALESCE(( | ||
| SELECT SUM(CASE finding.severity | ||
| WHEN 'CRITICAL' THEN 20 | ||
| WHEN 'HIGH' THEN 10 | ||
| WHEN 'MEDIUM' THEN 5 | ||
| WHEN 'LOW' THEN 2 | ||
| WHEN 'INFO' THEN 0 | ||
| END) | ||
| FROM findings AS finding | ||
| WHERE finding.scan_id = scan.scan_id | ||
| ), 0) | ||
| ), | ||
| severity_contract_version = '1.0.0' | ||
| WHERE scan.status = 'completed'; | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """Remove the v1 constraint; corrected historical scores remain corrected.""" | ||
| op.drop_constraint(_CONSTRAINT, "findings", type_="check") | ||
| op.drop_column("scans", "severity_contract_version") | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where the fork with PR #310's migration lives - both chain off
c7a2e9f1b3d4. Not something fixable from this side alone (confirmed by trying the equivalent on my own PR's migration - pointing down_revision at an unmerged sibling's revision file breaks that PR's own CI outright). This needs a merge-order call: whichever of #308/#310 merges second rebases onto the new head and repoints this. Not blocking my approval - flagging for whoever merges.