Skip to content

Commit

Permalink
chore: differentiate abstract purl type; move regex escape call to mo…
Browse files Browse the repository at this point in the history
…re suitable location

Signed-off-by: Ben Selwyn-Smith <[email protected]>
  • Loading branch information
benmss committed Nov 30, 2023
1 parent c27bd35 commit 4839352
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 11 additions & 9 deletions src/macaron/repo_finder/commit_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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
Expand All @@ -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]:
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions tests/repo_finder/test_commit_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -62,7 +62,7 @@ def _test_version(tags: list[str], name: str, version: str, target_tag: str) ->
"pkg:nuget/[email protected]",
"pkg:cargo/[email protected]",
],
PurlType.ARTIFACT,
AbstractPurlType.ARTIFACT,
id="Artifact PURLs",
),
pytest.param(
Expand All @@ -71,20 +71,20 @@ def _test_version(tags: list[str], name: str, version: str, target_tag: str) ->
"pkg:github/oracle/[email protected]",
"pkg:bitbucket/owner/project@tag_5",
],
PurlType.REPOSITORY,
AbstractPurlType.REPOSITORY,
id="Repository PURLs",
),
pytest.param(
["pkg:gem/[email protected]", "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:
Expand Down

0 comments on commit 4839352

Please sign in to comment.