Skip to content

Commit

Permalink
Add: Script to use delete by tag
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielschowe committed Sep 24, 2024
1 parent 5d1b1d4 commit 27c8a23
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pontos/github/api/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ async def package_version_tags(
if not response.is_success:
raise GitHubApiError(response)
resp = response.json()
return resp["tags"]
return resp["metadata"]["container"]["tags"]

async def delete_package(
self, organization: str, package_type: PackageType, package_name: str
Expand Down Expand Up @@ -355,9 +355,11 @@ async def delete_package_with_tag(
async for package_version in self.package_versions(
organization, package_type, package_name
):
print("in the loop")
if tag in await self.package_version_tags(
organization, package_type, package_name, package_version.id
):
print("tag found")
api = f"/orgs/{organization}/packages/{package_type}/{package_name}/versions/{package_version.id}"
response = await self._client.delete(api)
if not response.is_success:
Expand Down
44 changes: 44 additions & 0 deletions pontos/github/scripts/delete-package-with-tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
This script delete a package from a repository, if it contains the specified 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(
"--package-type",
type=package_type,
help="package type",
default=PackageType.CONTAINER,
)
parser.add_argument('tag', help='The tag to be deleted.')

async def github_script(api: GitHubAsyncRESTApi, args: Namespace) -> None:
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
print(f"Found package {args.package} in organization {args.organization}")

await api.packages.delete_package_with_tag(
organization=args.organization,
package_name=args.package,
package_type=args.package_type,
tag=args.tag,
)
print(f"Deleted tag {args.tag} from package {args.package} in organization {args.organization}")
12 changes: 7 additions & 5 deletions tests/github/api/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async def test_package_version(self):

async def test_package_version_tags(self):
response = create_response()
response.json.return_value = {"tags": ["latest", "stable"]}
response.json.return_value = {"metadata": {"container": {"tags": ["latest", "stable"]}}}

self.client.get.return_value = response

Expand Down Expand Up @@ -243,17 +243,19 @@ async def test_delete_package_with_tag(self):

package_version_response = create_response()
package_version_response.json.return_value = [PACKAGE_VERSION]
self.client.get_all.return_value = AsyncIteratorMock([package_version_response])
self.client.get_all.return_value = AsyncIteratorMock(
[package_version_response]
)

tags_response = create_response()
tags_response.json.return_value = {"tags": ["latest", "stable"]}
tags_response.json.return_value = {"metadata": {"container": {"tags": ["latest", "stable"]}}}
self.client.get.return_value = tags_response

await self.api.delete_package_with_tag(
organization="foo",
package_type=PackageType.CONTAINER,
package_name="bar",
tag="latest"
tag="latest",
)

self.client.get_all.assert_called_once_with(
Expand All @@ -264,4 +266,4 @@ async def test_delete_package_with_tag(self):
)
self.client.delete.assert_awaited_once_with(
"/orgs/foo/packages/container/bar/versions/1"
)
)

0 comments on commit 27c8a23

Please sign in to comment.