From c80e1d7fd1ab5e8ad1c2bef00fcd6b8f9a6d5dbc Mon Sep 17 00:00:00 2001 From: Jaspar Stach Date: Fri, 17 May 2024 08:43:36 +0200 Subject: [PATCH] Add: Test script to get the ID of a package with a specific tag. --- pontos/github/scripts/find-package-tag.py | 58 +++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 pontos/github/scripts/find-package-tag.py diff --git a/pontos/github/scripts/find-package-tag.py b/pontos/github/scripts/find-package-tag.py new file mode 100644 index 000000000..a0eda8f23 --- /dev/null +++ b/pontos/github/scripts/find-package-tag.py @@ -0,0 +1,58 @@ +# SPDX-FileCopyrightText: 2022-2023 Greenbone AG +# +# SPDX-License-Identifier: GPL-3.0-or-later +# + +""" +This script checks wether a package has a specific tag +""" + +from argparse import ArgumentParser, Namespace + +from pontos.github.api import GitHubAsyncRESTApi +from pontos.github.models.packages import PackageType + + +def package_type(value: str) -> PackageType: + if isinstance(value, PackageType): + return value + return PackageType(value.lower()) + + +def add_script_arguments(parser: ArgumentParser) -> None: + parser.add_argument("organization", help="organization name") + parser.add_argument("package", help="package name") + parser.add_argument("tag", help="tag to check") + parser.add_argument( + "--package-type", + type=package_type, + help="package type", + default=PackageType.CONTAINER, + ) + + +async def github_script(api: GitHubAsyncRESTApi, args: Namespace) -> int: + if not await api.packages.exists( + organization=args.organization, + package_name=args.package, + package_type=args.package_type, + ): + print( + f"Package {args.package} does not exist in organization {args.organization}" + ) + return 1 + + async for package in api.packages.package_versions( + organization=args.organization, + package_name=args.package, + package_type=args.package_type, + ): + if package.metadata.container.tags: + if args.tag in package.metadata.container.tags: + print( + f"Package {args.package} with id {package.id} has tag {args.tag}" + ) + return 0 + + print(f"Package {args.package} does not have tag {args.tag}") + return 0