Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Uploading Impacted Targets From Forked PRs #47

Merged
merged 6 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ runs:
env:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
TARGET_BRANCH: ${{ inputs.target-branch }}
PR_BRANCH: ${{ github.head_ref }}
PR_BRANCH_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
WORKSPACE_PATH: ${{ inputs.bazel-workspace-path }}
BAZEL_PATH: ${{ inputs.bazel-path }}
IMPACTS_FILTERS_CHANGES: ${{ steps.detect-changed-paths.outputs.changes }}
Expand All @@ -80,8 +80,7 @@ runs:
MERGE_INSTANCE_BRANCH: ${{ steps.prerequisites.outputs.merge_instance_branch }}
MERGE_INSTANCE_BRANCH_HEAD_SHA:
${{ steps.prerequisites.outputs.merge_instance_branch_head_sha }}
PR_BRANCH: ${{ steps.prerequisites.outputs.pr_branch }}
PR_BRANCH_HEAD_SHA: ${{ steps.prerequisites.outputs.pr_branch_head_sha }}
PR_BRANCH_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
VERBOSE: ${{ inputs.verbose }}
WORKSPACE_PATH: ${{ steps.prerequisites.outputs.workspace_path }}
BAZEL_PATH: ${{ inputs.bazel-path }}
Expand All @@ -98,6 +97,6 @@ runs:
IS_FORK: ${{ github.event.pull_request.head.repo.fork }}
TARGET_BRANCH: ${{ steps.prerequisites.outputs.merge_instance_branch }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_SHA: ${{ steps.prerequisites.outputs.pr_branch_head_sha }}
PR_BRANCH_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
IMPACTED_TARGETS_FILE: ${{ steps.compute-impacted-targets.outputs.impacted_targets_out }}
IMPACTS_ALL_DETECTED: ${{ steps.prerequisites.outputs.impacts_all_detected }}
17 changes: 4 additions & 13 deletions src/scripts/compute_impacted_targets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -euo pipefail
shopt -s expand_aliases

if [[ (-z ${MERGE_INSTANCE_BRANCH}) || (-z ${PR_BRANCH}) ]]; then
if [[ -z ${MERGE_INSTANCE_BRANCH} ]]; then
echo "Missing branch"
exit 2
fi
Expand Down Expand Up @@ -52,15 +52,6 @@ bazelDiff() {
fi
}

# NOTE: We cannot assume that the checked out Git repo (e.g. via actions-checkout)
# was a shallow vs a complete clone. The `--depth` options deepens the commit history
# in both clone modes: https://git-scm.com/docs/fetch-options#Documentation/fetch-options.txt---depthltdepthgt
fetchRemoteGitHistory() {
logIfVerbose "Fetching" "$@" "..."
git fetch --quiet --depth=2147483647 origin "$@"
logIfVerbose "...done!"
}

## Verbose logging for the Merge Instance and PR branch.
if [[ -n ${VERBOSE} ]]; then
# Find the merge base of the two branches
Expand All @@ -71,14 +62,14 @@ if [[ -n ${VERBOSE} ]]; then
merge_instance_depth=$(git rev-list "${merge_base_sha}".."${MERGE_INSTANCE_BRANCH_HEAD_SHA}" | wc -l)
echo "Merge Instance Depth= ${merge_instance_depth}"

git switch "${MERGE_INSTANCE_BRANCH}"
git checkout "${MERGE_INSTANCE_BRANCH}"
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}"

git switch "${PR_BRANCH}"
git checkout "${PR_BRANCH_HEAD_SHA}"
git log -n "${pr_depth}" --oneline
fi

Expand All @@ -97,7 +88,7 @@ git switch "${MERGE_INSTANCE_BRANCH}"
bazelDiff generate-hashes --bazelPath="${BAZEL_PATH}" --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}"
git -c "user.name=Trunk Actions" -c "[email protected]" merge --squash "${PR_BRANCH_HEAD_SHA}"
bazelDiff generate-hashes --bazelPath="${BAZEL_PATH}" --workspacePath="${WORKSPACE_PATH}" "-so=${bazel_startup_options}" "${merge_instance_with_pr_branch_out}"

