sbp commented on code in PR #430: URL: https://github.com/apache/tooling-trusted-releases/pull/430#discussion_r2635609427
########## atr/sbom/tool.py: ########## @@ -0,0 +1,108 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import datetime +from typing import TYPE_CHECKING, Any, Final + +from . import maven, models + +if TYPE_CHECKING: + from collections.abc import Callable + +try: + from atr import metadata + + atr_version = metadata.version +except ImportError: + metadata = None + atr_version = "cli" + +_KNOWN_TOOLS: Final[dict[str, tuple[str, str, Callable[[str], str | None]]]] = { + # name in file: ( canonical name, friendly name, version callable ) + "cyclonedx-maven-plugin": ("cyclonedx-maven-plugin", "CycloneDX Maven Plugin", maven.version_as_of), + "cyclonedx maven plugin": ("cyclonedx-maven-plugin", "CycloneDX Maven Plugin", maven.version_as_of), + "apache trusted releases": ("apache trusted releases", "Apache Trusted Releases platform", lambda _: atr_version), +} + + +def plugin_outdated_version(bom_value: models.bom.Bom) -> list[models.tool.Outdated] | None: + if bom_value.metadata is None: + return [models.tool.OutdatedMissingMetadata()] + timestamp = bom_value.metadata.timestamp + if timestamp is None: + # This quite often isn't available + # We could use the file mtime, but that's extremely heuristic + # return OutdatedMissingTimestamp() + timestamp = datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%dT%H:%M:%SZ") + tools: list[Any] = [] + tools_value = bom_value.metadata.tools + if isinstance(tools_value, list): + tools = tools_value + elif tools_value: + tools = tools_value.components or [] + services = tools_value.services or [] + tools.extend(services) + errors = [] + for tool in tools: + name_or_description = (tool.name or tool.description or "").lower() + if name_or_description not in _KNOWN_TOOLS: + continue + if tool.version is None: + errors.append(models.tool.OutdatedMissingVersion(name=name_or_description)) + _, name, version_func = _KNOWN_TOOLS[name_or_description] + available_version = outdated_version_core(timestamp, tool.version, version_func) + if available_version is not None: + errors.append( + models.tool.OutdatedTool( + name=name, + used_version=tool.version, + available_version=available_version, + ) + ) + return errors + + +def outdated_version_core(isotime: str, version: str, version_as_of: Callable[[str], str | None]) -> str | None: + expected_version = version_as_of(isotime) + if expected_version is None: + return None + if version == expected_version: + return None + expected_version_comparable = version_parse(expected_version) + version_comparable = version_parse(version) + if expected_version_comparable is None or version_comparable is None: + # Couldn't parse the version + return None + # If the version used is less than the version available + if version_comparable[0] < expected_version_comparable[0]: + # Then note the version available + return expected_version + # Otherwise, the user is using the latest version + return None + + +def version_parse(version_str: str) -> tuple[tuple[int, int, int], str] | None: Review Comment: I started to document some proposed conventions for Tooling Python project versions in https://github.com/apache/tooling-asf-example#proposed-conventions but there are still a few details to discuss. We should probably then migrate ATR to the outcome of that discussion, even though we won't be releasing ATR on any platforms. It would be good to make ATR consistent with packages that we do release, such as the ATR CLI for example. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
