|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import sys |
4 | | -from io import StringIO |
5 | 4 | from pathlib import Path |
6 | 5 |
|
7 | 6 | from ruamel.yaml import YAML |
@@ -84,36 +83,60 @@ def collect_workflow_files(root: Path, file_glob: str) -> list[Path]: |
84 | 83 |
|
85 | 84 | def apply_updates(text: str, upgrades: dict[tuple[str, str], str]) -> str: |
86 | 85 | """ |
87 | | - Apply updates to a YAML workflow file using ruamel.yaml. |
88 | | - This preserves formatting and comments. |
| 86 | + Apply updates to a YAML workflow file by doing targeted text replacements. |
| 87 | + This preserves all original formatting and comments, only modifying the 'uses:' lines. |
89 | 88 | """ |
90 | | - yaml = YAML() |
91 | | - yaml.preserve_quotes = True |
92 | | - yaml.default_flow_style = False |
93 | | - yaml.map_indent = 2 |
94 | | - yaml.sequence_indent = 4 |
95 | | - yaml.sequence_dash_offset = 2 |
| 89 | + lines = text.split('\n') |
96 | 90 |
|
97 | | - try: |
98 | | - docs = list(yaml.load_all(text)) |
99 | | - except Exception: |
100 | | - # If parsing fails, return original text unchanged |
101 | | - return text |
| 91 | + for i, line in enumerate(lines): |
| 92 | + # Look for lines that contain 'uses:' with a value |
| 93 | + # Handle both plain keys and list items with dashes |
| 94 | + stripped = line.lstrip() |
102 | 95 |
|
103 | | - # Check if any updates are needed |
104 | | - any_updates = False |
105 | | - for doc in docs: |
106 | | - if doc is not None and update_uses_in_structure(doc, upgrades): |
107 | | - any_updates = True |
| 96 | + # Check if line has 'uses:' (either "uses:" or "- uses:") |
| 97 | + if 'uses:' not in stripped: |
| 98 | + continue |
108 | 99 |
|
109 | | - if not any_updates: |
110 | | - return text |
| 100 | + # Find the position of 'uses:' in the line |
| 101 | + uses_idx = stripped.find('uses:') |
| 102 | + if uses_idx == -1: |
| 103 | + continue |
111 | 104 |
|
112 | | - # Write back with preserved formatting |
113 | | - output = StringIO() |
114 | | - if len(docs) == 1: |
115 | | - yaml.dump(docs[0], output) |
116 | | - else: |
117 | | - yaml.dump_all(docs, output) |
| 105 | + # Check if everything before 'uses:' is valid YAML (dash followed by spaces, or nothing) |
| 106 | + prefix = stripped[:uses_idx].strip() |
| 107 | + if prefix and prefix != '-': |
| 108 | + continue |
118 | 109 |
|
119 | | - return output.getvalue() |
| 110 | + # Extract the indentation from the original line |
| 111 | + indent = line[:len(line) - len(stripped)] |
| 112 | + |
| 113 | + # Get the part after 'uses:' |
| 114 | + rest = stripped[uses_idx + 5:].strip() # Remove 'uses:' and leading whitespace |
| 115 | + |
| 116 | + # Handle comments - extract value and any trailing comment |
| 117 | + comment = "" |
| 118 | + value_part = rest |
| 119 | + if '#' in rest: |
| 120 | + parts = rest.split('#', 1) |
| 121 | + value_part = parts[0].strip() |
| 122 | + comment = '#' + parts[1] |
| 123 | + |
| 124 | + # Check if this value matches any upgrade |
| 125 | + for (repo, current_tag), new_tag in upgrades.items(): |
| 126 | + old_value = f"{repo}@{current_tag}" |
| 127 | + new_value = f"{repo}@{new_tag}" |
| 128 | + if value_part == old_value: |
| 129 | + # Reconstruct the line, preserving list item syntax if present |
| 130 | + if stripped.startswith('- '): |
| 131 | + if comment: |
| 132 | + lines[i] = f"{indent}- uses: {new_value} {comment}" |
| 133 | + else: |
| 134 | + lines[i] = f"{indent}- uses: {new_value}" |
| 135 | + else: |
| 136 | + if comment: |
| 137 | + lines[i] = f"{indent}uses: {new_value} {comment}" |
| 138 | + else: |
| 139 | + lines[i] = f"{indent}uses: {new_value}" |
| 140 | + break |
| 141 | + |
| 142 | + return '\n'.join(lines) |
0 commit comments