|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 6 | +PROJECT_DIR="../../$SCRIPT_DIR" |
| 7 | +GLAB="${GLAB:-$PROJECT_DIR/stackgres-k8s/ci/utils/glabw}" |
| 8 | +TARGET_DIR="$SCRIPT_DIR/target" |
| 9 | +STATS_FILE="$SCRIPT_DIR/test.stats" |
| 10 | +ARTIFACT_ZIP="/tmp/job_artifact_$$.zip" |
| 11 | +COLLECTED_DURATIONS="/tmp/collected_durations_$$.txt" |
| 12 | +TEMP_STATS="/tmp/temp_stats_$$.txt" |
| 13 | + |
| 14 | +usage() { |
| 15 | + echo "Usage: $0 <pipeline_id>" |
| 16 | + echo "" |
| 17 | + echo "Updates test.stats file with test durations from CI/CD pipeline artifacts." |
| 18 | + echo "" |
| 19 | + echo "Arguments:" |
| 20 | + echo " pipeline_id GitLab pipeline ID to fetch artifacts from" |
| 21 | + echo "" |
| 22 | + echo "Requirements:" |
| 23 | + echo " - Docker installed" |
| 24 | + exit 1 |
| 25 | +} |
| 26 | + |
| 27 | +# Get duration for a test from collected durations file |
| 28 | +get_collected_duration() { |
| 29 | + local test_name="$1" |
| 30 | + grep "^${test_name}:" "$COLLECTED_DURATIONS" 2>/dev/null | cut -d: -f2 | head -1 |
| 31 | +} |
| 32 | + |
| 33 | +# Check if test exists in collected durations |
| 34 | +has_collected_duration() { |
| 35 | + local test_name="$1" |
| 36 | + grep -q "^${test_name}:" "$COLLECTED_DURATIONS" 2>/dev/null |
| 37 | +} |
| 38 | + |
| 39 | +# Get duration for a test from stats file |
| 40 | +get_stat_duration() { |
| 41 | + local test_name="$1" |
| 42 | + local file="$2" |
| 43 | + grep "^${test_name}:" "$file" 2>/dev/null | cut -d: -f2 | head -1 |
| 44 | +} |
| 45 | + |
| 46 | +# Clean up target directory |
| 47 | +clean_target() { |
| 48 | + rm -rf "$TARGET_DIR" |
| 49 | +} |
| 50 | + |
| 51 | +# Cleanup on exit |
| 52 | +cleanup() { |
| 53 | + rm -f "$ARTIFACT_ZIP" "$COLLECTED_DURATIONS" "$TEMP_STATS" |
| 54 | +} |
| 55 | +trap cleanup EXIT |
| 56 | + |
| 57 | +if [ -z "$1" ]; then |
| 58 | + usage |
| 59 | +fi |
| 60 | + |
| 61 | +PIPELINE_ID="$1" |
| 62 | + |
| 63 | +if ! "$GLAB" auth status > /dev/null 2>&1; then |
| 64 | + echo "Error: glab is not authenticated. Run '$GLAB auth login' first." |
| 65 | + exit 1 |
| 66 | +fi |
| 67 | + |
| 68 | +# Initialize collected durations file |
| 69 | +: > "$COLLECTED_DURATIONS" |
| 70 | + |
| 71 | +echo "Fetching e2e job IDs from pipeline $PIPELINE_ID..." |
| 72 | + |
| 73 | +# Get all e2e job IDs including retried ones |
| 74 | +e2e_jobs=$("$GLAB" api "projects/:id/pipelines/${PIPELINE_ID}/jobs?per_page=100&include_retried=true" 2>/dev/null \ |
| 75 | + | jq -r '.[] | select(.name | contains("e2e")) | .id') |
| 76 | + |
| 77 | +if [ -z "$e2e_jobs" ]; then |
| 78 | + echo "Error: No e2e jobs found in pipeline $PIPELINE_ID" |
| 79 | + exit 1 |
| 80 | +fi |
| 81 | + |
| 82 | +total_jobs=$(echo "$e2e_jobs" | wc -l | tr -d ' ') |
| 83 | +echo "Found $total_jobs e2e jobs" |
| 84 | +echo "" |
| 85 | + |
| 86 | +current=0 |
| 87 | +for job_id in $e2e_jobs; do |
| 88 | + current=$((current + 1)) |
| 89 | + echo "[$current/$total_jobs] Processing job ID: $job_id" |
| 90 | + |
| 91 | + # Clean target directory |
| 92 | + clean_target |
| 93 | + |
| 94 | + # Download artifacts |
| 95 | + rm -f "$ARTIFACT_ZIP" |
| 96 | + if ! "$GLAB" api "projects/:id/jobs/${job_id}/artifacts" 2>/dev/null > "$ARTIFACT_ZIP"; then |
| 97 | + echo " Failed to download artifacts" |
| 98 | + continue |
| 99 | + fi |
| 100 | + |
| 101 | + # Check if it's a valid zip file |
| 102 | + if ! file "$ARTIFACT_ZIP" | grep -q "Zip archive"; then |
| 103 | + echo " No valid artifacts" |
| 104 | + continue |
| 105 | + fi |
| 106 | + |
| 107 | + # Extract artifacts |
| 108 | + if ! unzip -o "$ARTIFACT_ZIP" -d "$SCRIPT_DIR/../.." > /dev/null 2>&1; then |
| 109 | + echo " Failed to extract artifacts" |
| 110 | + continue |
| 111 | + fi |
| 112 | + |
| 113 | + # Find duration files with matching success files |
| 114 | + found=0 |
| 115 | + for duration_file in "$TARGET_DIR"/*.duration; do |
| 116 | + if [ -f "$duration_file" ]; then |
| 117 | + test_name=$(basename "$duration_file" .duration) |
| 118 | + success_file="$TARGET_DIR/${test_name}.success" |
| 119 | + |
| 120 | + if [ -f "$success_file" ]; then |
| 121 | + duration=$(tr -d '[:space:]' < "$duration_file") |
| 122 | + if [ -n "$duration" ]; then |
| 123 | + # Only update if not already set (first match wins - latest run) |
| 124 | + if ! has_collected_duration "$test_name"; then |
| 125 | + echo "${test_name}:${duration}" >> "$COLLECTED_DURATIONS" |
| 126 | + echo " Found: $test_name -> $duration" |
| 127 | + found=$((found + 1)) |
| 128 | + fi |
| 129 | + fi |
| 130 | + fi |
| 131 | + fi |
| 132 | + done |
| 133 | + echo " Total found in this job: $found" |
| 134 | +done |
| 135 | + |
| 136 | +# Clean up target directory |
| 137 | +clean_target |
| 138 | + |
| 139 | +echo "" |
| 140 | +echo "=== Updating $STATS_FILE ===" |
| 141 | + |
| 142 | +# Copy existing stats to temp file, or create empty |
| 143 | +if [ -f "$STATS_FILE" ]; then |
| 144 | + cp "$STATS_FILE" "$TEMP_STATS" |
| 145 | +else |
| 146 | + : > "$TEMP_STATS" |
| 147 | +fi |
| 148 | + |
| 149 | +existing_count=$(wc -l < "$TEMP_STATS" | tr -d ' ') |
| 150 | +echo "Existing entries: $existing_count" |
| 151 | + |
| 152 | +# Update stats with collected durations |
| 153 | +updated=0 |
| 154 | +added=0 |
| 155 | +collected_count=$(wc -l < "$COLLECTED_DURATIONS" | tr -d ' ') |
| 156 | + |
| 157 | +while IFS=: read -r name duration; do |
| 158 | + if [ -n "$name" ]; then |
| 159 | + old_duration=$(get_stat_duration "$name" "$TEMP_STATS") |
| 160 | + if [ -z "$old_duration" ]; then |
| 161 | + echo " Added: $name -> $duration" |
| 162 | + echo "${name}:${duration}" >> "$TEMP_STATS" |
| 163 | + added=$((added + 1)) |
| 164 | + elif [ "$old_duration" != "$duration" ]; then |
| 165 | + echo " Updated: $name: $old_duration -> $duration" |
| 166 | + sed -i "s/^${name}:.*/${name}:${duration}/" "$TEMP_STATS" |
| 167 | + updated=$((updated + 1)) |
| 168 | + fi |
| 169 | + fi |
| 170 | +done < "$COLLECTED_DURATIONS" |
| 171 | + |
| 172 | +# Write sorted output to test.stats |
| 173 | +sort "$TEMP_STATS" > "$STATS_FILE" |
| 174 | + |
| 175 | +final_count=$(wc -l < "$STATS_FILE" | tr -d ' ') |
| 176 | + |
| 177 | +echo "" |
| 178 | +echo "Summary:" |
| 179 | +echo " Tests collected from pipeline: $collected_count" |
| 180 | +echo " New tests added: $added" |
| 181 | +echo " Tests updated: $updated" |
| 182 | +echo " Total entries in test.stats: $final_count" |
0 commit comments