|
1 | 1 | import os |
2 | 2 | from pathlib import Path |
3 | 3 |
|
4 | | -import ruamel.yaml |
5 | | - |
6 | 4 | from scripts.common import get_latest_github_tag |
7 | 5 |
|
8 | 6 |
|
9 | | -def get_latest_pin(action: str, current_version: str) -> str: |
10 | | - latest_tag_name, latest_tag_sha = get_latest_github_tag(action) |
11 | | - |
12 | | - pin_by_major = current_version.startswith("v") |
13 | | - |
14 | | - if pin_by_major: |
15 | | - major = latest_tag_name.split(".")[0] |
16 | | - return major |
17 | | - else: |
18 | | - return latest_tag_sha |
19 | | - |
20 | | - |
21 | | -def update_workflow_actions(file_path: Path, dry_run: bool = False): |
22 | | - yaml = ruamel.yaml.YAML(typ="jinja2") |
23 | | - yaml.preserve_quotes = True |
24 | | - yaml.indent(mapping=2, sequence=4, offset=2) |
25 | | - yaml.width = 1000 # Prevent default wrapping |
26 | | - |
27 | | - with open(file_path) as f: |
28 | | - workflow = yaml.load(f) |
29 | | - |
30 | | - for job in workflow.get("jobs", {}).values(): |
31 | | - for step in job.get("steps", []): |
32 | | - if "uses" in step: |
33 | | - action_ref = step["uses"] |
34 | | - action, _, current_version = action_ref.partition("@") |
35 | | - new_version = get_latest_pin(action, current_version) |
36 | | - print( |
37 | | - f"{action}:" |
38 | | - f" current version: {current_version}," |
39 | | - f" latest version: {new_version}" |
40 | | - ) |
41 | | - if new_version != current_version and not dry_run: |
42 | | - step["uses"] = f"{action}@{new_version}" |
43 | | - |
44 | | - with open(file_path, "w") as f: |
45 | | - yaml.dump(workflow, f) |
46 | | - |
47 | | - |
48 | | -def update_project_workflows( |
49 | | - path: str = "template/.github/workflows/", dry_run: bool = False |
50 | | -): |
| 7 | +def update_workflow_actions(file_path: Path): |
| 8 | + text = file_path.read_text() |
| 9 | + lines = text.splitlines(keepends=True) |
| 10 | + |
| 11 | + new_lines: list[str] = [] |
| 12 | + |
| 13 | + for line in lines: |
| 14 | + if "uses: " not in line: |
| 15 | + new_lines.append(line) |
| 16 | + else: |
| 17 | + action_with_rest = line.split(":")[1].strip() |
| 18 | + action, current_sha_with_version = action_with_rest.split("@") |
| 19 | + current_sha, current_version = current_sha_with_version.split("#") |
| 20 | + current_sha = current_sha.strip() |
| 21 | + current_version = current_version.strip() |
| 22 | + new_version, new_sha = get_latest_github_tag(action) |
| 23 | + print( |
| 24 | + f"{action}:" |
| 25 | + f" current version: {current_version}," |
| 26 | + f" latest version: {new_version}" |
| 27 | + ) |
| 28 | + new_line = line.replace(current_sha, new_sha).replace( |
| 29 | + current_version, new_version |
| 30 | + ) |
| 31 | + new_lines.append(new_line) |
| 32 | + |
| 33 | + file_path.write_text("".join(new_lines)) |
| 34 | + |
| 35 | + |
| 36 | +def update_project_workflows(path: str = "template/.github/workflows/"): |
51 | 37 | for file in os.listdir(path): |
52 | | - print(f"Updating {file}") |
53 | | - update_workflow_actions(Path(path) / file, dry_run=dry_run) |
| 38 | + print(f"Updating `{file}`") |
| 39 | + update_workflow_actions(Path(path) / file) |
54 | 40 |
|
55 | 41 |
|
56 | 42 | if __name__ == "__main__": |
|
0 commit comments