[ci] Fall back to files API when PR diff exceeds GitHub line limit (#18486)

This commit is contained in:
Jonathan Swoboda
2026-08-18 10:22:28 -04:00
committed by GitHub
parent ae730d6357
commit 476d540065
2 changed files with 41 additions and 2 deletions
+3 -2
View File
@@ -558,8 +558,9 @@ def _get_changed_files_github_actions() -> list[str] | None:
try: try:
return _get_changed_files_from_command(cmd) return _get_changed_files_from_command(cmd)
except Exception as e: except Exception as e:
# If it fails due to the 300 file limit, use the API method # If it fails due to a diff limit (300 files or 20000 lines),
if "maximum" in str(e) and "files" in str(e): # use the API method which only returns filenames
if "diff exceeded the maximum" in str(e):
cmd = [ cmd = [
"gh", "gh",
"api", "api",
+38
View File
@@ -244,6 +244,44 @@ def test_get_changed_files_github_actions_pull_request_large_pr(
assert result == expected_files assert result == expected_files
def test_get_changed_files_github_actions_pull_request_large_diff(
monkeypatch: MonkeyPatch,
) -> None:
"""Test _get_changed_files_github_actions fallback for PRs with >20000 diff lines."""
monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request")
expected_files = ["file1.py", "file2.cpp"]
with (
patch("helpers._get_pr_number_from_github_env", return_value="17909"),
patch("helpers._get_changed_files_from_command") as mock_get,
):
# First call fails with too many diff lines error, second succeeds with API method
mock_get.side_effect = [
Exception(
"could not find pull request diff: HTTP 406: Sorry, "
"the diff exceeded the maximum number of lines (20000)"
),
expected_files,
]
result = _get_changed_files_github_actions()
assert mock_get.call_count == 2
mock_get.assert_any_call(["gh", "pr", "diff", "17909", "--name-only"])
mock_get.assert_any_call(
[
"gh",
"api",
"repos/esphome/esphome/pulls/17909/files",
"--paginate",
"--jq",
".[].filename",
]
)
assert result == expected_files
def test_get_changed_files_github_actions_pull_request_other_error( def test_get_changed_files_github_actions_pull_request_other_error(
monkeypatch: MonkeyPatch, monkeypatch: MonkeyPatch,
) -> None: ) -> None: