|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Determine the current Codename One release version for documentation builds.""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import re |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import xml.etree.ElementTree as ET |
| 11 | +from pathlib import Path |
| 12 | +from typing import Iterable, List |
| 13 | +from urllib.error import HTTPError, URLError |
| 14 | +from urllib.request import Request, urlopen |
| 15 | + |
| 16 | + |
| 17 | +def _sanitize_tag(value: str) -> str: |
| 18 | + value = value.strip() |
| 19 | + if value.lower().startswith("refs/tags/"): |
| 20 | + value = value[10:] |
| 21 | + if value.lower().startswith("tags/"): |
| 22 | + value = value[5:] |
| 23 | + if value and value[0] in {"v", "V"} and value[1:2].isdigit(): |
| 24 | + value = value[1:] |
| 25 | + return value.strip() |
| 26 | + |
| 27 | + |
| 28 | +def _parse_version_components(version: str) -> List[int]: |
| 29 | + return [int(part) for part in version.split(".")] |
| 30 | + |
| 31 | + |
| 32 | +def release_tag_from_event() -> str: |
| 33 | + event_path = os.environ.get("GITHUB_EVENT_PATH") |
| 34 | + if event_path: |
| 35 | + event_file = Path(event_path) |
| 36 | + if event_file.is_file(): |
| 37 | + try: |
| 38 | + data = json.loads(event_file.read_text()) |
| 39 | + except json.JSONDecodeError: |
| 40 | + data = {} |
| 41 | + release = data.get("release") or {} |
| 42 | + tag = release.get("tag_name") or release.get("target_commitish") or "" |
| 43 | + if tag: |
| 44 | + return _sanitize_tag(tag) |
| 45 | + for key in ("GITHUB_REF_NAME", "GITHUB_REF"): |
| 46 | + value = os.environ.get(key) |
| 47 | + if value: |
| 48 | + sanitized = _sanitize_tag(value) |
| 49 | + if sanitized: |
| 50 | + return sanitized |
| 51 | + return "" |
| 52 | + |
| 53 | + |
| 54 | +def latest_release_from_api() -> str: |
| 55 | + repository = os.environ.get("GITHUB_REPOSITORY") |
| 56 | + if not repository: |
| 57 | + return "" |
| 58 | + api_base = os.environ.get("GITHUB_API_URL", "https://api.github.com") |
| 59 | + url = f"{api_base.rstrip('/')}/repos/{repository}/releases/latest" |
| 60 | + headers = { |
| 61 | + "Accept": "application/vnd.github+json", |
| 62 | + "User-Agent": "codenameone-docs-release-version", |
| 63 | + } |
| 64 | + token = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN") |
| 65 | + if token: |
| 66 | + headers["Authorization"] = f"Bearer {token}" |
| 67 | + request = Request(url, headers=headers) |
| 68 | + try: |
| 69 | + with urlopen(request, timeout=10) as response: |
| 70 | + if response.status != 200: |
| 71 | + return "" |
| 72 | + try: |
| 73 | + payload = json.load(response) |
| 74 | + except json.JSONDecodeError: |
| 75 | + return "" |
| 76 | + except (HTTPError, URLError, TimeoutError): |
| 77 | + return "" |
| 78 | + tag = payload.get("tag_name") |
| 79 | + if not tag: |
| 80 | + return "" |
| 81 | + return _sanitize_tag(str(tag)) |
| 82 | + |
| 83 | + |
| 84 | +def latest_git_tag() -> str: |
| 85 | + try: |
| 86 | + subprocess.run( |
| 87 | + ["git", "fetch", "--tags", "--force"], |
| 88 | + check=True, |
| 89 | + stdout=subprocess.PIPE, |
| 90 | + stderr=subprocess.PIPE, |
| 91 | + ) |
| 92 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 93 | + pass |
| 94 | + try: |
| 95 | + result = subprocess.run( |
| 96 | + ["git", "tag", "--list", "v*"], |
| 97 | + check=True, |
| 98 | + stdout=subprocess.PIPE, |
| 99 | + stderr=subprocess.PIPE, |
| 100 | + text=True, |
| 101 | + ) |
| 102 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 103 | + return "" |
| 104 | + tags: Iterable[str] = (_sanitize_tag(line) for line in result.stdout.splitlines()) |
| 105 | + numeric_tags = [tag for tag in tags if re.fullmatch(r"\d+(?:\.\d+)*", tag)] |
| 106 | + if not numeric_tags: |
| 107 | + return "" |
| 108 | + numeric_tags.sort(key=_parse_version_components) |
| 109 | + return numeric_tags[-1] |
| 110 | + |
| 111 | + |
| 112 | +def version_from_pom(root: Path) -> str: |
| 113 | + pom_path = root / "maven" / "pom.xml" |
| 114 | + if not pom_path.is_file(): |
| 115 | + return "" |
| 116 | + try: |
| 117 | + tree = ET.parse(pom_path) |
| 118 | + except ET.ParseError: |
| 119 | + return "" |
| 120 | + namespace = {"mvn": "http://maven.apache.org/POM/4.0.0"} |
| 121 | + version_element = tree.getroot().find("mvn:version", namespace) |
| 122 | + if version_element is None: |
| 123 | + return "" |
| 124 | + version = (version_element.text or "").strip() |
| 125 | + if not version: |
| 126 | + return "" |
| 127 | + if version.endswith("-SNAPSHOT"): |
| 128 | + version = version[: -len("-SNAPSHOT")] |
| 129 | + return version |
| 130 | + |
| 131 | + |
| 132 | +def main() -> int: |
| 133 | + repo_root = Path(__file__).resolve().parents[2] |
| 134 | + |
| 135 | + for candidate in (release_tag_from_event, latest_release_from_api, latest_git_tag): |
| 136 | + version = candidate() |
| 137 | + if version: |
| 138 | + print(version) |
| 139 | + return 0 |
| 140 | + |
| 141 | + version = version_from_pom(repo_root) |
| 142 | + if version: |
| 143 | + print(version) |
| 144 | + return 0 |
| 145 | + |
| 146 | + return 1 |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + sys.exit(main()) |
0 commit comments