Add types in api.py

This commit is contained in:
Stéphane Bidoul 2021-11-13 18:51:47 +01:00
parent 4d3e227d3f
commit 87a021137b
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92

View file

@ -8,7 +8,7 @@ from pydantic import BaseModel
from starlette.status import HTTP_404_NOT_FOUND from starlette.status import HTTP_404_NOT_FOUND
from . import github, models from . import github, models
from .controller import controller from .controller import Controller, controller
from .deps import authenticated from .deps import authenticated
from .settings import settings from .settings import settings
@ -60,12 +60,12 @@ class Build(BaseModel):
@router.get("/status", response_model=Status) @router.get("/status", response_model=Status)
async def controller_status(): async def controller_status() -> Controller:
return controller return controller
@router.get("/repos", response_model=list[Repo]) @router.get("/repos", response_model=list[Repo])
async def repos(): async def repos() -> list[models.Repo]:
return [models.Repo(name=name) for name in settings.supported_repos] return [models.Repo(name=name) for name in settings.supported_repos]
@ -74,7 +74,7 @@ async def repos():
response_model=list[Build], response_model=list[Build],
response_model_exclude_none=True, response_model_exclude_none=True,
) )
async def builds(repo: Optional[str] = None): async def builds(repo: Optional[str] = None) -> list[models.Build]:
return controller.db.search(repo) return controller.db.search(repo)
@ -82,7 +82,7 @@ async def builds(repo: Optional[str] = None):
"/builds/trigger/branch", "/builds/trigger/branch",
dependencies=[Depends(authenticated)], dependencies=[Depends(authenticated)],
) )
async def trigger_branch(repo: str, branch: str): async def trigger_branch(repo: str, branch: str) -> None:
"""Trigger build for a branch.""" """Trigger build for a branch."""
branch_info = await github.get_branch_info(repo, branch) branch_info = await github.get_branch_info(repo, branch)
await controller.deploy_or_start( await controller.deploy_or_start(
@ -97,7 +97,7 @@ async def trigger_branch(repo: str, branch: str):
"/builds/trigger/pr", "/builds/trigger/pr",
dependencies=[Depends(authenticated)], dependencies=[Depends(authenticated)],
) )
async def trigger_pull(repo: str, pr: int): async def trigger_pull(repo: str, pr: int) -> None:
"""Trigger build for a pull request.""" """Trigger build for a pull request."""
pull_info = await github.get_pull_info(repo, pr) pull_info = await github.get_pull_info(repo, pr)
await controller.deploy_or_start( await controller.deploy_or_start(
@ -116,7 +116,7 @@ async def _build_by_name(name: str) -> models.Build:
@router.get("/builds/{name}", response_model=Build) @router.get("/builds/{name}", response_model=Build)
async def build(name: str): async def build(name: str) -> models.Build:
return await _build_by_name(name) return await _build_by_name(name)
@ -145,21 +145,21 @@ async def log(name: str):
@router.post("/builds/{name}/start") @router.post("/builds/{name}/start")
async def start(name: str): async def start(name: str) -> None:
"""Start the deployment.""" """Start the deployment."""
build = await _build_by_name(name) build = await _build_by_name(name)
await build.start() await build.start()
@router.post("/builds/{name}/stop") @router.post("/builds/{name}/stop")
async def stop(name: str): async def stop(name: str) -> None:
"""Stop the deployment.""" """Stop the deployment."""
build = await _build_by_name(name) build = await _build_by_name(name)
await build.stop() await build.stop()
@router.delete("/builds/{name}", dependencies=[Depends(authenticated)]) @router.delete("/builds/{name}", dependencies=[Depends(authenticated)])
async def delete(name: str): async def delete(name: str) -> None:
"""Delete the deployment and drop the database.""" """Delete the deployment and drop the database."""
build = await _build_by_name(name) build = await _build_by_name(name)
await build.undeploy() await build.undeploy()