Skip to content

Commit b6651a4

Browse files
committed
Add data migration for github advisory
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 5386adc commit b6651a4

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Generated by Django 4.2.15 on 2024-09-27 14:31
2+
3+
from django.db import migrations
4+
5+
"""
6+
Update the created_by field on Advisory from the old qualified_name
7+
to the new pipeline_id.
8+
"""
9+
10+
11+
def update_created_by(apps, schema_editor):
12+
from vulnerabilities.pipelines.github_importer import GitHubAPIImporterPipeline
13+
14+
Advisory = apps.get_model("vulnerabilities", "Advisory")
15+
Advisory.objects.filter(created_by="vulnerabilities.importers.github.GitHubAPIImporter").update(
16+
created_by=GitHubAPIImporterPipeline.pipeline_id
17+
)
18+
19+
20+
21+
def reverse_update_created_by(apps, schema_editor):
22+
from vulnerabilities.pipelines.github_importer import GitHubAPIImporterPipeline
23+
24+
Advisory = apps.get_model("vulnerabilities", "Advisory")
25+
Advisory.objects.filter(created_by=GitHubAPIImporterPipeline.pipeline_id).update(
26+
created_by="vulnerabilities.importers.github.GitHubAPIImporter"
27+
)
28+
29+
30+
class Migration(migrations.Migration):
31+
32+
dependencies = [
33+
("vulnerabilities", "0066_update_gitlab_advisory_created_by"),
34+
]
35+
36+
operations = [
37+
migrations.RunPython(update_created_by, reverse_code=reverse_update_created_by),
38+
]

vulnerabilities/tests/test_data_migrations.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,3 +761,44 @@ def test_removal_of_duped_purls(self):
761761
adv.filter(created_by="vulnerabilities.importers.gitlab.GitLabAPIImporter").count() == 0
762762
)
763763
assert adv.filter(created_by="gitlab_importer").count() == 1
764+
765+
766+
class TestUpdateGitHubAdvisoryCreatedByField(TestMigrations):
767+
app_name = "vulnerabilities"
768+
migrate_from = "0066_update_gitlab_advisory_created_by"
769+
migrate_to = "0067_update_github_advisory_created_by"
770+
771+
advisory_data1 = AdvisoryData(
772+
aliases=["CVE-2020-13371337"],
773+
summary="vulnerability description here",
774+
affected_packages=[
775+
AffectedPackage(
776+
package=PackageURL(type="pypi", name="foobar"),
777+
affected_version_range=VersionRange.from_string("vers:pypi/>=1.0.0|<=2.0.0"),
778+
)
779+
],
780+
references=[Reference(url="https://example.com/with/more/info/CVE-2020-13371337")],
781+
date_published=timezone.now(),
782+
url="https://test.com",
783+
)
784+
785+
def setUpBeforeMigration(self, apps):
786+
Advisory = apps.get_model("vulnerabilities", "Advisory")
787+
adv1 = Advisory.objects.create(
788+
aliases=self.advisory_data1.aliases,
789+
summary=self.advisory_data1.summary,
790+
affected_packages=[pkg.to_dict() for pkg in self.advisory_data1.affected_packages],
791+
references=[ref.to_dict() for ref in self.advisory_data1.references],
792+
url=self.advisory_data1.url,
793+
created_by="vulnerabilities.importers.github.GitHubAPIImporter",
794+
date_collected=timezone.now(),
795+
)
796+
797+
def test_removal_of_duped_purls(self):
798+
Advisory = apps.get_model("vulnerabilities", "Advisory")
799+
adv = Advisory.objects.all()
800+
801+
assert (
802+
adv.filter(created_by="vulnerabilities.importers.github.GitHubAPIImporter").count() == 0
803+
)
804+
assert adv.filter(created_by="github_importer").count() == 1

0 commit comments

Comments
 (0)