Skip to content

Commit 991fbeb

Browse files
committed
Add tests
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 48d2144 commit 991fbeb

File tree

3 files changed

+196
-157
lines changed

3 files changed

+196
-157
lines changed

vulnerabilities/migrations/0086_codefix.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 4.2.16 on 2024-12-23 19:32
1+
# Generated by Django 4.2.16 on 2025-01-08 13:28
22

33
from django.db import migrations, models
44
import django.db.models.deletion
@@ -25,7 +25,7 @@ class Migration(migrations.Migration):
2525
models.JSONField(
2626
blank=True,
2727
default=list,
28-
help_text="List of commit identifiers associated with the code change.",
28+
help_text="List of commit identifiers using VCS URLs associated with the code change.",
2929
),
3030
),
3131
(
@@ -48,15 +48,15 @@ class Migration(migrations.Migration):
4848
"patch",
4949
models.TextField(
5050
blank=True,
51-
help_text="The code change in patch format (e.g., git diff).",
51+
help_text="The code change as a patch in unified diff format.",
5252
null=True,
5353
),
5454
),
5555
(
5656
"notes",
5757
models.TextField(
5858
blank=True,
59-
help_text="Additional notes or instructions about the code change.",
59+
help_text="Notes or instructions about this code change.",
6060
null=True,
6161
),
6262
),
@@ -65,55 +65,58 @@ class Migration(migrations.Migration):
6565
models.JSONField(
6666
blank=True,
6767
default=list,
68-
help_text="External references related to this code change.",
68+
help_text="URL references related to this code change.",
6969
),
7070
),
7171
(
72-
"status_reviewed",
72+
"is_reviewed",
7373
models.BooleanField(
74-
default=False, help_text="Indicates if the code change has been reviewed."
75-
),
76-
),
77-
(
78-
"base_commit",
79-
models.CharField(
80-
blank=True,
81-
help_text="The commit ID representing the state of the code before applying the fix or change.",
82-
max_length=255,
83-
null=True,
74+
default=False, help_text="Indicates if this code change has been reviewed."
8475
),
8576
),
8677
(
8778
"created_at",
8879
models.DateTimeField(
8980
auto_now_add=True,
90-
help_text="Timestamp indicating when the code change was created.",
81+
help_text="Timestamp indicating when this code change was created.",
9182
),
9283
),
9384
(
9485
"updated_at",
9586
models.DateTimeField(
9687
auto_now=True,
97-
help_text="Timestamp indicating when the code change was last updated.",
88+
help_text="Timestamp indicating when this code change was last updated.",
9889
),
9990
),
10091
(
101-
"base_version",
92+
"affected_package_vulnerability",
93+
models.ForeignKey(
94+
help_text="The affected package version to which this code fix applies.",
95+
on_delete=django.db.models.deletion.CASCADE,
96+
related_name="code_fix",
97+
to="vulnerabilities.affectedbypackagerelatedvulnerability",
98+
),
99+
),
100+
(
101+
"base_package_version",
102102
models.ForeignKey(
103103
blank=True,
104-
help_text="The base version of the package to which this code change applies.",
104+
help_text="The base package version to which this code change applies.",
105105
null=True,
106106
on_delete=django.db.models.deletion.SET_NULL,
107-
related_name="base_version_codechanges",
107+
related_name="codechanges",
108108
to="vulnerabilities.package",
109109
),
110110
),
111111
(
112-
"package_vulnerabilities",
113-
models.ManyToManyField(
114-
help_text="The vulnerabilities fixed by this code change.",
115-
related_name="code_fixes",
116-
to="vulnerabilities.affectedbypackagerelatedvulnerability",
112+
"fixed_package_vulnerability",
113+
models.ForeignKey(
114+
blank=True,
115+
help_text="The fixing package version with this code fix",
116+
null=True,
117+
on_delete=django.db.models.deletion.SET_NULL,
118+
related_name="code_fix",
119+
to="vulnerabilities.fixingpackagerelatedvulnerability",
117120
),
118121
),
119122
],

vulnerabilities/pipelines/collect_commits.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@
1010
import re
1111

1212
from aboutcode.pipeline import LoopProgress
13-
from packageurl.contrib.url2purl import url2purl
1413

1514
from vulnerabilities.models import AffectedByPackageRelatedVulnerability
1615
from vulnerabilities.models import CodeFix
17-
from vulnerabilities.models import FixingPackageRelatedVulnerability
18-
from vulnerabilities.models import Package
19-
from vulnerabilities.models import VulnerabilityReference
2016
from vulnerabilities.pipelines import VulnerableCodePipeline
2117

2218

@@ -59,8 +55,7 @@ def collect_and_store_fix_commits(self):
5955
affected_by_package_related_vulnerabilities.paginated(per_page=500)
6056
):
6157
vulnerability = apv.vulnerability
62-
for reference in vulnerability.references:
63-
58+
for reference in vulnerability.references.all():
6459
if not is_vcs_url(reference.url):
6560
continue
6661

@@ -171,6 +166,7 @@ def normalize_vcs_url(repo_url, vcs_tool=None):
171166

172167
# FIXME: where these URL schemes come from??
173168
if repo_url.startswith(("bitbucket:", "gitlab:", "github:", "gist:")):
169+
repo = repo_url.split(":")[1]
174170
hoster_urls = {
175171
"bitbucket": f"https://bitbucket.org/{repo}",
176172
"github": f"https://github.com/{repo}",
@@ -236,12 +232,15 @@ def is_vcs_url(repo_url):
236232
if not repo_url:
237233
return False
238234

239-
# 1. Match URLs with standard protocols
240-
if re.match(r"^(git|ssh|http|https)://", repo_url):
235+
# Define valid VCS domains
236+
vcs_domains = r"(github\.com|gitlab\.com|bitbucket\.org|gist\.github\.com)"
237+
238+
# 1. Match URLs with standard protocols pointing to VCS domains
239+
if re.match(rf"^(git|ssh|http|https)://{vcs_domains}/[\w\-.]+/[\w\-.]+", repo_url):
241240
return True
242241

243242
# 2. Match SSH URLs (e.g., git@github.com:user/repo.git)
244-
if re.match(r"^git@\w+\.\w+:[\w\-./]+$", repo_url):
243+
if re.match(rf"^git@{vcs_domains}:[\w\-.]+/[\w\-.]+(\.git)?$", repo_url):
245244
return True
246245

247246
# 3. Match shortcut syntax (e.g., github:user/repo)

0 commit comments

Comments
 (0)