Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

If you need to retrieve TGA for several periods of time in a row you can reuse the following sample code

  • This shell script produces a CSV file aggregating several iterations of TGA reports

  • This shell script is written for linux and requires jq installed on your system

  • You must update lines 2-10 with relevant values from your Sealights account and configuration

#!/usr/bin/env bash
DOMAIN="mycompany.sealights.co"
SL_API_TOKEN="123"
APP_NAME="MyApp"
BRANCH_NAME="master"
TeamName="Backend"

Period_StartDate="2020-01-01"
Report_Frequency="2 weeks" #For monthly reports use "1 month"
Report_Iterations="6"

########

CSV_FILENAME="${APP_NAME// /_}-${BRANCH_NAME//[\/ ]/_}"
TARGET_FOLDER="${APP_NAME// /_}"

if [ ! -d $TARGET_FOLDER ]; then
    mkdir -p $TARGET_FOLDER;
fi

PERIOD_FROM=`date -d "${Period_StartDate}"  +%s%3N`
REPORT_MAX=`date +%s%3N`

echo "Retrieving TGA Report details for $APP_NAME (Branch $BRANCH_NAME)" 
echo "> Report Start is ${Period_StartDate} to $PERIOD_FROM"
echo "> Report Frequency is ${Report_Frequency} for ${Report_Iterations} iterations"

if [ -z ${TeamName} ]; then
    echo "> No Group selected"
    
    CSV_FILENAME="${CSV_FILENAME}-NoGroup"
    
    CONFIG_ID=`curl -sX GET "https://$DOMAIN/sl-api/v1/tga/configs" -H "Authorization: Bearer $SL_API_TOKEN" \
      -H "Accept: application/json" | jq -c --arg APP_PARAM "$APP_NAME" --arg BRANCH_PARAM "$BRANCH_NAME" \
      '.data.configs[] | select( (.appName==$APP_PARAM ) and ( .branchName==$BRANCH_PARAM ) and ( .teamName==null ) )' | jq -r .configId`
else  
    echo "> Group $TeamName selected for the Report scope"
    
    CSV_FILENAME="${CSV_FILENAME}-${TeamName}"
    
    CONFIG_ID=`curl -sX GET "https://$DOMAIN/sl-api/v1/tga/configs" -H "Authorization: Bearer $SL_API_TOKEN" \
      -H "Accept: application/json" | jq -c --arg APP_PARAM "$APP_NAME" --arg BRANCH_PARAM "$BRANCH_NAME" --arg TEAM_PARAM "$TeamName"\
      '.data.configs[] | select( (.appName==$APP_PARAM ) and ( .branchName==$BRANCH_PARAM ) and ( .teamName==$TEAM_PARAM ) )' | jq -r .configId`
fi

NUM_OF_CONFIG=`echo "$CONFIG_ID" | wc -w `

if [ $NUM_OF_CONFIG != 1 ]; then
  echo "> $NUM_OF_CONFIG config(s) found for $APP_NAME / $BRANCH_NAME / $TeamName"
  echo "> Ending script."
  exit 1
fi

echo "> Using configId $CONFIG_ID"
echo

#Preparing CSV file 
if [ "$Report_Frequency" = "1 month" ]; then
    CSV_FILENAME="${CSV_FILENAME}-Monthly"
elif [ "$Report_Frequency" = "2 weeks" ]; then
    CSV_FILENAME="${CSV_FILENAME}-BiWeekly"
fi

CSV_FULLPATH_FILE="./$TARGET_FOLDER/$CSV_FILENAME-From_${Period_StartDate}"

echo "Generating CSV report as $CSV_FULLPATH_FILE"
#headers
echo '"StartDate","EndDate","Name","overall/totalMethods","overall/uncoveredMethods","overall/coveredMethods","overall/coverage","modifiedCode/totalMethods","modifiedCode/uncoveredMethods","modifiedCode/coveredMethods","modifiedCode/coverage"'>$CSV_FULLPATH_FILE

# Preparing variables for While loop
REPORT_FROM=${PERIOD_FROM}
REPORT_TO=`date -d "${Period_StartDate}+${Report_Frequency}"  +%s%3N`

i=1
while [ $i -le $Report_Iterations ] && [ $REPORT_FROM -lt $REPORT_MAX ]; do

    if [ $REPORT_TO -gt $REPORT_MAX ]; then
        echo "> Report cannot end in the future."
        REPORT_TO=$REPORT_MAX
    fi
    
    DATE_FROM=`date -d@"$((${REPORT_FROM}/1000))" +"%Y-%m-%d"`
    DATE_TO=`date -d@"$((${REPORT_TO}/1000))"  +"%Y-%m-%d"`

    echo ">> Generate TGA iteration #$i from $DATE_FROM to $DATE_TO"
  
    LINK=`curl -sX GET "https://$DOMAIN/sl-api/v1/tga/reports/$CONFIG_ID/regenerate?periodFrom=$REPORT_FROM&periodTo=$REPORT_TO" \
            -H "Authorization: Bearer $SL_API_TOKEN" -H "Accept: application/json" | jq '.data.statusLink'`
    STATUS="\"In Progress\""
    
    while [[ $STATUS != "\"Ready\"" ]]; do
      echo "Current status ${STATUS}. Sleeping 30 sec..."
      sleep 30
      echo -n "Checking report status..."
      REPORT_JSON=`curl -sX GET "https://$DOMAIN/sl-api/v1/tga/reports?configIds=$CONFIG_ID" \
                -H "Authorization: Bearer $SL_API_TOKEN" -H "Accept: application/json"`
      STATUS=`echo $REPORT_JSON | jq '.data.reports[0].status'`
    done
    
    echo "Report Status: Ready."

    #entire build
    echo $REPORT_JSON | jq --arg DateFrom $DATE_FROM --arg DateTo $DATE_TO -r \
        '.data.reports[0].coverage.entireBuild  | [$DateFrom,$DateTo,"EntireBuild",.overallCoverage.totalMethods,.overallCoverage.uncoveredMethods,.overallCoverage.coveredMethods,.overallCoverage.coverage,.modifiedCodeCoverage.totalMethods,.modifiedCodeCoverage.uncoveredMethods,.modifiedCodeCoverage.coveredMethods,.modifiedCodeCoverage.coverage] | @csv'>>$CSV_FULLPATH_FILE
    #stages
    echo $REPORT_JSON | jq --arg DateFrom $DATE_FROM --arg DateTo $DATE_TO -r \
        '.data.reports[0].coverage.testStages[] | [$DateFrom,$DateTo,.name,.overallCoverage.totalMethods,.overallCoverage.uncoveredMethods,.overallCoverage.coveredMethods,.overallCoverage.coverage,.modifiedCodeCoverage.totalMethods,.modifiedCodeCoverage.uncoveredMethods,.modifiedCodeCoverage.coveredMethods,.modifiedCodeCoverage.coverage] | @csv'>>$CSV_FULLPATH_FILE
    echo "--"
    echo
    
    ((i+=1))
    REPORT_FROM=$REPORT_TO
    REPORT_TO=`date -d "${DATE_TO}+${Report_Frequency}"  +%s%3N`
done

mv $CSV_FULLPATH_FILE "${CSV_FULLPATH_FILE}_To_${DATE_TO}.csv"

echo script completed
exit 0
  • No labels