Coverage for endpoints/version.py: 54.17%

22 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-04-12 14:12 +0000

1""" 

2Version endpoint for application versioning and build information. 

3""" 

4 

5import os 

6 

7from fastapi import APIRouter 

8from pydantic import BaseModel 

9 

10router = APIRouter(prefix="/api", tags=["version"]) 

11 

12 

13class VersionInfo(BaseModel): 

14 """Version information response model.""" 

15 

16 version: str 

17 build_id: str | None = None 

18 environment: str 

19 commit_sha: str | None = None 

20 build_date: str | None = None 

21 status: str = "healthy" 

22 

23 

24@router.get("/version", response_model=VersionInfo) 

25async def get_version(): 

26 """ 

27 Get application version and build information. 

28 

29 Returns: 

30 VersionInfo: Application version, environment, and build details 

31 

32 Environment Variables: 

33 - APP_VERSION: Semantic version (e.g., "1.0.0") 

34 - BUILD_ID: CI/CD build/workflow run ID 

35 - ENVIRONMENT: Environment name (local, prod) 

36 - COMMIT_SHA: Git commit SHA 

37 - BUILD_DATE: ISO format build timestamp 

38 """ 

39 version = os.getenv("APP_VERSION", "1.0.0") 

40 build_id = os.getenv("BUILD_ID") 

41 environment = os.getenv("ENVIRONMENT", "development") 

42 commit_sha = os.getenv("COMMIT_SHA") 

43 build_date = os.getenv("BUILD_DATE") 

44 

45 # Construct full version string with build ID if available 

46 full_version = version 

47 if build_id: 

48 full_version = f"{version}.{build_id}" 

49 

50 return VersionInfo( 

51 version=full_version, 

52 build_id=build_id, 

53 environment=environment, 

54 commit_sha=commit_sha, 

55 build_date=build_date, 

56 status="healthy", 

57 )