|
| 1 | +# Generated by Django 5.2.11 on 2026-07-02 09:50 |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | + |
| 5 | +from vulnerabilities.importer import AdvisoryDataV2 |
| 6 | +from vulnerabilities.importer import AffectedPackageV2 |
| 7 | +from vulnerabilities.importer import PatchData |
| 8 | +from vulnerabilities.importer import ReferenceV2 |
| 9 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 10 | +from vulnerabilities.utils import compute_content_id_v2 |
| 11 | +from vulnerabilities.utils import normalize_list |
| 12 | +from vulnerabilities.utils import purl_to_dict |
| 13 | + |
| 14 | + |
| 15 | +class Migration(migrations.Migration): |
| 16 | + |
| 17 | + dependencies = [ |
| 18 | + ("vulnerabilities", "0138_fix_malformed_cvss_vector"), |
| 19 | + ] |
| 20 | + |
| 21 | + def cleanup_none_severity_string(apps, schema_editor): |
| 22 | + AdvisorySeverity = apps.get_model("vulnerabilities", "AdvisorySeverity") |
| 23 | + AdvisoryV2 = apps.get_model("vulnerabilities", "AdvisoryV2") |
| 24 | + batch = [] |
| 25 | + batch_size = 5000 |
| 26 | + |
| 27 | + advisory_ids = list( |
| 28 | + AdvisoryV2.objects.filter(severities__value="None") |
| 29 | + .distinct() |
| 30 | + .values_list("id", flat=True) |
| 31 | + ) |
| 32 | + |
| 33 | + AdvisorySeverity.objects.filter(value="None").update(value="") |
| 34 | + |
| 35 | + for advisory in AdvisoryV2.objects.filter(id__in=advisory_ids).iterator(chunk_size=2000): |
| 36 | + advisory.unique_content_id = compute_content_id_v2(to_advisory_data(advisory)) |
| 37 | + batch.append(advisory) |
| 38 | + |
| 39 | + if len(batch) >= batch_size: |
| 40 | + AdvisoryV2.objects.bulk_update(batch, ["unique_content_id"]) |
| 41 | + batch.clear() |
| 42 | + |
| 43 | + if batch: |
| 44 | + AdvisoryV2.objects.bulk_update(batch, ["unique_content_id"]) |
| 45 | + |
| 46 | + operations = [ |
| 47 | + migrations.RunPython( |
| 48 | + cleanup_none_severity_string, |
| 49 | + reverse_code=migrations.RunPython.noop, |
| 50 | + ), |
| 51 | + ] |
| 52 | + |
| 53 | + |
| 54 | +def commit_patch_to_dict(patch): |
| 55 | + return { |
| 56 | + "vcs_url": patch.vcs_url, |
| 57 | + "commit_hash": patch.commit_hash, |
| 58 | + "patch_text": patch.patch_text, |
| 59 | + "patch_checksum": patch.patch_checksum, |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | +def to_affected_package_data(impact): |
| 64 | + """Return `AffectedPackageV2` data from the impact.""" |
| 65 | + return AffectedPackageV2.from_dict( |
| 66 | + { |
| 67 | + "package": purl_to_dict(impact.base_purl), |
| 68 | + "affected_version_range": impact.affecting_vers, |
| 69 | + "fixed_version_range": impact.fixed_vers, |
| 70 | + "introduced_by_commit_patches": [ |
| 71 | + commit_patch_to_dict(commit) |
| 72 | + for commit in impact.introduced_by_package_commit_patches.all() |
| 73 | + ], |
| 74 | + "fixed_by_commit_patches": [ |
| 75 | + commit_patch_to_dict(commit) |
| 76 | + for commit in impact.fixed_by_package_commit_patches.all() |
| 77 | + ], |
| 78 | + } |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def to_patch_data(patch): |
| 83 | + """Return `PatchData` from the Patch.""" |
| 84 | + |
| 85 | + return PatchData.from_dict( |
| 86 | + { |
| 87 | + "patch_url": patch.patch_url, |
| 88 | + "patch_text": patch.patch_text, |
| 89 | + "patch_checksum": patch.patch_checksum, |
| 90 | + } |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def to_reference_v2_data(ref): |
| 95 | + return ReferenceV2.from_dict( |
| 96 | + { |
| 97 | + "reference_id": ref.reference_id, |
| 98 | + "reference_type": ref.reference_type, |
| 99 | + "url": ref.url, |
| 100 | + } |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +def to_vulnerability_severity_data(severity): |
| 105 | + return VulnerabilitySeverity.from_dict( |
| 106 | + { |
| 107 | + "system": severity.scoring_system, |
| 108 | + "value": severity.value, |
| 109 | + "scoring_elements": severity.scoring_elements, |
| 110 | + "published_at": severity.published_at, |
| 111 | + "url": severity.url, |
| 112 | + } |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +def to_advisory_data(advisory): |
| 117 | + return AdvisoryDataV2( |
| 118 | + advisory_id=advisory.advisory_id, |
| 119 | + aliases=normalize_list([item.alias for item in advisory.aliases.all()]), |
| 120 | + summary=advisory.summary, |
| 121 | + affected_packages=normalize_list( |
| 122 | + [to_affected_package_data(impacted) for impacted in advisory.impacted_packages.all()] |
| 123 | + ), |
| 124 | + references=normalize_list([to_reference_v2_data(ref) for ref in advisory.references.all()]), |
| 125 | + patches=normalize_list([to_patch_data(patch) for patch in advisory.patches.all()]), |
| 126 | + date_published=advisory.date_published, |
| 127 | + weaknesses=normalize_list([weak.cwe_id for weak in advisory.weaknesses.all()]), |
| 128 | + severities=normalize_list( |
| 129 | + [to_vulnerability_severity_data(sev) for sev in advisory.severities.all()] |
| 130 | + ), |
| 131 | + url=advisory.url, |
| 132 | + ) |
0 commit comments