From 4839352d63c7524302c521cd659945e094c69a92 Mon Sep 17 00:00:00 2001 From: Ben Selwyn-Smith Date: Thu, 30 Nov 2023 16:24:56 +1000 Subject: [PATCH] chore: differentiate abstract purl type; move regex escape call to more suitable location Signed-off-by: Ben Selwyn-Smith --- src/macaron/repo_finder/commit_finder.py | 20 +++++++++++--------- tests/repo_finder/test_commit_finder.py | 12 ++++++------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/macaron/repo_finder/commit_finder.py b/src/macaron/repo_finder/commit_finder.py index 846214b1b..7ed4191eb 100644 --- a/src/macaron/repo_finder/commit_finder.py +++ b/src/macaron/repo_finder/commit_finder.py @@ -103,8 +103,8 @@ versioned_string = re.compile("^[a-z]+[0-9]+$", flags=re.IGNORECASE) # e.g. RC1, M5, etc. -class PurlType(Enum): - """The type represented by a PURL in terms of repository versus artifact. +class AbstractPurlType(Enum): + """The type represented by a PURL in terms of repositories versus artifacts. Unsupported types are allowed as a third type. """ @@ -138,16 +138,16 @@ def find_commit(git_obj: Git, purl: PackageURL) -> tuple[str, str]: logger.debug("Missing version for analysis target: %s", purl.name) return "", "" - repo_type = abstract_purl_type(purl) - if repo_type == PurlType.REPOSITORY: + repo_type = determine_abstract_purl_type(purl) + if repo_type == AbstractPurlType.REPOSITORY: return extract_commit_from_version(git_obj, version) - if repo_type == PurlType.ARTIFACT: + if repo_type == AbstractPurlType.ARTIFACT: return find_commit_from_version_and_name(git_obj, re.escape(purl.name), version) logger.debug("Type of PURL is not supported for commit finding: %s", purl.type) return "", "" -def abstract_purl_type(purl: PackageURL) -> PurlType: +def determine_abstract_purl_type(purl: PackageURL) -> AbstractPurlType: """Determine if the passed purl is a repository type, artifact type, or unsupported type. Parameters @@ -164,14 +164,14 @@ def abstract_purl_type(purl: PackageURL) -> PurlType: domain = to_domain_from_known_purl_types(purl.type) or (purl.type if purl.type in available_domains else None) if domain: # PURL is a repository type. - return PurlType.REPOSITORY + return AbstractPurlType.REPOSITORY try: repo_finder_deps_dev.DepsDevType(purl.type) # PURL is an artifact type. - return PurlType.ARTIFACT + return AbstractPurlType.ARTIFACT except ValueError: # PURL is an unsupported type. - return PurlType.UNSUPPORTED + return AbstractPurlType.UNSUPPORTED def extract_commit_from_version(git_obj: Git, version: str) -> tuple[str, str]: @@ -404,6 +404,8 @@ def match_tags(tag_list: list[str], name: str, version: str) -> list[str]: list[str] The list of tags that matched the pattern. """ + name = re.escape(name) + # Create the pattern for the passed version. pattern, parts = _build_version_pattern(name, version) if not pattern: diff --git a/tests/repo_finder/test_commit_finder.py b/tests/repo_finder/test_commit_finder.py index c35674c3f..db151de2e 100644 --- a/tests/repo_finder/test_commit_finder.py +++ b/tests/repo_finder/test_commit_finder.py @@ -14,7 +14,7 @@ from packageurl import PackageURL from macaron.repo_finder import commit_finder -from macaron.repo_finder.commit_finder import PurlType +from macaron.repo_finder.commit_finder import AbstractPurlType from tests.slsa_analyzer.mock_git_utils import commit_files, initiate_repo logger: logging.Logger = logging.getLogger(__name__) @@ -62,7 +62,7 @@ def _test_version(tags: list[str], name: str, version: str, target_tag: str) -> "pkg:nuget/system.text.json@8.0.0", "pkg:cargo/mailmeld@1.0.0", ], - PurlType.ARTIFACT, + AbstractPurlType.ARTIFACT, id="Artifact PURLs", ), pytest.param( @@ -71,20 +71,20 @@ def _test_version(tags: list[str], name: str, version: str, target_tag: str) -> "pkg:github/oracle/macaron@v0.6.0", "pkg:bitbucket/owner/project@tag_5", ], - PurlType.REPOSITORY, + AbstractPurlType.REPOSITORY, id="Repository PURLs", ), pytest.param( ["pkg:gem/ruby-advisory-db-check@0.12.4", "pkg:unknown-domain/project/owner@tag"], - PurlType.UNSUPPORTED, + AbstractPurlType.UNSUPPORTED, id="Unsupported PURLs", ), ], ) -def test_abstract_purl_type(purls: list[str], expected: PurlType) -> None: +def test_abstract_purl_type(purls: list[str], expected: AbstractPurlType) -> None: """Test each purl in list is of expected type.""" for purl in purls: - assert commit_finder.abstract_purl_type(PackageURL.from_string(purl)) == expected + assert commit_finder.determine_abstract_purl_type(PackageURL.from_string(purl)) == expected def test_commit_finder() -> None: