Generating TGA Report for an Integration Build
TGA report cannot be generated for an Integration Build via the Sealights dashboard, the sample code below allows you to do so via the Public APIs
- This shell script produces a CSV file aggregating every “entire build statistics” from the integration build’s components - it uses the list of components from the latest build of the period requested and does not take in account addition/removal of components 
 
- This shell script is written for - linuxand requires- jqinstalled on your system
- You must update lines 2-8 with relevant values from your Sealights account and configuration 
#!/bin/bash
DOMAIN="mycompany.sealights.co"
SL_API_TOKEN="123"
APP_NAME="MyIntegrationBuild"
BRANCH_NAME="master"
Period_StartDate="2020-06-01"
Report_Frequency="2 weeks" #For monthly reports use "1 month"
SLEEP_TIME=30
## set indexes
I_StageName=1
I_OverallTotal=2;I_OverallUncovered=3;I_OverallCovered=4;I_OverallCoveragePerc=5
I_ModifiedTotal=6;I_ModifiedUncovered=7;I_ModifiedCovered=8;I_ModifiedCoveragePerc=9
###
PERIOD_FROM=`date -d "${Report_StartDate}"  +%s%3N`
PERIOD_TO=`date -d "${Report_StartDate}+${Report_Duration}"  +%s%3N`
echo
echo "Retrieving TGA Report details for $APP_NAME (Branch $BRANCH_NAME)" 
echo "> Report Start is ${Report_StartDate} for ${Report_Duration}"
### Check if App is Integration Build
## Get list of builds for the period
APP_BSID=`curl -sX GET "https://$DOMAIN/sl-api/v1/slim-builds/resolve?appName=$APP_NAME&branchName=$BRANCH_NAME&periodFrom=$PERIOD_FROM&periodTo=$PERIOD_TO" \
  -H "Authorization: Bearer $SL_API_TOKEN" \
  -H "Accept: application/json" | jq .data.builds[-1].bsid -r `
APP_DETAILS=`curl -sX GET "https://$DOMAIN/sl-api/v1/builds/$APP_BSID" \
  -H "Authorization: Bearer $SL_API_TOKEN" \
  -H "Accept: application/json" `
IS_INTEGRATION_BUILD=`echo $APP_DETAILS | jq .data.build.isIntegration -r`
COMPONENTS_JSON=`echo $APP_DETAILS | jq .data.components -r`
NUM_OF_COMPONENTS=`echo $COMPONENTS_JSON | jq '. | length' `
if [[ ! $IS_INTEGRATION_BUILD = "true" ]]; then
	echo "> Application is not an Integration Build. Ending script."
    exit 1
else
	echo "> Application is confirmed as an Integration Build with $NUM_OF_COMPONENTS components."
fi
main_arr=()
i=0
while [[ $i -lt $NUM_OF_COMPONENTS ]]; do
	COMP_NAME=`echo $COMPONENTS_JSON | jq .[$i].appName -r`
	COMP_BRANCH=`echo $COMPONENTS_JSON | jq .[$i].branchName -r`
    echo
    echo "Retrieving TGA data for component $(($i+1)) / $NUM_OF_COMPONENTS - appName: $COMP_NAME"
    echo "> branchName: $COMP_BRANCH, teamName: (no-group)"
    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 "$COMP_NAME" --arg BRANCH_PARAM "$COMP_BRANCH" \
      '.data.configs[] | select( (.appName==$APP_PARAM ) and ( .branchName==$BRANCH_PARAM ) and ( .teamName==null ) )' | jq -r .configId`
	NUM_OF_CONFIG=`echo "$CONFIG_ID" | wc -w `
    
    if [ $NUM_OF_CONFIG != 1 ]; then
      echo "> $NUM_OF_CONFIG config(s) found for $COMP_NAME / $COMP_BRANCH / no-group"
      echo "> Please make sure a single TGA report already exists (in Settings) for every component."
      # There is a Public API to create a configuration for every app-branch-team combination.
      echo "> Skipping component."
      ((i+=1))
      continue
    fi
    
    echo "> Using configId $CONFIG_ID"
    echo -n "> "
    LINK=`curl -sX GET "https://$DOMAIN/sl-api/v1/tga/reports/$CONFIG_ID/regenerate?periodFrom=$PERIOD_FROM&periodTo=$PERIOD_TO" \
            -H "Authorization: Bearer $SL_API_TOKEN" -H "Accept: application/json" | jq '.data.statusLink'`
    STATUS="\"In Progress\""
    
    while [[ $STATUS != "\"Ready\"" && ${STATUS} != "null" ]]; do
      echo "Current status ${STATUS}. Sleeping ${SLEEP_TIME} sec..."
      sleep $SLEEP_TIME
      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: ${STATUS}."
    if  [[ ${STATUS} = "null" ]]; then
    	echo "RESPONSE: ${REPORT_JSON}"
	fi
	(( main_arr[$I_OverallTotal] += `echo $REPORT_JSON | jq '.data.reports[0].coverage.entireBuild.overallCoverage.totalMethods' -r` ))
	(( main_arr[$I_OverallUncovered] += `echo $REPORT_JSON | jq '.data.reports[0].coverage.entireBuild.overallCoverage.uncoveredMethods' -r` ))
	(( main_arr[$I_OverallCovered] += `echo $REPORT_JSON | jq '.data.reports[0].coverage.entireBuild.overallCoverage.coveredMethods' -r` ))
	(( main_arr[$I_ModifiedTotal] += `echo $REPORT_JSON | jq '.data.reports[0].coverage.entireBuild.modifiedCodeCoverage.totalMethods' -r` ))
	(( main_arr[$I_ModifiedUncovered] += `echo $REPORT_JSON | jq '.data.reports[0].coverage.entireBuild.modifiedCodeCoverage.uncoveredMethods' -r` ))
	(( main_arr[$I_ModifiedCovered] += `echo $REPORT_JSON | jq '.data.reports[0].coverage.entireBuild.modifiedCodeCoverage.coveredMethods' -r` ))    
	
    ((i+=1))
done
main_arr[$I_StageName]="entireBuild"
main_arr[$I_OverallCoveragePerc]="$( bc <<< "scale=2; ${main_arr[$I_OverallCovered]}/${main_arr[$I_OverallTotal]} ")"
main_arr[$I_ModifiedCoveragePerc]="$( bc <<< "scale=2; ${main_arr[$I_ModifiedCovered]}/${main_arr[$I_ModifiedTotal]} ")"
####
CSV_FILE="TGA-${AppName// /_}-IntegrBuild-"`date +"%Y%m%d_%H%M%S"`".csv"
#CSV_FILE="${AppName// /_}-IntegrBuild-${BRANCH_NAME//[\/ ]/_}-NoGroup-From_${Report_StartDate}.csv"
#Generate CSV file
echo "StageName,overall/totalMethods,overall/uncoveredMethods,overall/coveredMethods,overall/coverage,modifiedCode/totalMethods,modifiedCode/uncoveredMethods,modifiedCode/coveredMethods,modifiedCode/coverage"> ${CSV_FILE}
( IFS=$',';echo "${main_arr[*]}" >> ${CSV_FILE})
echo script completed
exit 0If running on Mac OS, the date command syntax is slightly different from regular Linux. Below is a Mac OS equivalent syntax for line 18 above.
PERIOD_FROM=`date -j -f "%Y-%m-%d %H:%M" "2020-06-01 00:00" +%s000`