|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eo pipefail |
| 4 | + |
| 5 | +# Constants should ideally be set as environment variables for security |
| 6 | +readonly API_URL="https://api.mobileboost.io" |
| 7 | +readonly API_ORG_KEY="${API_ORG_KEY}" |
| 8 | +readonly API_TOKEN="${API_TOKEN:-null}" |
| 9 | +readonly TEST_TIMEOUT="${TEST_TIMEOUT:-7200}" |
| 10 | +readonly TEST_TAGS="${TEST_TAGS:-}" |
| 11 | + |
| 12 | +# Function to post data using curl and handle errors |
| 13 | +post_data() { |
| 14 | + local url=$1 |
| 15 | + local body=$2 |
| 16 | + if ! response=$(curl -s -f -X POST -H "Authorization: Bearer $API_TOKEN" -H "Content-Type: application/json" -d "$body" "$url"); then |
| 17 | + echo "Error: Network request failed with error $response" >&2 |
| 18 | + exit 1 |
| 19 | + fi |
| 20 | + echo "$response" |
| 21 | +} |
| 22 | + |
| 23 | +# Validate inputs |
| 24 | +if [[ -z "$1" || -z "$2" ]]; then |
| 25 | + echo "Usage: $0 <build_filename> <build_platform>" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +# Validate environment variables |
| 30 | +if [[ -z "$API_ORG_KEY" ]]; then |
| 31 | + echo "Please set API_ORG_KEY to your organization key" |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +buildFilename="$1" |
| 36 | +buildPlatform="$2" |
| 37 | +tags=() |
| 38 | + |
| 39 | +# Check if TEST_TAGS is provided and split into an array |
| 40 | +if [[ -n "$TEST_TAGS" ]]; then |
| 41 | + IFS=',' read -ra tags <<< "$TEST_TAGS" |
| 42 | +fi |
| 43 | + |
| 44 | +# Upload build file |
| 45 | +echo -n "Uploading build from $buildFilename for $buildPlatform: " |
| 46 | +if ! uploadedBuildResponse=$(curl -s -f -X POST \ |
| 47 | + -H "Authorization: Bearer $API_TOKEN" \ |
| 48 | + -H "Content-Type: multipart/form-data" \ |
| 49 | + -F "build=@$buildFilename" \ |
| 50 | + -F "organisation_key=$API_ORG_KEY" \ |
| 51 | + -F "platform=$buildPlatform" \ |
| 52 | + -F "metadata={}" \ |
| 53 | + "$API_URL/uploadBuild/"); then |
| 54 | + echo "Error: Failed to upload build" >&2 |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +# Extract the buildId |
| 59 | +if ! buildId=$(jq -r '.buildId' <<< "$uploadedBuildResponse") || [ -z "$buildId" ]; then |
| 60 | + echo "Error: Failed to extract build ID from the response" >&2 |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | +echo "uploaded (ID: $buildId), app link: $(jq -r '.app_link' <<< "$uploadedBuildResponse")" |
| 64 | + |
| 65 | +# Execute test suite |
| 66 | +echo "Executing test suite..." |
| 67 | +jsonPayload="{\"organisationId\": \"$API_ORG_KEY\", \"uploadId\": \"$buildId\"" |
| 68 | +if [ ${#tags[@]} -gt 0 ]; then |
| 69 | + jsonTags=$(printf ',\"%s\"' "${tags[@]}") |
| 70 | + jsonTags="[${jsonTags:1}]" |
| 71 | + jsonPayload+=", \"tags\": $jsonTags" |
| 72 | +fi |
| 73 | +jsonPayload+="}" |
| 74 | +if ! testSuiteRunId=$(post_data "$API_URL/tests/execute" "$jsonPayload" | jq -r '.test_suite_ids[0]') || [ -z "$testSuiteRunId" ]; then |
| 75 | + echo "Error: Test suite execution failed" >&2 |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | + |
| 79 | +# Wait for test suite to finish |
| 80 | +echo -n "Waiting for test suite to finish..." |
| 81 | +startTime=$(date +%s) |
| 82 | +while true; do |
| 83 | + if ! testSuiteData=$(curl -s -f "$API_URL/testSuiteRuns/$testSuiteRunId/gh"); then |
| 84 | + echo "Error: Failed to retrieve test suite data" >&2 |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + testSuiteStatus=$(jq -r '.status' <<< "$testSuiteData") |
| 88 | + |
| 89 | + if [[ |
| 90 | + "$testSuiteStatus" == "completed" |
| 91 | + ]]; then |
| 92 | + echo "Status is $testSuiteStatus!" >&2 |
| 93 | + break |
| 94 | + fi |
| 95 | + |
| 96 | + if (( $(date +%s) - startTime >= TEST_TIMEOUT )); then |
| 97 | + echo "Timeout exceeded while waiting for test suite to finish." >&2 |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + |
| 101 | + echo -n "." |
| 102 | + sleep 1 |
| 103 | +done |
| 104 | +echo " done!" |
| 105 | + |
| 106 | +# Write test suite summary to file if available |
| 107 | +if [[ -n "$GITHUB_STEP_SUMMARY" && -w "$GITHUB_STEP_SUMMARY" ]]; then |
| 108 | + jq -r '.markdown' <<< "$testSuiteData" >> "$GITHUB_STEP_SUMMARY" |
| 109 | + echo "Step summary written to $GITHUB_STEP_SUMMARY" |
| 110 | +fi |
| 111 | + |
| 112 | +# Check test suite result |
| 113 | +if ! testSuiteResult=$(jq -r '.result' <<< "$testSuiteData"); then |
| 114 | + echo "Test suite did not pass, result: $testSuiteResult" >&2 |
| 115 | + exit 1 |
| 116 | +fi |
| 117 | + |
| 118 | +if [[ "$testSuiteResult" == "succeeded" ]]; then |
| 119 | + echo "Test passed successfully" |
| 120 | + exit 0 |
| 121 | +else |
| 122 | + echo "Test suite did not pass, result: $testSuiteResult" >&2 |
| 123 | + exit 1 |
| 124 | +fi |
0 commit comments