-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease_manager.py
More file actions
378 lines (312 loc) · 12.6 KB
/
release_manager.py
File metadata and controls
378 lines (312 loc) · 12.6 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
#!/usr/bin/env python3
"""
Release Manager for OpenClaw Workspace
Manages Git tags and GitHub Releases based on Task Registry versions.
Usage:
python3 release_manager.py check # Check for version changes
python3 release_manager.py tag # Create tags for new versions
python3 release_manager.py release # Create GitHub Releases
"""
import json
import os
import sys
import subprocess
import re
from datetime import datetime
from typing import Dict, List, Any, Optional, Tuple
# Configuration
REPO = "jaccchina-ai/openclaw-workspace"
TASK_REGISTRY_PATH = "task_registry.json"
def run_command(cmd: List[str], capture_output: bool = True) -> Tuple[int, str, str]:
"""Run a shell command and return results."""
try:
result = subprocess.run(
cmd,
capture_output=capture_output,
text=True,
timeout=30
)
if capture_output:
return result.returncode, result.stdout, result.stderr
else:
return result.returncode, "", ""
except FileNotFoundError as e:
return 1, "", str(e)
except subprocess.TimeoutExpired:
return 1, "", "Command timed out"
def get_git_tags() -> List[str]:
"""Get all git tags."""
returncode, stdout, stderr = run_command(["git", "tag", "--list"])
if returncode != 0:
print(f"❌ Failed to get git tags: {stderr}")
return []
return [tag.strip() for tag in stdout.split("\n") if tag.strip()]
def get_github_releases() -> List[Dict[str, Any]]:
"""Get existing GitHub releases."""
returncode, stdout, stderr = run_command(["gh", "release", "list", "--repo", REPO, "--limit", "50", "--json", "tagName,createdAt,name"])
if returncode != 0:
print(f"❌ Failed to get GitHub releases: {stderr}")
return []
try:
return json.loads(stdout)
except json.JSONDecodeError:
return []
def load_task_registry() -> Optional[Dict[str, Any]]:
"""Load the task registry file."""
try:
with open(TASK_REGISTRY_PATH, 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
print(f"❌ Task registry file not found: {TASK_REGISTRY_PATH}")
return None
except json.JSONDecodeError as e:
print(f"❌ Failed to parse task registry JSON: {e}")
return None
def parse_version(version_str: str) -> Tuple[int, int, int]:
"""Parse semantic version string into tuple."""
match = re.match(r'v?(\d+)\.(\d+)\.(\d+)', version_str)
if match:
return tuple(map(int, match.groups()))
return (0, 0, 0)
def get_task_tags(task_id: str) -> List[str]:
"""Get existing tags for a specific task."""
all_tags = get_git_tags()
task_tags = []
for tag in all_tags:
if tag.startswith(f"{task_id}/v") or tag.startswith(f"{task_id}-v"):
task_tags.append(tag)
return sorted(task_tags, key=parse_version, reverse=True)
def get_latest_task_tag(task_id: str) -> Optional[str]:
"""Get the latest tag for a task."""
task_tags = get_task_tags(task_id)
return task_tags[0] if task_tags else None
def create_git_tag(tag_name: str, message: str = "") -> bool:
"""Create a git tag."""
print(f"🏷️ Creating git tag: {tag_name}")
if not message:
message = f"Release {tag_name}"
returncode, stdout, stderr = run_command(["git", "tag", "-a", tag_name, "-m", message])
if returncode == 0:
print(f"✅ Created git tag: {tag_name}")
return True
else:
print(f"❌ Failed to create git tag {tag_name}: {stderr}")
return False
def push_git_tag(tag_name: str) -> bool:
"""Push a git tag to remote."""
print(f"📤 Pushing tag to remote: {tag_name}")
returncode, stdout, stderr = run_command(["git", "push", "origin", tag_name])
if returncode == 0:
print(f"✅ Pushed tag {tag_name} to remote")
return True
else:
print(f"❌ Failed to push tag {tag_name}: {stderr}")
return False
def create_github_release(tag_name: str, task: Dict[str, Any]) -> bool:
"""Create a GitHub release."""
print(f"🚀 Creating GitHub release: {tag_name}")
# Prepare release notes
release_notes = generate_release_notes(tag_name, task)
# Write release notes to temp file
notes_file = f"/tmp/release_notes_{tag_name}.md"
with open(notes_file, 'w', encoding='utf-8') as f:
f.write(release_notes)
# Create release using gh CLI
cmd = [
"gh", "release", "create",
tag_name,
"--repo", REPO,
"--title", f"{task['id']} {task.get('version', 'unknown')}",
"--notes-file", notes_file,
"--generate-notes" # Let GitHub generate additional notes
]
returncode, stdout, stderr = run_command(cmd, capture_output=False)
if returncode == 0:
print(f"✅ Created GitHub release: {tag_name}")
return True
else:
print(f"❌ Failed to create GitHub release {tag_name}: {stderr}")
return False
def generate_release_notes(tag_name: str, task: Dict[str, Any]) -> str:
"""Generate release notes for a task."""
notes = []
notes.append(f"# {task['id']} {task.get('version', 'unknown')}")
notes.append("")
notes.append(f"**Task:** {task.get('name', 'Unnamed task')}")
notes.append(f"**Release:** {tag_name}")
notes.append(f"**Date:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
notes.append("")
# Description
if task.get('description'):
notes.append("## Description")
notes.append(task['description'])
notes.append("")
# Changes based on version
if task.get('snapshots'):
notes.append("## Recent Changes")
# Get the latest snapshot
latest_snapshot = max(task['snapshots'], key=lambda x: x.get('created_at', ''))
notes.append(f"Latest snapshot: {latest_snapshot.get('created_at', 'unknown')}")
notes.append("")
# Configuration
if task.get('configuration_file'):
notes.append("## Configuration")
notes.append(f"Main configuration file: `{task['configuration_file']}`")
notes.append("")
# Dependencies
if task.get('dependencies'):
notes.append("## Dependencies")
for dep in task['dependencies']:
notes.append(f"- {dep}")
notes.append("")
notes.append("---")
notes.append("*This release was automatically generated by the OpenClaw Release Manager.*")
return "\n".join(notes)
def check_versions() -> bool:
"""Check for version changes that need tagging."""
print("🔍 Checking for version changes...")
registry = load_task_registry()
if not registry:
return False
tasks = registry.get('tasks', [])
print(f"📊 Found {len(tasks)} tasks in registry")
needs_tagging = []
for task in tasks:
task_id = task['id']
current_version = task.get('version', '0.0.0')
# Get latest tag for this task
latest_tag = get_latest_task_tag(task_id)
if latest_tag:
# Extract version from tag
tag_version_match = re.search(r'v(\d+\.\d+\.\d+)', latest_tag)
if tag_version_match:
tag_version = tag_version_match.group(1)
if tag_version != current_version:
needs_tagging.append({
'task_id': task_id,
'current_version': current_version,
'tagged_version': tag_version,
'latest_tag': latest_tag
})
print(f"🔄 {task_id}: Version changed from {tag_version} to {current_version}")
else:
print(f"✅ {task_id}: Version {current_version} is already tagged")
else:
print(f"⚠️ {task_id}: Could not parse version from tag {latest_tag}")
else:
# No existing tag
needs_tagging.append({
'task_id': task_id,
'current_version': current_version,
'tagged_version': None,
'latest_tag': None
})
print(f"🆕 {task_id}: No existing tag, current version is {current_version}")
# Print summary
if needs_tagging:
print(f"\n📋 Summary: {len(needs_tagging)} task(s) need tagging:")
for item in needs_tagging:
if item['tagged_version']:
print(f" • {item['task_id']}: {item['tagged_version']} → {item['current_version']}")
else:
print(f" • {item['task_id']}: No tag → {item['current_version']}")
return True
else:
print("✅ All tasks are up to date with tags")
return False
def create_tags() -> bool:
"""Create git tags for tasks with new versions."""
print("🏷️ Creating tags for new versions...")
registry = load_task_registry()
if not registry:
return False
tasks = registry.get('tasks', [])
success = True
for task in tasks:
task_id = task['id']
current_version = task.get('version', '0.0.0')
# Check if tag already exists for this version
expected_tag = f"{task_id}/v{current_version}"
existing_tags = get_task_tags(task_id)
tag_exists = any(tag == expected_tag or tag.endswith(f"/v{current_version}") for tag in existing_tags)
if tag_exists:
print(f"✅ {task_id} v{current_version} is already tagged")
continue
# Create new tag
tag_name = f"{task_id}/v{current_version}"
message = f"{task_id} version {current_version}: {task.get('name', '')}"
if create_git_tag(tag_name, message):
if push_git_tag(tag_name):
print(f"✅ Successfully created and pushed tag: {tag_name}")
else:
print(f"⚠️ Created tag {tag_name} but failed to push")
success = False
else:
print(f"❌ Failed to create tag: {tag_name}")
success = False
return success
def create_releases() -> bool:
"""Create GitHub releases for untagged versions."""
print("🚀 Creating GitHub releases...")
registry = load_task_registry()
if not registry:
return False
tasks = registry.get('tasks', [])
existing_releases = get_github_releases()
existing_release_tags = {release['tagName'] for release in existing_releases}
success = True
for task in tasks:
task_id = task['id']
current_version = task.get('version', '0.0.0')
# Check if release already exists
expected_tag = f"{task_id}/v{current_version}"
if expected_tag in existing_release_tags:
print(f"✅ Release already exists for {expected_tag}")
continue
# Check if tag exists
task_tags = get_task_tags(task_id)
has_tag = any(tag == expected_tag for tag in task_tags)
if not has_tag:
print(f"⚠️ No git tag found for {expected_tag}, creating tag first...")
if create_git_tag(expected_tag):
push_git_tag(expected_tag)
else:
print(f"❌ Skipping release for {expected_tag} (no tag)")
success = False
continue
# Create release
if create_github_release(expected_tag, task):
print(f"✅ Created release for {expected_tag}")
else:
print(f"❌ Failed to create release for {expected_tag}")
success = False
return success
def main():
"""Main entry point."""
if len(sys.argv) < 2:
print(__doc__)
print("\nCommands:")
print(" check - Check for version changes needing tags")
print(" tag - Create git tags for new versions")
print(" release - Create GitHub releases for new tags")
sys.exit(1)
command = sys.argv[1].lower()
# Ensure we're in the git repo
if not os.path.exists(".git"):
print("❌ Not in a git repository")
sys.exit(1)
if command == "check":
needs_update = check_versions()
sys.exit(0 if not needs_update else 1)
elif command == "tag":
success = create_tags()
sys.exit(0 if success else 1)
elif command == "release":
success = create_releases()
sys.exit(0 if success else 1)
else:
print(f"❌ Unknown command: {command}")
print(__doc__)
sys.exit(1)
if __name__ == "__main__":
main()