|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Usage |
| 4 | +# get_affected_targets.sh [-b <bazel_path>] [-f <output_path>] [-r <revision>] [-w <workspace path>] |
| 5 | +# |
| 6 | +# Flags |
| 7 | +# -b: specify path to bazel |
| 8 | +# -f: specify output file path |
| 9 | +# -r: specify the revision to use, default the current |
| 10 | +# -w: specify the workspace_path, default $(cwd) |
| 11 | +# |
| 12 | +# Notes: |
| 13 | +# This script relies on bazel-diff, which is installed in WORKSPACE via L6-12 of WORKSPACE file. |
| 14 | +# |
| 15 | +# Action |
| 16 | +# - Get affected targets list in our repo to the output_path file |
| 17 | + |
| 18 | +set -o pipefail |
| 19 | +set -u |
| 20 | +PROG=$0 |
| 21 | + |
| 22 | +help() { |
| 23 | + local exit_code=$1 |
| 24 | + echo "Usage: ${PROG} [-b <bazel_path>] [-f <output_path>] [-r <revision>] [-w <workspace>]" |
| 25 | + exit "${exit_code}" |
| 26 | +} |
| 27 | + |
| 28 | +echo "Running ${PROG}" |
| 29 | + |
| 30 | +bazel="bazel" |
| 31 | +current_revision=$(git rev-parse HEAD) |
| 32 | +pr_revision=${current_revision} |
| 33 | +output_path="/tmp/affected_targets/targets" |
| 34 | +workspace_path=$(pwd) |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +while getopts "b:f:r:w:h" opt; do |
| 39 | + case "${opt}" in |
| 40 | + b) |
| 41 | + bazel=${OPTARG} |
| 42 | + ;; |
| 43 | + f) |
| 44 | + output_path=${OPTARG} |
| 45 | + ;; |
| 46 | + r) |
| 47 | + pr_revision=${OPTARG} |
| 48 | + ;; |
| 49 | + w) |
| 50 | + workspace_path=${OPTARG} |
| 51 | + ;; |
| 52 | + h) |
| 53 | + help 0 |
| 54 | + ;; |
| 55 | + :) |
| 56 | + help 1 |
| 57 | + ;; |
| 58 | + ?) |
| 59 | + help 1 |
| 60 | + ;; |
| 61 | + esac |
| 62 | +done |
| 63 | + |
| 64 | +working_dir=$(mktemp -d "/tmp/tmp_XXXXX") |
| 65 | +trap 'rm -rf "${working_dir}"' EXIT |
| 66 | + |
| 67 | +starting_hashes_json="${working_dir}/starting_hashes.json" |
| 68 | +final_hashes_json="${working_dir}/final_hashes.json" |
| 69 | +impacted_targets_path="${working_dir}/impacted_targets.txt" |
| 70 | +bazel_diff="${working_dir}/bazel_diff" |
| 71 | + |
| 72 | +"${bazel}" run :bazel-diff --script_path="${bazel_diff}" |
| 73 | + |
| 74 | +git -C "${workspace_path}" checkout "${pr_revision}" --quiet |
| 75 | + |
| 76 | +echo "Generating Hashes for Revision '${pr_revision}'" |
| 77 | + |
| 78 | +"${bazel_diff}" generate-hashes -w "$workspace_path" -b "${bazel}" "${starting_hashes_json}" |
| 79 | + |
| 80 | +MERGE_BASE_MAIN=$(git merge-base "${pr_revision}" main) |
| 81 | +git -C "${workspace_path}" checkout "${MERGE_BASE_MAIN}" --quiet |
| 82 | + |
| 83 | +echo "Generating Hashes for merge base ${MERGE_BASE_MAIN}" |
| 84 | + |
| 85 | +$bazel_diff generate-hashes -w "${workspace_path}" -b "${bazel}" "${final_hashes_json}" |
| 86 | + |
| 87 | +echo "Determining Impacted Targets and output to ${output_path}" |
| 88 | +$bazel_diff get-impacted-targets -sh "${starting_hashes_json}" -fh "${final_hashes_json}" -o "${impacted_targets_path}" |
| 89 | + |
| 90 | +filter_query_rules_file="${working_dir}/filter_query_rules" |
| 91 | + |
| 92 | +# -- Begin of Query Rules Heredoc -- |
| 93 | +cat > "${filter_query_rules_file}" << EndOfMessage |
| 94 | +let raw_targets = set($(<"${impacted_targets_path}")) in |
| 95 | + \$raw_targets - kind('source file', \$raw_targets) - filter('//external[:/].*', \$raw_targets) |
| 96 | +EndOfMessage |
| 97 | +# -- End of Query Rules Heredoc -- |
| 98 | + |
| 99 | +"${bazel}" query --query_file="${filter_query_rules_file}" >"${output_path}" |
| 100 | + |
| 101 | +git -C "${workspace_path}" checkout "${current_revision}" --quiet |
0 commit comments