1#!/bin/bash
2set -e
3
4function usage() {
5  echo "Usage: $0 <buildId> <target> [<numBuilds>]"
6  echo "Fetches the build number log for <buildId> <target> and reruns the last <numBuilds> of that target that happened on the corresponding server"
7  exit 1
8}
9
10buildId="$1"
11target="$2"
12if [ "$buildId" == "" ]; then
13  usage
14fi
15if [ "$target" == "" ]; then
16  usage
17fi
18numBuilds="$3"
19if [ "$numBuilds" == "" ]; then
20  numBuilds="2"
21fi
22
23cd "$(dirname $0)/.."
24
25function cleanup() {
26  ./gradlew --stop || true
27  rm ../../out -rf
28  mkdir -p ../../out
29}
30cleanup
31
32function echoAndDo() {
33  echo "$*"
34  eval "$*"
35}
36
37function fetch_artifact() {
38  fetchBuildId="$1"
39  remotePath="$2"
40  localDir="$3"
41  cd "$localDir"
42  echoAndDo /google/data/ro/projects/android/fetch_artifact --bid "$fetchBuildId" --target "$target" "$remotePath"
43  cd -
44}
45function downloadBuildNumberLog() {
46  if [ "$target" == "androidx_incremental" ]; then
47    buildNumberLog=incremental/build_number.log
48  else
49    buildNumberLog=build_number.log
50  fi
51  echo Downloading build number history for build $buildId
52  fetch_artifact "$buildId" "$buildNumberLog" ../../out
53}
54downloadBuildNumberLog
55
56buildNumberHistory="$(cat ../../out/build_number.log | tail -n "$numBuilds")"
57echo Will run $target for sources from these builds: $buildNumberHistory .
58
59function checkoutSources() {
60  checkoutBuildId="$1"
61  echo checking out sources for build $checkoutBuildId
62
63  fetch_artifact "$checkoutBuildId" BUILD_INFO ../../out/
64  superprojectRevision="$(jq -r ".parsed_manifest.superproject.revision" ../../out/BUILD_INFO)"
65
66  echo checking out superproject revision $superprojectRevision
67
68  cd ../../
69  git fetch origin "$superprojectRevision"
70  git checkout "$superprojectRevision"
71  git submodule update
72  cd -
73  echo done checking out sources for build $checkoutBuildId
74}
75function runBuild() {
76  bash "./busytown/$target.sh"
77}
78
79for previousBuildId in $buildNumberHistory; do
80  checkoutSources $previousBuildId
81  if runBuild; then
82    echo build of $previousBuildId succeeded
83  else
84    echo build of $previousBuildId failed
85    exit 1
86  fi
87done
88