/
Generating a Trend Report (TGA Report over recent sprints)

Generating a Trend Report (TGA Report over recent sprints)

If you need to retrieve TGA for several periods 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 4-13 with relevant values from your Sealights account and configuration

The output is a CSV file that looks like this

image-20240708-112452.png

In the script below you have the option to download the detailed coverage for each TGA report (uncomment line 214)

#!/usr/bin/env bash # Copyright Sealights 2024 DOMAIN="mycompany.sealights.co" SL_API_TOKEN="123" APP_NAME="MyApp" BRANCH_NAME="master" #CategoryName="Teams" #LabelName="Backend" Period_StartDate="2024-05-15" Report_Frequency="2 weeks" #For monthly reports use "1 month" Report_Iterations="3" ######## FUNCTIONS ######## getPartialCSVFilePath() { CSV_FILENAME="${APP_NAME// /_}-${BRANCH_NAME//[\/ ]/_}" TARGET_FOLDER="${APP_NAME// /_}" if [ ! -d $TARGET_FOLDER ]; then mkdir -p $TARGET_FOLDER; fi if [ -z "${LabelName}" ] && [ -z "${CategoryName}" ]; then CSV_FILENAME="${CSV_FILENAME}-NoLabel" else CSV_FILENAME="${CSV_FILENAME}-${CategoryName}${LabelName}" fi #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 echo "./$TARGET_FOLDER/$CSV_FILENAME-From_${Period_StartDate}" } rawurlencode() { local string="${1}" local strlen=${#string} local encoded="" local pos c o #>&2 echo "encode $string" for (( pos=0 ; pos<strlen ; pos++ )); do c=${string:$pos:1} case "$c" in [-_.~a-zA-Z0-9] ) o="${c}" ;; *) printf -v o '%%%02x' "'$c" esac encoded+="${o}" done echo "${encoded}" # You can either set a return variable (FASTER) REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p } encode_array_for_curl() { local comma_separated_list="${1}" local json_array local encoded_array # Convert comma-separated list to JSON array json_array=$(echo "${comma_separated_list}" | jq -R 'split(",")') # URL encode the JSON array encoded_array=$(echo "${json_array}" | jq -sRr @uri) echo "${encoded_array}" # You can either set a return variable (FASTER) REPLY="${encoded_array}" #+or echo the result (EASIER)... or both... :p } waitForReportReady() { local REPORT_ID=$1 for ((retries=0;retries<600;retries++)); do local TGA_DETAILS=$(curl -s -X GET "https://${DOMAIN}/sl-api/v1/tga/report-templates/reports/${REPORT_ID}" -H 'accept: application/csv' -H "Authorization: Bearer $SL_API_TOKEN") local STATUS=$(echo ${TGA_DETAILS} | jq -r .data.status) if [[ "${STATUS}" != "In Progress" ]]; then break fi >&2 echo ... sleep 10 done if [[ "${STATUS}" == "Ready" ]]; then echo ${TGA_DETAILS} | jq .data else >&2 echo "Report ${REPORT_ID} is not Ready, its status is '${STATUS}'" exit 1 fi } updateTGAReport() { local REPORT_ID=$1 local PERIOD_FROM=$2 local PERIOD_TO=$3 local DATA="{\"range\":{\"dates\":{\"from\":${PERIOD_FROM},\"to\":${PERIOD_TO}}}}" local RESPONSE=$(curl -s -X PUT "https://${DOMAIN}/sl-api/v1/tga/report-templates/reports/${REPORT_ID}" -H 'accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer $SL_API_TOKEN" -H "Accept: application/json" -d "${DATA}")