Created
May 10, 2022 09:16
-
-
Save purkhusid/1812a777cfda54b44b0f231670d0ae64 to your computer and use it in GitHub Desktop.
Bazel diff script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
function get_affected_targets_path() { | |
echo "/tmp/bazel_diff_affected_targets" | |
} | |
function get_affected_targets() { | |
#Path to your Bazel WORKSPACE directory | |
local workspace_path=$1 | |
# Path to your Bazel executable | |
local bazel_path=$2 | |
# Starting Revision SHA | |
local previous_revision=$3 | |
# Final Revision SHA | |
local final_revision=$4 | |
starting_hashes_json="/tmp/bazel_diff_${previous_revision}_hashes.json" | |
final_hashes_json="/tmp/bazel_diff_${final_revision}_hashes.json" | |
affected_targets_path=$(get_affected_targets_path) | |
bazel_client_options=(--client_debug --output_base=${WORKSPACE_DIR}/bazel_output_base) | |
bazel_command_options=(--config=ci) | |
bazel_diff="/tmp/bazel_diff" | |
$bazel_path "${bazel_client_options[@]}" run :bazel-diff "${bazel_command_options[@]}" --script_path="$bazel_diff" | |
git -C "$workspace_path" checkout "$previous_revision" --quiet | |
echo "Generating Hashes for Revision '$previous_revision'" | |
$bazel_diff generate-hashes -so "${bazel_client_options[*]}" -co "${bazel_command_options[*]}" -w "$workspace_path" -b "$bazel_path" "$starting_hashes_json" | |
git -C "$workspace_path" checkout - --quiet | |
echo "Generating Hashes for Revision '$final_revision'" | |
$bazel_diff generate-hashes -so "${bazel_client_options[*]}" -co "${bazel_command_options[*]}" -w "$workspace_path" -b "$bazel_path" "$final_hashes_json" | |
echo "Determining Affected Targets" | |
$bazel_diff -so "${bazel_client_options[*]}" -co "${bazel_command_options[*]}" -sh "$starting_hashes_json" -fh "$final_hashes_json" -w "$workspace_path" -b "$bazel_path" -o "$affected_targets_path" | |
echo "Affected targets:" | |
cat "$affected_targets_path" | |
} | |
# Example usage: query_affected_targets /path/to/file "kind (rust_library rule, AFFECTED_TARGETS)" /tmp/output | |
# This will replace the AFFECTED_TARGETS string with the actual affected targets in the file from the first parameter | |
function query_affected_targets() { | |
local affected_targets_path="${1}" | |
local query_pattern="${2}" | |
local output_path="${3}" | |
if [[ ! -f "${affected_targets_path}" ]]; then | |
echo "affected targets file does not exist" | |
exit 1 | |
fi | |
if ! grep -q '[^[:space:]]' "${affected_targets_path}"; then | |
touch "${output_path}" | |
else | |
affected_targets=$(awk '{printf("\"%s\"\n", $0);}' "${affected_targets_path}" | awk '{print}' ORS=' + ' | sed '$s/...$//') | |
query_file_path="${output_path}_query" | |
replaced_query="${query_pattern//AFFECTED_TARGETS/${affected_targets}}" | |
echo "${replaced_query}" > "${query_file_path}" | |
bazel --client_debug --output_base=${WORKSPACE_DIR}/bazel_output_base query --config=ci --query_file="${query_file_path}" > "${output_path}" | |
fi | |
} | |
function get_affected_test_targets_path() { | |
echo "/tmp/bazel_diff_affected_tests" | |
} | |
function get_affected_test_targets() { | |
local affected_targets_path="${1}" | |
query_affected_targets "${affected_targets_path}" "kind(test, AFFECTED_TARGETS) except attr(tags, manual, AFFECTED_TARGETS)" "$(get_affected_test_targets_path)" | |
} | |
function get_affected_dev_artifact_targets_path() { | |
echo "/tmp/bazel_diff_affected_dev_artifacts" | |
} | |
function get_affected_dev_artifact_targets() { | |
local affected_targets_path="${1}" | |
query_affected_targets "${affected_targets_path}" "attr(tags, upload_artifacts_dev, AFFECTED_TARGETS)" "$(get_affected_dev_artifact_targets_path)" | |
} | |
function get_affected_prod_artifact_targets_path() { | |
echo "/tmp/bazel_diff_affected_prod_artifacts" | |
} | |
function get_affected_prod_artifact_targets() { | |
local affected_targets_path="${1}" | |
query_affected_targets "${affected_targets_path}" "attr(tags, upload_artifacts_prod, AFFECTED_TARGETS)" "$(get_affected_prod_artifact_targets_path)" | |
} | |
function get_affected_deployment_pipeline_targets_path() { | |
echo "/tmp/bazel_diff_affected_deployment_pipelines" | |
} | |
function get_affected_deployment_pipeline_targets() { | |
local affected_targets_path="${1}" | |
query_affected_targets "${affected_targets_path}" "kind(\"tekton_deployment rule\", AFFECTED_TARGETS)" "$(get_affected_deployment_pipeline_targets_path)" | |
} | |
function get_affected_deployment_app_targets_path() { | |
echo "/tmp/bazel_diff_affected_deployment_apps" | |
} | |
function get_affected_deployment_app_targets() { | |
local affected_targets_path="${1}" | |
query_affected_targets "${affected_targets_path}" "kind(\"deployment_app rule\", AFFECTED_TARGETS)" "$(get_affected_deployment_app_targets_path)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment