|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import xml.etree.ElementTree as ET |
| 8 | +from pathlib import Path |
| 9 | +from typing import Optional |
| 10 | + |
| 11 | + |
| 12 | +DEFAULT_MICROMETER_DIR = Path( |
| 13 | + os.environ.get("MICROMETER_DIR", "/tmp/micrometer-compat") |
| 14 | +) |
| 15 | +DEFAULT_MICROMETER_REPOSITORY = os.environ.get( |
| 16 | + "MICROMETER_REPOSITORY", "zeitlinger/micrometer" |
| 17 | +) |
| 18 | +DEFAULT_MICROMETER_REMOTE = os.environ.get("MICROMETER_REMOTE", "origin") |
| 19 | +DEFAULT_MICROMETER_REF = os.environ.get( |
| 20 | + "MICROMETER_REF", "feat/prometheus-client-opt-in" |
| 21 | +) |
| 22 | +DEFAULT_INIT_SCRIPT = Path( |
| 23 | + os.environ.get("MICROMETER_INIT_SCRIPT", "/tmp/micrometer-prom-local.init.gradle") |
| 24 | +) |
| 25 | +DEFAULT_PROM_VERSION = os.environ.get("PROM_VERSION") |
| 26 | + |
| 27 | + |
| 28 | +def run_cmd(cmd: list[str], cwd: Optional[Path] = None) -> None: |
| 29 | + subprocess.run(cmd, cwd=cwd, check=True) |
| 30 | + |
| 31 | + |
| 32 | +def micrometer_repository_url(repository: str) -> str: |
| 33 | + return f"https://github.com/{repository}.git" |
| 34 | + |
| 35 | + |
| 36 | +def check_clean_worktree(micrometer_dir: Path) -> None: |
| 37 | + result = subprocess.run( |
| 38 | + ["git", "status", "--short"], |
| 39 | + cwd=micrometer_dir, |
| 40 | + check=True, |
| 41 | + capture_output=True, |
| 42 | + text=True, |
| 43 | + ) |
| 44 | + if result.stdout.strip(): |
| 45 | + raise RuntimeError( |
| 46 | + f"{micrometer_dir} has uncommitted changes; use a clean clone or set MICROMETER_DIR" |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def get_prom_version(root_dir: Path = Path.cwd()) -> str: |
| 51 | + configured_version = DEFAULT_PROM_VERSION |
| 52 | + if configured_version: |
| 53 | + return configured_version |
| 54 | + pom = ET.parse(root_dir / "pom.xml") |
| 55 | + root = pom.getroot() |
| 56 | + version = root.findtext("./{*}version") |
| 57 | + if not version: |
| 58 | + version = root.findtext("./{*}parent/{*}version") |
| 59 | + if not version: |
| 60 | + raise RuntimeError("could not determine Prometheus version from pom.xml") |
| 61 | + return version |
| 62 | + |
| 63 | + |
| 64 | +def write_init_script( |
| 65 | + init_script: Path = DEFAULT_INIT_SCRIPT, prom_version: Optional[str] = None |
| 66 | +) -> None: |
| 67 | + if prom_version is None: |
| 68 | + prom_version = get_prom_version() |
| 69 | + init_script.write_text( |
| 70 | + f"""allprojects {{ |
| 71 | + repositories {{ |
| 72 | + mavenLocal() |
| 73 | + mavenCentral() |
| 74 | + gradlePluginPortal() |
| 75 | + }} |
| 76 | + configurations.configureEach {{ |
| 77 | + resolutionStrategy.eachDependency {{ details -> |
| 78 | + if (details.requested.group == 'io.prometheus') {{ |
| 79 | + details.useVersion('{prom_version}') |
| 80 | + details.because( |
| 81 | + 'Use local prom_client_java artifacts for downstream compatibility testing' |
| 82 | + ) |
| 83 | + }} |
| 84 | + }} |
| 85 | + }} |
| 86 | +}} |
| 87 | +""", |
| 88 | + encoding="utf-8", |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +def prepare_repo( |
| 93 | + micrometer_dir: Path = DEFAULT_MICROMETER_DIR, |
| 94 | + repository: str = DEFAULT_MICROMETER_REPOSITORY, |
| 95 | + remote: str = DEFAULT_MICROMETER_REMOTE, |
| 96 | + ref: str = DEFAULT_MICROMETER_REF, |
| 97 | +) -> None: |
| 98 | + repository_url = micrometer_repository_url(repository) |
| 99 | + if (micrometer_dir / ".git").is_dir(): |
| 100 | + check_clean_worktree(micrometer_dir) |
| 101 | + run_cmd( |
| 102 | + ["git", "remote", "set-url", remote, repository_url], cwd=micrometer_dir |
| 103 | + ) |
| 104 | + run_cmd(["git", "fetch", remote, ref], cwd=micrometer_dir) |
| 105 | + else: |
| 106 | + run_cmd( |
| 107 | + [ |
| 108 | + "git", |
| 109 | + "clone", |
| 110 | + repository_url, |
| 111 | + str(micrometer_dir), |
| 112 | + ] |
| 113 | + ) |
| 114 | + run_cmd(["git", "fetch", remote, ref], cwd=micrometer_dir) |
| 115 | + run_cmd( |
| 116 | + ["git", "checkout", "-B", "codex-micrometer-compat", "FETCH_HEAD"], |
| 117 | + cwd=micrometer_dir, |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +def install_local_artifacts(root_dir: Path = Path.cwd()) -> None: |
| 122 | + run_cmd( |
| 123 | + [ |
| 124 | + "./mvnw", |
| 125 | + "install", |
| 126 | + "-DskipTests", |
| 127 | + "-Dcoverage.skip=true", |
| 128 | + "-Dcheckstyle.skip=true", |
| 129 | + "-Dwarnings=-nowarn", |
| 130 | + ], |
| 131 | + cwd=root_dir, |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +def run_gradle_test( |
| 136 | + test_selector: Optional[str] = None, |
| 137 | + micrometer_dir: Path = DEFAULT_MICROMETER_DIR, |
| 138 | + init_script: Path = DEFAULT_INIT_SCRIPT, |
| 139 | +) -> None: |
| 140 | + cmd = [ |
| 141 | + "./gradlew", |
| 142 | + "--no-daemon", |
| 143 | + "-I", |
| 144 | + str(init_script), |
| 145 | + ":micrometer-registry-prometheus:test", |
| 146 | + ] |
| 147 | + if test_selector: |
| 148 | + cmd.extend(["--tests", test_selector]) |
| 149 | + run_cmd(cmd, cwd=micrometer_dir) |
0 commit comments