From d5e96516206b74dd1dcbb0eee7280ec96dab5386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Wed, 1 Mar 2023 13:29:09 +0100 Subject: [PATCH] Add: Add scripts for downloading release assets and workflow artifacts With these scripts both downloading release assets and downloading workflow run artifacts can be tested easily. --- scripts/github/artifacts-download.py | 53 +++++++++++++++++++ scripts/github/release-assets-download.py | 63 +++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 scripts/github/artifacts-download.py create mode 100644 scripts/github/release-assets-download.py diff --git a/scripts/github/artifacts-download.py b/scripts/github/artifacts-download.py new file mode 100644 index 00000000..a61844fa --- /dev/null +++ b/scripts/github/artifacts-download.py @@ -0,0 +1,53 @@ +# Copyright (C) 2022 Greenbone Networks GmbH +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" +This script downloads a single artifacts of a given repository +""" + +from argparse import ArgumentParser, Namespace +from pathlib import Path + +from rich.progress import Progress + +from pontos.github.api import GitHubAsyncRESTApi + + +def add_script_arguments(parser: ArgumentParser) -> None: + parser.add_argument("repository") + parser.add_argument("artifact", help="ID of the artifact to download") + parser.add_argument( + "--file", + help="File to write the artifact to. Default: %(default)s", + default="out.file", + type=Path, + ) + + +async def github_script(api: GitHubAsyncRESTApi, args: Namespace) -> int: + with args.file.open("wb") as f, Progress() as rich_progress: + task_id = rich_progress.add_task( + f"[red]Downloading Artifact {args.artifact}... ", total=None + ) + async with api.artifacts.download( + args.repository, args.artifact + ) as download: + async for content, progress in download: + rich_progress.advance(task_id, progress or 1) + f.write(content) + + rich_progress.update(task_id, total=1, completed=1) diff --git a/scripts/github/release-assets-download.py b/scripts/github/release-assets-download.py new file mode 100644 index 00000000..f9423cbf --- /dev/null +++ b/scripts/github/release-assets-download.py @@ -0,0 +1,63 @@ +# Copyright (C) 2022 Greenbone Networks GmbH +# +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +""" +This script downloads a single artifacts of a given repository +""" + +from argparse import ArgumentParser, Namespace +from pathlib import Path + +from rich.progress import Progress + +from pontos.github.api import GitHubAsyncRESTApi + + +def add_script_arguments(parser: ArgumentParser) -> None: + parser.add_argument("repository") + parser.add_argument("tag", help="Release Tag") + parser.add_argument( + "--file", + help="File to write the artifact to. Default: %(default)s", + default="out.file", + type=Path, + ) + parser.add_argument( + "--type", + choices=["zip", "tar"], + help="Download release asset type. Default: %(default)s", + default="tar", + ) + + +async def github_script(api: GitHubAsyncRESTApi, args: Namespace) -> int: + with args.file.open("wb") as f, Progress() as rich_progress: + task_id = rich_progress.add_task( + f"[red]Downloading asset for tag {args.tag} as {args.type}... ", + total=None, + ) + download_api = ( + api.releases.download_release_tarball + if args.type == "tar" + else api.releases.download_release_zip + ) + async with download_api(args.repository, args.tag) as download: + async for content, progress in download: + rich_progress.advance(task_id, progress or 1) + f.write(content) + + rich_progress.update(task_id, total=1, completed=1)