PLBA2-3. CI процесс
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
name: PLBA package CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened]
|
||||
|
||||
jobs:
|
||||
tests:
|
||||
name: Python tests
|
||||
runs-on: cloud-build
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Check Python version
|
||||
run: |
|
||||
python3 - <<'PY'
|
||||
import sys
|
||||
|
||||
print(f"Using Python {sys.version.split()[0]}")
|
||||
if sys.version_info < (3, 11):
|
||||
raise SystemExit("PLBA v2 CI requires Python 3.11 or newer")
|
||||
PY
|
||||
|
||||
- name: Install package and test dependencies
|
||||
run: |
|
||||
python3 -m venv .venv
|
||||
.venv/bin/python -m pip install -e ".[test]"
|
||||
|
||||
- name: Run tests
|
||||
run: .venv/bin/python -m pytest
|
||||
|
||||
package-version:
|
||||
name: Package version increment
|
||||
if: ${{ gitea.event_name == 'pull_request' }}
|
||||
runs-on: cloud-build
|
||||
steps:
|
||||
- name: Check out repository and target branch history
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Check package version increment
|
||||
env:
|
||||
BASE_REF: ${{ github.base_ref }}
|
||||
run: |
|
||||
test -n "$BASE_REF"
|
||||
python3 scripts/check_package_version.py --base-ref "origin/${BASE_REF}"
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "plba-v2"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
description = "Portable application lifecycle and observability components"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.11"
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check that a change advances the PLBA package version by one SemVer step."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tomllib
|
||||
from pathlib import Path
|
||||
|
||||
VERSION_PATTERN = re.compile(r"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$")
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def parse_version(raw_version: object, source: str) -> tuple[int, int, int]:
|
||||
if not isinstance(raw_version, str):
|
||||
raise ValueError(f"В {source} поле project.version должно быть строкой")
|
||||
match = VERSION_PATTERN.fullmatch(raw_version)
|
||||
if match is None:
|
||||
raise ValueError(
|
||||
f"Версия {raw_version!r} в {source} должна иметь формат MAJOR.MINOR.PATCH"
|
||||
)
|
||||
return tuple(int(part) for part in match.groups())
|
||||
|
||||
|
||||
def read_version(toml_text: str, source: str) -> tuple[int, int, int]:
|
||||
project = tomllib.loads(toml_text).get("project", {})
|
||||
if not isinstance(project, dict):
|
||||
raise ValueError(f"В {source} отсутствует секция [project]")
|
||||
return parse_version(project.get("version"), source)
|
||||
|
||||
|
||||
def read_base_version(base_ref: str) -> tuple[int, int, int]:
|
||||
result = subprocess.run(
|
||||
["git", "show", f"{base_ref}:pyproject.toml"],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=PROJECT_ROOT,
|
||||
)
|
||||
return read_version(result.stdout, f"{base_ref}:pyproject.toml")
|
||||
|
||||
|
||||
def allowed_next_versions(base: tuple[int, int, int]) -> set[tuple[int, int, int]]:
|
||||
major, minor, patch = base
|
||||
patch_bump = (major, minor, patch + 1)
|
||||
minor_bump = (major, minor + 1, 0)
|
||||
if major == 0:
|
||||
return {patch_bump, minor_bump, (1, 0, 0)}
|
||||
return {patch_bump, minor_bump, (major + 1, 0, 0)}
|
||||
|
||||
|
||||
def format_version(version: tuple[int, int, int]) -> str:
|
||||
return ".".join(str(part) for part in version)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument(
|
||||
"--base-ref",
|
||||
required=True,
|
||||
help="Git ref целевой ветки pull request, например origin/main",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
base = read_base_version(args.base_ref)
|
||||
current_text = (PROJECT_ROOT / "pyproject.toml").read_text(encoding="utf-8")
|
||||
current = read_version(current_text, "pyproject.toml")
|
||||
except (OSError, ValueError, tomllib.TOMLDecodeError, subprocess.CalledProcessError) as error:
|
||||
print(f"Ошибка проверки версии: {error}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
if current not in allowed_next_versions(base):
|
||||
allowed = ", ".join(
|
||||
format_version(version) for version in sorted(allowed_next_versions(base))
|
||||
)
|
||||
print(
|
||||
f"Версия {format_version(current)} должна быть ровно одним шагом выше "
|
||||
f"{format_version(base)}. Допустимые версии: {allowed}.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
print(f"Версия пакета увеличена: {format_version(base)} -> {format_version(current)}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user