|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | + |
| 9 | +def get_remote_commit_sha(repo_url: str, branch: str) -> str: |
| 10 | + output = subprocess.check_output( |
| 11 | + ["git", "ls-remote", repo_url, f"refs/heads/{branch}"], text=True |
| 12 | + ) |
| 13 | + return output.split("\t")[0] |
| 14 | + |
| 15 | + |
| 16 | +def get_remote_latest_tag(repo_url: str, tag_pattern: str) -> str: |
| 17 | + output = subprocess.check_output( |
| 18 | + ["git", "ls-remote", "--tags", repo_url], text=True |
| 19 | + ) |
| 20 | + tags = [] |
| 21 | + for line in output.split("\n"): |
| 22 | + if line.strip(): |
| 23 | + parts = line.split("\t") |
| 24 | + if len(parts) == 2: |
| 25 | + ref = parts[1] |
| 26 | + if ref.startswith("refs/tags/") and not ref.endswith("^{}"): |
| 27 | + tag = ref.replace("refs/tags/", "") |
| 28 | + if re.match(tag_pattern + "$", tag): |
| 29 | + tags.append(tag) |
| 30 | + |
| 31 | + def version_key(tag: str) -> tuple[int, ...]: |
| 32 | + version = tag.lstrip("v") |
| 33 | + return tuple(map(int, version.split("."))) |
| 34 | + |
| 35 | + return sorted(tags, key=version_key)[-1] |
| 36 | + |
| 37 | + |
| 38 | +def get_current_version_from_file(file_path: str, pattern: str) -> str: |
| 39 | + with open(file_path) as f: |
| 40 | + content = f.read() |
| 41 | + |
| 42 | + match = re.search(pattern, content) |
| 43 | + return match.group(1) |
| 44 | + |
| 45 | + |
| 46 | +def update_file_version( |
| 47 | + file_path: str, old_pattern: str, new_value: str, comment_pattern: str |
| 48 | +) -> None: |
| 49 | + with open(file_path) as f: |
| 50 | + content = f.read() |
| 51 | + |
| 52 | + new_content = re.sub(old_pattern, new_value, content) |
| 53 | + |
| 54 | + current_date = datetime.now().strftime("%b %d, %Y") |
| 55 | + new_content = re.sub( |
| 56 | + comment_pattern, |
| 57 | + lambda m: m.group(0).split(", as of")[0] + f", as of {current_date}.", |
| 58 | + new_content, |
| 59 | + ) |
| 60 | + |
| 61 | + with open(file_path, "w") as f: |
| 62 | + f.write(new_content) |
| 63 | + |
| 64 | + |
| 65 | +def generate_commit_message( |
| 66 | + repo_name: str, |
| 67 | + repo_url: str, |
| 68 | + old_version: str, |
| 69 | + new_version: str, |
| 70 | + is_tag: bool, |
| 71 | + commit_url_template: str, |
| 72 | + diff_url_template: str, |
| 73 | +) -> str: |
| 74 | + if is_tag: |
| 75 | + version_link = ( |
| 76 | + f"[Tag: {new_version}]({repo_url}/releases/tag/{new_version})" |
| 77 | + ) |
| 78 | + diff_url = diff_url_template.format( |
| 79 | + repo_url=repo_url, old_version=old_version, new_version=new_version |
| 80 | + ) |
| 81 | + diff_link = f"[Diff]({diff_url})" |
| 82 | + description = "between the previously used tag and the new tag." |
| 83 | + else: |
| 84 | + commit_url = commit_url_template.format( |
| 85 | + repo_url=repo_url, version=new_version |
| 86 | + ) |
| 87 | + version_link = f"[Commit: {new_version}]({commit_url})" |
| 88 | + diff_url = diff_url_template.format( |
| 89 | + repo_url=repo_url, old_version=old_version, new_version=new_version |
| 90 | + ) |
| 91 | + diff_link = f"[Diff]({diff_url})" |
| 92 | + description = ( |
| 93 | + "between the last commit hash merged to this repository " |
| 94 | + "and the new commit." |
| 95 | + ) |
| 96 | + |
| 97 | + return f"## {repo_name}\n{version_link}\n\n{diff_link} {description}" |
| 98 | + |
| 99 | + |
| 100 | +def main() -> int: |
| 101 | + parser = argparse.ArgumentParser( |
| 102 | + description="Bump a single dependency version" |
| 103 | + ) |
| 104 | + parser.add_argument( |
| 105 | + "--name", required=True, help="Display name for the dependency" |
| 106 | + ) |
| 107 | + parser.add_argument("--repo-url", required=True, help="Git repository URL") |
| 108 | + parser.add_argument( |
| 109 | + "--branch", default="main", help="Branch to check (default: main)" |
| 110 | + ) |
| 111 | + parser.add_argument( |
| 112 | + "--file-path", required=True, help="File containing current version" |
| 113 | + ) |
| 114 | + parser.add_argument( |
| 115 | + "--current-version-pattern", |
| 116 | + required=True, |
| 117 | + help="Regex to extract current version (group 1)", |
| 118 | + ) |
| 119 | + parser.add_argument( |
| 120 | + "--update-pattern", required=True, help="Regex pattern for replacement" |
| 121 | + ) |
| 122 | + parser.add_argument( |
| 123 | + "--comment-pattern", |
| 124 | + required=True, |
| 125 | + help="Regex pattern for comment update", |
| 126 | + ) |
| 127 | + parser.add_argument( |
| 128 | + "--tag", action="store_true", help="Check tags instead of commits" |
| 129 | + ) |
| 130 | + parser.add_argument( |
| 131 | + "--tag-pattern", default=r"v[0-9\.]*", help="Pattern for tag matching" |
| 132 | + ) |
| 133 | + parser.add_argument( |
| 134 | + "--commit-url-template", |
| 135 | + default="{repo_url}/commit/{version}", |
| 136 | + help="Template for commit URLs", |
| 137 | + ) |
| 138 | + parser.add_argument( |
| 139 | + "--diff-url-template", |
| 140 | + default="{repo_url}/compare/{old_version}...{new_version}", |
| 141 | + help="Template for diff URLs", |
| 142 | + ) |
| 143 | + |
| 144 | + args = parser.parse_args() |
| 145 | + |
| 146 | + current_version = get_current_version_from_file( |
| 147 | + args.file_path, args.current_version_pattern |
| 148 | + ) |
| 149 | + |
| 150 | + if args.tag: |
| 151 | + latest_version = get_remote_latest_tag(args.repo_url, args.tag_pattern) |
| 152 | + else: |
| 153 | + latest_version = get_remote_commit_sha(args.repo_url, args.branch) |
| 154 | + |
| 155 | + if current_version == latest_version: |
| 156 | + print(f"{args.name}: No update needed (current: {current_version})") |
| 157 | + with open(os.environ["GITHUB_OUTPUT"], "a") as f: |
| 158 | + f.write("HAS_UPDATES=false\n") |
| 159 | + return 0 |
| 160 | + |
| 161 | + print( |
| 162 | + f"{args.name}: Update available " |
| 163 | + f"({current_version} -> {latest_version})" |
| 164 | + ) |
| 165 | + |
| 166 | + replacement = args.update_pattern.replace("{new_version}", latest_version) |
| 167 | + update_file_version( |
| 168 | + args.file_path, |
| 169 | + args.current_version_pattern, |
| 170 | + replacement, |
| 171 | + args.comment_pattern, |
| 172 | + ) |
| 173 | + |
| 174 | + commit_msg = generate_commit_message( |
| 175 | + args.name, |
| 176 | + args.repo_url, |
| 177 | + current_version, |
| 178 | + latest_version, |
| 179 | + args.tag, |
| 180 | + args.commit_url_template, |
| 181 | + args.diff_url_template, |
| 182 | + ) |
| 183 | + |
| 184 | + with open(os.environ["GITHUB_OUTPUT"], "a") as f: |
| 185 | + f.write("COMMIT_MSG<<EOF\n") |
| 186 | + f.write(commit_msg) |
| 187 | + f.write("\nEOF\n") |
| 188 | + f.write("HAS_UPDATES=true\n") |
| 189 | + |
| 190 | + return 0 |
| 191 | + |
| 192 | + |
| 193 | +if __name__ == "__main__": |
| 194 | + sys.exit(main()) |
0 commit comments