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