Skip to content

Commit

Permalink
Refactor version update logic to determine target release branch and …
Browse files Browse the repository at this point in the history
…improve error handling for common.props retrieval
  • Loading branch information
skoc10 committed Feb 7, 2025
1 parent 88d220b commit c4c674e
Showing 1 changed file with 44 additions and 36 deletions.
80 changes: 44 additions & 36 deletions .github/scripts/update_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,62 @@
import xml.etree.ElementTree as ET
from github import Github

def get_latest_release_branch():
""" Lists all `rel-x.x` branches in the GitHub repository and returns the one with the highest version. """
g = Github(os.environ["GITHUB_TOKEN"])
repo = g.get_repo("abpframework/abp")

branches = repo.get_branches()
release_branches = []

# `rel-x.x` look for pattern
pattern = re.compile(r"rel-(\d+\.\d+)")

for branch in branches:
match = pattern.match(branch.name)
if match:
release_branches.append((branch.name, float(match.group(1)))) # (branch_name, version)

if not release_branches:
raise ValueError("No release branches found!")

# Find the branch with the highest version
latest_branch = max(release_branches, key=lambda x: x[1])[0]
return latest_branch
def get_target_release_branch(version):
"""
Extracts the first two numbers from the release version (`9.0.5` → `rel-9.0`)
to determine the corresponding `rel-x.x` branch.
"""
match = re.match(r"(\d+)\.(\d+)\.\d+", version)
if not match:
raise ValueError(f"Invalid version format: {version}")

major, minor = match.groups()
target_branch = f"rel-{major}.{minor}"
return target_branch

def get_version_from_common_props(branch):
""" Fetches the `Version` and `LeptonXVersion` information from the `common.props` file in the specified branch. """
"""
Retrieves `Version` and `LeptonXVersion` from the `common.props` file in the specified branch.
"""
g = Github(os.environ["GITHUB_TOKEN"])
repo = g.get_repo("abpframework/abp")

# Take the content of the file
file_content = repo.get_contents("common.props", ref=branch)
common_props_content = file_content.decoded_content.decode("utf-8")
try:
file_content = repo.get_contents("common.props", ref=branch)
common_props_content = file_content.decoded_content.decode("utf-8")

# XML parse it
root = ET.fromstring(common_props_content)
version = root.find(".//Version").text
leptonx_version = root.find(".//LeptonXVersion").text
root = ET.fromstring(common_props_content)
version = root.find(".//Version").text
leptonx_version = root.find(".//LeptonXVersion").text

return version, leptonx_version
return version, leptonx_version
except Exception as e:
raise FileNotFoundError(f"common.props not found in branch {branch}: {e}")

def update_latest_versions():
latest_branch = get_latest_release_branch()
version, leptonx_version = get_version_from_common_props(latest_branch)

"""
Updates `latest-versions.json` based on the most relevant release branch.
"""
# Get the release version from GitHub reference
release_version = os.environ["GITHUB_REF"].split("/")[-1] # Example: "refs/tags/v9.0.5" → "v9.0.5"
if release_version.startswith("v"):
release_version = release_version[1:] # Convert to "9.0.5" format

# Determine the correct `rel-x.x` branch
target_branch = get_target_release_branch(release_version)

# Retrieve `common.props` data from the target branch
version, leptonx_version = get_version_from_common_props(target_branch)

# Skip if the version is a preview or release candidate
if "preview" in version or "rc" in version:
return False

# Read the `latest-versions.json` file
with open("latest-versions.json", "r") as f:
latest_versions = json.load(f)

# Add the new version entry
new_version_entry = {
"version": version,
"releaseDate": "",
Expand All @@ -62,9 +69,10 @@ def update_latest_versions():
"version": leptonx_version
}
}

latest_versions.insert(0, new_version_entry) # Insert the new version at the top

latest_versions.insert(0, new_version_entry) # Add to the beginning of the list

# Update the file
with open("latest-versions.json", "w") as f:
json.dump(latest_versions, f, indent=2)

Expand Down

0 comments on commit c4c674e

Please sign in to comment.