Skip to content

Commit

Permalink
feat: Allow WORKSPACE Path Override (#12)
Browse files Browse the repository at this point in the history
- Relaxes the assumption that the `path/to/WORKSPACE` is at the root of
the git repo.
- Add a required E2E test which checks out this repo and runs the
`compute_impacted_targets` test.

---------

Co-authored-by: Phil Vendola <[email protected]>
  • Loading branch information
nikhilbirmiwal and pv72895 authored Jul 21, 2023
1 parent 7ceff3f commit b730783
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/e2e_tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Pull Request
on: pull_request

permissions: read-all

jobs:
tests:
runs-on: ubuntu-latest
name: E2E Tests
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: oracle
java-version: "17"

- name: Setup Bazel
# trunk-ignore(semgrep): Trust third-party `bazelbuild` GH Action
uses: bazelbuild/setup-bazelisk@v2

- name: Compute Impacted Targets
id: compute
run: ./src/scripts/compute_impacted_targets.sh
shell: bash
env:
MERGE_INSTANCE_BRANCH: main
PR_BRANCH: do_not_delete/stable_test_branch
VERBOSE: 1
WORKSPACE_PATH: ./tests/simple_bazel_workspace

- name: Validate Impacted Targets Computation
shell: bash
run: |
target_count=`cat ${{ steps.compute.outputs.impacted_targets_out }} | wc -l`
if [[ $target_count -ne 2 ]]; then
exit 1
fi
10 changes: 9 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ inputs:
The Merge Instance's target branch. If unspecified, defaults to the repository's default
branch.
required: false
bazel-workspace-path:
description:
The path to the bazel WORKSPACE, relative to the root of the git repository. If unspecified,
defaults to the root of the repository.
required: false
verbose:
description: Whether to enable verbose logging. Defaults to false.
required: false
Expand All @@ -25,6 +30,7 @@ runs:
java-version: "17"

- name: Setup Bazel
# trunk-ignore(semgrep): Trust third-party `bazelbuild` GH Action
uses: bazelbuild/setup-bazelisk@v2

- name: Setup Tools
Expand All @@ -38,8 +44,9 @@ runs:
run: ${GITHUB_ACTION_PATH}/src/scripts/prerequisites.sh
shell: bash
env:
TARGET_BRANCH: ${{ inputs.target-branch }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
TARGET_BRANCH: ${{ inputs.target-branch }}
WORKSPACE_PATH: ${{ inputs.bazel-workspace-path }}

- name: Compute Impacted Targets
id: compute-impacted-targets
Expand All @@ -49,6 +56,7 @@ runs:
MERGE_INSTANCE_BRANCH: ${{ steps.prerequisites.outputs.merge_instance_branch }}
PR_BRANCH: ${{ github.head_ref }}
VERBOSE: ${{ inputs.verbose }}
WORKSPACE_PATH: ${{ steps.prerequisites.outputs.workspace_path }}

- name: Upload Impacted Targets
run: ${GITHUB_ACTION_PATH}/src/scripts/upload_impacted_targets.sh
Expand Down
10 changes: 7 additions & 3 deletions src/scripts/compute_impacted_targets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ if [[ (-z ${MERGE_INSTANCE_BRANCH}) || (-z ${PR_BRANCH}) ]]; then
exit 2
fi

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

logIfVerbose "Fetching all remotes..."
git fetch --all --quiet
logIfVerbose "...done!"

# Install the bazel-diff JAR. Avoid cloning the repo, as there will be conflicting WORKSPACES.
curl -Lo bazel-diff.jar https://github.com/Tinder/bazel-diff/releases/latest/download/bazel-diff_deploy.jar
workspace_path=$(pwd)

git switch "${MERGE_INSTANCE_BRANCH}"
git fetch --unshallow --quiet
Expand Down Expand Up @@ -59,11 +63,11 @@ impacted_targets_out=./impacted_targets_${pr_branch_head_sha}

# Generate Hashes for the Merge Instance Branch
git switch "${MERGE_INSTANCE_BRANCH}"
java -jar bazel-diff.jar generate-hashes --workspacePath="${workspace_path}" "${merge_instance_branch_out}"
java -jar bazel-diff.jar generate-hashes --workspacePath="${WORKSPACE_PATH}" "${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}"
java -jar bazel-diff.jar generate-hashes --workspacePath="${workspace_path}" "${merge_instance_with_pr_branch_out}"
java -jar bazel-diff.jar generate-hashes --workspacePath="${WORKSPACE_PATH}" "${merge_instance_with_pr_branch_out}"

# Compute impacted targets
java -jar bazel-diff.jar get-impacted-targets --startingHashes="${merge_instance_branch_out}" --finalHashes="${merge_instance_with_pr_branch_out}" --output="${impacted_targets_out}"
Expand Down
7 changes: 7 additions & 0 deletions src/scripts/prerequisites.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ if [[ -z ${merge_instance_branch} ]]; then
exit 2
fi

# trunk-ignore(shellcheck/SC2153): Passed in as env variable
workspace_path="${WORKSPACE_PATH}"
if [[ -z ${workspace_path} ]]; then
workspace_path=$(pwd)
fi

# Outputs
echo "merge_instance_branch=${merge_instance_branch}" >>"${GITHUB_OUTPUT}"
echo "workspace_path=${workspace_path}" >>"${GITHUB_OUTPUT}"

0 comments on commit b730783

Please sign in to comment.