1#!/bin/bash
2set -e
3
4buildId="$1"
5target="$2"
6sendUpToDateScan=false
7
8function usage() {
9  echo "usage: $0 <buildId> <target> [--include-up-to-date]"
10  echo
11  echo "Downloads build scan information for the corresponding build and uploads it to the enterprise server configured in settings.gradle"
12  echo
13  echo "  --include-up-to-date Also upload scan-up-to-date.zip, the scan of the second build which should be mostly UP-TO-DATE"
14  exit 1
15}
16
17if [ "$buildId" == "" ]; then
18  usage
19fi
20
21if [ "$target" == "" ]; then
22  usage
23fi
24
25if [ "$3" != "" ]; then
26  if [ "$3" == "--include-up-to-date" ]; then
27    sendUpToDateScan=true
28  else
29    usage
30  fi
31fi
32# find scan dir
33if [ -z "${OUT_DIR+x}" ] ; then
34  SCRIPT_PATH="$(cd $(dirname $0) && pwd -P)"
35  export OUT_DIR=$SCRIPT_PATH/../../../out
36fi
37effectiveGradleUserHome="$OUT_DIR/.gradle"
38scanDir="$effectiveGradleUserHome/build-scan-data"
39
40function downloadScan() {
41  filename="$1"
42  echo downloading build scan from $buildId $target
43  if [ "$target" == "androidx_incremental" ]; then
44    downloadPath="incremental/$filename"
45  else
46    downloadPath="$filename"
47  fi
48  cd /tmp
49  /google/data/ro/projects/android/fetch_artifact --bid $buildId --target $target "$downloadPath"
50  cd -
51}
52
53function unzipScan() {
54  filename="$1"
55  echo
56  echo unzipping build scan
57  rm -rf "$scanDir"
58  unzip -q /tmp/"$filename" -d "$scanDir"
59}
60
61function uploadScan() {
62  log="$scanDir/upload-failure.log"
63  rm -f "$log"
64  echo
65  echo uploading build scan
66  ./gradlew :buildScanPublishPrevious
67  sleep 2
68  if cat "$log" 2>/dev/null; then
69    echo upload failed
70  fi
71}
72
73function sendScan() {
74  filename="$1"
75  downloadScan "$filename"
76  unzipScan "$filename"
77  uploadScan
78}
79
80sendScan scan.1.zip
81echo uploaded scan
82if [ "$sendUpToDateScan" == "true" ]; then
83  sendScan scan-up-to-date.zip
84  echo uploaded scan of second, up-to-date build
85fi
86