diff --git a/.github/scripts/update_versions.py b/.github/scripts/update_versions.py index dcbd749796..1f593273bc 100644 --- a/.github/scripts/update_versions.py +++ b/.github/scripts/update_versions.py @@ -1,54 +1,79 @@ import os import json +import re +import xml.etree.ElementTree as ET from github import Github -def update_latest_versions(): - version = os.environ["GITHUB_REF"].split("/")[-1] +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}") - if "rc" in version: - return False + major, minor = match.groups() + target_branch = f"rel-{major}.{minor}" + return target_branch - with open("latest-versions.json", "r") as f: - latest_versions = json.load(f) +def get_version_from_common_props(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") - latest_versions[0]["version"] = version + try: + file_content = repo.get_contents("common.props", ref=branch) + common_props_content = file_content.decoded_content.decode("utf-8") - with open("latest-versions.json", "w") as f: - json.dump(latest_versions, f, indent=2) + root = ET.fromstring(common_props_content) + version = root.find(".//Version").text + leptonx_version = root.find(".//LeptonXVersion").text - return True + return version, leptonx_version + except Exception as e: + raise FileNotFoundError(f"common.props not found in branch {branch}: {e}") -def create_pr(): - g = Github(os.environ["GITHUB_TOKEN"]) - repo = g.get_repo("abpframework/abp") +def update_latest_versions(): + """ + 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 - branch_name = f"update-latest-versions-{os.environ['GITHUB_REF'].split('/')[-1]}" - base = repo.get_branch("dev") - repo.create_git_ref(ref=f"refs/heads/{branch_name}", sha=base.commit.sha) + # 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) - # Get the current latest-versions.json file and its sha - contents = repo.get_contents("latest-versions.json", ref="dev") - file_sha = contents.sha + # Skip if the version is a preview or release candidate + if "preview" in version or "rc" in version: + return False - # Update the file in the repo - repo.update_file( - path="latest-versions.json", - message=f"Update latest-versions.json to version {os.environ['GITHUB_REF'].split('/')[-1]}", - content=open("latest-versions.json", "r").read().encode("utf-8"), - sha=file_sha, - branch=branch_name, - ) + # Read the `latest-versions.json` file + with open("latest-versions.json", "r") as f: + latest_versions = json.load(f) - try: - pr = repo.create_pull(title="Update latest-versions.json", - body="Automated PR to update the latest-versions.json file.", - head=branch_name, base="dev") - except Exception as e: - print(f"Error while creating PR: {e}") + # Add the new version entry + new_version_entry = { + "version": version, + "releaseDate": "", + "type": "stable", + "message": "", + "leptonx": { + "version": leptonx_version + } + } + + latest_versions.insert(0, new_version_entry) # Insert the new version at the top - pr.create_review_request(reviewers=["ebicoglu", "gizemmutukurt", "skoc10"]) + # Update the file + with open("latest-versions.json", "w") as f: + json.dump(latest_versions, f, indent=2) -if __name__ == "__main__": - should_create_pr = update_latest_versions() - if should_create_pr: - create_pr() \ No newline at end of file + return True