<commit_before>#!/bin/bash

set -eu -o pipefail

readonly thisDir=$(cd $(dirname $0); pwd)
readonly parentDir=$(dirname $thisDir)
readonly TOKEN=${ANGULAR_PAYLOAD_FIREBASE_TOKEN:-}
readonly PROJECT_NAME="angular-payload-size"

# temporarily turn on debugging - we disable it later in the script to prevent token leak
set -x

source ${thisDir}/_payload-limits.sh

failed=false
payloadData=""
for filename in dist/*.bundle.js; do
  size=$(stat -c%s "$filename")
  label=$(echo "$filename" | sed "s/.*\///" | sed "s/\..*//")
  payloadData="$payloadData\"uncompressed/$label\": $size, "


  gzip -7 $filename -c >> "${filename}7.gz"
  size7=$(stat -c%s "${filename}7.gz")
  payloadData="$payloadData\"gzip7/$label\": $size7, "

  gzip -9 $filename -c >> "${filename}9.gz"
  size9=$(stat -c%s "${filename}9.gz")
  payloadData="$payloadData\"gzip9/$label\": $size9, "

  if [[ $size -gt ${limitUncompressed[$label]} ]]; then
    failed=true
    echo "Uncompressed $label size is $size which is greater than ${limitUncompressed[$label]}"
  elif [[ $size7 -gt ${limitGzip7[$label]} ]]; then
    failed=true
    echo "Gzip7 $label size is $size7 which is greater than ${limitGzip7[$label]}"
  elif [[ $size9 -gt ${limitGzip9[$label]} ]]; then
    failed=true
    echo "Gzip9 $label size is $size9 which is greater than ${limitGzip9[$label]}"
  fi
done

# Add Timestamp
timestamp=$(date +%s)
payloadData="$payloadData\"timestamp\": $timestamp, "

# Add change source: application, dependencies, or 'application+dependencies'
yarnChanged=false
allChangedFiles=$(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir | wc -l)
allChangedFileNames=$(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir)

if [[ $allChangedFileNames == *"yarn.lock"* ]]; then
  yarnChanged=true
fi

if [[ $allChangedFiles -eq 1 ]] && [[ "$yarnChanged" = true ]]; then
  # only yarn.lock changed
  change='dependencies'
elif [[ $allChangedFiles -gt 1 ]] && [[ "$yarnChanged" = true ]]; then
  change='application+dependencies'
elif [[ $allChangedFiles -gt 0 ]]; then
  change='application'
else
  # Nothing changed in aio/
  exit 0
fi
payloadData="$payloadData\"change\": \"$change\""

payloadData="{${payloadData}}"

echo $payloadData

if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then
  readonly safeBranchName=$(echo $TRAVIS_BRANCH | sed -e 's/\./_/g')
  readonly dbPath=/payload/aio/$safeBranchName/$TRAVIS_COMMIT

  # WARNING: FIREBASE_TOKEN should NOT be printed.
  set +x
  firebase database:update --data "$payloadData" --project $PROJECT_NAME --confirm --token "$TOKEN" $dbPath
fi

if [[ $failed = true ]]; then
  exit 1
fi<commit_msg>ci(aio): fix aio payload script<commit_after>#!/bin/bash

set -eu -o pipefail

readonly thisDir=$(cd $(dirname $0); pwd)
readonly parentDir=$(dirname $thisDir)
readonly PROJECT_NAME="angular-payload-size"

source ${thisDir}/_payload-limits.sh

failed=false
payloadData=""
for filename in dist/*.bundle.js; do
  size=$(stat -c%s "$filename")
  label=$(echo "$filename" | sed "s/.*\///" | sed "s/\..*//")
  payloadData="$payloadData\"uncompressed/$label\": $size, "


  gzip -7 $filename -c >> "${filename}7.gz"
  size7=$(stat -c%s "${filename}7.gz")
  payloadData="$payloadData\"gzip7/$label\": $size7, "

  gzip -9 $filename -c >> "${filename}9.gz"
  size9=$(stat -c%s "${filename}9.gz")
  payloadData="$payloadData\"gzip9/$label\": $size9, "

  if [[ $size -gt ${limitUncompressed[$label]} ]]; then
    failed=true
    echo "Uncompressed $label size is $size which is greater than ${limitUncompressed[$label]}"
  elif [[ $size7 -gt ${limitGzip7[$label]} ]]; then
    failed=true
    echo "Gzip7 $label size is $size7 which is greater than ${limitGzip7[$label]}"
  elif [[ $size9 -gt ${limitGzip9[$label]} ]]; then
    failed=true
    echo "Gzip9 $label size is $size9 which is greater than ${limitGzip9[$label]}"
  fi
done

# Add Timestamp
timestamp=$(date +%s)
payloadData="$payloadData\"timestamp\": $timestamp, "

# Add change source: application, dependencies, or 'application+dependencies'
applicationChanged=false
dependenciesChanged=false
if [[ $(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir | grep -v aio/yarn.lock | grep -v content) ]]; then
  applicationChanged=true
fi
if [[ $(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir/yarn.lock) ]]; then
  dependenciesChanged=true
fi

if $dependenciesChanged && $applicationChanged; then
  change='application+dependencies'
elif $dependenciesChanged; then
  # only yarn.lock changed
  change='dependencies'
elif $applicationChanged; then
  change='application'
else
  # Nothing changed in aio/
  exit 0
fi
payloadData="$payloadData\"change\": \"$change\""

payloadData="{${payloadData}}"

echo $payloadData

if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then
  readonly safeBranchName=$(echo $TRAVIS_BRANCH | sed -e 's/\./_/g')
  readonly dbPath=/payload/aio/$safeBranchName/$TRAVIS_COMMIT

  # WARNING: FIREBASE_TOKEN should NOT be printed.
  set +x
  firebase database:update --data "$payloadData" --project $PROJECT_NAME --confirm --token "$ANGULAR_PAYLOAD_FIREBASE_TOKEN" $dbPath
fi

if [[ $failed = true ]]; then
  exit 1
fi
{'fraction_non_alphanumeric': 0.1671893375147001, 'fraction_numerical': 0.010388083104664837, 'mean_word_length': 4.71444568868981, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7621613', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1703, 'n_words': 452}