Skip to content

Commit

Permalink
feat: Pipe Bazel Startup Options (#22)
Browse files Browse the repository at this point in the history
Support plumbing Bazel startup options into `bazel-diff`.

Also: rename the e2e tests --> compute_impacted_targets tests, they
aren't e2e.

Closes
https://linear.app/trunk/issue/TRUNK-8646/plumb-bazel-startup-options-into-merge-action

---------

Co-authored-by: Phil Vendola <[email protected]>
  • Loading branch information
nikhilbirmiwal and pv72895 authored Aug 30, 2023
1 parent 42a6391 commit f3e14b2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ permissions: read-all
jobs:
tests:
runs-on: ubuntu-latest
name: E2E Tests
name: Compute Impacted Targets Tests
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -30,6 +30,7 @@ jobs:
PR_BRANCH: do_not_delete/stable_test_branch
VERBOSE: 1
WORKSPACE_PATH: ./tests/simple_bazel_workspace
BAZEL_STARTUP_OPTIONS: --host_jvm_args=-Xmx12G,--block_for_lock

- name: Validate Impacted Targets Computation
shell: bash
Expand Down
7 changes: 7 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ inputs:
verbose:
description: Whether to enable verbose logging. Defaults to false.
required: false
bazel-startup-options:
description:
A comma separated list of startup options to pass to Bazel. See
https://bazel.build/reference/command-line-reference#startup-options for a complete list. If
unspecified, no startup options are specified.
required: false

runs:
using: composite
Expand Down Expand Up @@ -55,6 +61,7 @@ runs:
PR_BRANCH: ${{ github.head_ref }}
VERBOSE: ${{ inputs.verbose }}
WORKSPACE_PATH: ${{ steps.prerequisites.outputs.workspace_path }}
BAZEL_STARTUP_OPTIONS: ${{ inputs.bazel-startup-options }}

- name: Upload Impacted Targets
run: ${GITHUB_ACTION_PATH}/src/scripts/upload_impacted_targets.sh
Expand Down
81 changes: 46 additions & 35 deletions src/scripts/compute_impacted_targets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

set -euo pipefail

if [[ (-z ${MERGE_INSTANCE_BRANCH}) || (-z ${PR_BRANCH}) ]]; then
echo "Missing branch"
exit 2
fi

if [[ -z ${WORKSPACE_PATH} ]]; then
echo "Missing workspace path"
exit 2
fi

ifVerbose() {
if [[ -n ${VERBOSE} ]]; then
"$@"
Expand All @@ -13,6 +23,13 @@ logIfVerbose() {
ifVerbose echo "$(date -u)" "$@"
}

# If specified, parse the Bazel startup options when generating hashes.
bazel_startup_options=""
if [[ -n ${BAZEL_STARTUP_OPTIONS} ]]; then
bazel_startup_options=$(echo "${BAZEL_STARTUP_OPTIONS}" | tr ',' ' ')
fi
logIfVerbose "Bazel startup options" "${bazel_startup_options}"

bazelDiff() {
if [[ -n ${VERBOSE} ]]; then
java -jar bazel-diff.jar "$@" --verbose
Expand All @@ -30,49 +47,43 @@ fetchRemoteGitHistory() {
logIfVerbose "...done!"
}

if [[ (-z ${MERGE_INSTANCE_BRANCH}) || (-z ${PR_BRANCH}) ]]; then
echo "Missing branch"
exit 2
fi

if [[ -z ${WORKSPACE_PATH} ]]; then
echo "Missing workspace path"
exit 2
fi

fetchRemoteGitHistory "${MERGE_INSTANCE_BRANCH}"
fetchRemoteGitHistory "${PR_BRANCH}"

# Install the bazel-diff JAR. Avoid cloning the repo, as there will be conflicting WORKSPACES.
curl --retry 5 -Lo bazel-diff.jar https://github.com/Tinder/bazel-diff/releases/latest/download/bazel-diff_deploy.jar
java -jar bazel-diff.jar -V
bazel --version
## Verbose logging for the Merge Instance and PR branch.
if [[ -n ${VERBOSE} ]]; then
git switch "${MERGE_INSTANCE_BRANCH}"
merge_instance_branch_head_sha=$(git rev-parse "${MERGE_INSTANCE_BRANCH}")
echo "Merge Instance Branch Head= ${merge_instance_branch_head_sha}"

git switch "${MERGE_INSTANCE_BRANCH}"
merge_instance_branch_head_sha=$(git rev-parse "${MERGE_INSTANCE_BRANCH}")
logIfVerbose "Merge Instance Branch Head= ${merge_instance_branch_head_sha}"
git switch "${PR_BRANCH}"
pr_branch_head_sha=$(git rev-parse "${PR_BRANCH}")
echo "PR Branch Head= ${pr_branch_head_sha}"

git switch "${PR_BRANCH}"
pr_branch_head_sha=$(git rev-parse "${PR_BRANCH}")
logIfVerbose "PR Branch Head= ${pr_branch_head_sha}"
# Find the merge base of the two branches
merge_base_sha=$(git merge-base "${merge_instance_branch_head_sha}" "${pr_branch_head_sha}")
echo "Merge Base= ${merge_base_sha}"

# Find the merge base of the two branches
merge_base_sha=$(git merge-base "${merge_instance_branch_head_sha}" "${pr_branch_head_sha}")
logIfVerbose "Merge Base= ${merge_base_sha}"
# Find the number of commits between the merge base and the merge instance's HEAD
merge_instance_depth=$(git rev-list "${merge_base_sha}".."${merge_instance_branch_head_sha}" | wc -l)
echo "Merge Instance Depth= ${merge_instance_depth}"

# Find the number of commits between the merge base and the merge instance's HEAD
merge_instance_depth=$(git rev-list "${merge_base_sha}".."${merge_instance_branch_head_sha}" | wc -l)
logIfVerbose "Merge Instance Depth= ${merge_instance_depth}"
git switch "${MERGE_INSTANCE_BRANCH}"
git log -n "${merge_instance_depth}" --oneline

git switch "${MERGE_INSTANCE_BRANCH}"
ifVerbose git log -n "${merge_instance_depth}" --oneline
# Find the number of commits between the merge base and the PR's HEAD
pr_depth=$(git rev-list "${merge_base_sha}".."${pr_branch_head_sha}" | wc -l)
echo "PR Depth= ${pr_depth}"

# Find the number of commits between the merge base and the PR's HEAD
pr_depth=$(git rev-list "${merge_base_sha}".."${pr_branch_head_sha}" | wc -l)
logIfVerbose "PR Depth= ${pr_depth}"
git switch "${PR_BRANCH}"
git log -n "${pr_depth}" --oneline
fi

git switch "${PR_BRANCH}"
ifVerbose git log -n "${pr_depth}" --oneline
# Install the bazel-diff JAR. Avoid cloning the repo, as there will be conflicting WORKSPACES.
curl --retry 5 -Lo bazel-diff.jar https://github.com/Tinder/bazel-diff/releases/latest/download/bazel-diff_deploy.jar
java -jar bazel-diff.jar -V
# trunk-ignore(shellcheck/SC2086)
bazel ${bazel_startup_options} version

# Output Files
merge_instance_branch_out=./${merge_instance_branch_head_sha}
Expand All @@ -81,11 +92,11 @@ impacted_targets_out=./impacted_targets_${pr_branch_head_sha}

# Generate Hashes for the Merge Instance Branch
git switch "${MERGE_INSTANCE_BRANCH}"
bazelDiff generate-hashes --workspacePath="${WORKSPACE_PATH}" "${merge_instance_branch_out}"
bazelDiff generate-hashes --workspacePath="${WORKSPACE_PATH}" "-so=${bazel_startup_options}" "${merge_instance_branch_out}"

# Generate Hashes for the Merge Instance Branch + PR Branch
git -c "user.name=Trunk Actions" -c "[email protected]" merge --squash "${PR_BRANCH}"
bazelDiff generate-hashes --workspacePath="${WORKSPACE_PATH}" "${merge_instance_with_pr_branch_out}"
bazelDiff generate-hashes --workspacePath="${WORKSPACE_PATH}" "-so=${bazel_startup_options}" "${merge_instance_with_pr_branch_out}"

# Compute impacted targets
bazelDiff get-impacted-targets --startingHashes="${merge_instance_branch_out}" --finalHashes="${merge_instance_with_pr_branch_out}" --output="${impacted_targets_out}"
Expand Down

0 comments on commit f3e14b2

Please sign in to comment.