Skip to content

Commit f7d664d

Browse files
author
latdx-mirror-bot
committed
ci: overlay fork CI for upstream PR jongpie#995
Upstream-SHA: 3c1b973 Fork-CI-Tree: 3f1fe17
1 parent 3c1b973 commit f7d664d

5 files changed

Lines changed: 1102 additions & 115 deletions

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
name: 'LATdx Apex Tests'
2+
description: >-
3+
Install the LATdx CLI, resolve a license (explicit key via the
4+
LATDX_LICENSE_KEY env var, or a short-lived OSS license minted from the
5+
GitHub Actions OIDC token on public repos), and run the full local Apex
6+
test suite against the job's default Salesforce org.
7+
8+
inputs:
9+
cli-version:
10+
description: "LATdx CLI version to install (semver like '0.31.1') or 'latest'."
11+
required: false
12+
default: 'latest'
13+
14+
runs:
15+
using: composite
16+
steps:
17+
- name: 'Install LATdx CLI'
18+
shell: bash
19+
env:
20+
CLI_VERSION: ${{ inputs.cli-version }}
21+
run: |
22+
set -euo pipefail
23+
if [[ ! "$CLI_VERSION" =~ ^(latest|[0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
24+
echo "::error::Invalid 'cli-version' input. Must be 'latest' or a semver like '0.31.1'."
25+
exit 1
26+
fi
27+
# latdx.com serves the maintained install script, but Cloudflare
28+
# returns 403 to some hosted-runner egress IP ranges; fall back to
29+
# the GitHub raw mirror so the install succeeds from any runner.
30+
script=""
31+
for url in "https://latdx.com/install.sh" "https://raw.githubusercontent.com/nebulity/latdx-cli/main/install.sh"; do
32+
if script="$(curl -fsSL "$url")"; then
33+
break
34+
fi
35+
script=""
36+
done
37+
if [ -z "$script" ]; then
38+
echo "::error::Could not download the LATdx install script from any source."
39+
exit 1
40+
fi
41+
if [ "$CLI_VERSION" = "latest" ]; then
42+
printf '%s' "$script" | bash
43+
else
44+
printf '%s' "$script" | bash -s -- "$CLI_VERSION"
45+
fi
46+
echo "$HOME/.local/bin" >> "$GITHUB_PATH"
47+
48+
- name: 'Verify LATdx CLI'
49+
shell: bash
50+
run: latdx --version
51+
52+
- name: 'Resolve LATdx license'
53+
shell: bash
54+
run: |
55+
# The runner injects -e -o pipefail; this step must never fail the
56+
# job: every problem degrades to the free-tier cap with a warning.
57+
set +e +o pipefail
58+
59+
# Precedence:
60+
# 1. LATDX_LICENSE_KEY already in the environment (repo secret) -> use it.
61+
# 2. GH OIDC token available + repo public -> exchange for an OSS
62+
# auto-license via https://latdx.com/api/oss/license.
63+
# 3. Nothing -> daemon runs free-tier (capped at 100 tests, exit 2).
64+
65+
if [ -n "${LATDX_LICENSE_KEY:-}" ]; then
66+
echo "Using LATdx license from environment."
67+
exit 0
68+
fi
69+
70+
if [ -z "${ACTIONS_ID_TOKEN_REQUEST_TOKEN:-}" ] || [ -z "${ACTIONS_ID_TOKEN_REQUEST_URL:-}" ]; then
71+
echo "::warning title=LATdx OSS license::OIDC token unavailable (missing 'permissions: id-token: write' on the job). Free-tier cap (100 tests/run) applies."
72+
exit 0
73+
fi
74+
75+
OIDC_RESPONSE="$(curl -sS -f -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
76+
"${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=https%3A%2F%2Flatdx.com" 2>/dev/null)"
77+
CURL_RC=$?
78+
if [ "$CURL_RC" -ne 0 ]; then
79+
echo "::warning title=LATdx OSS license::OIDC token mint failed (curl rc=${CURL_RC}); falling back to free-tier cap."
80+
exit 0
81+
fi
82+
OIDC_TOKEN="$(printf '%s' "$OIDC_RESPONSE" | jq -r '.value // empty' 2>/dev/null)"
83+
if [ -z "$OIDC_TOKEN" ]; then
84+
echo "::warning title=LATdx OSS license::OIDC response not parseable (length ${#OIDC_RESPONSE}); falling back to free-tier cap."
85+
exit 0
86+
fi
87+
echo "Minted GitHub OIDC token (length ${#OIDC_TOKEN})."
88+
89+
EXCHANGE_BODY="$(mktemp)"
90+
EXCHANGE_HEADERS="$(mktemp)"
91+
trap 'rm -f "$EXCHANGE_BODY" "$EXCHANGE_HEADERS"' EXIT
92+
HTTP_CODE="$(curl -sS -o "$EXCHANGE_BODY" -D "$EXCHANGE_HEADERS" -w "%{http_code}" \
93+
-X POST https://latdx.com/api/oss/license \
94+
-H "Authorization: Bearer $OIDC_TOKEN" \
95+
-H "Content-Type: application/json" \
96+
--max-time 15)"
97+
if [ $? -ne 0 ]; then
98+
HTTP_CODE="000"
99+
fi
100+
echo "OSS license exchange returned HTTP ${HTTP_CODE}."
101+
# On any non-200, surface whether the response is the LATdx route's
102+
# JSON or an edge (Cloudflare) interstitial. The denied-request body
103+
# carries no secrets. `server`/`cf-ray` headers identify the edge.
104+
if [ "$HTTP_CODE" != "200" ]; then
105+
echo "--- response server header: $(grep -i '^server:' "$EXCHANGE_HEADERS" | tr -d '\r')"
106+
echo "--- cf-ray header: $(grep -i '^cf-ray:' "$EXCHANGE_HEADERS" | tr -d '\r')"
107+
echo "--- content-type: $(grep -i '^content-type:' "$EXCHANGE_HEADERS" | tr -d '\r')"
108+
echo "--- body (first 300 chars): $(head -c 300 "$EXCHANGE_BODY" | tr '\n' ' ')"
109+
fi
110+
111+
case "$HTTP_CODE" in
112+
200)
113+
LATDX_JWT="$(jq -r '.license // empty' < "$EXCHANGE_BODY" 2>/dev/null)"
114+
if [ -z "$LATDX_JWT" ]; then
115+
echo "::warning title=LATdx OSS license::Exchange returned 200 without a license; falling back to free-tier cap."
116+
exit 0
117+
fi
118+
echo "::add-mask::$LATDX_JWT"
119+
echo "LATDX_LICENSE_KEY=$LATDX_JWT" >> "$GITHUB_ENV"
120+
USED="$(jq -r '.monthly_used // "?"' < "$EXCHANGE_BODY" 2>/dev/null)"
121+
CAP="$(jq -r '.monthly_cap // "?"' < "$EXCHANGE_BODY" 2>/dev/null)"
122+
echo "OSS auto-license active (monthly_used=$USED, monthly_cap=$CAP) - free-tier cap lifted for this run."
123+
;;
124+
403)
125+
REASON="$(jq -r '.reason // "denied"' < "$EXCHANGE_BODY" 2>/dev/null)"
126+
ERROR="$(jq -r '.error // "OSS license denied."' < "$EXCHANGE_BODY" 2>/dev/null)"
127+
echo "::warning title=LATdx OSS license::${ERROR} (reason=${REASON}). Free-tier cap (100 tests/run) applies."
128+
;;
129+
429)
130+
USED="$(jq -r '.monthly_used // "?"' < "$EXCHANGE_BODY" 2>/dev/null)"
131+
CAP="$(jq -r '.monthly_cap // "?"' < "$EXCHANGE_BODY" 2>/dev/null)"
132+
RESET="$(jq -r '.reset_at // "next UTC month"' < "$EXCHANGE_BODY" 2>/dev/null)"
133+
echo "::warning title=LATdx OSS license::Monthly cap reached (${USED}/${CAP}); resets ${RESET}. Free-tier cap applies for this run."
134+
;;
135+
503)
136+
echo "::warning title=LATdx OSS license::OSS path temporarily unavailable. Free-tier cap applies for this run."
137+
;;
138+
000)
139+
echo "::warning title=LATdx OSS license::Could not reach the LATdx site. Free-tier cap applies for this run."
140+
;;
141+
*)
142+
ERROR="$(jq -r '.error // ""' < "$EXCHANGE_BODY" 2>/dev/null)"
143+
echo "::warning title=LATdx OSS license::Unexpected response ${HTTP_CODE} from /api/oss/license (${ERROR}). Free-tier cap applies."
144+
;;
145+
esac
146+
147+
- name: 'Run Apex tests with LATdx'
148+
shell: bash
149+
run: |
150+
set +e
151+
latdx test run
152+
rc=$?
153+
set -e
154+
# Exit 2 is EXIT_LICENSE_REQUIRED: the free-tier cap (100 tests) was
155+
# hit. Treat it as a non-fatal, informational outcome so the pipeline
156+
# stays green on the tests that did run; lift the cap with an OSS
157+
# auto-license or LATDX_LICENSE_KEY to run the full suite.
158+
if [ "$rc" -eq 2 ]; then
159+
echo "::warning title=LATdx license cap::Run halted at the free-tier cap (100 tests). Provide a license to run the full suite."
160+
exit 0
161+
fi
162+
exit "$rc"

0 commit comments

Comments
 (0)