# Compute impacted targets
Expand Down
17 changes: 7 additions & 10 deletions src/scripts/prerequisites.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ fetchRemoteGitHistory() {
git fetch --quiet --depth=2147483647 origin "$@"
}

# trunk-ignore(shellcheck)
pr_branch="${PR_BRANCH}"
pr_head_sha="${PR_BRANCH_HEAD_SHA}"
merge_instance_branch="${TARGET_BRANCH}"
if [[ -z ${merge_instance_branch} ]]; then
merge_instance_branch="${DEFAULT_BRANCH}"
Expand Down Expand Up @@ -45,22 +44,20 @@ if [[ -n ${IMPACTS_FILTERS_CHANGES+x} ]]; then
fi

fetchRemoteGitHistory "${merge_instance_branch}"
fetchRemoteGitHistory "${pr_branch}"
fetchRemoteGitHistory "${pr_head_sha}"

git switch "${merge_instance_branch}"
merge_instance_branch_head_sha=$(git rev-parse "${merge_instance_branch}")

git switch "${pr_branch}"
pr_branch_head_sha=$(git rev-parse "${pr_branch}")
merge_instance_branch_head_sha=$(git rev-parse "origin/${merge_instance_branch}")
if [[ -z ${merge_instance_branch_head_sha} ]]; then
echo "Could not identify merge instance branch head sha"
exit 2
fi

echo "Identified changes: " "${impacts_all_detected}"

# Outputs
# trunk-ignore(shellcheck/SC2129)
echo "merge_instance_branch=${merge_instance_branch}" >>"${GITHUB_OUTPUT}"
echo "merge_instance_branch_head_sha=${merge_instance_branch_head_sha}" >>"${GITHUB_OUTPUT}"
echo "pr_branch=${pr_branch}" >>"${GITHUB_OUTPUT}"
echo "pr_branch_head_sha=${pr_branch_head_sha}" >>"${GITHUB_OUTPUT}"
echo "impacts_all_detected=${impacts_all_detected}" >>"${GITHUB_OUTPUT}"
echo "workspace_path=${workspace_path}" >>"${GITHUB_OUTPUT}"
echo "requires_default_bazel_installation=${requires_default_bazel_installation}" >>"${GITHUB_OUTPUT}"
18 changes: 13 additions & 5 deletions src/scripts/upload_impacted_targets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fi
REPO_OWNER=$(echo "${REPOSITORY}" | cut -d "/" -f 1)
REPO_NAME=$(echo "${REPOSITORY}" | cut -d "/" -f 2)

if [[ (-z ${PR_NUMBER}) || (-z ${PR_SHA}) ]]; then
if [[ (-z ${PR_NUMBER}) || (-z ${PR_BRANCH_HEAD_SHA}) ]]; then
echo "Missing PR params"
exit 2
fi
Expand All @@ -44,7 +44,7 @@ REPO_BODY=$(
PR_BODY=$(
jq --null-input \
--arg number "${PR_NUMBER}" \
--arg sha "${PR_SHA}" \
--arg sha "${PR_BRANCH_HEAD_SHA}" \
'{ "number": $number, "sha": $sha }'
)

Expand Down Expand Up @@ -83,8 +83,10 @@ else
num_impacted_targets=$(wc -l <"${IMPACTED_TARGETS_FILE}")
fi

RESPONSE_BODY_FILE="./response.txt"

HTTP_STATUS_CODE=$(
curl -s -o /dev/null -w '%{http_code}' -X POST \
curl -s -o "${RESPONSE_BODY_FILE}" -w '%{http_code}' -X POST \
-H "Content-Type: application/json" -H "x-api-token:${API_TOKEN-}" -H "x-forked-workflow-run-id:${RUN_ID-}" \
-d "@${POST_BODY}" \
"${API_URL}"
Expand All @@ -93,10 +95,10 @@ HTTP_STATUS_CODE=$(
EXIT_CODE=0
COMMENT_TEXT=""
if [[ ${HTTP_STATUS_CODE} == 200 ]]; then
COMMENT_TEXT="✨ Uploaded ${num_impacted_targets} impacted targets for ${PR_NUMBER} @ ${PR_SHA}"
COMMENT_TEXT="✨ Uploaded ${num_impacted_targets} impacted targets for ${PR_NUMBER} @ ${PR_BRANCH_HEAD_SHA}"
else
EXIT_CODE=1
COMMENT_TEXT="❌ Unable to upload impacted targets. Encountered ${HTTP_STATUS_CODE} @ ${PR_SHA}. Please contact us at slack.trunk.io."
COMMENT_TEXT="❌ Unable to upload impacted targets. Encountered ${HTTP_STATUS_CODE} @ ${PR_BRANCH_HEAD_SHA}. Please contact us at slack.trunk.io."

# Dependabot doesn't have access to GitHub action Secrets.
# On authn failure, prompt the user to update their token.
Expand All @@ -111,4 +113,10 @@ else
fi

echo "${COMMENT_TEXT}"

if [[ ${HTTP_STATUS_CODE} != 200 ]]; then
echo "Response Body:"
cat "${RESPONSE_BODY_FILE}"
fi

exit "${EXIT_CODE}"
11 changes: 7 additions & 4 deletions tests/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { describe, beforeEach, beforeAll, afterAll, it, expect, afterEach } from
import { strict as assert } from "node:assert";

const PORT = 4567;
const RESPONSE_FILE = "./response.txt";

type ImpactedTargets = string[] | "ALL";

Expand All @@ -18,7 +19,7 @@ type EnvVar =
| "REPOSITORY"
| "TARGET_BRANCH"
| "PR_NUMBER"
| "PR_SHA"
| "PR_BRANCH_HEAD_SHA"
| "IMPACTED_TARGETS_FILE"
| "IMPACTS_ALL_DETECTED"
| "API_URL"
Expand All @@ -34,7 +35,7 @@ const DEFAULT_ENV_VARIABLES: EnvVarSet = {
REPOSITORY: "test-repo-owner/test-repo-name",
TARGET_BRANCH: "test-target-branch",
PR_NUMBER: "123",
PR_SHA: "test-pr-sha",
PR_BRANCH_HEAD_SHA: "test-pr-sha",
IMPACTED_TARGETS_FILE: "/tmp/test-impacted-targets-file",
IMPACTS_ALL_DETECTED: "false",
API_URL: fetchUrl("/testUploadImpactedTargets"),
Expand Down Expand Up @@ -81,7 +82,8 @@ describe("upload_impacted_targets", () => {
assert(exportedEnvVars);
assert(uploadedImpactedTargetsPayload);

const { API_TOKEN, REPOSITORY, TARGET_BRANCH, PR_NUMBER, PR_SHA, RUN_ID } = exportedEnvVars;
const { API_TOKEN, REPOSITORY, TARGET_BRANCH, PR_NUMBER, PR_BRANCH_HEAD_SHA, RUN_ID } =
exportedEnvVars;
const { apiTokenHeader, forkedWorkflowIdHeader, requestBody } = uploadedImpactedTargetsPayload;

expect(apiTokenHeader).toEqual(API_TOKEN);
Expand All @@ -94,7 +96,7 @@ describe("upload_impacted_targets", () => {
},
pr: {
number: PR_NUMBER,
sha: PR_SHA,
sha: PR_BRANCH_HEAD_SHA,
},
targetBranch: TARGET_BRANCH,
impactedTargets,
Expand Down Expand Up @@ -131,6 +133,7 @@ describe("upload_impacted_targets", () => {

afterEach(function () {
fs.rmSync(DEFAULT_ENV_VARIABLES.IMPACTED_TARGETS_FILE, { force: true });
fs.rmSync(RESPONSE_FILE, { force: true });
});

afterAll(function () {
Expand Down
Loading