|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Publish-time body re-anchor (#356 rounds 6–7), as one tested path. |
| 16 | +
|
| 17 | +Asset reconciliation never reads the draft body, and anything holding |
| 18 | +``contents: write`` could have edited it during the approval pause. This |
| 19 | +helper composes the full re-anchor: extract the digest from the ANCHOR |
| 20 | +wheel → render the notes from the protected checkout → publish |
| 21 | +atomically with ``gh release edit --notes-file … --draft=false |
| 22 | +--latest=false``. PR-tested with a fake anchor wheel and a stubbed gh. |
| 23 | +""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +import argparse |
| 28 | +import pathlib |
| 29 | +import subprocess |
| 30 | +import sys |
| 31 | +from typing import Callable |
| 32 | + |
| 33 | +sys.path.insert(0, str(pathlib.Path(__file__).parent)) |
| 34 | +import release_image_tool |
| 35 | +import render_release_notes |
| 36 | + |
| 37 | + |
| 38 | +def _run_gh(argv: list[str]) -> int: |
| 39 | + return subprocess.run(argv, check=False).returncode |
| 40 | + |
| 41 | + |
| 42 | +def publish( |
| 43 | + *, |
| 44 | + version: str, |
| 45 | + public_image: str, |
| 46 | + anchor_dir: pathlib.Path, |
| 47 | + tag: str, |
| 48 | + repo: str, |
| 49 | + out_dir: pathlib.Path, |
| 50 | + run_gh: Callable[[list[str]], int] = _run_gh, |
| 51 | +) -> int: |
| 52 | + wheel = ( |
| 53 | + anchor_dir |
| 54 | + / f"bigquery_agent_analytics_tracing-{version}-py3-none-any.whl" |
| 55 | + ) |
| 56 | + if not wheel.is_file(): |
| 57 | + raise FileNotFoundError(f"anchor wheel not found: {wheel}") |
| 58 | + reference = release_image_tool.extract_from_wheel(wheel) |
| 59 | + digest = reference.split("@", 1)[1] |
| 60 | + body_path = out_dir / "release_body.md" |
| 61 | + body_path.write_text( |
| 62 | + render_release_notes.render( |
| 63 | + version=version, digest=digest, public_image=public_image |
| 64 | + ) |
| 65 | + ) |
| 66 | + return run_gh( |
| 67 | + [ |
| 68 | + "gh", |
| 69 | + "release", |
| 70 | + "edit", |
| 71 | + tag, |
| 72 | + "--repo", |
| 73 | + repo, |
| 74 | + "--notes-file", |
| 75 | + str(body_path), |
| 76 | + "--draft=false", |
| 77 | + "--latest=false", |
| 78 | + ] |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def main(argv: list[str] | None = None) -> int: |
| 83 | + parser = argparse.ArgumentParser(description=__doc__) |
| 84 | + parser.add_argument("--version", required=True) |
| 85 | + parser.add_argument("--public-image", required=True) |
| 86 | + parser.add_argument("--anchor-dir", type=pathlib.Path, required=True) |
| 87 | + parser.add_argument("--tag", required=True) |
| 88 | + parser.add_argument("--repo", required=True) |
| 89 | + parser.add_argument("--out-dir", type=pathlib.Path, default=pathlib.Path(".")) |
| 90 | + args = parser.parse_args(argv) |
| 91 | + return publish( |
| 92 | + version=args.version, |
| 93 | + public_image=args.public_image, |
| 94 | + anchor_dir=args.anchor_dir, |
| 95 | + tag=args.tag, |
| 96 | + repo=args.repo, |
| 97 | + out_dir=args.out_dir, |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + raise SystemExit(main()) |
0 commit comments