Some more typing

This commit is contained in:
Stéphane Bidoul 2021-10-17 15:43:14 +02:00
parent ac0bd848d4
commit 6020c9b25b
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92
6 changed files with 39 additions and 20 deletions

View file

@ -19,10 +19,26 @@ dependencies = [
] ]
dynamic = ["version", "description"] dynamic = ["version", "description"]
[project.optional-dependencies]
test = [
"pytest",
"pytest-cov",
]
mypy = [
"mypy",
"sqlalchemy[mypy]",
"types-requests",
]
[project.urls] [project.urls]
Home = "https://github.com/sbidoul/runboat" Home = "https://github.com/sbidoul/runboat"
[tool.isort] [tool.isort]
profile = 'black' profile = 'black'
[tool.mypy]
plugins = [
"sqlalchemy.ext.mypy.plugin",
]
# flake8 config is in .flake8 # flake8 config is in .flake8

View file

@ -117,8 +117,8 @@ def stop(repo_id: str, branch_id: str, build_id: str):
) )
def trigger_branch(org: str, repo: str, branch: str, db: Session = Depends(get_db)): def trigger_branch(org: str, repo: str, branch: str, db: Session = Depends(get_db)):
branch_info = github.get_branch_info(org, repo, branch) branch_info = github.get_branch_info(org, repo, branch)
branch = models.Branch.for_github_branch(db, branch_info) _branch = models.Branch.for_github_branch(db, branch_info)
return models.Build.for_branch(db, branch, branch_info.head_sha) return models.Build.for_branch(db, _branch, branch_info.head_sha)
@app.post( @app.post(
@ -127,5 +127,5 @@ def trigger_branch(org: str, repo: str, branch: str, db: Session = Depends(get_d
) )
def trigger_pr(org: str, repo: str, pr: int, db: Session = Depends(get_db)): def trigger_pr(org: str, repo: str, pr: int, db: Session = Depends(get_db)):
pr_info = github.get_pr_info(org, repo, pr) pr_info = github.get_pr_info(org, repo, pr)
branch = models.Branch.for_github_pr(db, pr_info) _branch = models.Branch.for_github_pr(db, pr_info)
return models.Build.for_branch(db, branch, pr_info.head_sha) return models.Build.for_branch(db, _branch, pr_info.head_sha)

View file

@ -6,5 +6,5 @@ app = FastAPI(title="Runboat")
@app.on_event("startup") @app.on_event("startup")
async def startup(): async def startup() -> None:
create_tables() create_tables()

View file

@ -1,3 +1,5 @@
from typing import Generator
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, sessionmaker from sqlalchemy.orm import Session, sessionmaker
@ -9,11 +11,11 @@ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base() Base = declarative_base()
def create_tables(): def create_tables() -> None:
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
def get_db() -> Session: def get_db() -> Generator[Session]:
db = SessionLocal() db = SessionLocal()
try: try:
yield db yield db

View file

@ -1,10 +1,12 @@
from typing import List
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func
from sqlalchemy.ext.compiler import compiles from sqlalchemy.ext.compiler import compiles
from sqlalchemy.orm import relationship from sqlalchemy.orm import Session, relationship
from sqlalchemy.sql import expression from sqlalchemy.sql import expression
from .build_images import get_build_image from .build_images import get_build_image
from .db import Base, Session from .db import Base
from .exceptions import RepoNotFound from .exceptions import RepoNotFound
from .github import BranchInfo, PullRequestInfo from .github import BranchInfo, PullRequestInfo
from .settings import settings from .settings import settings
@ -28,7 +30,7 @@ class Repo(Base):
org = Column(String, nullable=False) org = Column(String, nullable=False)
name = Column(String, nullable=False) name = Column(String, nullable=False)
branches = relationship("Branch", back_populates="repo") branches: List["Branch"] = relationship("Branch", back_populates="repo")
@property @property
def display_name(self) -> str: def display_name(self) -> str:
@ -62,13 +64,13 @@ class Branch(Base):
id = Column(Integer, primary_key=True, index=True) id = Column(Integer, primary_key=True, index=True)
created = Column(DateTime, nullable=False, server_default=utcnow()) created = Column(DateTime, nullable=False, server_default=utcnow())
target_branch = Column(String, nullable=False) target_branch: str = Column(String, nullable=False)
pr = Column(Integer, nullable=True) pr = Column(Integer, nullable=True)
repo_id = Column(Integer, ForeignKey("repo.id"), nullable=False, index=True) repo_id = Column(Integer, ForeignKey("repo.id"), nullable=False, index=True)
repo = relationship("Repo", back_populates="branches") repo: Repo = relationship(Repo, back_populates="branches")
builds = relationship("Build", back_populates="branch") builds: List["Build"] = relationship("Build", back_populates="branch")
@property @property
def display_name(self) -> str: def display_name(self) -> str:
@ -143,11 +145,11 @@ class Build(Base):
created = Column(DateTime, nullable=False, server_default=utcnow()) created = Column(DateTime, nullable=False, server_default=utcnow())
branch_id = Column(Integer, ForeignKey("branch.id"), nullable=False, index=True) branch_id = Column(Integer, ForeignKey("branch.id"), nullable=False, index=True)
branch = relationship("Branch", back_populates="builds") branch: Branch = relationship(Branch, back_populates="builds")
build_image = Column(String, nullable=False) build_image = Column(String, nullable=False)
git_sha = Column(String, nullable=False) git_sha: str = Column(String, nullable=False)
status = Column(String, nullable=False) status: str = Column(String, nullable=False)
# ressource_label = Column(String, nullable=False, unique=True, index=True) # ressource_label = Column(String, nullable=False, unique=True, index=True)
# TODO: add unique constraint on branch_id + git_sha # TODO: add unique constraint on branch_id + git_sha
@ -166,7 +168,6 @@ class Build(Base):
@classmethod @classmethod
def for_branch(cls, db: Session, branch: Branch, git_sha: str) -> "Build": def for_branch(cls, db: Session, branch: Branch, git_sha: str) -> "Build":
print("*******", branch)
build = ( build = (
db.query(Build) db.query(Build)
.filter( .filter(

View file

@ -1,4 +1,4 @@
def on_pr_open_or_update(): def on_pr_open_or_update() -> None:
# find Repo # find Repo
# find image from target branch (exit if not found) # find image from target branch (exit if not found)
# find or create Branch # find or create Branch
@ -7,13 +7,13 @@ def on_pr_open_or_update():
... ...
def on_pr_close_or_merge(): def on_pr_close_or_merge() -> None:
# find Repo, Branch # find Repo, Branch
# delete branch (enqueue) # delete branch (enqueue)
... ...
def on_push(): def on_push() -> None:
# find Repo, branch # find Repo, branch
# find image from target branch (exit if not found) # find image from target branch (exit if not found)
# find or create Branch # find or create Branch