Skip to content

Instantly share code, notes, and snippets.

@jprinet
Created August 25, 2023 14:20
Show Gist options
  • Save jprinet/ba3a1b0801d6d25452b0d54b35155c5c to your computer and use it in GitHub Desktop.
Save jprinet/ba3a1b0801d6d25452b0d54b35155c5c to your computer and use it in GitHub Desktop.
Collect builds having push enabled and remote cache disabled due to an error
#!/bin/bash
# Call with
# ./process-scans-for-remote-cache-errors.sh <GE_URL> <GE_ACCESS_TOKEN> <NB_DAYS_FROM_NOW>
# This will count the build scans belonging to the date range [NOW, NOW - <NB_DAYS_FROM_NOW> days] with push enabled and remote cache disabled due to an error
# init parameters
readonly geUrl=$1
readonly bearerToken=$2
readonly daysAgo=$3
# init global vars
readonly maxBuildsPerBatch=100
readonly recoveryFile="$0.out.build-scan-id"
readonly outputFile="$0.out.txt"
readonly workFile="$0.out.tmp.txt"
fromInstantInMs=0
buildScans=""
buildCount=1
buildWithRemotePushCount=1
#############
# functions #
#############
function getFromInstantByDaysAgo() {
local currentInstant=$(date +%s)
fromInstantInMs=$((${currentInstant}*1000 - ${daysAgo}*60*60*24*1000))
}
function getBuildScansByFilter() {
local filter=$1
buildScans=$(curl --silent "${geUrl}/api/builds?${filter}&maxBuilds=${maxBuildsPerBatch}&reverse=false" --header "authorization: Bearer ${bearerToken}" | jq -r ".[] | [.id,.availableAt,.buildToolType] | @csv")
}
function getBuildScansFromInstant() {
local instant=$1
getBuildScansByFilter "fromInstant=${instant}"
}
function getBuildScansFromBuildId() {
local buildId=$1
getBuildScansByFilter "fromBuild=${buildId}"
}
########
# main #
########
# check if recovery data are present
recoveryBuildScanId=$(cat ${recoveryFile} 2>/dev/null)
if [[ -z "${recoveryBuildScanId}" ]]
then
# Clean output files
rm -f ${outputFile}
rm -f ${workFile}
# Get current timestamp
getFromInstantByDaysAgo
echo "Fetching builds from $(date -d @$(echo ${fromInstantInMs} | head -c 10))"
# Get first batch of builds
getBuildScansFromInstant ${fromInstantInMs}
else
echo "Recovering data from build scan id ${recoveryBuildScanId} (you can trigger a fresh run by deleting ${recoveryFile})"
# Get first batch of builds
getBuildScansFromBuildId ${recoveryBuildScanId}
fi
# Iterate over build scan list
while [ ! -z "${buildScans}" ]
do
buildScanId=""
for buildScan in ${buildScans}; do
# Remove "
buildScan=$(echo "${buildScan}" | sed -e 's/"//g')
# Parse fields
buildScanId=$(echo "${buildScan}" | cut -d, -f 1)
buildScanDate=$(echo "${buildScan}" | cut -d, -f 2)
buildTool=$(echo "${buildScan}" | cut -d, -f 3)
echo "Build ${buildCount}: Processing ${buildScanId} (${buildTool}) published at $(date -d @$(echo ${buildScanDate} | head -c 10))"
# save build scan id for recovery mode
echo "${buildScanId}" > ${recoveryFile}
buildData=""
# Check build tool
if [[ "${buildTool}" == "gradle" ]]
then
# Get Git repository
buildData=$(curl --silent ${geUrl}/api/builds/${buildScanId}/gradle-build-cache-performance --header "authorization: Bearer ${bearerToken}" | jq -r '[.buildCaches.remote.isPushEnabled,.buildCaches.remote.isDisabledDueToError] | @csv')
elif [[ "${buildTool}" == "maven" ]]
then
# Get Git repository
buildData=$(curl --silent "${geUrl}/api/builds/${buildScanId}/maven-build-cache-performance" --header "authorization: Bearer ${bearerToken}" | jq -r '[.buildCaches.remote.isPushEnabled,.buildCaches.remote.isDisabledDueToError] | @csv')
fi
# Parse fields
isPushEnabled=$(echo "${buildData}" | cut -d, -f 1)
isDisabledDueToError=$(echo "${buildData}" | cut -d, -f 2)
echo "${buildData}"
echo "${isPushEnabled},${isDisabledDueToError}"
if [[ "${isPushEnabled}" == "true" ]]
then
if [[ "${isDisabledDueToError}" == "true" ]]
then
# append build to file
echo "Adding $buildDataId"
echo ${buildScanId} >> $0.out.tmp.txt
fi
buildWithRemotePushCount=$((buildWithRemotePushCount+1))
fi
buildCount=$((buildCount+1))
done
getBuildScansFromBuildId ${buildScanId}
done
# Remove recovery data
rm -f ${recoveryFile}
errorCount=0
if [[ -f "${outputFile}" ]]
then
errorCount=$(wc -l "${outputFile}")
fi
# Display results
echo ""
echo "${errorCount}/${buildWithRemotePushCount} builds found with push enabled and remote cache disabled due to an error"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment