Repository navigation
CAMEL-26775: ci - a PR whose diff the GitHub API refuses is tested from the local diff #29469
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
Merged
davsclaus
merged 2 commits into
apache:main
from
jamesnetherton:CAMEL-26775-ci-pr-diff-too-large
Nov 9, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| # Helpers that give the unified diff of a pull request. | ||
|
|
||
| # The PR diff from the GitHub API, or nothing when the API does not return it. | ||
| # The API refuses the diff of a PR beyond the GitHub diff limits (HTTP 406, for | ||
| # example more than 300 files). | ||
| fetchApiDiff() { | ||
| local prId="$1" | ||
| local repository="$2" | ||
|
|
||
| local diff_output | ||
| diff_output=$(curl -s -w "\n%{http_code}" \ | ||
| -H "Authorization: Bearer ${GITHUB_TOKEN}" \ | ||
| -H "Accept: application/vnd.github.v3.diff" \ | ||
| "https://api.github.com/repos/${repository}/pulls/${prId}" || true) | ||
|
|
||
| local http_code | ||
| http_code=$(echo "$diff_output" | tail -n 1) | ||
| local diff_body | ||
| diff_body=$(echo "$diff_output" | sed '$d') | ||
|
|
||
| if [[ ! "$http_code" =~ ^2[0-9][0-9]$ || -z "$diff_body" ]]; then | ||
| local message | ||
| message=$(echo "$diff_body" | jq -r '.message // empty' 2>/dev/null || true) | ||
| echo "WARNING: The GitHub API did not return the PR diff (HTTP ${http_code:-none})${message:+: $message}" >&2 | ||
| return 0 | ||
| fi | ||
| echo "$diff_body" | ||
| } | ||
|
|
||
| # Fetches origin/<base branch> and deepens a shallow checkout until the merge | ||
| # base with HEAD is reachable: 50, 200, 1000 commits, then the full history. | ||
| # Never fails, the caller checks the merge base. | ||
| fetchBaseBranch() { | ||
| local baseRef="$1" | ||
|
|
||
| local depth | ||
| for depth in 50 200 1000; do | ||
| git fetch --no-tags --deepen="$depth" origin "$baseRef" 2>/dev/null || true | ||
| git fetch --no-tags --depth="$depth" origin "${baseRef}:refs/remotes/origin/${baseRef}" 2>/dev/null || true | ||
| if git merge-base HEAD "origin/${baseRef}" >/dev/null 2>&1; then | ||
| echo "Merge base reachable at depth $depth" >&2 | ||
| return 0 | ||
| fi | ||
| echo "Merge base not reachable at depth $depth, deepening..." >&2 | ||
| done | ||
| echo "Merge base still not reachable, fetching full history" >&2 | ||
| git fetch --no-tags --unshallow origin "$baseRef" 2>/dev/null || true | ||
| git fetch --no-tags origin "${baseRef}:refs/remotes/origin/${baseRef}" 2>/dev/null || true | ||
| } | ||
|
|
||
| # The diff of HEAD against its merge base with origin/<base branch>, or nothing | ||
| # when the merge base cannot be fetched. Fetches the base branch when the | ||
| # checkout does not have the merge base (callers that skip the fetch, such as | ||
| # camel-spring-boot, which runs this action from main). | ||
| localDiff() { | ||
| local baseRef="$1" | ||
|
|
||
| if ! git merge-base HEAD "origin/${baseRef}" >/dev/null 2>&1; then | ||
| fetchBaseBranch "$baseRef" | ||
| fi | ||
| local merge_base | ||
| if ! merge_base=$(git merge-base HEAD "origin/${baseRef}" 2>/dev/null); then | ||
| echo "WARNING: No merge base between HEAD and origin/${baseRef} in the local history" >&2 | ||
| return 0 | ||
| fi | ||
| git diff "$merge_base" HEAD | ||
| } | ||
|
|
||
| # The PR diff: from the GitHub API, else from the local checkout. | ||
| fetchDiff() { | ||
| local prId="$1" | ||
| local repository="$2" | ||
| local baseRef="${3:-main}" | ||
|
|
||
| local diff_body | ||
| diff_body=$(fetchApiDiff "$prId" "$repository") | ||
| if [ -z "$diff_body" ]; then | ||
| echo "Computing the PR diff from the local checkout against origin/${baseRef}" >&2 | ||
| diff_body=$(localDiff "$baseRef") | ||
| fi | ||
| echo "$diff_body" | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| # shellcheck source=pr_diff.sh | ||
| source "${SCRIPT_DIR}/pr_diff.sh" | ||
|
|
||
| pass=0 | ||
| fail=0 | ||
|
|
||
| assert_eq() { | ||
| local expected="$1" | ||
| local actual="$2" | ||
| local message="$3" | ||
| if [[ "$expected" == "$actual" ]]; then | ||
| pass=$((pass + 1)) | ||
| else | ||
| echo "FAIL: $message" | ||
| echo " expected: [$expected]" | ||
| echo " actual: [$actual]" | ||
| fail=$((fail + 1)) | ||
| fi | ||
| } | ||
|
|
||
| changed_files() { | ||
| sed -n -e '/^diff --git a/p' | awk '{print $3}' | cut -b 3- | sort | ||
| } | ||
|
|
||
| # A repository whose origin/main moved on after the PR branch left it | ||
| repo=$(mktemp -d) | ||
| clone=$(mktemp -d) | ||
| trap 'rm -rf "$repo" "$clone"' EXIT | ||
| cd "$repo" | ||
| git init -q -b main | ||
| git config user.email test@example.com | ||
| git config user.name test | ||
| mkdir -p components/a components/b | ||
| echo a > components/a/A.java | ||
| git add -A && git commit -q -m base | ||
| git checkout -q -b pr | ||
| echo b > components/b/B.java | ||
| echo a2 > components/a/A.java | ||
| git add -A && git commit -q -m pr | ||
| git checkout -q main | ||
| echo c > main-only.txt | ||
| git add -A && git commit -q -m "main moves on" | ||
| git update-ref refs/remotes/origin/main main | ||
| git checkout -q pr | ||
|
|
||
| assert_eq "$(printf 'components/a/A.java\ncomponents/b/B.java')" "$(localDiff main | changed_files)" \ | ||
| "local diff has the PR changes only, not the later changes of the base branch" | ||
| assert_eq "" "$(localDiff other 2>/dev/null)" "no local diff without the base branch" | ||
|
|
||
| # A pull_request build checks out the merge of the PR into the base branch | ||
| git checkout -q --detach main | ||
| git merge -q --no-edit pr | ||
| assert_eq "$(printf 'components/a/A.java\ncomponents/b/B.java')" "$(localDiff main | changed_files)" \ | ||
| "local diff of the merge commit has the PR changes only" | ||
| git checkout -q pr | ||
|
|
||
| # A PR that removes a component | ||
| git checkout -q -b removal main | ||
| git rm -q -r components/a | ||
| git commit -q -m "remove a" | ||
| assert_eq "components/a/A.java" "$(localDiff main | changed_files)" "local diff has the removed files" | ||
| git checkout -q pr | ||
|
|
||
| # A depth=1 checkout of the PR without the base branch, as camel-spring-boot does | ||
| git clone -q --depth 1 --branch pr "file://${repo}" "$clone" | ||
| assert_eq "" "$(git -C "$clone" rev-parse -q --verify origin/main)" "the shallow clone has no origin/main" | ||
| assert_eq "$(printf 'components/a/A.java\ncomponents/b/B.java')" "$(cd "$clone" && localDiff main 2>/dev/null | changed_files)" \ | ||
| "local diff fetches the base branch and deepens the checkout to the merge base" | ||
|
|
||
| export GITHUB_TOKEN=test-token | ||
|
|
||
| # The API refuses the diff of a PR beyond the diff limits | ||
| curl() { | ||
| printf '%s\n%s' '{"message": "Sorry, the diff exceeded the maximum number of files (300).", "status": "406"}' 406 | ||
| } | ||
| assert_eq "" "$(fetchApiDiff 1 apache/camel 2>/dev/null)" "no API diff on HTTP 406" | ||
| assert_eq "WARNING: The GitHub API did not return the PR diff (HTTP 406): Sorry, the diff exceeded the maximum number of files (300)." \ | ||
| "$(fetchApiDiff 1 apache/camel 2>&1 >/dev/null)" "the warning names the HTTP status and the API message" | ||
| assert_eq "$(printf 'components/a/A.java\ncomponents/b/B.java')" "$(fetchDiff 1 apache/camel main 2>/dev/null | changed_files)" \ | ||
| "falls back to the local diff on HTTP 406" | ||
| assert_eq "" "$(fetchDiff 1 apache/camel other 2>/dev/null)" "no diff when neither the API nor the local history has it" | ||
|
|
||
| # The API returns the diff | ||
| curl() { | ||
| printf '%s\n%s' 'diff --git a/components/x/X.java b/components/x/X.java' 200 | ||
| } | ||
| assert_eq "components/x/X.java" "$(fetchDiff 1 apache/camel main 2>/dev/null | changed_files)" "uses the API diff when there is one" | ||
|
|
||
| # The API cannot be reached | ||
| curl() { | ||
| return 6 | ||
| } | ||
| assert_eq "$(printf 'components/a/A.java\ncomponents/b/B.java')" "$(fetchDiff 1 apache/camel main 2>/dev/null | changed_files)" \ | ||
| "falls back to the local diff when curl fails" | ||
|
|
||
| echo "" | ||
| echo "pr_diff_test.sh: ${pass} passed, ${fail} failed" | ||
| if [[ "$fail" -ne 0 ]]; then | ||
| exit 1 | ||
| fi |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.