|
| 1 | +#!/usr/bin/env bash |
| 2 | +# build-lookup.sh — Resolve a versionCode to its git commit and GitHub Action run. |
| 3 | +# |
| 4 | +# The Flipcash versionCode is derived from `git rev-list --count HEAD` (see |
| 5 | +# apps/flipcash/app/build.gradle.kts:20-25). versionCode N = the Nth commit |
| 6 | +# from the root of the repo. |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# ./build-lookup.sh <versionCode> |
| 10 | +# ./build-lookup.sh 3797 |
| 11 | +# |
| 12 | +# Output: JSON with commit_sha, commit_short, commit_message, and (if found) |
| 13 | +# the GitHub Actions run ID and URL for the Flipcash2 workflow. |
| 14 | + |
| 15 | +set -euo pipefail |
| 16 | + |
| 17 | +if [[ $# -lt 1 ]]; then |
| 18 | + echo "Usage: build-lookup.sh <versionCode>" >&2 |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | + |
| 22 | +VERSION_CODE="$1" |
| 23 | +WORKFLOW_ID="229420296" # "Flipcash2 Build and Deploy" |
| 24 | + |
| 25 | +# ── Resolve versionCode to commit ─────────────────────────────────── |
| 26 | +# versionCode = git rev-list --count HEAD at the time of build. |
| 27 | +# The Nth commit (1-indexed from root) is at position N in `git rev-list --reverse HEAD`. |
| 28 | +# Equivalently: git rev-list HEAD | sed -n '<offset>p' where offset = total - N + 1. |
| 29 | + |
| 30 | +TOTAL=$(git rev-list --count HEAD) |
| 31 | + |
| 32 | +if (( VERSION_CODE > TOTAL )); then |
| 33 | + echo "ERROR: versionCode $VERSION_CODE exceeds current commit count ($TOTAL)" >&2 |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +if (( VERSION_CODE < 1 )); then |
| 38 | + echo "ERROR: versionCode must be >= 1" >&2 |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +OFFSET=$(( TOTAL - VERSION_CODE + 1 )) |
| 43 | +COMMIT_SHA=$(git rev-list HEAD | sed -n "${OFFSET}p") |
| 44 | +COMMIT_SHORT="${COMMIT_SHA:0:9}" |
| 45 | +COMMIT_MESSAGE=$(git log -1 --format='%s' "$COMMIT_SHA") |
| 46 | +COMMIT_DATE=$(git log -1 --format='%aI' "$COMMIT_SHA") |
| 47 | + |
| 48 | +# ── Find GitHub Actions run for this commit ───────────────────────── |
| 49 | +RUN_JSON="" |
| 50 | +RUN_ID="" |
| 51 | +RUN_URL="" |
| 52 | +RUN_STATUS="" |
| 53 | +RUN_CONCLUSION="" |
| 54 | + |
| 55 | +if command -v gh &>/dev/null; then |
| 56 | + # gh run list --commit requires a full SHA match, so fetch recent runs and |
| 57 | + # filter by headSha prefix instead. We pull the last 100 runs to cover ~2 |
| 58 | + # weeks of builds. |
| 59 | + ALL_RUNS=$(gh run list \ |
| 60 | + --workflow="$WORKFLOW_ID" \ |
| 61 | + --json databaseId,url,status,conclusion,headSha \ |
| 62 | + --limit 100 2>/dev/null || true) |
| 63 | + |
| 64 | + if [[ -n "$ALL_RUNS" ]]; then |
| 65 | + MATCH=$(echo "$ALL_RUNS" | jq --arg sha "$COMMIT_SHA" '[.[] | select(.headSha == $sha)] | .[0] // empty') |
| 66 | + if [[ -n "$MATCH" ]]; then |
| 67 | + RUN_ID=$(echo "$MATCH" | jq -r '.databaseId') |
| 68 | + RUN_URL=$(echo "$MATCH" | jq -r '.url') |
| 69 | + RUN_STATUS=$(echo "$MATCH" | jq -r '.status') |
| 70 | + RUN_CONCLUSION=$(echo "$MATCH" | jq -r '.conclusion') |
| 71 | + fi |
| 72 | + fi |
| 73 | +fi |
| 74 | + |
| 75 | +# ── Emit result ───────────────────────────────────────────────────── |
| 76 | +jq -n \ |
| 77 | + --arg version_code "$VERSION_CODE" \ |
| 78 | + --arg commit_sha "$COMMIT_SHA" \ |
| 79 | + --arg commit_short "$COMMIT_SHORT" \ |
| 80 | + --arg commit_message "$COMMIT_MESSAGE" \ |
| 81 | + --arg commit_date "$COMMIT_DATE" \ |
| 82 | + --arg total_commits "$TOTAL" \ |
| 83 | + --arg run_id "${RUN_ID:-}" \ |
| 84 | + --arg run_url "${RUN_URL:-}" \ |
| 85 | + --arg run_status "${RUN_STATUS:-}" \ |
| 86 | + --arg run_conclusion "${RUN_CONCLUSION:-}" \ |
| 87 | + '{ |
| 88 | + version_code: ($version_code | tonumber), |
| 89 | + commit: { |
| 90 | + sha: $commit_sha, |
| 91 | + short: $commit_short, |
| 92 | + message: $commit_message, |
| 93 | + date: $commit_date |
| 94 | + }, |
| 95 | + total_commits: ($total_commits | tonumber), |
| 96 | + github_actions: ( |
| 97 | + if $run_id != "" then { |
| 98 | + run_id: ($run_id | tonumber), |
| 99 | + url: $run_url, |
| 100 | + status: $run_status, |
| 101 | + conclusion: $run_conclusion |
| 102 | + } else null end |
| 103 | + ) |
| 104 | + }' |
0 commit comments