Skip to content

Commit

Permalink
add back check_version to version_util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
zrezke committed May 18, 2023
1 parent 5d355b4 commit acd6c73
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions scripts/version_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
EXPECTED_VERSION=$(python3 scripts/version_util.py --bare_cargo_version)
"""

import os
import re
import subprocess
import sys
Expand All @@ -21,6 +22,7 @@

# A regex to match the version number in Cargo.toml as SemVer, e.g., 1.2.3-alpha.0
CARGO_VERSION_REGEX: Final = r"^version\s*=\s*\"(.+)\"$"
VERSION_TAG_REGEX: Final = r"^v(?P<version>([0-9]+)\.([0-9]+)\.([0-9]+))$"


def get_cargo_version(cargo_toml: str) -> semver.VersionInfo:
Expand All @@ -39,6 +41,24 @@ def get_git_sha() -> str:
return subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).decode("utf-8").strip()


def get_ref_name_version() -> str:
"""Return the parsed tag version from the GITHUB_REF_NAME environment variable."""

# This is the branch, or tag name that triggered the workflow.
ref_name = os.environ.get("GITHUB_REF_NAME")

if ref_name is None:
raise Exception("GITHUB_REF_NAME environment variable not set")

# Extract the version number from the tag name
match = re.search(VERSION_TAG_REGEX, ref_name)

if match is None:
raise Exception("Could not find valid version number in GITHUB_REF_NAME")

return match.group("version")


def patch_cargo_version(cargo_toml: str, new_version: str) -> str:
"""Patch the version number in Cargo.toml with `new_version`."""

Expand Down Expand Up @@ -81,6 +101,14 @@ def main() -> None:
# is expected to be fed into an environment variable.
print(f"{cargo_version}")

elif sys.argv[1] == "--check_version":
ref_version = get_ref_name_version()
if cargo_version != ref_version:
raise Exception(
f"Version number in Cargo.toml ({cargo_version}) does not match tag version ({ref_version})"
)
print(f"Version numbers match: {cargo_version} == {ref_version}")

else:
raise Exception("Invalid argument")

Expand Down

0 comments on commit acd6c73

Please sign in to comment.