Skip to content

Commit 8723c4a

Browse files
committed
Move generate_patch_url function to utils
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent 6583958 commit 8723c4a

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

vulnerabilities/improvers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
from vulnerabilities.pipelines.v2_improvers import (
3131
enhance_with_metasploit as enhance_with_metasploit_v2,
3232
)
33+
from vulnerabilities.pipelines.v2_improvers import fetch_patch_url as fetch_patch_url_v2
3334
from vulnerabilities.pipelines.v2_improvers import flag_ghost_packages as flag_ghost_packages_v2
3435
from vulnerabilities.pipelines.v2_improvers import relate_severities
3536
from vulnerabilities.pipelines.v2_improvers import unfurl_version_range as unfurl_version_range_v2
36-
from vulnerabilities.pipelines.v2_improvers import fetch_patch_url as fetch_patch_url_v2
3737
from vulnerabilities.utils import create_registry
3838

3939
IMPROVERS_REGISTRY = create_registry(

vulnerabilities/pipelines/v2_improvers/fetch_patch_url.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

10-
from vulnerabilities.models import PackageCommitPatch, Patch
10+
from vulnerabilities.models import PackageCommitPatch
11+
from vulnerabilities.models import Patch
1112
from vulnerabilities.pipelines import VulnerableCodePipeline
1213
from vulnerabilities.utils import fetch_response
14+
from vulnerabilities.utils import generate_patch_url
1315

1416

1517
class FetchPatchURLImproverPipeline(VulnerableCodePipeline):
@@ -20,9 +22,7 @@ class FetchPatchURLImproverPipeline(VulnerableCodePipeline):
2022

2123
@classmethod
2224
def steps(cls):
23-
return (
24-
cls.collect_patch_text,
25-
)
25+
return (cls.collect_patch_text,)
2626

2727
def fetch_patch_content(self, url):
2828
"""
@@ -42,8 +42,8 @@ def fetch_patch_content(self, url):
4242

4343
def advisories_count(self) -> int:
4444
return (
45-
PackageCommitPatch.objects.filter(patch_text__isnull=True).count() +
46-
Patch.objects.filter(patch_text__isnull=True).count()
45+
PackageCommitPatch.objects.filter(patch_text__isnull=True).count()
46+
+ Patch.objects.filter(patch_text__isnull=True).count()
4747
)
4848

4949
def collect_patch_text(self):
@@ -62,22 +62,3 @@ def collect_patch_text(self):
6262

6363
patch.patch_text = content
6464
patch.save()
65-
66-
def generate_patch_url(vcs_url, commit_hash):
67-
"""
68-
Generate patch URL from VCS URL and commit hash.
69-
"""
70-
if not vcs_url or not commit_hash:
71-
return None
72-
73-
vcs_url = vcs_url.rstrip("/")
74-
75-
if vcs_url.startswith("https://github.com"):
76-
return f"{vcs_url}/commit/{commit_hash}.patch"
77-
elif vcs_url.startswith("https://gitlab.com"):
78-
return f"{vcs_url}/-/commit/{commit_hash}.patch"
79-
elif vcs_url.startswith("https://bitbucket.org"):
80-
return f"{vcs_url}/-/commit/{commit_hash}/raw"
81-
elif vcs_url.startswith("https://git.kernel.org"):
82-
return f"{vcs_url}.git/patch/?id={commit_hash}"
83-
return

vulnerabilities/tests/pipelines/v2_improvers/test_fetch_patch_url.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
import pytest
1414

15-
from vulnerabilities.models import PackageCommitPatch, Patch
15+
from vulnerabilities.models import PackageCommitPatch
16+
from vulnerabilities.models import Patch
1617
from vulnerabilities.pipelines.v2_improvers.fetch_patch_url import FetchPatchURLImproverPipeline
1718

1819

@@ -24,14 +25,11 @@ def test_collect_patch_text_success(mock_get):
2425
mock_get.side_effect = [res1, res2]
2526

2627
pcp = PackageCommitPatch.objects.create(
27-
vcs_url="https://github.com/nexB/vulnerablecode",
28-
commit_hash="abc1234",
29-
patch_text=None
28+
vcs_url="https://github.com/nexB/vulnerablecode", commit_hash="abc1234", patch_text=None
3029
)
3130

3231
patch = Patch.objects.create(
33-
patch_url="https://gitlab.com/nexB/vulnerablecode/-/commit/def5678.patch",
34-
patch_text=None
32+
patch_url="https://gitlab.com/nexB/vulnerablecode/-/commit/def5678.patch", patch_text=None
3533
)
3634
pipeline = FetchPatchURLImproverPipeline()
3735
pipeline.collect_patch_text()
@@ -42,17 +40,16 @@ def test_collect_patch_text_success(mock_get):
4240
assert pcp.patch_text == "diff --git a/file1"
4341
assert patch.patch_text == "diff --git a/file2"
4442

43+
4544
@pytest.mark.django_db
4645
@mock.patch("vulnerabilities.utils.requests.get")
4746
def test_collect_patch_text_failure(mock_get):
4847
mock_get.side_effect = Exception("Connection Error")
4948

5049
pcp = PackageCommitPatch.objects.create(
51-
vcs_url="https://github.com/nexB/vulnerablecode",
52-
commit_hash="abc1234",
53-
patch_text=None
50+
vcs_url="https://github.com/nexB/vulnerablecode", commit_hash="abc1234", patch_text=None
5451
)
5552

5653
pipeline = FetchPatchURLImproverPipeline()
5754
pipeline.collect_patch_text()
58-
assert pcp.patch_text is None
55+
assert pcp.patch_text is None

vulnerabilities/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,3 +867,23 @@ def group_advisories_by_content(advisories):
867867
entry["secondary"].add(advisory)
868868

869869
return grouped
870+
871+
872+
def generate_patch_url(vcs_url, commit_hash):
873+
"""
874+
Generate patch URL from VCS URL and commit hash.
875+
"""
876+
if not vcs_url or not commit_hash:
877+
return None
878+
879+
vcs_url = vcs_url.rstrip("/")
880+
881+
if vcs_url.startswith("https://github.com"):
882+
return f"{vcs_url}/commit/{commit_hash}.patch"
883+
elif vcs_url.startswith("https://gitlab.com"):
884+
return f"{vcs_url}/-/commit/{commit_hash}.patch"
885+
elif vcs_url.startswith("https://bitbucket.org"):
886+
return f"{vcs_url}/-/commit/{commit_hash}/raw"
887+
elif vcs_url.startswith("https://git.kernel.org"):
888+
return f"{vcs_url}.git/patch/?id={commit_hash}"
889+
return

0 commit comments

Comments
 (0)