|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import base64 |
| 5 | +import hashlib |
| 6 | +import json |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +def main() -> None: |
| 11 | + parser = argparse.ArgumentParser() |
| 12 | + parser.add_argument("--source", type=Path, required=True) |
| 13 | + parser.add_argument("--registry", type=Path, required=True) |
| 14 | + parser.add_argument("--tag", required=True) |
| 15 | + args = parser.parse_args() |
| 16 | + |
| 17 | + if args.tag != "v0.63.0": |
| 18 | + raise ValueError(f"unexpected release tag: {args.tag}") |
| 19 | + |
| 20 | + version = args.tag.removeprefix("v") |
| 21 | + module_directory = args.registry / "modules" / "rules_go" / version |
| 22 | + source_module = (args.source / "MODULE.bazel").read_text() |
| 23 | + marker = ' repo_name = "io_bazel_rules_go",\n' |
| 24 | + if source_module.count(marker) != 1: |
| 25 | + raise ValueError("could not identify the rules_go module declaration") |
| 26 | + |
| 27 | + module_block = source_module.partition("\n)\n")[0] |
| 28 | + if " version = " in module_block: |
| 29 | + raise ValueError("the tagged rules_go module already declares a version") |
| 30 | + |
| 31 | + corrected_module = source_module.replace( |
| 32 | + marker, marker + f' version = "{version}",\n', 1 |
| 33 | + ) |
| 34 | + expected_dependency = 'bazel_dep(name = "bazel_features", version = "1.36.0",' |
| 35 | + if expected_dependency not in corrected_module: |
| 36 | + raise ValueError("the released bazel_features dependency is unexpected") |
| 37 | + (module_directory / "MODULE.bazel").write_text(corrected_module) |
| 38 | + |
| 39 | + previous_patch = ( |
| 40 | + args.registry |
| 41 | + / "modules" |
| 42 | + / "rules_go" |
| 43 | + / "0.62.0" |
| 44 | + / "patches" |
| 45 | + / "module_dot_bazel_version.patch" |
| 46 | + ).read_text() |
| 47 | + if previous_patch.count('"0.62.0"') != 1: |
| 48 | + raise ValueError("the previous module-version patch is unexpected") |
| 49 | + |
| 50 | + corrected_patch = previous_patch.replace('"0.62.0"', f'"{version}"', 1) |
| 51 | + patch_path = module_directory / "patches" / "module_dot_bazel_version.patch" |
| 52 | + patch_path.write_text(corrected_patch) |
| 53 | + |
| 54 | + patch_digest = hashlib.sha256(patch_path.read_bytes()).digest() |
| 55 | + patch_integrity = "sha256-" + base64.b64encode(patch_digest).decode() |
| 56 | + expected_patch_integrity = "sha256-78UHBUbfVo/yuxxWI+c+Irs0ujETcjVVhr7V76rAkVg=" |
| 57 | + if patch_integrity != expected_patch_integrity: |
| 58 | + raise ValueError(f"unexpected module-version patch integrity: {patch_integrity}") |
| 59 | + |
| 60 | + source_json_path = module_directory / "source.json" |
| 61 | + source_json = json.loads(source_json_path.read_text()) |
| 62 | + expected_archive_integrity = "sha256-w+JTI3EJqy4qjTywdWiLmKbm/OQ9hJhJZI6OOoTyDW8=" |
| 63 | + if source_json["integrity"] != expected_archive_integrity: |
| 64 | + raise ValueError("the published release archive integrity changed") |
| 65 | + source_json["patches"]["module_dot_bazel_version.patch"] = patch_integrity |
| 66 | + source_json_path.write_text(json.dumps(source_json, indent=4) + "\n") |
| 67 | + |
| 68 | + attestations_path = module_directory / "attestations.json" |
| 69 | + if not attestations_path.is_file(): |
| 70 | + raise ValueError("the invalid registry attestation manifest is missing") |
| 71 | + attestations_path.unlink() |
| 72 | + |
| 73 | + print(f"Corrected rules_go module version: {version}") |
| 74 | + print(f"Preserved bazel_features version: 1.36.0") |
| 75 | + print(f"Preserved archive integrity: {expected_archive_integrity}") |
| 76 | + print(f"Updated module patch integrity: {patch_integrity}") |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + main() |
0 commit comments