fix: Restore consistency in Api interface and return types

This commit is contained in:
oSumAtrIX 2023-11-03 23:04:30 +01:00
parent 3c3edb3cf8
commit 62280eab91
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
4 changed files with 60 additions and 46 deletions

View File

@ -16,36 +16,42 @@ class Api:
self._api_key = api_key
@abstractmethod
async def get_release(
async def get_releases(
self, repository: str, all: bool = False, prerelease: bool = False
) -> dict | list:
"""Gets the release(s) for a repository.
) -> list:
"""Gets the releases for a repository.
Args:
repository (str): The repository to get releases for.
all (bool, optional): Whether to get all releases or not. Defaults to False.
prerelease (bool, optional): Whether to get prereleases or not. Defaults to False.
Returns:
dict | list: The release(s) for the repository.
list: The releases for the repository.
"""
raise NotImplementedError
@abstractmethod
async def get_contributor(self, repository):
async def get_contributors(self, repository) -> list:
"""Gets the contributors for a repository.
Args:
repository (str): The repository to get contributors for.
Returns:
list: The contributors for the repository.
"""
raise NotImplementedError
@abstractmethod
async def get_members(self, organization):
'''Gets the team for an organization.
async def get_members(self, organization) -> list:
"""Gets the team for an organization.
Args:
organization (str): The organization to get the team for.
'''
Returns:
list: The team for the organization.
"""
raise NotImplementedError
@abstractmethod
@ -63,7 +69,7 @@ class GitHubApi(Api):
super().__init__()
pass
async def get_contributor(self, repository):
async def get_contributors(self, repository) -> list:
def transform_contributor(contributor: dict) -> dict:
"""Transforms a contributor into a dict.
@ -94,10 +100,9 @@ class GitHubApi(Api):
contributors.sort(key=sort_and_delete_key, reverse=True)
return contributors
# TODO: Return a list of objects instead of a dict.
async def get_release(
async def get_releases(
self, repository: str, all: bool = False, prerelease: bool = False
) -> dict | list:
) -> list:
def transform_release(release: dict) -> dict:
"""Transforms a release dict into a dict.
@ -131,9 +136,9 @@ class GitHubApi(Api):
else:
async with self._client_session.get(f"https://api.github.com/repos/{repository}/releases/latest?prerelease={prerelease}") as resp:
latest_release = await resp.json()
return transform_release(latest_release)
return [transform_release(latest_release)]
async def get_members(self, organization):
async def get_members(self, organization) -> list:
def transform_team_member(member: dict) -> dict:
'''Transforms a team member into a dict.

View File

@ -2,8 +2,10 @@ from dependency_injector import providers, containers
from app.api import GitHubApi
class ApiContainer(containers.DeclarativeContainer):
api = providers.Singleton(GitHubApi)
def wire_dependencies():
ApiContainer().wire(modules=["app.generator"])

View File

@ -50,29 +50,32 @@ class ReleasesGenerator(Generator):
repositories = config["repositories"]
for repository in repositories:
release = await self._api.get_release(repository)
releases = await self._api.get_releases(repository)
repository_name = get_repository_name(repository)
index_path = join(path, f"{repository_name}.json")
index = read_json(index_path, [])
for i, release in enumerate(releases):
tag = release["tag"]
release_path = join(path, repository_name)
release_json = to_json(release)
release_json = to_json(releases)
# Create the release directory if it doesn't exist and write the release file
create_if_not_exists(release_path)
write_json(release_json, join(
release_path, f"{tag}.json"), overwrite=False)
write_json(
release_json, join(release_path, "latest.json")
) # Overwrite the latest release
# At last join the current tag to an index file
index_path = join(path, f"{repository_name}.json")
if i == 0:
# Overwrite the latest release file
write_json(release_json, join(release_path, "latest.json"))
index = read_json(index_path, [])
if tag not in index: # TODO: Check if there a better way to do this
index.append(tag) # Add the current tag to the index
if tag not in index:
# Add the current tag to the index
index.append(tag)
# Overwrite the index file with the new releases
write_json(index, index_path)
@ -97,7 +100,7 @@ class ContributorsGenerator(Generator):
for repository in repositories:
repository_name = get_repository_name(repository)
contributors = await self._api.get_contributor(repository)
contributors = await self._api.get_contributors(repository)
contributors_path = join(path, f"{repository_name}.json")
write_json(contributors, contributors_path)
@ -226,7 +229,9 @@ class RemoveAnnouncementGenerator(Generator):
pass
generators = {
# This function is needed. A variable in the global scope cannot be used, because the dependency injector is not initialized yet.
def get_generators():
return {
generator.name: generator for generator in [
ReleasesGenerator(),
ContributorsGenerator(),
@ -236,4 +241,4 @@ generators = {
AnnouncementGenerator(),
RemoveAnnouncementGenerator()
]
}
}

View File

@ -4,7 +4,7 @@ import os
import shutil
from app.config import load_config
from app.dependencies import wire_dependencies
from app.generator import generators
from app.generator import get_generators
def main():
@ -16,6 +16,8 @@ def main():
def generate(config):
generators = get_generators()
loop = asyncio.get_event_loop()
tasks = []