| 1 | name: Live test |
| 2 | |
| 3 | # End-to-end checks against the real diffinery.com after each deploy: email |
| 4 | # sending, web UI features (syntax highlighting, settings pages, view transitions, milestones, variables), hosted Actions jobs in Cloudflare Containers (with Docker builds and services), the container registry, agent sandboxes, |
| 5 | # webhooks, scheduled workflows, Git LFS, imports from GitHub, that tokens can't mint tokens, and pull requests (test merge commits, pull_request_target and the merge |
| 6 | # queue). Each run works in a throwaway private repository that it |
| 7 | # deletes at the end. |
| 8 | # |
| 9 | # Needs the repository secret DIFFINERY_LIVE_TOKEN: a Diffinery token (Settings -> |
| 10 | # Tokens) with the admin scope and access to all repositories. |
| 11 | on: |
| 12 | workflow_dispatch: |
| 13 | workflow_run: |
| 14 | workflows: [Deploy] |
| 15 | types: [completed] |
| 16 | branches: [main] |
| 17 | |
| 18 | permissions: |
| 19 | contents: read |
| 20 | |
| 21 | concurrency: |
| 22 | group: live-test |
| 23 | cancel-in-progress: false |
| 24 | |
| 25 | jobs: |
| 26 | live: |
| 27 | name: Containers, registry, sandboxes, webhooks, schedules, LFS and pull requests |
| 28 | # Runs on the host that deploys production (see the gate in deploy.yml). |
| 29 | if: ${{ github.event_name == 'workflow_dispatch' || (github.server_url == 'https://diffinery.com' && github.event.workflow_run.conclusion == 'success') }} |
| 30 | runs-on: ubuntu-latest |
| 31 | timeout-minutes: 45 |
| 32 | env: |
| 33 | DIFFINERY: https://diffinery.com |
| 34 | TOKEN: ${{ secrets.DIFFINERY_LIVE_TOKEN }} |
| 35 | REPO: live-${{ github.run_id }}-${{ github.run_attempt }} |
| 36 | HEAD_SHA: ${{ github.event.workflow_run.head_sha || github.sha }} |
| 37 | steps: |
| 38 | - name: Check the token |
| 39 | id: token |
| 40 | run: | |
| 41 | if [ -z "$TOKEN" ]; then |
| 42 | echo "::warning::DIFFINERY_LIVE_TOKEN is not set, so the live test is skipped. Create an admin token at $DIFFINERY/settings/tokens and save it as a repository secret." |
| 43 | echo "skip=true" >> "$GITHUB_OUTPUT" |
| 44 | exit 0 |
| 45 | fi |
| 46 | login=$(curl -fsS "$DIFFINERY/api/v3/user" -H "Authorization: Bearer $TOKEN" | jq -r .login) |
| 47 | echo "OWNER=$login" >> "$GITHUB_ENV" |
| 48 | echo "testing as $login in $login/$REPO" |
| 49 | |
| 50 | - name: Choose the registry check's image sizes |
| 51 | if: steps.token.outputs.skip != 'true' |
| 52 | run: | |
| 53 | set -euo pipefail |
| 54 | # Layers over Cloudflare's 100 MB request limit (which hosted runners push through |
| 55 | # the registry relay) take minutes to push and pull. They're pushed on manual runs and |
| 56 | # whenever the registry, the runner image or this test changed since the last |
| 57 | # successful live test; other deploys push small images through the same path. |
| 58 | full=true |
| 59 | if [ "$GITHUB_EVENT_NAME" = workflow_run ]; then |
| 60 | api() { curl -fsS "$DIFFINERY/api/v3/repos/$GITHUB_REPOSITORY$1" -H "Authorization: Bearer $TOKEN"; } |
| 61 | last=$(api "/actions/runs?workflow=live-test.yml&status=success&per_page=1" | jq -r '.workflow_runs[0].head_sha // empty' || true) |
| 62 | if [ -n "$last" ] && files=$(api "/compare/$last...$HEAD_SHA" | jq -r '.files[].filename'); then |
| 63 | grep -qE '^(apps/diffinery/src/registry/|containers/runner/|\.github/workflows/live-test\.yml$)' <<<"$files" || full=false |
| 64 | echo "changed since $last: $(wc -l <<<"$files") files" |
| 65 | fi |
| 66 | fi |
| 67 | if [ "$full" = true ]; then |
| 68 | echo "BIG=150000000" >> "$GITHUB_ENV" |
| 69 | echo "BUILDX=120000000" >> "$GITHUB_ENV" |
| 70 | else |
| 71 | echo "BIG=5000000" >> "$GITHUB_ENV" |
| 72 | echo "BUILDX=4000000" >> "$GITHUB_ENV" |
| 73 | fi |
| 74 | echo "registry check: full=$full" |
| 75 | |
| 76 | - name: Profile picture upload (WASM resize in the Worker) |
| 77 | if: steps.token.outputs.skip != 'true' |
| 78 | run: | |
| 79 | set -euo pipefail |
| 80 | av="$DIFFINERY/api/avatars/$OWNER" |
| 81 | # Keep the account's current picture, if it has one, to put it back afterwards. |
| 82 | had=$(curl -fsS -o before.webp -w '%{content_type}' "$av?s=256&v=1") |
| 83 | restore() { |
| 84 | if [ "$had" = image/webp ]; then |
| 85 | curl -fsS -X PUT "$DIFFINERY/api/v3/user/avatar" -H "Authorization: Bearer $TOKEN" -H "content-type: image/webp" --data-binary @before.webp | jq -r '"restored the previous picture: \(.avatar_url)"' |
| 86 | else |
| 87 | curl -fsS -o /dev/null -X DELETE "$DIFFINERY/api/v3/user/avatar" -H "Authorization: Bearer $TOKEN" |
| 88 | fi |
| 89 | } |
| 90 | trap restore EXIT |
| 91 | # A 600×400 PNG gradient; the server crops it to a square and makes small WebPs. |
| 92 | python3 - <<'EOF' |
| 93 | import struct, zlib |
| 94 | w, h = 600, 400 |
| 95 | rows = b"".join(b"\0" + bytes(c for x in range(w) for c in (x * 255 // w, y * 255 // h, 128)) for y in range(h)) |
| 96 | chunk = lambda t, d: struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d)) |
| 97 | open("avatar.png", "wb").write(b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(rows)) + chunk(b"IEND", b"")) |
| 98 | EOF |
| 99 | url=$(curl -fsS -X PUT "$DIFFINERY/api/v3/user/avatar" -H "Authorization: Bearer $TOKEN" -H "content-type: image/png" --data-binary @avatar.png | jq -r .avatar_url) |
| 100 | echo "uploaded: $url" |
| 101 | v=${url##*v=} |
| 102 | type=$(curl -fsS -o small.webp -w '%{content_type}' "$av?s=64&v=$v") |
| 103 | [ "$type" = image/webp ] || { echo "::error::expected image/webp, got $type"; exit 1; } |
| 104 | python3 - <<'EOF' |
| 105 | import struct, sys |
| 106 | b = open("small.webp", "rb").read() |
| 107 | ok = b[:4] == b"RIFF" and b[8:12] == b"WEBP" |
| 108 | if b[12:16] == b"VP8 ": |
| 109 | size = (struct.unpack("<H", b[26:28])[0] & 0x3FFF, struct.unpack("<H", b[28:30])[0] & 0x3FFF) |
| 110 | elif b[12:16] == b"VP8X": |
| 111 | size = (int.from_bytes(b[24:27], "little") + 1, int.from_bytes(b[27:30], "little") + 1) |
| 112 | else: |
| 113 | size = None |
| 114 | print(f"64 px avatar: {len(b)} bytes, {size}") |
| 115 | if not ok or size != (64, 64) or len(b) > 5000: |
| 116 | print("::error::the 64 px avatar is not a 64x64 WebP under 5 KB"); sys.exit(1) |
| 117 | EOF |
| 118 | curl -fsS -X DELETE "$DIFFINERY/api/v3/user/avatar" -H "Authorization: Bearer $TOKEN" >/dev/null |
| 119 | # A `v` that is not current bypasses the edge cache, so this sees the removal from any colo. |
| 120 | type=$(curl -fsS -o /dev/null -w '%{content_type}' "$av?s=64&v=1") |
| 121 | [[ "$type" == image/svg+xml* ]] || { echo "::error::after removal expected the initials SVG, got $type"; exit 1; } |
| 122 | echo "removed: initials again ($type)" |
| 123 | |
| 124 | - name: Public docs are served without signing in |
| 125 | run: | |
| 126 | set -euo pipefail |
| 127 | check() { # path, content type, text it must contain |
| 128 | body=$(curl -fsS -D /tmp/h "$DIFFINERY$1") |
| 129 | grep -qi "content-type: $2" /tmp/h || { echo "::error::$1 is not $2"; exit 1; } |
| 130 | grep -qF -- "$3" <<<"$body" || { echo "::error::$1 doesn't contain: $3"; exit 1; } |
| 131 | echo "$1 ok ($(wc -c <<<"$body") bytes)" |
| 132 | } |
| 133 | check /docs "text/html" "Overview" |
| 134 | check /docs/actions/workflow-syntax "text/html" "View as Markdown" |
| 135 | check /docs/quickstart.md "text/markdown" "# Quickstart" |
| 136 | check /llms.txt "text/plain" "/docs/actions/workflow-syntax.md" |
| 137 | check /llms-full.txt "text/plain" "# REST API reference" |
| 138 | check /docs/api/openapi.json "application/json" '"openapi": "3.1.0"' |
| 139 | check /-/cli/diffinery.mjs "text/javascript" "#!/usr/bin/env node" |
| 140 | md=$(curl -fsS -H "Accept: text/markdown" "$DIFFINERY/docs/agents/mcp") |
| 141 | grep -q "^# MCP" <<<"$md" || { echo "::error::Accept: text/markdown didn't return Markdown"; exit 1; } |
| 142 | curl -fsS "$DIFFINERY/-/cli/diffinery.mjs" -o /tmp/diffinery.mjs && node /tmp/diffinery.mjs --version |
| 143 | |
| 144 | - name: Email sending is configured |
| 145 | if: steps.token.outputs.skip != 'true' |
| 146 | run: | |
| 147 | set -euo pipefail |
| 148 | s=$(curl -fsS "$DIFFINERY/api/v3/diffinery/admin/email" -H "Authorization: Bearer $TOKEN") |
| 149 | echo "$s" | jq '{sending, queue, from, last_24h}' |
| 150 | [ "$(jq -r .sending <<<"$s")" = true ] || { echo "::error::the Worker has no Email Sending binding"; exit 1; } |
| 151 | [ "$(jq -r .queue <<<"$s")" = true ] || { echo "::error::the Worker has no mail queue binding"; exit 1; } |
| 152 | failed=$(jq -r .last_24h.failed <<<"$s") |
| 153 | [ "$failed" = 0 ] || echo "::warning::$failed emails failed in the last 24 hours; see Settings -> Email in site administration" |
| 154 | |
| 155 | - name: Create a throwaway repository with a webhook |
| 156 | if: steps.token.outputs.skip != 'true' |
| 157 | run: | |
| 158 | set -euo pipefail |
| 159 | api() { curl -fsS -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; } |
| 160 | api POST /user/repos "{\"name\":\"$REPO\",\"private\":true}" >/dev/null |
| 161 | # Webhooks may only reach public hosts other than Diffinery itself; the push's delivery goes to example.com. |
| 162 | code=$(curl -sS -o /dev/null -w '%{http_code}' -X POST "$DIFFINERY/api/v3/repos/$OWNER/$REPO/hooks" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data "{\"config\":{\"url\":\"$DIFFINERY/healthz\"},\"events\":[\"push\"]}") |
| 163 | [ "$code" = 422 ] || { echo "::error::a webhook to Diffinery itself answered $code, not 422"; exit 1; } |
| 164 | api POST "/repos/$OWNER/$REPO/hooks" "{\"config\":{\"url\":\"https://example.com/diffinery-live-test\"},\"events\":[\"push\"]}" >/dev/null |
| 165 | echo "created $OWNER/$REPO" |
| 166 | |
| 167 | - name: Wait for jobs to start on the deployed runner image |
| 168 | if: steps.token.outputs.skip != 'true' |
| 169 | run: | |
| 170 | set -euo pipefail |
| 171 | # A deploy's container rollout updates instances over a while after it starts, and an |
| 172 | # instance not yet updated starts jobs on the previous runner image. Jobs report their |
| 173 | # image (`runner.image`), so a probe workflow runs until three jobs in a row land on the |
| 174 | # image the deploy set (/meta's runner_image) before the Docker and registry checks. |
| 175 | api() { curl -fsS -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; } |
| 176 | expected=$(api GET /meta | jq -r '.runner_image // empty') |
| 177 | if [ -z "$expected" ]; then |
| 178 | echo "::warning::/meta doesn't name the deployed runner image, so jobs aren't checked against it" |
| 179 | exit 0 |
| 180 | fi |
| 181 | echo "DEPLOYED_RUNNER_IMAGE=$expected" >> "$GITHUB_ENV" |
| 182 | mkdir -p src/.github/workflows && cd src |
| 183 | git init -q -b main |
| 184 | git config user.name "Live test" && git config user.email "live-test@diffinery.com" |
| 185 | cat > .github/workflows/probe.yml <<'EOF' |
| 186 | on: workflow_dispatch |
| 187 | jobs: |
| 188 | probe: |
| 189 | runs-on: ubuntu-latest |
| 190 | strategy: |
| 191 | matrix: |
| 192 | n: [1, 2, 3] |
| 193 | steps: |
| 194 | - run: echo "probe ok" |
| 195 | EOF |
| 196 | git add -A && git commit -q -m "Runner image probe" |
| 197 | git push -q "https://x-token:${TOKEN}@${DIFFINERY#https://}/$OWNER/$REPO.git" main |
| 198 | deadline=$(( $(date +%s) + 15 * 60 )) |
| 199 | while :; do |
| 200 | before=$(api GET "/repos/$OWNER/$REPO/actions/runs?event=workflow_dispatch" | jq '.workflow_runs | length') |
| 201 | api POST "/repos/$OWNER/$REPO/actions/workflows/probe.yml/dispatches" '{"ref":"main"}' >/dev/null |
| 202 | run="" |
| 203 | # Up to 10 minutes: a job turned away from a replaced image starts again after 30 s, 1, 2 and 4 minutes. |
| 204 | for i in $(seq 1 120); do |
| 205 | runs=$(api GET "/repos/$OWNER/$REPO/actions/runs?event=workflow_dispatch") |
| 206 | if [ "$(jq '.workflow_runs | length' <<<"$runs")" -gt "$before" ]; then |
| 207 | run=$(jq -c '.workflow_runs[0]' <<<"$runs") |
| 208 | [ "$(jq -r .status <<<"$run")" = completed ] && break |
| 209 | fi |
| 210 | sleep 5 |
| 211 | done |
| 212 | [ -n "$run" ] || { echo "::error::the probe workflow didn't start"; exit 1; } |
| 213 | probe=$(api GET "/repos/$OWNER/$REPO/actions/runs/$(jq -r .id <<<"$run")/jobs") |
| 214 | images=$(jq -r '.jobs[] | .runner.image // "no image reported"' <<<"$probe") |
| 215 | echo "probe jobs ran on: $(sort <<<"$images" | uniq -c | sed 's/^ *//' | paste -sd ','), started again $(jq '[.jobs[].placementTries // 0] | add // 0' <<<"$probe") time(s) in all" |
| 216 | [ "$(grep -cxF "$expected" <<<"$images" || true)" = 3 ] && { echo "jobs start on $expected"; exit 0; } |
| 217 | if [ "$(date +%s)" -ge "$deadline" ]; then |
| 218 | echo "::error::15 minutes after the deploy, jobs still start on a runner image other than the deployed $expected" |
| 219 | exit 1 |
| 220 | fi |
| 221 | sleep 20 |
| 222 | done |
| 223 | |
| 224 | - name: Push workflows and an LFS file |
| 225 | id: push |
| 226 | if: steps.token.outputs.skip != 'true' |
| 227 | run: | |
| 228 | set -euo pipefail |
| 229 | mkdir -p src/.github/workflows && cd src |
| 230 | git init -q -b main |
| 231 | git config user.name "Live test" && git config user.email "live-test@diffinery.com" |
| 232 | git lfs install --local >/dev/null |
| 233 | git lfs track "*.bin" >/dev/null |
| 234 | head -c 3145728 /dev/urandom > blob.bin |
| 235 | sha256sum blob.bin | cut -d' ' -f1 > ../blob.sha |
| 236 | echo "# $REPO" > README.md |
| 237 | cat > .github/workflows/ci.yml <<'EOF' |
| 238 | on: push |
| 239 | jobs: |
| 240 | hosted: |
| 241 | runs-on: ubuntu-latest |
| 242 | strategy: |
| 243 | matrix: |
| 244 | n: [1, 2] |
| 245 | steps: |
| 246 | - uses: actions/checkout@v4 |
| 247 | - run: echo "live ok ${{ '${{' }} matrix.n }} $(cat README.md)" |
| 248 | docker: |
| 249 | runs-on: ubuntu-latest |
| 250 | services: |
| 251 | cache: |
| 252 | image: redis:7-alpine |
| 253 | ports: ["6379"] |
| 254 | options: --health-cmd "redis-cli ping" --health-interval 1s --health-retries 30 |
| 255 | steps: |
| 256 | - run: | |
| 257 | printf 'FROM alpine:3.20\nRUN echo built > /msg\nCMD ["cat", "/msg"]\n' > Dockerfile |
| 258 | docker build -q -t live-image . |
| 259 | echo "docker ok $(docker run --rm live-image)" |
| 260 | exec 3<>/dev/tcp/127.0.0.1/${{ '${{' }} job.services.cache.ports['6379'] }} |
| 261 | printf 'PING\r\n' >&3 |
| 262 | read -r line <&3 |
| 263 | echo "service ok ${line%$'\r'}" |
| 264 | # Container registry: a layer pushed with the job's GITHUB_TOKEN (over Cloudflare's |
| 265 | # 100 MB request limit on full runs), plus the docker/*-action route most ghcr.io |
| 266 | # workflows use. __BIG__ and __BUILDX__ are the layer sizes. |
| 267 | publish: |
| 268 | runs-on: ubuntu-latest |
| 269 | permissions: |
| 270 | packages: write |
| 271 | steps: |
| 272 | - run: | |
| 273 | image="diffinery.com/${GITHUB_REPOSITORY,,}" |
| 274 | echo "${{ '${{' }} secrets.GITHUB_TOKEN }}" | docker login diffinery.com -u "${{ '${{' }} github.actor }}" --password-stdin |
| 275 | head -c __BIG__ /dev/urandom > big.bin |
| 276 | sha256sum big.bin | cut -d' ' -f1 > big.sha |
| 277 | printf 'FROM scratch\nCOPY big.bin big.sha /\n' > Dockerfile |
| 278 | docker build -q -t "$image:live" . |
| 279 | docker push "$image:live" |
| 280 | echo "pushed $(cat big.sha)" |
| 281 | buildx: |
| 282 | runs-on: ubuntu-latest |
| 283 | permissions: |
| 284 | packages: write |
| 285 | steps: |
| 286 | - run: | |
| 287 | echo "IMAGE=diffinery.com/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV" |
| 288 | head -c __BUILDX__ /dev/urandom > big.bin |
| 289 | printf 'FROM scratch\nCOPY big.bin /\n' > Dockerfile |
| 290 | - uses: docker/setup-buildx-action@v3 |
| 291 | - uses: docker/login-action@v3 |
| 292 | with: |
| 293 | registry: diffinery.com |
| 294 | username: ${{ '${{' }} github.actor }} |
| 295 | password: ${{ '${{' }} secrets.GITHUB_TOKEN }} |
| 296 | - uses: docker/build-push-action@v6 |
| 297 | with: |
| 298 | context: . |
| 299 | push: true |
| 300 | tags: ${{ '${{' }} env.IMAGE }}:buildx |
| 301 | pull: |
| 302 | needs: [publish, buildx] |
| 303 | runs-on: ubuntu-latest |
| 304 | permissions: |
| 305 | packages: read |
| 306 | steps: |
| 307 | - run: | |
| 308 | image="diffinery.com/${GITHUB_REPOSITORY,,}" |
| 309 | echo "${{ '${{' }} secrets.GITHUB_TOKEN }}" | docker login diffinery.com -u "${{ '${{' }} github.actor }}" --password-stdin |
| 310 | docker pull -q "$image:live" |
| 311 | id=$(docker create "$image:live" true) |
| 312 | docker cp "$id:/big.bin" big.bin && docker cp "$id:/big.sha" big.sha |
| 313 | [ "$(sha256sum big.bin | cut -d' ' -f1)" = "$(cat big.sha)" ] && echo "registry ok $(stat -c %s big.bin) bytes" |
| 314 | docker pull -q "$image:buildx" |
| 315 | id=$(docker create "$image:buildx" true) |
| 316 | docker cp "$id:/big.bin" buildx.bin |
| 317 | echo "buildx ok $(stat -c %s buildx.bin) bytes" |
| 318 | EOF |
| 319 | cat > .github/workflows/tick.yml <<'EOF' |
| 320 | on: |
| 321 | schedule: |
| 322 | - cron: "*/5 * * * *" |
| 323 | jobs: |
| 324 | tick: |
| 325 | runs-on: ubuntu-latest |
| 326 | steps: |
| 327 | - run: echo "scheduled ok" |
| 328 | EOF |
| 329 | sed -i "s/__BIG__/$BIG/g; s/__BUILDX__/$BUILDX/g" .github/workflows/ci.yml |
| 330 | git add -A && git commit -q -m "Live test" |
| 331 | git push -q "https://x-token:${TOKEN}@${DIFFINERY#https://}/$OWNER/$REPO.git" main |
| 332 | git lfs ls-files |
| 333 | |
| 334 | - name: Web UI features (highlighting, settings pages, view transitions, base permission, invitations) |
| 335 | if: steps.token.outputs.skip != 'true' |
| 336 | run: | |
| 337 | set -euo pipefail |
| 338 | page() { curl -fsS "$DIFFINERY$1" -H "Authorization: Bearer $TOKEN"; } |
| 339 | has() { grep -qF -- "$2" <<<"$1" || { echo "::error::$3"; exit 1; }; } |
| 340 | html=$(page "/$OWNER/$REPO/blob/main/.github/workflows/ci.yml") |
| 341 | has "$html" '<td class="sh">' "the workflow file is not syntax highlighted" |
| 342 | has "$html" "--shiki-dark" "highlighted code has no dark theme colours" |
| 343 | has "$html" "@view-transition{navigation:auto}" "pages don't opt in to view transitions" |
| 344 | has "$html" "prefers-reduced-motion:no-preference" "view transitions ignore reduced motion" |
| 345 | has "$(page /settings/appearance)" 'name="appearance"' "Settings -> Appearance has no theme choice" |
| 346 | has "$(page /notifications)" 'href="/settings/notifications"' "the notifications page doesn't link to notification settings" |
| 347 | code=$(curl -sS -o /tmp/out -w '%{http_code}' -X POST "$DIFFINERY/api/v3/user/repos" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data "{\"name\":\"$REPO-internal\",\"visibility\":\"internal\"}") |
| 348 | [ "$code" = 422 ] || { echo "::error::visibility internal got $code, expected 422"; curl -sS -o /dev/null -X DELETE "$DIFFINERY/api/v3/repos/$OWNER/$REPO-internal" -H "Authorization: Bearer $TOKEN"; exit 1; } |
| 349 | org=$(page /api/v3/user/orgs | jq -r '.[0].login // empty') |
| 350 | if [ -n "$org" ]; then |
| 351 | perm=$(page "/api/v3/orgs/$org" | jq -r '.default_repository_permission // empty') |
| 352 | case "$perm" in none|read|write|admin) echo "$org's base permission: $perm" ;; *) echo "::error::GET /orgs/$org has no default_repository_permission"; exit 1 ;; esac |
| 353 | # A malformed rename is refused before anything is written (422 for owners, 403 for everyone else). |
| 354 | code=$(curl -sS -o /tmp/out -w '%{http_code}' -X PATCH "$DIFFINERY/api/v3/orgs/$org" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{"name":123}') |
| 355 | case "$code" in 422|403) ;; *) echo "::error::PATCH /orgs/$org with a numeric name got $code"; cat /tmp/out; exit 1 ;; esac |
| 356 | fi |
| 357 | page "/api/v3/repos/$OWNER/$REPO/invitations" | jq -e 'type == "array"' >/dev/null || { echo "::error::GET /repos/$OWNER/$REPO/invitations doesn't list pending invitations"; exit 1; } |
| 358 | tools=$(curl -fsS -X POST "$DIFFINERY/mcp" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" -H "accept: application/json, text/event-stream" --data '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | jq -r '.result.tools[].name') |
| 359 | for t in get_organization list_invitations revoke_invitation; do grep -qx "$t" <<<"$tools" || { echo "::error::the MCP server has no $t tool"; exit 1; }; done |
| 360 | echo "highlighting, Appearance, notification settings link, view transitions, visibility, organization and invitation checks passed" |
| 361 | |
| 362 | - name: Diffy placeholders, Actions settings, milestones and variables |
| 363 | if: steps.token.outputs.skip != 'true' |
| 364 | run: | |
| 365 | set -euo pipefail |
| 366 | page() { curl -fsS "$DIFFINERY$1" -H "Authorization: Bearer $TOKEN"; } |
| 367 | api() { curl -fsS -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; } |
| 368 | code() { curl -sS -o /tmp/out -w '%{http_code}' -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; } |
| 369 | fail() { echo "::error::$1"; cat /tmp/out 2>/dev/null || true; exit 1; } |
| 370 | has() { grep -qF -- "$2" <<<"$1" || fail "$3"; } |
| 371 | R="/repos/$OWNER/$REPO" |
| 372 | has "$(page /new/import)" 'diffy/hello-world' "the importer's placeholder isn't Diffy" |
| 373 | has "$(page "/$OWNER/$REPO/settings/actions")" 'Allow GitHub Actions to create and approve pull requests' "repository settings have no Actions page" |
| 374 | # Milestones: created closed, checked before an issue edit is saved, shown in the response, and filterable. |
| 375 | [ "$(api POST "$R/milestones" '{"title":"Shipped","state":"closed"}' | jq -r .state)" = closed ] || fail "POST milestones ignored state" |
| 376 | ms=$(api POST "$R/milestones" '{"title":"Live v1"}' | jq -r .number) |
| 377 | n=$(api POST "$R/issues" '{"title":"Planned"}' | jq -r .number) |
| 378 | [ "$(code PATCH "$R/issues/$n" '{"title":"Renamed","milestone":999999}')" = 422 ] || fail "an unknown milestone wasn't refused with 422" |
| 379 | [ "$(api GET "$R/issues/$n" | jq -r .title)" = Planned ] || fail "a refused milestone edit saved the title" |
| 380 | [ "$(api PATCH "$R/issues/$n" "{\"milestone\":$ms}" | jq -r .milestone.number)" = "$ms" ] || fail "PATCH issue doesn't return the new milestone" |
| 381 | [ "$(api GET "$R/issues?milestone=$ms&state=all" | jq -r '[.[].number] | join(",")')" = "$n" ] || fail "issues aren't filtered by milestone" |
| 382 | loc=$(curl -sS -o /dev/null -w '%{redirect_url}' "$DIFFINERY/$OWNER/$REPO/milestone/$ms" -H "Authorization: Bearer $TOKEN") |
| 383 | has "$loc" "/issues?milestone=$ms" "the milestone link doesn't open its issue list" |
| 384 | # Repository variable deletes ignore letter case. |
| 385 | [ "$(code PUT "$R/actions/variables/LIVE_CASE" '{"value":"x"}')" = 204 ] || fail "couldn't set a repository variable" |
| 386 | [ "$(code DELETE "$R/actions/variables/live_case")" = 204 ] || fail "couldn't delete a repository variable" |
| 387 | [ "$(code GET "$R/actions/variables/LIVE_CASE")" = 404 ] || fail "deleting a variable in lower case left it in place" |
| 388 | org=$(page /api/v3/user/orgs | jq -r '.[0].login // empty') |
| 389 | if [ -n "$org" ]; then |
| 390 | # Deleting an organization variable or secret that doesn't exist is a 404 (403 for someone who isn't an admin). |
| 391 | for path in variables secrets; do |
| 392 | c=$(code DELETE "/orgs/$org/actions/$path/LIVE_NEVER_SET_$GITHUB_RUN_ID") |
| 393 | case "$c" in 404|403) ;; *) fail "DELETE of a missing organization $path entry got $c" ;; esac |
| 394 | done |
| 395 | fi |
| 396 | echo "Diffy placeholders, Actions settings page, milestone and variable checks passed" |
| 397 | |
| 398 | - name: Hosted Actions jobs (and Docker) run in Cloudflare Containers |
| 399 | if: steps.token.outputs.skip != 'true' |
| 400 | run: | |
| 401 | set -euo pipefail |
| 402 | api() { curl -fsS "$DIFFINERY/api/v3$1" -H "Authorization: Bearer $TOKEN"; } |
| 403 | for i in $(seq 1 90); do |
| 404 | run=$(api "/repos/$OWNER/$REPO/actions/runs?event=push" | jq -c '.workflow_runs[0] // empty') |
| 405 | [ -n "$run" ] && [ "$(jq -r .status <<<"$run")" = completed ] && break |
| 406 | sleep 10 |
| 407 | done |
| 408 | echo "$run" | jq '{id, status, conclusion}' |
| 409 | id=$(jq -r .id <<<"$run") |
| 410 | jobs=$(api "/repos/$OWNER/$REPO/actions/runs/$id/jobs") |
| 411 | # placementTries: times a job went back in line (no machine, or a runner on a replaced image turned away). |
| 412 | jq -r '.jobs[] | " \(.name): \(.conclusion)\(if .failure then " - " + (.failure.message // "") else "" end)\(try " (\((.completed_at | fromdateiso8601) - (.started_at | fromdateiso8601))s)" catch "")\(if (.placementTries // 0) > 0 then ", started again \(.placementTries) time(s)" else "" end)"' <<<"$jobs" |
| 413 | if [ "$(jq -r .conclusion <<<"$run")" != success ]; then |
| 414 | for job in $(jq -r '.jobs[] | select(.conclusion == "failure" or .conclusion == "timed_out") | .id' <<<"$jobs"); do |
| 415 | echo "::group::Last log lines of job $job" |
| 416 | api "/repos/$OWNER/$REPO/actions/jobs/$job/logs" | tail -n 80 || true |
| 417 | echo "::endgroup::" |
| 418 | done |
| 419 | echo "::error::the push run did not succeed" |
| 420 | exit 1 |
| 421 | fi |
| 422 | if [ -n "${DEPLOYED_RUNNER_IMAGE:-}" ]; then |
| 423 | # Every job ran the deployed runner, so these checks test what was just deployed. |
| 424 | stale=$(jq -r --arg want "$DEPLOYED_RUNNER_IMAGE" '.jobs[] | select(.runner.image != $want) | "\(.name) on \(.runner.image // "no image reported")"' <<<"$jobs") |
| 425 | [ -z "$stale" ] || { echo "::error::jobs ran on a runner image other than the deployed $DEPLOYED_RUNNER_IMAGE: $(paste -sd ';' <<<"$stale")"; exit 1; } |
| 426 | echo "every job ran on $DEPLOYED_RUNNER_IMAGE" |
| 427 | fi |
| 428 | for job in $(jq -r '.jobs[] | select(.name | startswith("hosted")) | .id' <<<"$jobs"); do |
| 429 | log=$(api "/repos/$OWNER/$REPO/actions/jobs/$job/logs") |
| 430 | grep -F "live ok" <<<"$log" | grep -F "# $REPO" || { echo "::error::job $job did not print its line (checkout or run step failed)"; exit 1; } |
| 431 | done |
| 432 | job=$(jq -r '.jobs[] | select(.name == "docker") | .id' <<<"$jobs") |
| 433 | log=$(api "/repos/$OWNER/$REPO/actions/jobs/$job/logs") |
| 434 | grep -F "docker ok built" <<<"$log" || { echo "::error::docker build or run failed on a hosted runner"; exit 1; } |
| 435 | grep -F "service ok +PONG" <<<"$log" || { echo "::error::the redis service container was not reachable"; exit 1; } |
| 436 | job=$(jq -r '.jobs[] | select(.name == "pull") | .id' <<<"$jobs") |
| 437 | log=$(api "/repos/$OWNER/$REPO/actions/jobs/$job/logs") |
| 438 | grep -F "registry ok $BIG bytes" <<<"$log" || { echo "::error::the image pushed with a $BIG-byte layer did not come back intact from the registry"; exit 1; } |
| 439 | grep -F "buildx ok $BUILDX bytes" <<<"$log" || { echo "::error::the image pushed by docker/build-push-action did not come back from the registry"; exit 1; } |
| 440 | api "/users/$OWNER/packages/container/${REPO,,}/versions" | jq -c '.[] | {tags: .metadata.container.tags, size, platforms}' |
| 441 | |
| 442 | - name: An organization's packages page keeps its tabs |
| 443 | if: steps.token.outputs.skip != 'true' |
| 444 | run: | |
| 445 | set -euo pipefail |
| 446 | org=$(curl -fsS "$DIFFINERY/api/v3/user/orgs" -H "Authorization: Bearer $TOKEN" | jq -r '.[0].login // empty') |
| 447 | [ -n "$org" ] || { echo "$OWNER is in no organization; skipped"; exit 0; } |
| 448 | html=$(curl -fsS "$DIFFINERY/$org/-/packages" -H "Authorization: Bearer $TOKEN") |
| 449 | grep -qF "<a href=\"/$org/-/packages\" class=\"on\">" <<<"$html" || { echo "::error::$org's packages page lost the organization's tab bar"; exit 1; } |
| 450 | grep -qF "href=\"/$org?tab=repositories\"" <<<"$html" || { echo "::error::$org's packages page has no Repositories tab"; exit 1; } |
| 451 | echo "$org's packages page shows the organization's tabs with Packages selected" |
| 452 | |
| 453 | - name: Git LFS round trip |
| 454 | if: steps.token.outputs.skip != 'true' |
| 455 | run: | |
| 456 | set -euo pipefail |
| 457 | git clone -q "https://x-token:${TOKEN}@${DIFFINERY#https://}/$OWNER/$REPO.git" clone |
| 458 | [ "$(sha256sum clone/blob.bin | cut -d' ' -f1)" = "$(cat blob.sha)" ] || { echo "::error::the LFS file came back different"; exit 1; } |
| 459 | echo "LFS file matches ($(stat -c %s clone/blob.bin) bytes)" |
| 460 | |
| 461 | - name: Webhook delivered |
| 462 | if: steps.token.outputs.skip != 'true' |
| 463 | run: | |
| 464 | set -euo pipefail |
| 465 | for i in $(seq 1 30); do |
| 466 | hook=$(curl -fsS "$DIFFINERY/api/v3/repos/$OWNER/$REPO/hooks" -H "Authorization: Bearer $TOKEN" | jq -c '.[0]') |
| 467 | # GitHub's hook shape: the target is config.url, the last delivery's status last_response.code. |
| 468 | [ "$(jq -r '.last_response.code // empty' <<<"$hook")" != "" ] && break |
| 469 | sleep 5 |
| 470 | done |
| 471 | echo "$hook" | jq '{target: .config.url, last_response, updated_at}' |
| 472 | [ "$(jq -r '.last_response.code // empty' <<<"$hook")" != "" ] || { echo "::error::the push webhook was never delivered"; exit 1; } |
| 473 | |
| 474 | - name: Tokens can't mint tokens (API or web forms) |
| 475 | if: steps.token.outputs.skip != 'true' |
| 476 | run: | |
| 477 | set -euo pipefail |
| 478 | # Revokes anything a failed check minted, then fails. |
| 479 | minted() { echo "::error::$1"; curl -fsS "$DIFFINERY/api/v3/diffinery/tokens" -H "Authorization: Bearer $TOKEN" | jq -r '.[] | select(.name == "live-test escalation") | .id' | while read -r id; do curl -sS -o /dev/null -X DELETE "$DIFFINERY/api/v3/diffinery/tokens/$id" -H "Authorization: Bearer $TOKEN"; done; exit 1; } |
| 480 | code=$(curl -sS -o /tmp/out -w '%{http_code}' -X POST "$DIFFINERY/api/v3/diffinery/tokens" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{"name":"live-test escalation","scopes":["read"],"expires_in_days":1}') |
| 481 | [ "$code" = 403 ] || minted "an admin token minted a token through the API ($code)" |
| 482 | grep -q "browser session" /tmp/out || minted "unexpected refusal: $(cat /tmp/out)" |
| 483 | code=$(curl -sS -o /dev/null -w '%{http_code}' -X POST "$DIFFINERY/api/v3/diffinery/agents" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{"login":"live-test-escalation"}') |
| 484 | [ "$code" = 403 ] || { echo "::error::an admin token created an agent ($code); disable live-test-escalation by hand"; exit 1; } |
| 485 | # A web form with a token and a forged double-submit CSRF pair. |
| 486 | code=$(curl -sS -o /dev/null -w '%{http_code}' -X POST "$DIFFINERY/settings/tokens" -H "Authorization: Bearer $TOKEN" -H "cookie: dfy_csrf=forged" --data "_csrf=forged&name=live-test%20escalation&scope=read&days=1") |
| 487 | [ "$code" = 403 ] || minted "a token submitted the web token form ($code)" |
| 488 | echo "tokens can't mint tokens or agents, through the API or the web forms" |
| 489 | |
| 490 | - name: Agent sandbox |
| 491 | if: steps.token.outputs.skip != 'true' |
| 492 | run: | |
| 493 | set -euo pipefail |
| 494 | base="$DIFFINERY/api/v3/repos/$OWNER/$REPO/diffinery/sandboxes" |
| 495 | id=$(curl -fsS --retry 1 --retry-delay 15 -X POST "$base" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{}' | jq -r .id) |
| 496 | echo "sandbox $id" |
| 497 | for i in $(seq 1 40); do |
| 498 | out=$(curl -sS -X POST "$base/$id/exec" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{"cmd":"cat README.md && git lfs ls-files | wc -l"}' || true) |
| 499 | jq -e '.exitCode == 0' >/dev/null 2>&1 <<<"$out" && break |
| 500 | sleep 6 |
| 501 | done |
| 502 | echo "$out" | jq '{exitCode, stdout}' |
| 503 | curl -sS -X POST "$base/$id/close" -H "Authorization: Bearer $TOKEN" >/dev/null || true |
| 504 | jq -e --arg r "# $REPO" '.exitCode == 0 and (.stdout | contains($r))' >/dev/null <<<"$out" || { echo "::error::the sandbox did not run the command in the repository"; exit 1; } |
| 505 | |
| 506 | # Owned by the agentic thread: proves a terminal task's jail (containers/runner/src/jail.ts) on Cloudflare. |
| 507 | - name: A terminal task's jail (bubblewrap, no network but the allowlist proxy) |
| 508 | if: steps.token.outputs.skip != 'true' |
| 509 | run: | |
| 510 | set -euo pipefail |
| 511 | base="$DIFFINERY/api/v3/repos/$OWNER/$REPO/diffinery/sandboxes" |
| 512 | id="" |
| 513 | close() { if [ -n "$id" ]; then curl -sS --max-time 60 -X POST "$base/$id/close" -H "Authorization: Bearer $TOKEN" >/dev/null || true; fi; id=""; } |
| 514 | trap close EXIT |
| 515 | run() { curl -sS --max-time "${2:-240}" -X POST "$base/$id/exec" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data "$(jq -n --arg c "$1" '{cmd: $c, timeoutMs: 180000}')"; } |
| 516 | # One create (no retry: a retried POST could open a second sandbox nothing closes), then wait for it to answer. |
| 517 | open() { |
| 518 | close |
| 519 | id=$(curl -sS --max-time 240 -X POST "$base" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{}' | jq -r '.id // empty' || true) |
| 520 | [ -n "$id" ] || { echo "::error::could not open a sandbox for the jail check"; exit 1; } |
| 521 | for i in $(seq 1 30); do |
| 522 | jq -e '.exitCode == 0' >/dev/null 2>&1 <<<"$(run true 20 || true)" && return 0 |
| 523 | sleep 6 |
| 524 | done |
| 525 | echo "::error::the sandbox never became ready" |
| 526 | exit 1 |
| 527 | } |
| 528 | # Sandboxes roll out on their own: one opened right after a deploy can still be on the previous image. |
| 529 | jail=no |
| 530 | for attempt in 1 2 3 4 5; do |
| 531 | open |
| 532 | has=$(run '[ -n "${DIFFINERY_JAIL:-}" ] && [ -x "$DIFFINERY_JAIL" ] && echo yes || echo no' 20 | jq -r '.stdout // empty' || true) |
| 533 | if [ "$has" = yes ]; then jail=yes; break; fi |
| 534 | echo "sandbox $id is on a runner image without the jail; trying another in a minute" |
| 535 | close |
| 536 | sleep 60 |
| 537 | done |
| 538 | [ "$jail" = yes ] || { echo "::error::the sandbox started on a runner image without the jail (rollout not finished?)"; exit 1; } |
| 539 | inside='echo "direct=$(node -e "require(\"net\").connect(443, \"1.1.1.1\").on(\"connect\", () => (console.log(\"open\"), process.exit())).on(\"error\", (e) => (console.log(e.code), process.exit()))")" |
| 540 | echo "other=$(curl -sS -m 20 -o /dev/null -w "%{http_connect}" https://example.com 2>/dev/null || true)" |
| 541 | echo "checkout=$(ls -A /workspace/repo | paste -sd,)" |
| 542 | echo "credential=$(cat /workspace/repo/.git/config 2>/dev/null || echo hidden)" |
| 543 | v=$(npm view left-pad version --no-update-notifier --loglevel=error 2>/tmp/npm.err) |
| 544 | echo "npm=$(printf "%s\n" "$v" | grep -E "^[0-9]+\.[0-9]+\.[0-9]+$" | tail -1)" |
| 545 | echo "npmerr=$(tail -3 /tmp/npm.err | paste -sd" " -)"' |
| 546 | out=$(run "mkdir -p ../jail-check ../jail-check.out && \"\$DIFFINERY_JAIL\" agent --dir ../jail-check --out ../jail-check.out -- sh -c $(printf '%q' "$inside")") |
| 547 | echo "$out" | jq -r '"exit \(.exitCode)", .stdout, .stderr' |
| 548 | o=$(jq -r .stdout <<<"$out") |
| 549 | fail=0 |
| 550 | jq -e '.exitCode == 0' >/dev/null <<<"$out" || { echo "::error::the jail didn't run (exit $(jq -r .exitCode <<<"$out"))"; fail=1; } |
| 551 | grep -qE '^direct=(ENETUNREACH|EHOSTUNREACH|ECONNREFUSED|EACCES)$' <<<"$o" || { echo "::error::the jail has a route out of its own"; fail=1; } |
| 552 | grep -qx 'other=403' <<<"$o" || { echo "::error::the proxy didn't refuse example.com"; fail=1; } |
| 553 | grep -qx 'checkout=.git' <<<"$o" || { echo "::error::the jail sees the person's checkout"; fail=1; } |
| 554 | grep -qx 'credential=hidden' <<<"$o" || { echo "::error::the jail sees the checkout's git config"; fail=1; } |
| 555 | grep -qE '^npm=[0-9]+\.[0-9]+' <<<"$o" || { echo "::error::npm view left-pad didn't work through the proxy"; fail=1; } |
| 556 | exit $fail |
| 557 | |
| 558 | - name: Pull requests and the merge queue |
| 559 | if: steps.token.outputs.skip != 'true' |
| 560 | run: | |
| 561 | set -euo pipefail |
| 562 | Q="$REPO-pr" |
| 563 | api() { curl -fsS -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; } |
| 564 | api POST /user/repos "{\"name\":\"$Q\",\"private\":true}" >/dev/null |
| 565 | echo "PR_REPO=$Q" >> "$GITHUB_ENV" |
| 566 | api PUT "/repos/$OWNER/$Q/actions/secrets/TOPSECRET" '{"value":"s3cret"}' >/dev/null |
| 567 | remote="https://x-token:${TOKEN}@${DIFFINERY#https://}/$OWNER/$Q.git" |
| 568 | mkdir -p pr/.github/workflows && cd pr |
| 569 | git init -q -b main |
| 570 | git config user.name "Live test" && git config user.email "live-test@diffinery.com" |
| 571 | printf 'a\nb\nc\n' > app.txt |
| 572 | cat > .github/workflows/ci.yml <<'EOF' |
| 573 | name: CI |
| 574 | on: |
| 575 | pull_request: |
| 576 | merge_group: |
| 577 | jobs: |
| 578 | test: |
| 579 | runs-on: ubuntu-latest |
| 580 | steps: |
| 581 | - uses: actions/checkout@v4 |
| 582 | - run: echo "pr ok event=${{ '${{' }} github.event_name }} ref=${{ '${{' }} github.ref }} sha=${{ '${{' }} github.sha }} head=$(git rev-parse HEAD) app=$(tr '\n' ' ' < app.txt)" |
| 583 | EOF |
| 584 | cat > .github/workflows/target.yml <<'EOF' |
| 585 | name: Target |
| 586 | on: pull_request_target |
| 587 | jobs: |
| 588 | t: |
| 589 | runs-on: ubuntu-latest |
| 590 | steps: |
| 591 | - run: echo "target ok secret=${{ '${{' }} secrets.TOPSECRET == 's3cret' }} ref=${{ '${{' }} github.ref }}" |
| 592 | EOF |
| 593 | git add -A && git commit -q -m "PR test" && git push -q "$remote" main |
| 594 | # The pull request changes app.txt and tries to rewrite the pull_request_target workflow. |
| 595 | git checkout -q -b change |
| 596 | printf 'A\nb\nc\n' > app.txt |
| 597 | sed -i 's/target ok/PWNED/' .github/workflows/target.yml |
| 598 | git commit -q -am "Change" && git push -q "$remote" change |
| 599 | head=$(git rev-parse HEAD) |
| 600 | # Main moves on, so the test merge commit holds both sides. |
| 601 | git checkout -q main |
| 602 | printf 'a\nb\nC\n' > app.txt |
| 603 | git commit -q -am "Main moves" && git push -q "$remote" main |
| 604 | n=$(api POST "/repos/$OWNER/$Q/pulls" '{"title":"Change","head":"change","base":"main"}' | jq -r .number) |
| 605 | echo "opened pull request #$n (head $head)" |
| 606 | |
| 607 | wait_run() { # event -> the completed run JSON |
| 608 | for i in $(seq 1 60); do |
| 609 | run=$(api GET "/repos/$OWNER/$Q/actions/runs?event=$1" | jq -c '.workflow_runs[0] // empty') |
| 610 | [ -n "$run" ] && [ "$(jq -r .status <<<"$run")" = completed ] && { echo "$run"; return; } |
| 611 | sleep 10 |
| 612 | done |
| 613 | echo "::error::no completed $1 run" >&2; exit 1 |
| 614 | } |
| 615 | log_of() { for job in $(api GET "/repos/$OWNER/$Q/actions/runs/$1/jobs" | jq -r '.jobs[].id'); do api GET "/repos/$OWNER/$Q/actions/jobs/$job/logs"; done; } |
| 616 | pr=$(wait_run pull_request) |
| 617 | echo "$pr" | jq '{event, ref, sha, head_sha, conclusion}' |
| 618 | [ "$(jq -r .conclusion <<<"$pr")" = success ] || { echo "::error::the pull_request run failed"; exit 1; } |
| 619 | [ "$(jq -r .ref <<<"$pr")" = "refs/pull/$n/merge" ] && [ "$(jq -r .head_sha <<<"$pr")" = "$head" ] && [ "$(jq -r .sha <<<"$pr")" != "$head" ] || { echo "::error::the pull_request run did not use the test merge commit"; exit 1; } |
| 620 | log_of "$(jq -r .id <<<"$pr")" | grep -F "pr ok event=pull_request ref=refs/pull/$n/merge" | grep -F "app=A b C" || { echo "::error::the pull_request job did not check out the merged code"; exit 1; } |
| 621 | target=$(wait_run pull_request_target) |
| 622 | echo "$target" | jq '{event, ref, sha, head_sha, conclusion}' |
| 623 | tlog=$(log_of "$(jq -r .id <<<"$target")") |
| 624 | grep -F "target ok secret=true ref=refs/heads/main" <<<"$tlog" || { echo "::error::pull_request_target did not run the base workflow with secrets"; exit 1; } |
| 625 | ! grep -q PWNED <<<"$tlog" || { echo "::error::pull_request_target ran the pull request's workflow"; exit 1; } |
| 626 | |
| 627 | api PUT "/repos/$OWNER/$Q/branches/main/protection" '{"requirePullRequest":true,"requiredChecks":["CI / test"],"mergeQueue":{"method":"squash","checkTimeoutMinutes":20}}' >/dev/null |
| 628 | code=$(curl -sS -o /dev/null -w '%{http_code}' -X PUT "$DIFFINERY/api/v3/repos/$OWNER/$Q/pulls/$n/merge" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" --data '{}') |
| 629 | [ "$code" = 405 ] || { echo "::error::a direct merge into a merge queue branch returned $code"; exit 1; } |
| 630 | api PUT "/repos/$OWNER/$Q/pulls/$n/merge-queue" '{"delete_branch":true}' | jq '{state, position: .entry.position, merge_group: .entry.merge_group}' |
| 631 | for i in $(seq 1 60); do |
| 632 | merged=$(api GET "/repos/$OWNER/$Q/pulls/$n" | jq -r .merged) |
| 633 | [ "$merged" = true ] && break |
| 634 | sleep 10 |
| 635 | done |
| 636 | [ "$merged" = true ] || { echo "::error::the merge queue did not merge #$n"; api GET "/repos/$OWNER/$Q/merge-queue/main" | jq .; exit 1; } |
| 637 | group=$(wait_run merge_group) |
| 638 | echo "$group" | jq '{event, ref, sha, conclusion}' |
| 639 | [[ "$(jq -r .ref <<<"$group")" == refs/heads/gh-readonly-queue/main/pr-$n-* ]] || { echo "::error::the merge_group run is not on a queue branch"; exit 1; } |
| 640 | sha=$(api GET "/repos/$OWNER/$Q/branches/main" | jq -r .commit.sha) |
| 641 | [ "$sha" = "$(jq -r .sha <<<"$group")" ] || { echo "::error::main ($sha) is not the tested merge group"; exit 1; } |
| 642 | git fetch -q "$remote" main && [ "$(git show FETCH_HEAD:app.txt | tr '\n' ' ')" = "A b C " ] || { echo "::error::main does not hold the merged change"; exit 1; } |
| 643 | echo "pull request #$n merged through the queue as $sha" |
| 644 | |
| 645 | - name: Import a public GitHub repository |
| 646 | if: steps.token.outputs.skip != 'true' |
| 647 | env: |
| 648 | # A small public repository with issues and pull requests; a repository variable can change it. |
| 649 | SOURCE: ${{ vars.LIVE_IMPORT_SOURCE || 'octocat/git-consortium' }} |
| 650 | # A read-only GitHub token (copied here by the sync), so GitHub's rate limit doesn't skip the check. Never printed. |
| 651 | GITHUB_TOKEN_FOR_IMPORT: ${{ secrets.IMPORT_GITHUB_TOKEN }} |
| 652 | run: | |
| 653 | set -euo pipefail |
| 654 | api() { curl -fsS -X "$1" "$DIFFINERY/api/v3$2" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" ${3:+--data "$3"}; } |
| 655 | name="$REPO-import" |
| 656 | body=$(jq -nc --arg s "$SOURCE" --arg o "$OWNER" --arg n "$name" --arg t "${GITHUB_TOKEN_FOR_IMPORT:-}" \ |
| 657 | '{source: $s, owner: $o, name: $n, visibility: "private"} + (if $t == "" then {} else {github_token: $t} end)') |
| 658 | if [ -n "${GITHUB_TOKEN_FOR_IMPORT:-}" ]; then echo "importing with IMPORT_GITHUB_TOKEN"; else echo "::warning::IMPORT_GITHUB_TOKEN is not set, so the import runs token-free"; fi |
| 659 | # Token-free, GitHub allows 60 requests an hour per address: a rate limit skips this check with a warning. |
| 660 | # Starting the import reads the repository from GitHub; a GitHub outage (502 "could not reach GitHub") |
| 661 | # is retried, and skips this check with a warning if it outlasts the retries. While other imports hold |
| 662 | # every slot (an import campaign), it waits up to 15 minutes for one, so a busy account doesn't hide this check. |
| 663 | tries=0 waits=0 |
| 664 | while :; do |
| 665 | code=$(curl -sS -o import.json -w '%{http_code}' -X POST "$DIFFINERY/api/v3/diffinery/imports" -H "Authorization: Bearer $TOKEN" -H "content-type: application/json" \ |
| 666 | --data-binary @- <<<"$body" || true) |
| 667 | unreachable=$(jq -r '.message // "" | startswith("could not reach GitHub")' import.json 2>/dev/null || echo false) |
| 668 | busy=$(jq -r '.message // "" | startswith("too many repository imports are already running")' import.json 2>/dev/null || echo false) |
| 669 | if [ "$code" = 429 ] && [ "$busy" = true ]; then |
| 670 | waits=$((waits + 1)) |
| 671 | [ "$waits" -le 30 ] || break |
| 672 | [ "$waits" -gt 1 ] || echo "every import slot is taken ($(jq -r .message import.json)); waiting for one, up to 15 minutes" |
| 673 | sleep 30 |
| 674 | continue |
| 675 | fi |
| 676 | { [ "$code" = 502 ] && [ "$unreachable" = true ]; } || [ "$code" = 000 ] || break |
| 677 | tries=$((tries + 1)) |
| 678 | [ "$tries" -lt 4 ] || break |
| 679 | echo "starting the import returned $code ($(jq -r .message import.json 2>/dev/null)); trying again in 30 seconds" |
| 680 | sleep 30 |
| 681 | done |
| 682 | if [ "$code" = 429 ] && [ "$busy" = true ]; then echo "::warning::no import slot came free in 15 minutes, so the import check is skipped: $(jq -r .message import.json)"; exit 0; fi |
| 683 | if [ "$code" = 429 ]; then echo "::warning::GitHub's rate limit stopped the import check: $(jq -r .message import.json)"; exit 0; fi |
| 684 | if [ "$code" = 502 ] && [ "$unreachable" = true ]; then echo "::warning::GitHub could not be reached, so the import check is skipped: $(jq -r .message import.json)"; exit 0; fi |
| 685 | [ "$code" = 201 ] || { echo "::error::starting the import returned $code: $(cat import.json)"; exit 1; } |
| 686 | echo "IMPORT_REPO=$name" >> "$GITHUB_ENV" |
| 687 | id=$(jq -r .id import.json) |
| 688 | echo "import $id: $SOURCE -> $OWNER/$name" |
| 689 | for i in $(seq 1 90); do |
| 690 | imp=$(api GET "/diffinery/imports/$id") |
| 691 | status=$(jq -r .status <<<"$imp") |
| 692 | case "$status" in succeeded|failed|cancelled) break ;; esac |
| 693 | if jq -e '[.steps[].message // ""] | any(test("rate limit"))' >/dev/null <<<"$imp"; then |
| 694 | api POST "/diffinery/imports/$id/cancel" >/dev/null || true |
| 695 | echo "::warning::GitHub's rate limit paused the import, so the import check is skipped: $(jq -r '[.steps[].message // ""] | map(select(test("rate limit")))[0]' <<<"$imp")" |
| 696 | exit 0 |
| 697 | fi |
| 698 | sleep 10 |
| 699 | done |
| 700 | jq '{status, error, warnings, steps: [.steps[] | "\(.name): \(.status) \(.message // "")"], git}' <<<"$imp" |
| 701 | [ "$status" = succeeded ] || { echo "::error::the import ended $status"; exit 1; } |
| 702 | |
| 703 | # The same refs, issue and pull request numbers as on GitHub (when GitHub still answers this runner). |
| 704 | R="/repos/$OWNER/$name" |
| 705 | [ "$(api GET "$R" | jq -r .private)" = true ] || { echo "::error::the imported repository is not private"; exit 1; } |
| 706 | gh() { curl -fsS "https://api.github.com/repos/$SOURCE$1" -H "Accept: application/vnd.github+json" ${GITHUB_TOKEN_FOR_IMPORT:+-H "Authorization: Bearer $GITHUB_TOKEN_FOR_IMPORT"}; } |
| 707 | if src=$(gh "") && branches=$(gh "/branches?per_page=100") && items=$(gh "/issues?state=all&per_page=30&sort=created&direction=asc"); then |
| 708 | def=$(jq -r .default_branch <<<"$src") |
| 709 | want=$(jq -r --arg b "$def" '.[] | select(.name == $b) | .commit.sha' <<<"$branches") |
| 710 | got=$(api GET "$R/branches/$def" | jq -r .commit.sha) |
| 711 | [ "$got" = "$want" ] || { echo "::error::$def is $got on Diffinery, $want on GitHub"; exit 1; } |
| 712 | diff <(jq -r '.[].name' <<<"$branches" | sort) <(api GET "$R/branches?per_page=100" | jq -r '.[].name' | sort) || { echo "::error::the branches differ"; exit 1; } |
| 713 | n=0 |
| 714 | for row in $(jq -r '.[] | "\(.number):\(if .pull_request then "pulls" else "issues" end)"' <<<"$items"); do |
| 715 | num=${row%%:*} kind=${row#*:} |
| 716 | want=$(jq -r --argjson n "$num" '.[] | select(.number == $n) | .title' <<<"$items") |
| 717 | got=$(api GET "$R/$kind/$num" | jq -r .title) |
| 718 | [ "$got" = "$want" ] || { echo "::error::$kind #$num is \"$got\" on Diffinery, \"$want\" on GitHub"; exit 1; } |
| 719 | n=$((n + 1)) |
| 720 | done |
| 721 | echo "$def at $got, $(jq length <<<"$branches") branches and $n issues and pull requests match GitHub" |
| 722 | else |
| 723 | echo "::warning::GitHub did not answer this runner (rate limit?), so the import is checked without comparing to GitHub" |
| 724 | fi |
| 725 | # No side effects: importing runs no workflows and sends no notifications. |
| 726 | runs=$(api GET "$R/actions/runs" | jq .total_count) |
| 727 | [ "$runs" = 0 ] || { echo "::error::the import started $runs workflow runs"; exit 1; } |
| 728 | |
| 729 | - name: Scheduled workflow |
| 730 | # Runs even when the import check failed: the two are independent. It needs the scheduled |
| 731 | # workflow pushed, though, so a failed setup doesn't add a 13-minute wait for a run that can't come. |
| 732 | if: ${{ !cancelled() && steps.token.outputs.skip != 'true' && steps.push.outcome == 'success' }} |
| 733 | run: | |
| 734 | set -euo pipefail |
| 735 | # The cron trigger fires every 5 minutes, so allow two windows. |
| 736 | for i in $(seq 1 75); do |
| 737 | run=$(curl -fsS "$DIFFINERY/api/v3/repos/$OWNER/$REPO/actions/runs?event=schedule" -H "Authorization: Bearer $TOKEN" | jq -c '.workflow_runs[0] // empty') |
| 738 | [ -n "$run" ] && [ "$(jq -r .status <<<"$run")" = completed ] && break |
| 739 | sleep 10 |
| 740 | done |
| 741 | if [ -z "$run" ]; then |
| 742 | # Whether the scheduler stopped ticking (last_tick_at is old) or ticked without starting the run. |
| 743 | echo "scheduler: $(curl -sS "$DIFFINERY/api/v3/diffinery/admin/scheduler" -H "Authorization: Bearer $TOKEN") (now $(date -u +%FT%TZ))" |
| 744 | echo "::error::no scheduled run started" |
| 745 | exit 1 |
| 746 | fi |
| 747 | echo "$run" | jq '{id, event, status, conclusion}' |
| 748 | [ "$(jq -r .conclusion <<<"$run")" = success ] || { echo "::error::the scheduled run did not succeed"; exit 1; } |
| 749 | |
| 750 | - name: Delete the throwaway repositories |
| 751 | if: always() && steps.token.outputs.skip != 'true' |
| 752 | run: | |
| 753 | curl -sS -o /dev/null -w 'package %{http_code}\n' -X DELETE "$DIFFINERY/api/v3/users/$OWNER/packages/container/${REPO,,}" -H "Authorization: Bearer $TOKEN" |
| 754 | curl -sS -o /dev/null -w '%{http_code}\n' -X DELETE "$DIFFINERY/api/v3/repos/$OWNER/$REPO" -H "Authorization: Bearer $TOKEN" |
| 755 | [ -z "${PR_REPO:-}" ] || curl -sS -o /dev/null -w '%{http_code}\n' -X DELETE "$DIFFINERY/api/v3/repos/$OWNER/$PR_REPO" -H "Authorization: Bearer $TOKEN" |
| 756 | [ -z "${IMPORT_REPO:-}" ] || curl -sS -o /dev/null -w '%{http_code}\n' -X DELETE "$DIFFINERY/api/v3/repos/$OWNER/$IMPORT_REPO" -H "Authorization: Bearer $TOKEN" |