|
26 | 26 | HOMEBREW_TAP_REPO: smorinlabs/homebrew-tap |
27 | 27 | TAP_REPO_DIR: ${{ github.workspace }}/homebrew-tap |
28 | 28 | HOMEBREW_SOURCE_JSON: ${{ github.workspace }}/.homebrew/source.json |
| 29 | + HOMEBREW_TOKEN_DOC_SECTION: "RELEASING.md -> Reset HOMEBREW_TAP_GITHUB_TOKEN (step-by-step)" |
29 | 30 | steps: |
30 | 31 | - name: Resolve tag |
31 | 32 | id: tag |
|
64 | 65 | TAG="${{ steps.tag.outputs.tag }}" \ |
65 | 66 | HOMEBREW_SOURCE_JSON="$HOMEBREW_SOURCE_JSON" |
66 | 67 |
|
| 68 | + - name: Validate tap token permissions |
| 69 | + shell: bash |
| 70 | + env: |
| 71 | + HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }} |
| 72 | + run: | |
| 73 | + if [[ -z "${HOMEBREW_TAP_GITHUB_TOKEN}" ]]; then |
| 74 | + echo "::error::Missing secret HOMEBREW_TAP_GITHUB_TOKEN." |
| 75 | + echo "::error::Fix by following: ${HOMEBREW_TOKEN_DOC_SECTION}" |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | +
|
| 79 | + API_URL="https://api.github.com/repos/${HOMEBREW_TAP_REPO}" |
| 80 | + HTTP_CODE="$(curl -sS -o /tmp/homebrew-tap-repo.json -w "%{http_code}" \ |
| 81 | + -H "Authorization: Bearer ${HOMEBREW_TAP_GITHUB_TOKEN}" \ |
| 82 | + -H "Accept: application/vnd.github+json" \ |
| 83 | + "${API_URL}")" |
| 84 | +
|
| 85 | + python3 - "${HTTP_CODE}" <<'PY' |
| 86 | + import json |
| 87 | + import os |
| 88 | + import sys |
| 89 | +
|
| 90 | + code = int(sys.argv[1]) |
| 91 | + repo = os.environ["HOMEBREW_TAP_REPO"] |
| 92 | + doc = os.environ["HOMEBREW_TOKEN_DOC_SECTION"] |
| 93 | + payload_path = "/tmp/homebrew-tap-repo.json" |
| 94 | +
|
| 95 | + payload = {} |
| 96 | + try: |
| 97 | + with open(payload_path, "r", encoding="utf-8") as fh: |
| 98 | + payload = json.load(fh) |
| 99 | + except Exception: |
| 100 | + payload = {} |
| 101 | +
|
| 102 | + def emit_error(message: str) -> None: |
| 103 | + print(f"::error::{message}") |
| 104 | +
|
| 105 | + if code != 200: |
| 106 | + emit_error( |
| 107 | + f"HOMEBREW_TAP_GITHUB_TOKEN cannot access {repo} (GitHub API HTTP {code})." |
| 108 | + ) |
| 109 | + emit_error( |
| 110 | + "Expected fine-grained token scope: repo access to smorinlabs/homebrew-tap with " |
| 111 | + "Contents: Read and write and Pull requests: Read and write." |
| 112 | + ) |
| 113 | + emit_error(f"Fix by following: {doc}") |
| 114 | + gh_message = payload.get("message") |
| 115 | + if gh_message: |
| 116 | + emit_error(f"GitHub response: {gh_message}") |
| 117 | + raise SystemExit(1) |
| 118 | +
|
| 119 | + permissions = payload.get("permissions") or {} |
| 120 | + if permissions and not permissions.get("push", False): |
| 121 | + emit_error( |
| 122 | + f"HOMEBREW_TAP_GITHUB_TOKEN can read {repo} but cannot push " |
| 123 | + "(permissions.push=false)." |
| 124 | + ) |
| 125 | + emit_error( |
| 126 | + "Expected fine-grained token scope: Contents: Read and write and " |
| 127 | + "Pull requests: Read and write." |
| 128 | + ) |
| 129 | + emit_error(f"Fix by following: {doc}") |
| 130 | + raise SystemExit(1) |
| 131 | +
|
| 132 | + print(f"Token validation passed for {repo}.") |
| 133 | + PY |
| 134 | +
|
67 | 135 | - name: Checkout tap repository |
68 | 136 | uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 |
69 | 137 | with: |
@@ -95,7 +163,16 @@ jobs: |
95 | 163 | env: |
96 | 164 | GH_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }} |
97 | 165 | run: | |
| 166 | + set +e |
98 | 167 | make homebrew-open-tap-pr \ |
99 | 168 | TAG="${{ steps.tag.outputs.tag }}" \ |
100 | 169 | TAP_REPO_DIR="$TAP_REPO_DIR" \ |
101 | 170 | HOMEBREW_TAP_REPO="$HOMEBREW_TAP_REPO" |
| 171 | + RC="$?" |
| 172 | + set -e |
| 173 | +
|
| 174 | + if [[ "$RC" -ne 0 ]]; then |
| 175 | + echo "::error::Failed to open/update tap PR. This can happen when HOMEBREW_TAP_GITHUB_TOKEN is expired or missing repo permissions." |
| 176 | + echo "::error::Fix by following: ${HOMEBREW_TOKEN_DOC_SECTION}" |
| 177 | + exit "$RC" |
| 178 | + fi |
0 commit comments