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

View File

@ -50,29 +50,32 @@ class ReleasesGenerator(Generator):
repositories = config["repositories"] repositories = config["repositories"]
for repository in 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) 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"] tag = release["tag"]
release_path = join(path, repository_name) 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) create_if_not_exists(release_path)
write_json(release_json, join( write_json(release_json, join(
release_path, f"{tag}.json"), overwrite=False) 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 if i == 0:
index_path = join(path, f"{repository_name}.json") # Overwrite the latest release file
write_json(release_json, join(release_path, "latest.json"))
index = read_json(index_path, []) if tag not in index:
if tag not in index: # TODO: Check if there a better way to do this # Add the current tag to the index
index.append(tag) # Add the current tag to the index index.append(tag)
# Overwrite the index file with the new releases
write_json(index, index_path) write_json(index, index_path)
@ -97,7 +100,7 @@ class ContributorsGenerator(Generator):
for repository in repositories: for repository in repositories:
repository_name = get_repository_name(repository) 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") contributors_path = join(path, f"{repository_name}.json")
write_json(contributors, contributors_path) write_json(contributors, contributors_path)
@ -226,7 +229,9 @@ class RemoveAnnouncementGenerator(Generator):
pass 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 [ generator.name: generator for generator in [
ReleasesGenerator(), ReleasesGenerator(),
ContributorsGenerator(), ContributorsGenerator(),

View File

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