Skip to content

Commit 3de8610

Browse files
authored
fix(ci): compare release validation against the previous stable version (#9636)
Ancestry-based tag selection skips stable tags that live only on older release branches. Signed-off-by: paomian <[email protected]>
1 parent 778f455 commit 3de8610

4 files changed

Lines changed: 235 additions & 5 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2023 Greptime Team
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import importlib.util
17+
import subprocess
18+
import sys
19+
import unittest
20+
from pathlib import Path
21+
22+
23+
def load_module():
24+
path = Path(__file__).resolve().parent / "previous-release-tag.py"
25+
spec = importlib.util.spec_from_file_location("previous_release_tag", path)
26+
assert spec is not None and spec.loader is not None
27+
module = importlib.util.module_from_spec(spec)
28+
spec.loader.exec_module(module)
29+
return module
30+
31+
32+
TAGS = [
33+
"v1.1.0",
34+
"v1.2.0",
35+
"v1.2.1",
36+
"v1.2.1^{}",
37+
"v1.3.0-alpha.1",
38+
"v1.3.0-beta.1",
39+
"v1.3.0-beta.2",
40+
"v1.2.0-nightly-20260706",
41+
]
42+
43+
44+
class PreviousReleaseTagTest(unittest.TestCase):
45+
@classmethod
46+
def setUpClass(cls) -> None:
47+
cls.mod = load_module()
48+
49+
def test_beta_uses_latest_older_stable_not_an_ancestor(self) -> None:
50+
self.assertEqual(self.mod.previous_stable_tag("v1.3.0-beta.2", TAGS), "v1.2.1")
51+
52+
def test_stable_release_skips_itself(self) -> None:
53+
self.assertEqual(self.mod.previous_stable_tag("v1.2.1", TAGS), "v1.2.0")
54+
55+
def test_same_line_stable_is_not_baseline_for_its_prerelease(self) -> None:
56+
tags = TAGS + ["v1.3.0"]
57+
self.assertEqual(self.mod.previous_stable_tag("v1.3.0-beta.2", tags), "v1.2.1")
58+
self.assertEqual(self.mod.previous_stable_tag("v1.3.0", tags), "v1.2.1")
59+
60+
def test_nightly_of_unreleased_version_uses_previous_stable(self) -> None:
61+
self.assertEqual(
62+
self.mod.previous_stable_tag("v1.3.0-nightly-20261013", TAGS),
63+
"v1.2.1",
64+
)
65+
66+
def test_no_older_stable_tag(self) -> None:
67+
self.assertIsNone(self.mod.previous_stable_tag("v0.1.0", ["v0.1.0", "v1.2.1"]))
68+
69+
def test_invalid_version(self) -> None:
70+
with self.assertRaises(ValueError):
71+
self.mod.previous_stable_tag("nightly-20261013", TAGS)
72+
73+
def test_unordered_tags_use_numeric_order(self) -> None:
74+
forward = ["v1.2.1", "v1.9.0", "v1.10.0", "v0.10.0", "v1.9.9"]
75+
backward = list(reversed(forward))
76+
for tags in (forward, backward):
77+
self.assertEqual(self.mod.previous_stable_tag("v1.10.0", tags), "v1.9.9")
78+
self.assertEqual(self.mod.previous_stable_tag("v1.10.0-rc.1", tags), "v1.9.9")
79+
80+
def test_multi_digit_components_compare_as_integers(self) -> None:
81+
tags = ["v1.9.0", "v1.10.0", "v0.9.10", "v0.10.0", "v2.9.15", "v2.10.0", "v10.2.0"]
82+
self.assertEqual(self.mod.previous_stable_tag("v1.10.0", tags), "v1.9.0")
83+
self.assertEqual(self.mod.previous_stable_tag("v0.10.0", tags), "v0.9.10")
84+
self.assertEqual(self.mod.previous_stable_tag("v2.10.0", tags), "v2.9.15")
85+
self.assertEqual(self.mod.previous_stable_tag("v10.2.0", ["v10.0.9", "v9.20.0"]), "v10.0.9")
86+
87+
88+
class PreviousReleaseTagCliTest(unittest.TestCase):
89+
SCRIPT = Path(__file__).resolve().parent / "previous-release-tag.py"
90+
91+
def run_cli(
92+
self, version: str, tags: list[str] | None = None, stdin: str | None = None
93+
) -> subprocess.CompletedProcess[str]:
94+
command = [sys.executable, str(self.SCRIPT), "--version", version]
95+
if tags is not None:
96+
command.extend(tags)
97+
return subprocess.run(
98+
command,
99+
input=stdin,
100+
text=True,
101+
capture_output=True,
102+
check=False,
103+
)
104+
105+
def test_stdin_selects_baseline_and_exits_zero(self) -> None:
106+
completed = self.run_cli(
107+
"v1.10.0",
108+
stdin="v1.2.1\nv1.9.0\n\nv1.10.0\nv1.9.0^{}\n",
109+
)
110+
self.assertEqual(completed.returncode, 0)
111+
self.assertEqual(completed.stdout, "v1.9.0\n")
112+
self.assertEqual(completed.stderr, "")
113+
114+
def test_positional_tags_ignore_stdin(self) -> None:
115+
completed = self.run_cli("v1.10.0", tags=["v1.9.0"], stdin="v1.9.1\n")
116+
self.assertEqual(completed.returncode, 0)
117+
self.assertEqual(completed.stdout, "v1.9.0\n")
118+
119+
def test_no_older_tag_exits_zero_with_empty_stdout(self) -> None:
120+
completed = self.run_cli("v0.1.0", stdin="v0.1.0\nv1.2.1\n")
121+
self.assertEqual(completed.returncode, 0)
122+
self.assertEqual(completed.stdout, "")
123+
self.assertEqual(completed.stderr, "")
124+
125+
def test_invalid_version_exits_one(self) -> None:
126+
completed = self.run_cli("nightly", stdin="v1.2.1\n")
127+
self.assertEqual(completed.returncode, 1)
128+
self.assertEqual(completed.stdout, "")
129+
self.assertIn("not a release version: nightly", completed.stderr)
130+
131+
132+
if __name__ == "__main__":
133+
unittest.main()
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2023 Greptime Team
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""Pick the query-regression baseline tag for a release.
17+
18+
Compatibility tests do not use this tag. They read ``tests/compatibility/ci.toml``.
19+
20+
Release branches diverge, so a tag on ``release/v1.3`` is not a descendant of
21+
``v1.2.1``. Ancestry (``git tag --merged``) therefore skips the real previous
22+
release. Compare version numbers instead: the baseline is the greatest
23+
``vX.Y.Z`` tag whose version is strictly older than the release being built.
24+
Prerelease and nightly tags are not baselines.
25+
"""
26+
27+
from __future__ import annotations
28+
29+
import argparse
30+
import re
31+
import sys
32+
33+
STABLE_TAG = re.compile(r"^v(\d+)\.(\d+)\.(\d+)$")
34+
RELEASE_VERSION = re.compile(r"^v(\d+)\.(\d+)\.(\d+)(?:-.+)?$")
35+
36+
37+
def _triple(match: re.Match[str]) -> tuple[int, int, int]:
38+
return int(match.group(1)), int(match.group(2)), int(match.group(3))
39+
40+
41+
def release_version(version: str) -> tuple[int, int, int]:
42+
match = RELEASE_VERSION.fullmatch(version.strip())
43+
if match is None:
44+
raise ValueError(f"not a release version: {version}")
45+
return _triple(match)
46+
47+
48+
def previous_stable_tag(version: str, tags: list[str]) -> str | None:
49+
"""Return the newest stable tag older than ``version``, or None."""
50+
current = release_version(version)
51+
best: tuple[int, int, int] | None = None
52+
best_tag: str | None = None
53+
for raw in tags:
54+
tag = raw.strip()
55+
if tag.endswith("^{}"):
56+
tag = tag[: -len("^{}")]
57+
match = STABLE_TAG.fullmatch(tag)
58+
if match is None:
59+
continue
60+
triple = _triple(match)
61+
if triple < current and (best is None or triple > best):
62+
best = triple
63+
best_tag = tag
64+
return best_tag
65+
66+
67+
def main(argv: list[str] | None = None) -> int:
68+
parser = argparse.ArgumentParser(description=__doc__)
69+
parser.add_argument("--version", required=True, help="Release version, e.g. v1.3.0-beta.2")
70+
parser.add_argument(
71+
"tags",
72+
nargs="*",
73+
help="Candidate tags. Read stdin when omitted.",
74+
)
75+
args = parser.parse_args(argv)
76+
tags = list(args.tags)
77+
if not tags and not sys.stdin.isatty():
78+
tags = sys.stdin.read().splitlines()
79+
try:
80+
tag = previous_stable_tag(args.version, tags)
81+
except ValueError as exc:
82+
print(exc, file=sys.stderr)
83+
return 1
84+
if tag is None:
85+
return 0
86+
print(tag)
87+
return 0
88+
89+
90+
if __name__ == "__main__":
91+
raise SystemExit(main())

‎.github/workflows/checks.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ jobs:
8282
version: "latest"
8383
- name: Run check-version script tests
8484
run: uv run --no-project python .github/scripts/check-version-test.py
85+
- name: Run previous-release-tag script tests
86+
run: python3 .github/scripts/previous-release-tag-test.py
8587
- name: Run enterprise license check tests
8688
run: python3 scripts/check-enterprise-license-test.py
8789
- name: Run fuzz orchestration script tests

‎.github/workflows/release.yml‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,15 @@ jobs:
415415
REQUESTED_VALIDATION_POLICY: ${{ inputs.release_validation }}
416416
run: |
417417
set -euo pipefail
418-
base_commit="${GITHUB_SHA}"
419-
if [[ "${GITHUB_REF_TYPE}" == "tag" ]]; then
420-
base_commit="${GITHUB_SHA}^"
421-
fi
422-
previous_release_tag="$(git tag --merged "${base_commit}" --sort=-v:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1 || true)"
418+
# Stable tags on older release branches are not ancestors of this
419+
# commit. Pick the newest vX.Y.Z older than the version being released.
420+
release_version="${{ needs.allocate-runners.outputs.version }}"
421+
previous_release_tag="$(
422+
git ls-remote --tags origin 'refs/tags/v*' \
423+
| awk '{print $2}' \
424+
| sed -e 's#^refs/tags/##' \
425+
| python3 .github/scripts/previous-release-tag.py --version "${release_version}"
426+
)"
423427
if [[ -z "${previous_release_tag}" ]]; then
424428
previous_release_tag="main"
425429
fi

0 commit comments

Comments
 (0)