This repository was archived by the owner on Apr 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.py
More file actions
executable file
·380 lines (317 loc) · 12.7 KB
/
release.py
File metadata and controls
executable file
·380 lines (317 loc) · 12.7 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python3
"""
Enhanced release automation script for UV-based projects.
Works without Poetry dependency, directly manipulating pyproject.toml.
"""
import argparse
import json
import os
import re
import subprocess
import sys
import urllib.error
import urllib.request
from datetime import datetime
from pathlib import Path
from typing import Literal
VERSION_TYPES = Literal["major", "minor", "patch"]
COMMIT_TYPES = {
"feat": "✨ Features",
"fix": "🐛 Bug Fixes",
"docs": "📚 Documentation",
"style": "💄 Styling",
"refactor": "♻️ Code Refactoring",
"perf": "⚡ Performance Improvements",
"test": "✅ Tests",
"build": "📦 Build System",
"ci": "👷 CI",
"chore": "🔧 Chores",
}
def run_command(cmd: str | list[str], check: bool = True) -> str:
"""Run a command safely without shell=True.
Args:
cmd: Command as list of arguments (preferred) or string (will be parsed)
check: Whether to raise exception on non-zero exit
Returns:
Command output as string
"""
import shlex
if isinstance(cmd, str):
cmd = shlex.split(cmd)
try:
result = subprocess.run(cmd, shell=False, capture_output=True, text=True, check=check)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
if check:
print(f"❌ Command failed: {' '.join(cmd) if isinstance(cmd, list) else cmd}")
print(f" Error: {e.stderr}")
sys.exit(1)
return ""
def get_current_version() -> str:
"""Get current version from pyproject.toml."""
pyproject_path = Path("pyproject.toml")
with open(pyproject_path) as f:
content = f.read()
match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE)
if match:
return match.group(1)
print("❌ Could not find version in pyproject.toml")
sys.exit(1)
def bump_version(current_version: str, version_type: VERSION_TYPES) -> str:
"""Bump version based on type."""
parts = current_version.split('.')
major, minor, patch = int(parts[0]), int(parts[1]), int(parts[2])
if version_type == "major":
return f"{major + 1}.0.0"
elif version_type == "minor":
return f"{major}.{minor + 1}.0"
else: # patch
return f"{major}.{minor}.{patch + 1}"
def update_version_in_files(new_version: str) -> None:
"""Update version in pyproject.toml and __init__.py."""
# Update pyproject.toml
pyproject_path = Path("pyproject.toml")
with open(pyproject_path) as f:
content = f.read()
content = re.sub(
r'^version\s*=\s*"[^"]+"',
f'version = "{new_version}"',
content,
flags=re.MULTILINE
)
with open(pyproject_path, 'w') as f:
f.write(content)
# Update __init__.py
init_path = Path("commitloom/__init__.py")
if init_path.exists():
with open(init_path) as f:
content = f.read()
content = re.sub(
r'__version__\s*=\s*"[^"]+"',
f'__version__ = "{new_version}"',
content
)
with open(init_path, 'w') as f:
f.write(content)
def parse_commit_message(commit: str) -> tuple[str, str]:
"""Parse a commit message into type and description."""
match = re.match(r'^(\w+)(?:\(.*?\))?: (.+)$', commit.strip())
if match:
return match.group(1), match.group(2)
return "other", commit.strip()
def categorize_commits(commits: list[str]) -> dict[str, list[str]]:
"""Categorize commits by type."""
categorized: dict[str, list[str]] = {type_key: [] for type_key in COMMIT_TYPES}
categorized["other"] = []
for commit in commits:
if not commit.strip():
continue
commit_type, description = parse_commit_message(commit)
if commit_type in categorized:
categorized[commit_type].append(description)
else:
categorized["other"].append(description)
return {k: v for k, v in categorized.items() if v}
def update_changelog(version: str) -> None:
"""Update CHANGELOG.md with new version entry."""
changelog_path = Path("CHANGELOG.md")
current_date = datetime.now().strftime("%Y-%m-%d")
# Get commits since last tag
try:
last_tag = run_command(["git", "describe", "--tags", "--abbrev=0"], check=True)
except Exception:
last_tag = ""
if last_tag:
raw_commits = run_command(["git", "log", f"{last_tag}..HEAD", "--pretty=format:%s"]).split('\n')
else:
raw_commits = run_command(["git", "log", "--pretty=format:%s"]).split('\n')
# Categorize commits
categorized_commits = categorize_commits(raw_commits)
# Create new changelog entry
new_entry = [f"## [{version}] - {current_date}\n"]
for commit_type, emoji_title in COMMIT_TYPES.items():
if commit_type in categorized_commits and categorized_commits[commit_type]:
new_entry.append(f"\n### {emoji_title}")
for change in categorized_commits[commit_type]:
new_entry.append(f"- {change}")
if "other" in categorized_commits and categorized_commits["other"]:
new_entry.append("\n### 🔄 Other Changes")
for change in categorized_commits["other"]:
new_entry.append(f"- {change}")
new_entry.append("\n")
new_entry_text = "\n".join(new_entry)
# Read existing changelog
if changelog_path.exists():
with open(changelog_path) as f:
content = f.read()
else:
content = "# Changelog\n\n"
# Add new entry after header
updated_content = re.sub(
r"(# Changelog\n\n)",
f"\\1{new_entry_text}",
content
)
with open(changelog_path, "w") as f:
f.write(updated_content)
def create_version_commits(new_version: str) -> None:
"""Create granular commits for version changes."""
# Update version files
update_version_in_files(new_version)
# Commit version bump
run_command(["git", "add", "pyproject.toml", "commitloom/__init__.py"])
run_command(["git", "commit", "-m", f"build: bump version to {new_version}"])
print("✅ Committed version bump")
# Update changelog
update_changelog(new_version)
run_command(["git", "add", "CHANGELOG.md"])
run_command(["git", "commit", "-m", f"docs: update changelog for {new_version}"])
print("✅ Committed changelog update")
def get_changelog_entry(version: str) -> str:
"""Extract changelog entry for a specific version."""
changelog_path = Path("CHANGELOG.md")
if not changelog_path.exists():
return ""
with open(changelog_path) as f:
content = f.read()
# Extract the entry for this version
pattern = rf"## \[{re.escape(version)}\].*?\n\n(.*?)(?=\n## \[|\Z)"
match = re.search(pattern, content, re.DOTALL)
if match:
return match.group(1).strip()
return ""
def create_github_release(version: str, dry_run: bool = False) -> None:
"""Create GitHub release with tag."""
tag = f"v{version}"
if dry_run:
print(f"[DRY RUN] Would create tag: {tag}")
return
# Create and push tag
changelog_content = get_changelog_entry(version)
tag_message = f"Release {tag}\n\n{changelog_content}" if changelog_content else f"Release {tag}"
run_command(["git", "tag", "-a", tag, "-m", tag_message])
print(f"✅ Created tag {tag}")
# Push commits and tag
run_command(["git", "push", "origin", "main"])
print("✅ Pushed commits to main")
run_command(["git", "push", "origin", "--tags"])
print("✅ Pushed tag to origin")
# Create GitHub Release via API
github_token = os.getenv("GITHUB_TOKEN")
if github_token:
try:
# Get repository info from git remote
remote_url = run_command(["git", "remote", "get-url", "origin"])
repo_match = re.search(r"github\.com[:/](.+?)(?:\.git)?$", remote_url)
if not repo_match:
print("⚠️ Could not parse GitHub repository from remote URL")
return
repo_path = repo_match.group(1)
# Prepare release data
release_data = {
"tag_name": tag,
"name": f"Release {tag}",
"body": changelog_content,
"draft": False,
"prerelease": False
}
# Create release via GitHub API
url = f"https://api.github.com/repos/{repo_path}/releases"
headers = {
"Authorization": f"token {github_token}",
"Accept": "application/vnd.github.v3+json"
}
request = urllib.request.Request(
url,
data=json.dumps(release_data).encode(),
headers=headers,
method="POST"
)
with urllib.request.urlopen(request) as response:
if response.status == 201:
print("✅ Created GitHub Release")
else:
print(f"⚠️ GitHub Release creation returned status {response.status}")
except Exception as e:
print(f"⚠️ Could not create GitHub Release: {str(e)}")
print(" You may need to set the GITHUB_TOKEN environment variable")
else:
print("ℹ️ GITHUB_TOKEN not found. Skipping GitHub Release creation")
print(" Set GITHUB_TOKEN to enable automatic GitHub Release creation")
def check_prerequisites() -> None:
"""Check that we can proceed with release."""
# Ensure we're on main branch
current_branch = run_command(["git", "branch", "--show-current"])
if current_branch != "main":
print(f"❌ Must be on main branch to release (currently on {current_branch})")
sys.exit(1)
# Ensure working directory is clean
if run_command(["git", "status", "--porcelain"]):
print("❌ Working directory is not clean. Commit or stash changes first.")
sys.exit(1)
# Ensure git user is configured
user_name = run_command(["git", "config", "user.name"], check=False)
user_email = run_command(["git", "config", "user.email"], check=False)
if not user_name or not user_email:
print("❌ Git user not configured. Please configure git user:")
print(" git config user.name 'Your Name'")
print(" git config user.email 'your.email@example.com'")
sys.exit(1)
def main() -> None:
parser = argparse.ArgumentParser(
description="Enhanced release automation for UV-based projects"
)
parser.add_argument(
"version_type",
choices=["major", "minor", "patch"],
help="Type of version bump"
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be done without making changes"
)
parser.add_argument(
"--skip-github",
action="store_true",
help="Skip GitHub release creation"
)
args = parser.parse_args()
print("🚀 Starting release process...")
# Check prerequisites
check_prerequisites()
# Get current version and calculate new version
old_version = get_current_version()
new_version = bump_version(old_version, args.version_type)
print(f"📦 Version bump: {old_version} → {new_version}")
if args.dry_run:
print("\n[DRY RUN] Would perform the following actions:")
print(f" 1. Update version to {new_version} in pyproject.toml and __init__.py")
print(f" 2. Create commit: 'build: bump version to {new_version}'")
print(f" 3. Update CHANGELOG.md with new entries")
print(f" 4. Create commit: 'docs: update changelog for {new_version}'")
print(f" 5. Create tag: v{new_version}")
if not args.skip_github:
print(f" 6. Push to origin and create GitHub Release")
else:
# Create version commits
create_version_commits(new_version)
# Create release
if not args.skip_github:
create_github_release(new_version, dry_run=args.dry_run)
else:
# Just create local tag
tag = f"v{new_version}"
changelog_content = get_changelog_entry(new_version)
tag_message = f"Release {tag}\n\n{changelog_content}" if changelog_content else f"Release {tag}"
run_command(["git", "tag", "-a", tag, "-m", tag_message])
print(f"✅ Created tag {tag}")
print("ℹ️ Skipped GitHub release (use --skip-github=false to enable)")
print(f"\n🎉 Release {new_version} is ready!")
print("\nNext steps:")
if args.skip_github:
print(" 1. Push changes: git push origin main --tags")
print(" 2. Create GitHub release manually if needed")
print(" 3. Publish to PyPI: uv publish (or your CI/CD will do this)")
if __name__ == "__main__":
main()