Modular logistics and supply chain operating system (LSOS)
JavaScript driver-appfleet-managementfreight-managementinfrastructureinventory-managementlast-mile-deliverylogisticsondemandopen-sourceorder-managementroute-optimizationsupply-chain-managementtracking-and-monitoringtransport-management-systemtransportationwarehouse-management
ci(api-contract): defer deletes at run time and mint a platform token
Ronald A. Richardson84538f6b8d45
diff --git a/.github/actions/postman-contract/action.yml b/.github/actions/postman-contract/action.ymlindex 14596c50..cd3c4f56 100644--- a/.github/actions/postman-contract/action.yml+++ b/.github/actions/postman-contract/action.yml@@ -60,6 +60,7 @@ runs:CODE="$(base64 scripts/ci/mint-api-key.php | tr -d '\n')"OUT="$(docker compose exec -T application php artisan tinker --execute="eval(base64_decode('${CODE}'));" 2>&1 || true)"KEY="$(printf '%s\n' "$OUT" | grep -oE 'flb_(live|test)_[A-Za-z0-9_-]+' | tail -1 || true)"+ PLATFORM_TOKEN="$(printf '%s\n' "$OUT" | grep -oE 'flb_platform_[A-Za-z0-9]+' | tail -1 || true)"if [[ -z "$KEY" ]]; thenecho "::error::Could not mint an API key from the running stack. Tinker output:"printf '%s\n' "$OUT"@@ -67,6 +68,15 @@ runs:fiecho "::add-mask::$KEY"echo "api_key=$KEY" >> "$GITHUB_OUTPUT"+ # A few endpoints (organizations listing, driver onboarding) sit behind the+ # platform API token rather than an org credential and answer 401 to an+ # flb_live_ key. Absent is not fatal — only those requests will 401.+ if [[ -n "$PLATFORM_TOKEN" ]]; then+ echo "::add-mask::$PLATFORM_TOKEN"+ echo "platform_token=$PLATFORM_TOKEN" >> "$GITHUB_OUTPUT"+ else+ echo "::warning::No platform token minted; platform-scoped requests will 401."+ fiecho "Verifying key against ${{ inputs.base-url }}/${{ inputs.namespace }}/organizations/current ..."HTTP_CODE="$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer ${KEY}" \"${{ inputs.base-url }}/${{ inputs.namespace }}/organizations/current" || true)"@@ -102,6 +112,7 @@ runs:shell: bashenv:API_KEY: ${{ steps.key.outputs.api_key }}+ PLATFORM_TOKEN: ${{ steps.key.outputs.platform_token }}COLLECTIONS: ${{ inputs.collections }}BASE_URL: ${{ inputs.base-url }}NAMESPACE: ${{ inputs.namespace }}@@ -130,6 +141,7 @@ runs:' --env-var "base_url=${BASE_URL}" \' \' --env-var "namespace=${NAMESPACE}" \' \' --env-var "api_key=${API_KEY}" \' \+ ' --env-var "platform_token=${PLATFORM_TOKEN}" \' \' "${@:2}"' > "$RUNNER"chmod +x "$RUNNER"@@ -165,7 +177,22 @@ runs:# for them outright:# Error: Reporter "json" is not supported for multi-protocol collections.# So the summary below is parsed from the cli reporter's ASCII table.+ # Run every request explicitly, in the collection's own order but with each+ # DELETE moved to the end. The collection is published as API reference+ # documentation, so its structure and ordering cannot be changed to suit the+ # test run — but a delete that fires mid-run destroys a record the remaining+ # requests still need, which is what produced most of the 404s. The CLI+ # honours the order of repeated -i flags, so deferring them costs nothing and+ # keeps DELETE coverage intact.args=("$dir" -r cli)+ if [[ -f scripts/ci/order-collection-requests.py ]]; then+ while IFS= read -r req; do+ [[ -n "$req" ]] && args+=(-i "$req")+ done < <(python3 scripts/ci/order-collection-requests.py "$dir")+ echo " running $(( (${#args[@]} - 2) / 2 )) requests, deletes deferred to the end"+ else+ echo "::warning::order-collection-requests.py not found; running in natural order (deletes may strand later requests)."+ fi# The CLI does NOT resolve the collection variables declared in# .resources/definition.yaml — verified: with no --env-var at all, evendiff --git a/scripts/ci/mint-api-key.php b/scripts/ci/mint-api-key.phpindex 048bf30d..fa1c4b11 100644--- a/scripts/ci/mint-api-key.php+++ b/scripts/ci/mint-api-key.php@@ -40,6 +40,15 @@ try {);echo PHP_EOL . 'MINTED_API_KEY=' . $credential->fresh()->key . PHP_EOL;++ // A handful of endpoints (organizations listing, driver onboarding) sit behind+ // AuthenticatePlatformApiToken rather than an org API credential, and answer 401+ // to an flb_live_ key. Only the hash is persisted, so the plaintext cannot be read+ // back — rotating is the only way to learn a usable token. Safe here because this+ // script only ever runs against a throwaway CI stack; do not run it against an+ // install whose platform token is already in use.+ $platformToken = \Fleetbase\Support\PlatformApi::rotateToken();+ echo 'MINTED_PLATFORM_TOKEN=' . $platformToken . PHP_EOL;} catch (\Throwable $e) {echo PHP_EOL . 'MINT_ERROR: ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL;echo $e->getFile() . ':' . $e->getLine() . PHP_EOL;diff --git a/scripts/ci/order-collection-requests.py b/scripts/ci/order-collection-requests.pynew file mode 100755index 00000000..274c5edc--- /dev/null+++ b/scripts/ci/order-collection-requests.py@@ -0,0 +1,35 @@+#!/usr/bin/env python3+"""Emit `-i <Folder/Request>` args for a Postman git-native collection directory,+in the collection's own order, with every DELETE deferred to the end.++The collection doubles as published API reference, so its on-disk structure and+ordering must not change. Deferring deletes at run time keeps DELETE coverage+without letting a delete destroy a record the rest of the run still needs.+"""+import os, re, sys++root = sys.argv[1]+def field(path, key, default=None):+ try:+ m = re.search(rf'^{key}:\s*(\S+)', open(path, encoding='utf-8').read(), re.M)+ return m.group(1) if m else default+ except OSError:+ return default++items = []+for folder in os.listdir(root):+ d = os.path.join(root, folder)+ if not os.path.isdir(d) or folder.startswith('.'):+ continue+ forder = int(field(os.path.join(d, '.resources', 'definition.yaml'), 'order', 10**9))+ for fn in os.listdir(d):+ if not fn.endswith('.request.yaml'):+ continue+ p = os.path.join(d, fn)+ name = fn[:-len('.request.yaml')]+ method = (field(p, 'method', '?') or '?').upper()+ rorder = int(field(p, 'order', 10**9))+ items.append((method == 'DELETE', forder, folder, rorder, name))++for _is_delete, _fo, folder, _ro, name in sorted(items):+ print(f'{folder}/{name}')