Skip to content

Instantly share code, notes, and snippets.

@noahd1
Created May 23, 2025 02:55
Show Gist options
  • Select an option

  • Save noahd1/1ae7db3fcca3216d38ee6ed4ad69b0d8 to your computer and use it in GitHub Desktop.

Select an option

Save noahd1/1ae7db3fcca3216d38ee6ed4ad69b0d8 to your computer and use it in GitHub Desktop.
Debug Qlty Coverage Without Using CLI
#!/bin/bash
# Debug script to mimic qlty coverage upload functionality
# Usage: ./debug_coverage_upload.sh <coverage.zip>
set -euo pipefail
# Check dependencies
command -v curl >/dev/null 2>&1 || { echo "Error: curl is required but not installed." >&2; exit 1; }
command -v jq >/dev/null 2>&1 || { echo "Error: jq is required but not installed." >&2; exit 1; }
# Check environment variables
if [[ -z "${QLTY_COVERAGE_TOKEN:-}" ]]; then
echo "Error: QLTY_COVERAGE_TOKEN environment variable is required" >&2
exit 1
fi
# Check arguments
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <coverage.zip>" >&2
exit 1
fi
COVERAGE_FILE="$1"
if [[ ! -f "$COVERAGE_FILE" ]]; then
echo "Error: Coverage file '$COVERAGE_FILE' not found" >&2
exit 1
fi
# Configuration
API_BASE_URL="${QLTY_API_URL:-https://qlty.sh/api}"
COVERAGE_ENDPOINT="/coverage"
QLTY_VERSION="debug-script-1.0.0"
echo "=== Qlty Coverage Upload Debug Script ==="
echo "API Base URL: $API_BASE_URL"
echo "Coverage file: $COVERAGE_FILE"
echo "Token: ${QLTY_COVERAGE_TOKEN:0:8}..." # Show only first 8 chars for security
# Step 1: Prepare minimal coverage metadata
echo ""
echo "Step 1: Preparing coverage metadata..."
# Create minimal metadata payload
METADATA=$(cat <<EOF
{
"data": {
"branch": "main",
"buildId": "debug-build",
"commitSha": "0000000000000000000000000000000000000000",
"cliVersion": "$QLTY_VERSION",
"publishCommand": "debug",
"incomplete": false
}
}
EOF
)
echo "Metadata payload created"
# Step 2: Request upload URLs from the API
echo ""
echo "Step 2: Requesting upload URLs from API..."
API_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $QLTY_COVERAGE_TOKEN" \
-H "User-Agent: qlty/$QLTY_VERSION" \
-d "$METADATA" \
"$API_BASE_URL$COVERAGE_ENDPOINT") || {
echo "Error: Failed to make API request" >&2
exit 1
}
# Extract HTTP status and body
HTTP_STATUS=$(echo "$API_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
RESPONSE_BODY=$(echo "$API_RESPONSE" | sed -E 's/HTTPSTATUS:[0-9]{3}$//')
echo "HTTP Status: $HTTP_STATUS"
if [[ "$HTTP_STATUS" -lt 200 || "$HTTP_STATUS" -ge 300 ]]; then
echo "Error: API request failed with status $HTTP_STATUS" >&2
echo "Response: $RESPONSE_BODY" >&2
exit 1
fi
echo "API Response: $RESPONSE_BODY"
# Step 3: Parse the response data
echo ""
echo "Step 3: Parsing response data..."
UPLOAD_URL=$(echo "$RESPONSE_BODY" | jq -r '.data."coverage.zip"') || {
echo "Error: Failed to parse JSON response" >&2
exit 1
}
UPLOAD_ID=$(echo "$RESPONSE_BODY" | jq -r '.data.id') || {
echo "Error: Failed to parse upload ID from response" >&2
exit 1
}
if [[ "$UPLOAD_URL" == "null" || -z "$UPLOAD_URL" ]]; then
echo "Error: Could not find coverage.zip upload URL in response" >&2
echo "Response: $RESPONSE_BODY" >&2
exit 1
fi
if [[ "$UPLOAD_ID" == "null" || -z "$UPLOAD_ID" ]]; then
echo "Error: Could not find upload ID in response" >&2
echo "Response: $RESPONSE_BODY" >&2
exit 1
fi
echo "Upload ID: $UPLOAD_ID"
echo "Upload URL: $UPLOAD_URL"
# Step 4: Upload the coverage file
echo ""
echo "Step 4: Uploading coverage file..."
UPLOAD_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" \
-X PUT \
-H "Content-Type: application/zip" \
--data-binary "@$COVERAGE_FILE" \
"$UPLOAD_URL") || {
echo "Error: Failed to upload coverage file" >&2
exit 1
}
# Extract HTTP status and body
UPLOAD_HTTP_STATUS=$(echo "$UPLOAD_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
UPLOAD_RESPONSE_BODY=$(echo "$UPLOAD_RESPONSE" | sed -E 's/HTTPSTATUS:[0-9]{3}$//')
echo "Upload HTTP Status: $UPLOAD_HTTP_STATUS"
if [[ "$UPLOAD_HTTP_STATUS" -lt 200 || "$UPLOAD_HTTP_STATUS" -ge 300 ]]; then
echo "Error: Coverage upload failed with status $UPLOAD_HTTP_STATUS" >&2
echo "Response: $UPLOAD_RESPONSE_BODY" >&2
exit 1
fi
echo "Upload successful!"
if [[ -n "$UPLOAD_RESPONSE_BODY" ]]; then
echo "Upload response: $UPLOAD_RESPONSE_BODY"
fi
echo ""
echo "=== Upload Complete ==="
echo "Upload ID: $UPLOAD_ID"
echo "File: $COVERAGE_FILE"
echo "Size: $(wc -c < "$COVERAGE_FILE") bytes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment