Skip to content

Commit

Permalink
Add: working unit tests, Refractor
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielschowe committed Sep 24, 2024
1 parent cce53d8 commit 5d1b1d4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 68 deletions.
121 changes: 57 additions & 64 deletions pontos/github/api/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,6 @@ async def exists(
response = await self._client.get(api)
return response.is_success

async def packages(
self, organization: str, package_type: str
) -> AsyncIterator[Package]:
"""
Get information about organization packages
https://docs.github.com/en/rest/reference/packages#list-packages-for-an-organization
Args:
organization: GitHub organization to use
package_type: Type of the packages to list
Raises:
`httpx.HTTPStatusError`: If there was an error in the request
Returns:
An async iterator yielding packages information
Example:
.. code-block:: python
from pontos.github.api import GitHubAsyncRESTApi
async with GitHubAsyncRESTApi(token) as api:
async for package in api.packages.packages(
organization="foo",
package_type="container",
):
print(package)
"""
api = f"/orgs/{organization}/packages/{package_type}"
params = {"per_page": "100"}
async for response in self._client.get_all(api, params=params):
response.raise_for_status()

for package in response.json():
yield Package.from_dict(package)

async def package(
self, organization: str, package_type: PackageType, package_name: str
) -> Package:
Expand Down Expand Up @@ -113,48 +74,44 @@ async def package(
response.raise_for_status()
return Package.from_dict(response.json())

async def package_versions(
self, organization: str, package_type: PackageType, package_name: str
) -> AsyncIterator[PackageVersion]:
async def packages(
self, organization: str, package_type: str
) -> AsyncIterator[Package]:
"""
Get information about package versions
https://docs.github.com/en/rest/reference/packages#list-package-versions-for-an-organization
Get information about organization packages
https://docs.github.com/en/rest/reference/packages#list-packages-for-an-organization
Args:
organization: GitHub organization to use
package_type: Type of the package to get
package_name: Name of the package to get
package_type: Type of the packages to list
Raises:
`httpx.HTTPStatusError`: If there was an error in the request
Returns:
An async iterator yielding package versions
An async iterator yielding packages information
Example:
.. code-block:: python
from pontos.github.api import GitHubAsyncRESTApi
async with GitHubAsyncRESTApi(token) as api:
async for package in api.packages.package_versions(
async for package in api.packages.packages(
organization="foo",
package_type="container",
package_name="bar",
):
print(package)
"""
api = f"/orgs/{organization}/packages/{package_type}/{package_name}/versions"
async for response in self._client.get_all(api):
api = f"/orgs/{organization}/packages/{package_type}"
params = {"per_page": "100"}
async for response in self._client.get_all(api, params=params):
response.raise_for_status()

versions = response.json()
if versions:
for version in versions:
yield PackageVersion.from_dict(version)
for package in response.json():
yield Package.from_dict(package)

async def package_version(
self,
Expand Down Expand Up @@ -201,6 +158,48 @@ async def package_version(
raise GitHubApiError(response)
return PackageVersion.from_dict(response.json())

async def package_versions(
self, organization: str, package_type: PackageType, package_name: str
) -> AsyncIterator[PackageVersion]:
"""
Get information about package versions
https://docs.github.com/en/rest/reference/packages#list-package-versions-for-an-organization
Args:
organization: GitHub organization to use
package_type: Type of the package to get
package_name: Name of the package to get
Raises:
`httpx.HTTPStatusError`: If there was an error in the request
Returns:
An async iterator yielding package versions
Example:
.. code-block:: python
from pontos.github.api import GitHubAsyncRESTApi
async with GitHubAsyncRESTApi(token) as api:
async for package in api.packages.package_versions(
organization="foo",
package_type="container",
package_name="bar",
):
print(package)
"""
api = f"/orgs/{organization}/packages/{package_type}/{package_name}/versions"
async for response in self._client.get_all(api):
response.raise_for_status()
versions = response.json()
if versions:
for version in versions:
yield PackageVersion.from_dict(version)

async def package_version_tags(
self,
organization: str,
Expand Down Expand Up @@ -246,7 +245,7 @@ async def package_version_tags(
if not response.is_success:
raise GitHubApiError(response)
resp = response.json()
return resp["metadata"][package_type]["tags"]
return resp["tags"]

async def delete_package(
self, organization: str, package_type: PackageType, package_name: str
Expand Down Expand Up @@ -356,16 +355,10 @@ async def delete_package_with_tag(
async for package_version in self.package_versions(
organization, package_type, package_name
):
print("id")
if tag in await self.package_version_tags(
organization, package_type, package_name, package_version.id
):
api = f"/orgs/{organization}/packages/{package_type}/{package_name}/versions/{package_version.id}"
response = await self._client.delete(api)
print("deleted")
if not response.is_success:
raise GitHubApiError(response)
api = f"/orgs/{organization}/packages/{package_type}/{package_name}/versions/tags/{tag}"
response = await self._client.delete(api)
if not response.is_success:
raise GitHubApiError(response)
23 changes: 19 additions & 4 deletions tests/github/api/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async def test_package_version_tags(self):
)

self.client.get.assert_awaited_once_with(
"/orgs/foo/packages/container/bar/versions/1/tags"
"/orgs/foo/packages/container/bar/versions/1"
)

self.assertEqual(tags, ["latest", "stable"])
Expand Down Expand Up @@ -241,12 +241,27 @@ async def test_delete_package_with_tag(self):
response = create_response(is_success=True)
self.client.delete.return_value = response

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

tags_response = create_response()
tags_response.json.return_value = {"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.delete.assert_awaited_once_with(
"/orgs/foo/packages/container/bar/versions/tags/latest"

self.client.get_all.assert_called_once_with(
"/orgs/foo/packages/container/bar/versions"
)
self.client.get.assert_awaited_once_with(
"/orgs/foo/packages/container/bar/versions/1"
)
self.client.delete.assert_awaited_once_with(
"/orgs/foo/packages/container/bar/versions/1"
)

0 comments on commit 5d1b1d4

Please sign in to comment.