Require pydantic 2

Resolve pydantic 2 deprecation warnings.
This commit is contained in:
Stéphane Bidoul 2023-11-19 12:52:10 +01:00
parent 243ec771de
commit 563f7b5a42
5 changed files with 26 additions and 34 deletions

View file

@ -16,6 +16,8 @@ dependencies = [
"httpx", "httpx",
"jinja2", "jinja2",
"kubernetes", "kubernetes",
"pydantic>=2",
"pydantic-settings",
"rich", "rich",
"sse-starlette", "sse-starlette",
"uvicorn", "uvicorn",

View file

@ -5,7 +5,7 @@ from collections.abc import AsyncGenerator
from ansi2html import Ansi2HTMLConverter from ansi2html import Ansi2HTMLConverter
from fastapi import APIRouter, Depends, HTTPException, Request, status from fastapi import APIRouter, Depends, HTTPException, Request, status
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
from pydantic import BaseModel from pydantic import BaseModel, ConfigDict
from sse_starlette.sse import EventSourceResponse from sse_starlette.sse import EventSourceResponse
from starlette.status import HTTP_404_NOT_FOUND from starlette.status import HTTP_404_NOT_FOUND
@ -18,6 +18,8 @@ router = APIRouter()
class Status(BaseModel): class Status(BaseModel):
model_config = ConfigDict(from_attributes=True)
deployed: int deployed: int
max_deployed: int max_deployed: int
failed: int failed: int
@ -29,19 +31,17 @@ class Status(BaseModel):
max_initializing: int max_initializing: int
undeploying: int undeploying: int
class Config:
orm_mode = True
class Repo(BaseModel): class Repo(BaseModel):
model_config = ConfigDict(from_attributes=True)
name: str name: str
link: str link: str
class Config:
orm_mode = True
class Build(BaseModel): class Build(BaseModel):
model_config = ConfigDict(from_attributes=True)
name: str name: str
commit_info: github.CommitInfo commit_info: github.CommitInfo
deploy_link: str deploy_link: str
@ -54,9 +54,6 @@ class Build(BaseModel):
created: datetime.datetime created: datetime.datetime
last_scaled: datetime.datetime last_scaled: datetime.datetime
class Config:
orm_mode = True
class BuildEvent(BaseModel): class BuildEvent(BaseModel):
event: models.BuildEvent event: models.BuildEvent

View file

@ -3,7 +3,7 @@ from enum import Enum
from typing import Any from typing import Any
import httpx import httpx
from pydantic import BaseModel, validator from pydantic import BaseModel, field_validator
from .exceptions import NotFoundOnGitHub from .exceptions import NotFoundOnGitHub
from .settings import settings from .settings import settings
@ -32,7 +32,7 @@ class CommitInfo(BaseModel):
pr: int | None pr: int | None
git_commit: str git_commit: str
@validator("repo") @field_validator("repo")
def validate_repo(cls, v: str) -> str: def validate_repo(cls, v: str) -> str:
return v.lower() return v.lower()

View file

@ -5,7 +5,7 @@ from enum import Enum
from typing import Optional from typing import Optional
from kubernetes.client.models.v1_deployment import V1Deployment from kubernetes.client.models.v1_deployment import V1Deployment
from pydantic import BaseModel from pydantic import BaseModel, ConfigDict
from . import github, k8s from . import github, k8s
from .github import CommitInfo, GitHubStatusState from .github import CommitInfo, GitHubStatusState
@ -38,6 +38,8 @@ class BuildInitStatus(str, Enum):
class Build(BaseModel): class Build(BaseModel):
model_config = ConfigDict(from_attributes=True)
name: str name: str
deployment_name: str deployment_name: str
commit_info: CommitInfo commit_info: CommitInfo
@ -47,9 +49,6 @@ class Build(BaseModel):
last_scaled: datetime.datetime last_scaled: datetime.datetime
created: datetime.datetime created: datetime.datetime
class Config:
read_with_orm_mode = True
def __str__(self) -> str: def __str__(self) -> str:
return f"{self.slug} ({self.name})" return f"{self.slug} ({self.name})"
@ -377,11 +376,10 @@ class Build(BaseModel):
class Repo(BaseModel): class Repo(BaseModel):
model_config = ConfigDict(from_attributes=True)
name: str name: str
@property @property
def link(self) -> str: def link(self) -> str:
return f"https://github.com/{self.name}" return f"https://github.com/{self.name}"
class Config:
read_with_orm_mode = True

View file

@ -1,7 +1,9 @@
import re import re
from pathlib import Path from pathlib import Path
from typing import Annotated
from pydantic import BaseModel, BaseSettings, validator from pydantic import BaseModel, BeforeValidator, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from .exceptions import RepoOrBranchNotSupported from .exceptions import RepoOrBranchNotSupported
@ -21,11 +23,7 @@ class BuildSettings(BaseModel):
env: dict[str, str] = {} env: dict[str, str] = {}
secret_env: dict[str, str] = {} secret_env: dict[str, str] = {}
template_vars: dict[str, str] = {} template_vars: dict[str, str] = {}
kubefiles_path: Path | None kubefiles_path: Annotated[Path | None, BeforeValidator(validate_path)] = None
validate_kubefiles_path = validator("kubefiles_path", allow_reuse=True, pre=True)(
validate_path
)
class RepoSettings(BaseModel): class RepoSettings(BaseModel):
@ -33,7 +31,7 @@ class RepoSettings(BaseModel):
branch: str # regex branch: str # regex
builds: list[BuildSettings] builds: list[BuildSettings]
@validator("builds") @field_validator("builds")
def validate_builds(cls, v: list[BuildSettings]) -> list[BuildSettings]: def validate_builds(cls, v: list[BuildSettings]) -> list[BuildSettings]:
if len(v) != 1: if len(v) != 1:
raise ValueError( raise ValueError(
@ -43,6 +41,8 @@ class RepoSettings(BaseModel):
class Settings(BaseSettings): class Settings(BaseSettings):
model_config = SettingsConfigDict(env_prefix="RUNBOAT_")
# Configuration for supported repositories and branches. # Configuration for supported repositories and branches.
repos: list[RepoSettings] repos: list[RepoSettings]
# A user and password to protect the most sensitive operations of the API. # A user and password to protect the most sensitive operations of the API.
@ -67,7 +67,9 @@ class Settings(BaseSettings):
# kubefiles. # kubefiles.
build_template_vars: dict[str, str] = {} build_template_vars: dict[str, str] = {}
# The path of the default kubefiles to be used. # The path of the default kubefiles to be used.
build_default_kubefiles_path: Path | None build_default_kubefiles_path: Annotated[
Path | None, BeforeValidator(validate_path)
] = None
# The token to use for the GitHub api calls (to query branches and pull requests, # The token to use for the GitHub api calls (to query branches and pull requests,
# and report build statuses). # and report build statuses).
github_token: str | None github_token: str | None
@ -83,10 +85,6 @@ class Settings(BaseSettings):
# Disable posting of statuses to GitHub commits # Disable posting of statuses to GitHub commits
disable_commit_statuses: bool = False disable_commit_statuses: bool = False
validate_build_default_kubefiles_path = validator(
"build_default_kubefiles_path", allow_reuse=True, pre=True
)(validate_path)
def get_build_settings(self, repo: str, target_branch: str) -> list[BuildSettings]: def get_build_settings(self, repo: str, target_branch: str) -> list[BuildSettings]:
for repo_settings in self.repos: for repo_settings in self.repos:
if not re.match(repo_settings.repo, repo, re.IGNORECASE): if not re.match(repo_settings.repo, repo, re.IGNORECASE):
@ -106,8 +104,5 @@ class Settings(BaseSettings):
else: else:
return True return True
class Config:
env_prefix = "RUNBOAT_"
settings = Settings() settings = Settings()