Skip to content

Commit f8ed349

Browse files
authored
ci: block the release if the version numbers is invalid (#2386)
1 parent ceb5193 commit f8ed349

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

.github/utils/validate_version.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import re
2+
import requests
3+
4+
# * integrations/<INTEGRATION_FOLDER_NAME>-v1.0.0
5+
INTEGRATION_VERSION_REGEX = r"integrations/([a-zA-Z-]+)-v([0-9]\.[0-9]+\.[0-9]+)"
6+
7+
8+
def validate_version_number(tag: str):
9+
"""
10+
Verify that a release version number follows semantic versioning rules by checking the latest version on PyPI.
11+
"""
12+
13+
matches = re.match(INTEGRATION_VERSION_REGEX, tag)
14+
if not matches or len(matches.groups()) != 2:
15+
raise ValueError(f"Invalid tag: {tag}")
16+
17+
integration_name, version_to_release = matches.groups()
18+
print(f"Integration name: {integration_name}")
19+
print(f"Integration version to release: {version_to_release}")
20+
21+
integration_package = f"{integration_name}-haystack"
22+
print(f"Integration PyPi package: {integration_package}")
23+
24+
# connect to PyPi and check the latest version
25+
try:
26+
response = requests.get(f"https://pypi.org/pypi/{integration_package}/json")
27+
response.raise_for_status()
28+
latest_version = response.json()["info"]["version"]
29+
print(f"Latest version on PyPI: {latest_version}")
30+
except requests.exceptions.HTTPError as e:
31+
if e.response.status_code == 404 and version_to_release in ["0.0.1", "0.1.0", "1.0.0"]:
32+
print("Package not found on PyPI. Assuming this is the first release and skipping version check.")
33+
return
34+
raise e
35+
36+
x, y, z = [int(i) for i in latest_version.split(".")]
37+
38+
acceptable_new_versions = [
39+
f"{x}.{y}.{z + 1}",
40+
f"{x}.{y + 1}.0",
41+
f"{x + 1}.0.0",
42+
]
43+
if version_to_release not in acceptable_new_versions:
44+
msg = (
45+
f"Invalid version to release: {version_to_release}. Acceptable new versions are: {acceptable_new_versions}"
46+
)
47+
raise ValueError(msg)
48+
print("The version to release is acceptable.")
49+
50+
51+
if __name__ == "__main__":
52+
import argparse
53+
54+
parser = argparse.ArgumentParser()
55+
parser.add_argument("--tag", help="Git tag in format 'integrations/<name>-v<version>'", required=True, type=str)
56+
args = parser.parse_args()
57+
58+
validate_version_number(args.tag)

.github/workflows/CI_pypi_release.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ jobs:
2424
token: ${{ secrets.HAYSTACK_BOT_TOKEN }}
2525
fetch-depth: 0
2626

27-
- name: Install Hatch
28-
run: pip install hatch
27+
- name: Install dependencies
28+
run: pip install hatch requests
29+
30+
- name: Validate version number
31+
run: python .github/utils/validate_version.py --tag ${{ github.ref_name }}
2932

3033
- name: Get project folder
3134
id: pathfinder

0 commit comments

Comments
 (0)