name: PR Rebuild On Version Bump run-name: rebuild test builds below versionCode on main on: push: branches: [main] paths: [app/build.gradle] workflow_dispatch: permissions: actions: write contents: read issues: write pull-requests: read concurrency: group: pr-rebuild-on-version cancel-in-progress: false queue: max jobs: rebuild: runs-on: ubuntu-24.04 timeout-minutes: 180 steps: - uses: actions/checkout@v4 with: ref: main fetch-depth: 2 persist-credentials: false - name: Rebuild open PRs whose test build Android would refuse over main env: GH_TOKEN: ${{ github.token }} EVENT_NAME: ${{ github.event_name }} BEFORE: ${{ github.event.before }} PUSHED_AT: ${{ github.event.repository.pushed_at }} run: | set -euo pipefail code() { sed -n 's/^[[:space:]]*versionCode[[:space:]][[:space:]]*\([0-9][0-9]*\)[[:space:]]*$/\1/p'; } now=$(code < app/build.gradle) [[ "$now" =~ ^[0-9]+$ ]] || { echo "::error::No numeric versionCode on main"; exit 1; } if [ "$EVENT_NAME" = push ]; then was=$(git show "$BEFORE:app/build.gradle" 2>/dev/null | code || true) if [[ "$was" =~ ^[0-9]+$ ]] && [ "$was" -ge "$now" ]; then echo "versionCode stayed at $now" exit 0 fi fi # This run is created right after the push, so a PR build that started earlier may have pinned the old main. bumped=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" --jq .created_at) # A build can finish between the push and this run's creation; the push event carries the push time. if [[ "${PUSHED_AT:-}" =~ ^[0-9]+$ ]]; then pushed=$(date -u -d "@$PUSHED_AT" +%Y-%m-%dT%H:%M:%SZ) [[ "$pushed" < "$bumped" ]] && bumped=$pushed else pushed=$(date -u -d "$bumped - 10 minutes" +%Y-%m-%dT%H:%M:%SZ) fi note() { local number=$1 local marker="" gh api "repos/$GITHUB_REPOSITORY/issues/$number/comments?per_page=100" --jq '.[].body' | grep -qF "$marker" && return 0 gh api -X POST "repos/$GITHUB_REPOSITORY/issues/$number/comments" -f body="$marker Main is now at versionCode $now, so this PR's test build no longer installs over current DroidDeck, and its last build is too old for GitHub to re-run. Push a commit or merge main to get a new test build." >/dev/null } latest() { gh api "repos/$GITHUB_REPOSITORY/actions/workflows/build.yml/runs?event=pull_request&head_sha=$1&per_page=20" \ --jq '[.workflow_runs[] | select(.head_sha == "'"$1"'")] | sort_by(.created_at) | last // empty | [.id, .status, (.run_started_at // .created_at), .created_at, .updated_at] | @tsv' } consider() { local number=$1 head=$2 built=$3 id status started created updated IFS=$'\t' read -r id status started created updated <<<"$4" if [[ "$started" > "$bumped" ]]; then echo "PR #$number: build $id started after the bump" elif ! gh api "repos/$GITHUB_REPOSITORY/pulls/$number" | jq -e --arg head "$head" '.state == "open" and .draft == false and .base.ref == "main" and .head.sha == $head' >/dev/null; then echo "PR #$number: moved on since the scan, its own build covers it" elif gh run rerun "$id" -R "$GITHUB_REPOSITORY"; then echo "PR #$number: rebuilding ${head:0:7} on main (versionCode $built -> $now)" else echo "::warning::PR #$number: could not rerun build $id" # GitHub only re-runs a workflow within 30 days of its first run. if [[ "$created" < "$(date -u -d '-30 days' +%Y-%m-%dT%H:%M:%SZ)" ]]; then note "$number" || echo "::warning::PR #$number: could not leave a note" fi fi } pending=() declare -A seen=() candidates() { gh api --paginate "repos/Droid-Deck/DroidDeck-CI/releases?per_page=100" \ --jq '.[] | select(.tag_name | test("^pr-[0-9]+$")) | [(.tag_name | ltrimstr("pr-")), ((.body // "") | capture("(?m)^VersionCode: (?[0-9]+)").v // "0")] | @tsv' # The latest build of every open PR: one that started before the bump and was still running at it # may have pinned the old main. This also covers a PR's first build and fork builds. gh api --paginate -X GET "repos/$GITHUB_REPOSITORY/pulls" -f state=open -f base=main -f per_page=100 \ --jq '.[] | select(.draft | not) | [(.number | tostring), .head.sha] | @tsv' | while IFS=$'\t' read -r number sha; do run=$(latest "$sha") [ -n "$run" ] || continue IFS=$'\t' read -r _ status started _ updated <<<"$run" if [[ "$started" < "$bumped" ]] && { [ "$status" != completed ] || [[ ! "$updated" < "$pushed" ]]; }; then printf '%s\t?\n' "$number" fi done } found=$(mktemp) candidates > "$found" while IFS=$'\t' read -r number built; do [ "$built" = "?" ] || [ "$built" -lt "$now" ] || continue [ -z "${seen[$number]:-}" ] || continue seen[$number]=1 pr=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$number") || continue jq -e '.state == "open" and .draft == false and .base.ref == "main"' <<<"$pr" >/dev/null || continue head=$(jq -r .head.sha <<<"$pr") run=$(latest "$head") if [ -z "$run" ]; then echo "PR #$number: no build of ${head:0:7} to rerun" elif [ "$(cut -f2 <<<"$run")" = completed ]; then consider "$number" "$head" "$built" "$run" else pending+=("$number $head $built") fi done < "$found" # Builds still running are polled together so one slow build cannot hold up the rest. deadline=$((SECONDS + 150 * 60)) while [ ${#pending[@]} -gt 0 ] && [ "$SECONDS" -lt "$deadline" ]; do sleep 60 left=() for item in "${pending[@]}"; do read -r number head built <<<"$item" run=$(latest "$head") if [ -n "$run" ] && [ "$(cut -f2 <<<"$run")" = completed ]; then consider "$number" "$head" "$built" "$run" else left+=("$item") fi done pending=(${left[@]+"${left[@]}"}) done for item in ${pending[@]+"${pending[@]}"}; do echo "::warning::PR #${item%% *}: its build did not finish in time" done