Skip to content

Commit 0e1cc31

Browse files
committed
Introduce a new commit diff field
Fix a typo in the importer Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent cc91f09 commit 0e1cc31

File tree

7 files changed

+53
-2
lines changed

7 files changed

+53
-2
lines changed

vulnerabilities/importer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class CodeCommitData:
202202

203203
commit_author: Optional[str] = None
204204
commit_message: Optional[str] = None
205+
commit_diff: Optional[str] = None
205206
commit_date: Optional[datetime.datetime] = None
206207

207208
def __post_init__(self):
@@ -226,6 +227,7 @@ def _cmp_key(self):
226227
self.vcs_url,
227228
self.commit_author,
228229
self.commit_message,
230+
self.commit_diff,
229231
self.commit_date,
230232
)
231233

@@ -236,6 +238,7 @@ def to_dict(self) -> dict:
236238
"vcs_url": self.vcs_url,
237239
"commit_author": self.commit_author,
238240
"commit_message": self.commit_message,
241+
"commit_diff": self.commit_diff,
239242
"commit_date": self.commit_date,
240243
}
241244

@@ -244,10 +247,11 @@ def from_dict(cls, data: dict):
244247
"""Create a Commit instance from a dictionary."""
245248
commit_date = data.get("commit_date")
246249
return cls(
247-
commit_hash=str(data.get("commit_hash", "")),
248-
vcs_url=data.get("vcs_url", ""),
250+
commit_hash=data.get("commit_hash"),
251+
vcs_url=data.get("vcs_url"),
249252
commit_author=data.get("commit_author"),
250253
commit_message=data.get("commit_message"),
254+
commit_diff=data.get("commit_diff"),
251255
commit_date=datetime.datetime.fromisoformat(commit_date) if commit_date else None,
252256
)
253257

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 4.2.22 on 2025-11-17 12:17
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("vulnerabilities", "0103_codecommit_impactedpackage_affecting_commits_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="codecommit",
15+
name="commit_diff",
16+
field=models.TextField(
17+
blank=True, help_text="Full diff content of the commit.", null=True
18+
),
19+
),
20+
]

vulnerabilities/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,5 +3412,9 @@ class CodeCommit(models.Model):
34123412
null=True, blank=True, help_text="Commit message or description."
34133413
)
34143414

3415+
commit_diff = models.TextField(
3416+
null=True, blank=True, help_text="Full diff content of the commit."
3417+
)
3418+
34153419
class Meta:
34163420
unique_together = ("commit_hash", "vcs_url")

vulnerabilities/pipes/advisory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def get_or_create_advisory_code_commits(code_commits_data: List) -> List["CodeCo
121121
vcs_url=c.vcs_url,
122122
commit_author=getattr(c, "commit_author", None),
123123
commit_message=getattr(c, "commit_message", None),
124+
commit_diff=getattr(c, "commit_diff", None),
124125
commit_date=getattr(c, "commit_date", None),
125126
)
126127
for c in code_commits_data

vulnerabilities/tests/pipelines/v2_importers/test_github_osv_importer_v2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ def delete(self):
9090
vcs_url="https://github.com/aboutcode-org/vulnerablecode",
9191
commit_author=None,
9292
commit_message=None,
93+
commit_diff=None,
9394
commit_date=None,
9495
),
9596
CodeCommitData(
9697
commit_hash="b58c68c38a9de451818bac6c96d08d61e7f348a2",
9798
vcs_url="https://github.com/aboutcode-org/vulnerablecode",
9899
commit_author=None,
99100
commit_message=None,
101+
commit_diff=None,
100102
commit_date=None,
101103
),
102104
]
@@ -107,13 +109,15 @@ def delete(self):
107109
vcs_url="https://github.com/aboutcode-org/vulnerablecode",
108110
commit_author=None,
109111
commit_message=None,
112+
commit_diff=None,
110113
commit_date=None,
111114
),
112115
CodeCommitData(
113116
commit_hash="61621982593152c47b520ce893eb90c332427483",
114117
vcs_url="https://github.com/aboutcode-org/vulnerablecode",
115118
commit_author=None,
116119
commit_message=None,
120+
commit_diff=None,
117121
commit_date=None,
118122
),
119123
]

vulnerabilities/tests/pipes/test_advisory.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def advisory_commit():
170170
vcs_url="https://github.com/aboutcode-org/vulnerablecode",
171171
commit_author="tester1",
172172
commit_message="message1",
173+
commit_diff="diff commit1",
173174
commit_date=datetime.now(),
174175
),
175176
CodeCommitData(

vulnerabilities/tests/test_commit_code.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import datetime
22

33
import pytest
4+
from django.core.exceptions import ValidationError
45

56
from vulnerabilities.models import AdvisoryV2
67
from vulnerabilities.models import CodeCommit
@@ -32,6 +33,7 @@ def setup_method(self):
3233
vcs_url="https://github.com/aboutcode-org/test1/",
3334
commit_author="tester1",
3435
commit_message="test message1",
36+
commit_diff="diff text",
3537
commit_date=datetime.now(),
3638
)
3739

@@ -56,3 +58,18 @@ def test_commit_fields(self):
5658
def test_impacted_packages_creation(self):
5759
assert ImpactedPackage.objects.count() == 1
5860
assert self.code_commit1 == self.impacted.fixed_by_commits.first()
61+
62+
def test_invalid_commit_creation(self):
63+
with pytest.raises(ValidationError):
64+
commit = CodeCommit(
65+
commit_hash="", vcs_url="https://github.com/aboutcode-org/test1/" # invalid
66+
)
67+
commit.full_clean() # triggers model validation
68+
commit.save()
69+
70+
with pytest.raises(ValidationError):
71+
commit = CodeCommit(
72+
commit_hash="7c001a11dbcb3eb6d851e18f4cefa080af5fb398", vcs_url="" # invalid
73+
)
74+
commit.full_clean() # triggers model validation
75+
commit.save()

0 commit comments

Comments
 (0)