Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions vulnerabilities/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,29 @@ def test_resolve_version_range_without_ignorable_versions():
def test_get_severity_range():
assert get_severity_range({""}) is None
assert get_severity_range({}) is None


def test_get_advisory_url():
from pathlib import Path

from vulnerabilities.utils import get_advisory_url

# Test case 1: Both parameters as strings (the bug fix scenario)
file_str = "/tmp/advisories/istio/ISTIO-2021-001.yaml"
base_str = "/tmp/advisories"
url = "https://github.com/istio/istio.io/tree/master/advisories/"

result = get_advisory_url(file_str, base_str, url)
expected = "https://github.com/istio/istio.io/tree/master/advisories/istio/ISTIO-2021-001.yaml"
assert result == expected

# Test case 2: Both parameters as Path objects
file_path = Path("/tmp/advisories/istio/ISTIO-2021-001.yaml")
base_path = Path("/tmp/advisories")

result = get_advisory_url(file_path, base_path, url)
assert result == expected

# Test case 3: Mixed - file as Path, base_path as string
result = get_advisory_url(file_path, base_str, url)
assert result == expected
5 changes: 5 additions & 0 deletions vulnerabilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from collections import defaultdict
from functools import total_ordering
from http import HTTPStatus
from pathlib import Path
from typing import List
from typing import Optional
from typing import Tuple
Expand Down Expand Up @@ -543,6 +544,10 @@ def get_advisory_url(file, base_path, url):
"""
Return the advisory URL constructed by combining the base URL with the relative file path.
"""
if isinstance(file, str):
file = Path(file)
if isinstance(base_path, str):
base_path = Path(base_path)
relative_path = str(file.relative_to(base_path)).strip("/")
advisory_url = urljoin(url, relative_path)
return advisory_url
Expand Down
Loading