-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_tool.py
More file actions
416 lines (352 loc) · 13.5 KB
/
sync_tool.py
File metadata and controls
416 lines (352 loc) · 13.5 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/usr/bin/env python3
"""
Kubernetes Release Notes Sync Tool
A tool to synchronize release notes across YAML map files, JSON, and Markdown files.
"""
import argparse
import os
import sys
from release_notes_sync.constants import (OUTPUT_CSV, OUTPUT_JSON,
OUTPUT_TABLE, get_release_dir)
from release_notes_sync.formatter import (colorize, format_diff_section,
format_pr_sync_header,
format_sync_summary,
format_validation_csv,
format_validation_json,
format_validation_table)
from release_notes_sync.git_helper import (get_changed_pr_numbers, is_git_repo,
validate_commit_reference)
from release_notes_sync.sync_engine import sync_pr
from release_notes_sync.validator import (has_validation_issues,
validate_all_prs)
def display_validation_results(results: dict, output_format: str):
"""
Display validation results in the specified format.
Args:
results: Validation results dictionary
output_format: Output format (table, json, csv)
"""
if output_format == OUTPUT_JSON:
print(format_validation_json(results))
elif output_format == OUTPUT_CSV:
print(format_validation_csv(results))
else: # table (default)
print(format_validation_table(results))
def interactive_sync_approval(pr_number: str, diffs: list) -> bool:
"""
Show diffs to user and get approval for syncing.
Args:
pr_number: PR number being synced
diffs: List of diff dictionaries
Returns:
True if user approves, False otherwise
"""
# Show all diffs
for i, diff_info in enumerate(diffs, 1):
print(format_diff_section(i, len(diffs), diff_info["type"], diff_info["diff"]))
# Prompt for approval
while True:
response = (
input(colorize(f"Apply changes for PR #{pr_number}? [y/n/q]: ", "cyan"))
.lower()
.strip()
)
if response == "y":
return True
elif response == "n":
return False
elif response == "q":
print(colorize("\nSync cancelled by user", "yellow"))
sys.exit(0)
else:
print("Please enter 'y' for yes, 'n' for no, or 'q' to quit")
def handle_validate_command(args):
"""
Handle the validate command.
Args:
args: Parsed command line arguments
"""
release_version = args.release
repo_root = getattr(args, "repo_root", None)
# Check if release directory exists
release_dir = get_release_dir(release_version, repo_root)
if not os.path.exists(release_dir):
print(colorize(f"Error: Release directory not found: {release_dir}", "red"))
sys.exit(1)
# Determine which PRs to validate
pr_numbers = None
if hasattr(args, "prs") and args.prs:
# Specific PRs provided
pr_numbers = args.prs.split(",")
print(f"Validating {len(pr_numbers)} specific PRs...")
elif hasattr(args, "since_commit") and args.since_commit:
# Get PRs changed since commit
if not is_git_repo(repo_root):
print(colorize("Error: Not a git repository", "red"))
sys.exit(1)
if not validate_commit_reference(args.since_commit, repo_root):
print(
colorize(f"Error: Invalid commit reference: {args.since_commit}", "red")
)
sys.exit(1)
pr_numbers = get_changed_pr_numbers(
args.since_commit, release_version, repo_root
)
print(
f"Found {len(pr_numbers)} changed map files since commit {args.since_commit}"
)
if not pr_numbers:
print(colorize("No changed map files found", "yellow"))
return
elif args.global_mode:
# Validate all PRs
print(f"Validating all PRs in release {release_version}...")
else:
print(colorize("Error: Must specify --since-commit, --prs, or --global", "red"))
sys.exit(1)
# Run validation
results = validate_all_prs(release_version, pr_numbers, repo_root)
# Display results
output_format = getattr(args, "output", OUTPUT_TABLE)
display_validation_results(results, output_format)
# Exit with error code if issues found
if has_validation_issues(results):
sys.exit(1)
def handle_sync_command(args):
"""
Handle the sync command.
Args:
args: Parsed command line arguments
"""
release_version = args.release
repo_root = getattr(args, "repo_root", None)
auto_approve = getattr(args, "auto_yes", False)
dry_run = getattr(args, "dry_run", False)
# Check if release directory exists
release_dir = get_release_dir(release_version, repo_root)
if not os.path.exists(release_dir):
print(colorize(f"Error: Release directory not found: {release_dir}", "red"))
sys.exit(1)
# Skip uncommitted changes check for large repos (can be slow)
# Users should manage their git state manually
# if not dry_run and has_uncommitted_changes(repo_root):
# print(colorize("Warning: You have uncommitted changes", 'yellow'))
# response = input("Continue anyway? [y/n]: ").lower().strip()
# if response != 'y':
# print("Sync cancelled")
# return
# Determine which PRs to sync
pr_numbers = None
if hasattr(args, "prs") and args.prs:
# Specific PRs provided
pr_numbers = args.prs.split(",")
print(f"Syncing {len(pr_numbers)} specific PRs...")
elif hasattr(args, "since_commit") and args.since_commit:
# Get PRs changed since commit
if not is_git_repo(repo_root):
print(colorize("Error: Not a git repository", "red"))
sys.exit(1)
if not validate_commit_reference(args.since_commit, repo_root):
print(
colorize(f"Error: Invalid commit reference: {args.since_commit}", "red")
)
sys.exit(1)
pr_numbers = get_changed_pr_numbers(
args.since_commit, release_version, repo_root
)
print(
f"Found {len(pr_numbers)} changed map files since commit {args.since_commit}"
)
if not pr_numbers:
print(colorize("No changed map files found", "yellow"))
return
elif args.global_mode:
# Get all PRs that need syncing
from release_notes_sync.constants import get_maps_dir
from release_notes_sync.file_loader import (
extract_pr_number_from_filename, get_all_map_files)
maps_dir = get_maps_dir(release_version, repo_root)
map_files = get_all_map_files(maps_dir)
pr_numbers = [
extract_pr_number_from_filename(os.path.basename(f)) for f in map_files
]
pr_numbers = [pr for pr in pr_numbers if pr]
print(f"Syncing all PRs in release {release_version}...")
print(colorize("Warning: This will process all map files", "yellow"))
if not auto_approve and not dry_run:
response = input("Continue? [y/n]: ").lower().strip()
if response != "y":
print("Sync cancelled")
return
else:
print(colorize("Error: Must specify --since-commit, --prs, or --global", "red"))
sys.exit(1)
if dry_run:
print(
colorize("\n=== DRY RUN MODE - No changes will be applied ===\n", "yellow")
)
# Sync PRs
sync_results = []
for pr_num in pr_numbers:
try:
# First, get the sync details
result = sync_pr(
pr_num,
release_version,
auto_approve=False,
dry_run=True,
repo_root=repo_root,
)
# If there's an error, report it
if result.get("error"):
print(colorize(f"Error with PR #{pr_num}: {result['error']}", "red"))
sync_results.append(result)
continue
# If no changes needed, skip
if not result["changes_needed"]:
print(f"PR #{pr_num}: No changes needed")
sync_results.append(result)
continue
# Show the sync header and diffs
from release_notes_sync.constants import get_map_file
map_file = get_map_file(pr_num, release_version, repo_root)
print(format_pr_sync_header(pr_num, map_file))
# Get approval (if needed)
approved = True
if not auto_approve and not dry_run:
approved = interactive_sync_approval(pr_num, result["diffs"])
if not approved:
result["user_approved"] = False
print(colorize(f"Skipped PR #{pr_num}", "yellow"))
sync_results.append(result)
continue
# Apply changes (if not dry-run)
if not dry_run:
result = sync_pr(
pr_num,
release_version,
auto_approve=True,
dry_run=False,
repo_root=repo_root,
)
if result["changes_made"]:
print(colorize(f"✓ Changes applied for PR #{pr_num}", "green"))
else:
print(
colorize(f"✗ Failed to apply changes for PR #{pr_num}", "red")
)
else:
# In dry-run, just show what would happen
for diff_info in result["diffs"]:
print(
format_diff_section(
result["diffs"].index(diff_info) + 1,
len(result["diffs"]),
diff_info["type"],
diff_info["diff"],
)
)
sync_results.append(result)
except KeyboardInterrupt:
print(colorize("\n\nSync interrupted by user", "yellow"))
break
except Exception as e:
print(colorize(f"Error syncing PR #{pr_num}: {e}", "red"))
sync_results.append(
{"pr_number": pr_num, "error": str(e), "changes_made": False}
)
# Display summary
print(format_sync_summary(sync_results))
def main():
"""Main entry point for the CLI tool."""
parser = argparse.ArgumentParser(
description="Kubernetes Release Notes Sync Tool",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Validate changes since last commit
%(prog)s validate --since-commit HEAD~1 --release 1.35
# Sync changes interactively
%(prog)s sync --since-commit HEAD~1 --release 1.35
# Validate specific PRs
%(prog)s validate --prs 133540,132549 --release 1.35
# Validate entire release
%(prog)s validate --global --release 1.35
# Dry-run sync to see what would change
%(prog)s sync --since-commit HEAD~1 --release 1.35 --dry-run
""",
)
# Common arguments
parser.add_argument(
"--repo-root",
help="Repository root path (default: current directory)",
default=None,
)
# Subcommands
subparsers = parser.add_subparsers(dest="command", required=True)
# Validate command
validate_parser = subparsers.add_parser(
"validate", help="Validate consistency across map files, JSON, and Markdown"
)
validate_parser.add_argument(
"--release", required=True, help="Release version (e.g., 1.35)"
)
validate_parser.add_argument(
"--since-commit", help="Validate files changed since this commit"
)
validate_parser.add_argument(
"--prs", help="Comma-separated list of PR numbers to validate"
)
validate_parser.add_argument(
"--global",
action="store_true",
dest="global_mode",
help="Validate all map files in the release",
)
validate_parser.add_argument(
"--output",
choices=[OUTPUT_TABLE, OUTPUT_JSON, OUTPUT_CSV],
default=OUTPUT_TABLE,
help="Output format (default: table)",
)
# Sync command
sync_parser = subparsers.add_parser(
"sync", help="Synchronize changes from map files to JSON and Markdown"
)
sync_parser.add_argument(
"--release", required=True, help="Release version (e.g., 1.35)"
)
sync_parser.add_argument(
"--since-commit", help="Sync files changed since this commit"
)
sync_parser.add_argument("--prs", help="Comma-separated list of PR numbers to sync")
sync_parser.add_argument(
"--global",
action="store_true",
dest="global_mode",
help="Sync all map files in the release",
)
sync_parser.add_argument(
"--auto-yes", action="store_true", help="Skip all confirmations (dangerous!)"
)
sync_parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be done without applying changes",
)
# Parse arguments
args = parser.parse_args()
# Execute command
try:
if args.command == "validate":
handle_validate_command(args)
elif args.command == "sync":
handle_sync_command(args)
except KeyboardInterrupt:
print(colorize("\n\nOperation cancelled by user", "yellow"))
sys.exit(130)
except Exception as e:
print(colorize(f"\nError: {e}", "red"))
sys.exit(1)
if __name__ == "__main__":
main()