|
| 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) |
0 commit comments