Files
kevinveenbirkenbachandClaude Opus 5.5 306382e03d feat(ci): merge update PRs automatically once CI deployed every changed role
entry-pr-green-update-merge.yml runs on a successful pull_request run of the PR pipeline for update/* and dependabot/* branches. cli.contributing.update.merge merges only when the updater App or Dependabot opened the PR from its own branch against the default branch, it carries the update label, every check run and status on its head is green, and the role-runtimes artifacts of that run list every role the PR changes. The gate reads with the read-only GITHUB_TOKEN; the App token is minted only after it passes and merges through the REST API pinned to the tested head SHA. entry-pr-change-dependabot-label.yml gives Dependabot PRs the same update plus level labels, and dependabot.yml adds the update label to every active ecosystem. Both workflows stay off unless CI_ENABLE_AUTO_UPDATES is true.

🎯 Fix: 82 %
💥 Break: 3 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 80
🚦 Gate: ⛔ fix 82 % < 90

MEDUSA accepted false positives: RH005 cli/contributing/update/merge/__main__.py (standard CLI entry), WEB-PATH-002 cli/contributing/update/merge/github.py (zip read in memory with size caps, never extracted), B603 cli/contributing/update/merge/github.py (list-form gh subprocess, no shell).

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 09:12:56 +00:00

138 lines
4.3 KiB
Python

"""Read what the merge gate judges from the GitHub REST API through ``gh``."""
from __future__ import annotations
import io
import json
import subprocess
import zipfile
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from collections.abc import Callable, Sequence
Gh = Callable[[Sequence[str]], bytes]
GH_TIMEOUT_SECONDS = 120
ROLE_RUNTIMES_PREFIX = "role-runtimes-"
MAX_ARTIFACT_BYTES = 16 * 1024 * 1024
MAX_CSV_BYTES = 4 * 1024 * 1024
def run_gh(args: Sequence[str]) -> bytes:
return subprocess.run(
["gh", *args],
capture_output=True,
check=True,
timeout=GH_TIMEOUT_SECONDS,
).stdout
def items(gh: Gh, path: str, jq: str) -> list[dict[str, Any]]:
"""Every object a paginated REST list yields through *jq*.
Args:
gh: runs ``gh`` with the given arguments and returns its stdout.
path: the REST path, query string included.
jq: a filter emitting one JSON object per item.
Returns:
the objects, across all pages.
"""
out = gh(["api", "--paginate", path, "--jq", jq]).decode("utf-8")
return [json.loads(line) for line in out.splitlines() if line.strip()]
def open_pull_requests(gh: Gh, repo: str, sha: str) -> list[dict[str, Any]]:
"""The open pull requests whose head commit is *sha*."""
return [
pr
for pr in items(gh, f"repos/{repo}/commits/{sha}/pulls?per_page=100", ".[]")
if pr["state"] == "open" and pr["head"]["sha"] == sha
]
def check_runs(gh: Gh, repo: str, sha: str) -> list[dict[str, Any]]:
"""The latest check run per name on *sha*."""
return items(
gh,
f"repos/{repo}/commits/{sha}/check-runs?filter=latest&per_page=100",
".check_runs[] | {name, status, conclusion, details_url}",
)
def statuses(gh: Gh, repo: str, sha: str) -> list[dict[str, Any]]:
"""The latest commit status per context on *sha*."""
return items(
gh,
f"repos/{repo}/commits/{sha}/status?per_page=100",
".statuses[] | {context, state}",
)
def changed_paths(gh: Gh, repo: str, number: int) -> list[str]:
"""Every path pull request *number* touches, old names of renamed files included."""
paths: list[str] = []
for entry in items(
gh,
f"repos/{repo}/pulls/{number}/files?per_page=100",
".[] | {filename, previous_filename}",
):
paths.append(entry["filename"])
if entry["previous_filename"]:
paths.append(entry["previous_filename"])
return paths
def csv_members(blob: bytes) -> list[str]:
"""The CSV files inside an artifact zip, read in memory.
The zip is never extracted to disk, so its member names cannot reach a
path, and a member larger than ``MAX_CSV_BYTES`` is left out.
Args:
blob: the artifact zip as the REST API serves it.
Returns:
the text of each ``*.csv`` member.
"""
texts: list[str] = []
with zipfile.ZipFile(io.BytesIO(blob)) as archive:
for member in archive.infolist():
if member.is_dir() or not member.filename.endswith(".csv"):
continue
if member.file_size > MAX_CSV_BYTES:
continue
with archive.open(member) as handle:
data = handle.read(MAX_CSV_BYTES + 1)
if len(data) <= MAX_CSV_BYTES:
texts.append(data.decode("utf-8", errors="replace"))
return texts
def role_runtime_csvs(gh: Gh, repo: str, run_id: int) -> list[str]:
"""The ``role-runtimes-*`` CSVs workflow run *run_id* uploaded.
Args:
gh: runs ``gh`` with the given arguments and returns its stdout.
repo: ``owner/name``.
run_id: the pull request run whose deploy rows uploaded them.
Returns:
the text of every CSV in every unexpired ``role-runtimes-*`` artifact
of at most ``MAX_ARTIFACT_BYTES``.
"""
texts: list[str] = []
for artifact in items(
gh,
f"repos/{repo}/actions/runs/{run_id}/artifacts?per_page=100",
".artifacts[] | {id, name, expired, size_in_bytes}",
):
if not artifact["name"].startswith(ROLE_RUNTIMES_PREFIX):
continue
if artifact["expired"] or artifact["size_in_bytes"] > MAX_ARTIFACT_BYTES:
continue
blob = gh(["api", f"repos/{repo}/actions/artifacts/{artifact['id']}/zip"])
texts.extend(csv_members(blob))
return texts