|
2 | 2 |
|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
| 5 | +import os |
5 | 6 | from pathlib import Path
|
6 | 7 |
|
7 | 8 | from fastapi.templating import Jinja2Templates
|
|
21 | 22 | ]
|
22 | 23 |
|
23 | 24 |
|
| 25 | +# Version and repository configuration |
| 26 | +VERSION = os.getenv("VERSION", "unknown") |
| 27 | +REPOSITORY_URL = os.getenv("REPOSITORY_URL", "https://github.com/coderamp-labs/gitingest") |
| 28 | + |
| 29 | +# Minimum number of parts expected in branch-commit format (e.g., "main-abc1234") |
| 30 | +MIN_BRANCH_COMMIT_PARTS = 2 |
| 31 | + |
| 32 | + |
| 33 | +def get_version_info() -> dict[str, str]: |
| 34 | + """Get version information including display version and link. |
| 35 | +
|
| 36 | + Returns |
| 37 | + ------- |
| 38 | + dict[str, str] |
| 39 | + Dictionary containing 'version' and 'version_link' keys. |
| 40 | +
|
| 41 | + """ |
| 42 | + version = VERSION |
| 43 | + repo_url = REPOSITORY_URL.rstrip("/") |
| 44 | + |
| 45 | + # Check if version looks like a tag (doesn't contain branch-commit pattern) |
| 46 | + if version != "unknown" and "-" in version and len(version.split("-")) >= MIN_BRANCH_COMMIT_PARTS: |
| 47 | + # This looks like branch-commit format (e.g., "main-abc1234") |
| 48 | + parts = version.split("-") |
| 49 | + if len(parts) >= MIN_BRANCH_COMMIT_PARTS: |
| 50 | + # Take the last part as commit hash |
| 51 | + commit_hash = parts[-1] |
| 52 | + version_link = f"{repo_url}/commit/{commit_hash}" |
| 53 | + else: |
| 54 | + # Fallback to main branch |
| 55 | + version_link = f"{repo_url}/tree/main" |
| 56 | + elif version != "unknown": |
| 57 | + # This looks like a tag version |
| 58 | + version_link = f"{repo_url}/releases/tag/{version}" |
| 59 | + else: |
| 60 | + # Unknown version, link to main branch |
| 61 | + version_link = f"{repo_url}/tree/main" |
| 62 | + |
| 63 | + return { |
| 64 | + "version": version, |
| 65 | + "version_link": version_link, |
| 66 | + } |
| 67 | + |
| 68 | + |
24 | 69 | # Use absolute path to templates directory
|
25 | 70 | templates_dir = Path(__file__).parent / "templates"
|
26 | 71 | templates = Jinja2Templates(directory=templates_dir)
|
0 commit comments