Skip to content

fix(vcs): stop the GitHub pull request webhook crashing on non-JSON file pages - #14168

Merged
HarshMN2345 merged 6 commits into
mainfrom
fix/github-pull-request-files
Oct 7, 2026
Merged

HarshMN2345 merged 6 commits into
mainfrom
fix/github-pull-request-files

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

The GitHub pull_request webhook crashed with array_merge(): Argument #2 must be of type array, string given in GitHub::getPullRequestFiles(). That made POST /v1/vcs/github/events return 500 (206 Sentry events in a month on Cloud, all from region tor in the sample).

getPullRequestFiles() merged every page body without checking it. Adapter::call() only decodes application/json, so any other page stayed a string and array_merge threw. The PHPStan baseline was suppressing exactly this (array_merge expects array, mixed given).

The trigger is a fork pull request that merges upstream main into the fork and changes about 5,478 files. It gets a synchronize event on every upstream push. GitHub lists at most 3000 files, so each event paged through 101 requests. Cloud forwards every GitHub webhook to every region, and repositories and projects live in the shared platform database. So every region built the adapter, fetched the commit and listed all the files, and only then createGitDeployments skipped the projects hosted elsewhere.

Adapter (packages/vcs)

  • GitHub::getPullRequestFiles() throws on an HTTP error (code = status) or on a non-JSON body (code 0, status in the message, so it reaches Sentry and the log says what came back). It asks for 100 files per page, so the 3000-file cap now takes 31 requests instead of 101.
  • Gitea::getPullRequestFiles() had the same unchecked array_merge and gets the same two checks.

Webhook (GitHub/Events/Create.php)

  • The push and pull_request handlers load the linked repositories first. They return before building the adapter (which can mint an installation token) unless one of them belongs to a project in this region. The full list still goes to createGitDeployments, so the owning region behaves exactly as before.
  • A failed file listing is caught and logged, like the getCommit() call next to it. Pull request events never build: they update the PR comment and commit statuses. With no file list, path triggers can't filter, so resources are listed on the pull request whatever paths they watch. Before, nothing was posted and the webhook returned 500.

Authorize endpoint (GitHub/Authorize/External/Update.php)

  • Same catch. Without it, a failed listing came after the pull request id was saved, so every retry returned 409.

Test Plan

  • Ran the patched adapter against the real pull request: same 3000 files, in 31 requests and 47s. The old adapter took 101 requests and 108s.
  • Pointed the old and new adapters at a local server:
    • text/html 502: the old adapter throws the exact production TypeError; the new one throws HTTP 502 (code 502).
    • text/html 200: the new one throws HTTP 200 returned a non-JSON body (code 0).
    • JSON 404: the new one throws HTTP 404.
  • Fetching all 101 pages myself returned 200 JSON every time, so I don't know which response GitHub sent in production.
  • bin/monorepo check vcs passes and the vcs unit suite passes (128 tests). The regenerated baseline only removes the two array_merge entries and moves counts for the existing status-code idiom. PHPStan and Pint pass on both app files.
  • The region gate was not run end to end; Docker was unavailable. It reuses isProjectInCurrentRegion(), the check the installation handler already uses.
  • No new tests. Real GitHub can't be made to return a non-JSON page, and the region gate only differs across Cloud regions. The only tests that could cover either would run against a fake adapter. The GitHub e2e suite is also skipped in CI (_APP_VCS_GITHUB_PRIVATE_KEY=disabled; 18 tests, 18 skipped on main).

Not covered here

  • Other GitHub adapter methods (createComment, updateComment, getRepositoryName, generateAccessToken) still pass a possibly non-JSON body to array_key_exists. They make one call each, not 101 per region, so they are less exposed.
  • The Origin pull request handler has the same shape: listing before the repository lookup, and an uncaught failure.
  • A pull request with more than 3000 files is still matched against the first 3000 only, as before.

getPullRequestFiles merged every page body into the result without
looking at the response. Adapter::call() only decodes application/json
bodies, so a page that came back as anything else was a string and
array_merge threw a TypeError. An error page with a JSON body was merged
in as if it were a list of files.

Throw on an HTTP error or a non-array body, as getPullRequest and the
Gitea, GitLab and Bitbucket adapters already do. Ask for 100 files per
page instead of 30: GitHub lists at most 3000 files, so a large pull
request now takes 31 requests instead of 101.
…s fail to load

The pull_request webhook and the external contribution authorize endpoint
now catch a failed file listing, log a warning and continue with no
affected files, like the getCommit call in the webhook. Path triggers are
skipped without affected files, so the deployment still runs instead of
the request failing. In the authorize endpoint a failure here came after
the pull request id was saved, so every retry returned a 409.
@ghost

ghost commented Oct 6, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The pull request validates GitHub pull-request file responses and increases the page size to 100. GitHub webhook and external-contribution authorization handlers log file-fetch failures and continue without path filtering. It also adds a non-array response guard for Gitea and skips installation-specific GitHub calls in regions without a linked project.

Latest changes: The newest commits separate GitHub HTTP and body errors, add the Gitea body guard, clarify fallback comments, and check for region-local projects before initializing GitHub adapters or fetching pull-request details.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 5
File Change
packages/vcs/phpstan-baseline.neon Removes guarded array-merge diagnostics and adjusts remaining diagnostic counts.
packages/vcs/src/Adapter/Git/GitHub.php Fetches 100 files per page and throws distinct exceptions for HTTP errors and non-array bodies.
packages/vcs/src/Adapter/Git/Gitea.php Rejects non-array pull-request file responses before merging them.
src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php Logs file-fetch failures and continues authorization processing without path filtering.
src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php Handles file-fetch failures and skips installation-specific GitHub calls when no linked project belongs to the current region.

Reviewed the commits since 02feb38 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Comment thread packages/vcs/src/Adapter/Git/GitHub.php Resolved
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/github-pull-request-files (after).

Metric Before After Change
🚀 Requests/sec 192 203.51 🟢 +6%
⏱️ Latency P50 89.77 ms 84.92 ms 🟢 -5.4%
⏱️ Latency P95 211.4 ms 202.79 ms ⚪ -4.1%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 84.92 202.79 12,597 203.51 -8.61
Account 161.36 313.29 663 11.02 -33.27
TablesDB 81.44 150.12 6,851 112.1 -18.61
Storage 79.39 181.41 3,315 56.48 +3.82
Functions 122.77 244.3 1,768 30.86 -8.75

Top API waits (after)

API request Max wait (ms)
functions.create 428.16
account.prefs.update 396.5
account.name.update 373.2
functions.delete 371.8
account.get 346.21

…rror

A page whose body is not decoded JSON now throws its own exception with
code 0 and the status in the message, instead of reusing the HTTP status
as the code. A 200 with an HTML body would otherwise carry code 200, which
the error handler turns into a 500 that Sentry never sees.

Gitea's getPullRequestFiles had the same unchecked array_merge, so a
non-JSON page (for example an auth proxy's login page) raised the same
TypeError. It now gets the same two checks.
… project

Repositories and projects live in the shared platform database, and a
webhook can reach every region, so each region looked up the linked
repositories and, for pull requests, built the adapter, fetched the
commit and listed every changed file before createGitDeployments skipped
the projects hosted elsewhere. Building the adapter can mint an
installation token.

The push and pull_request handlers now load the repositories first and
return before touching GitHub unless one of them belongs to a project in
this region. The full list still goes to createGitDeployments, so the
owning region behaves as before.

Pull request events never build; they update the pull request comment
and commit statuses. The fallback comments now say what an empty file
list does there: path triggers can't filter, so resources are listed on
the pull request whatever paths they watch.
@HarshMN2345 HarshMN2345 changed the title fix(vcs): handle GitHub pull request file pages that are not a JSON list fix(vcs): stop the GitHub pull request webhook crashing on non-JSON file pages Oct 6, 2026
@HarshMN2345

Copy link
Copy Markdown
Member Author

@tenki-reviewer review

@luxor-code-review

luxor-code-review Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)

  • 🟡 Fail-open path triggers when PR files fetch fails — Update.php:137

The change makes GitHub/Gitea adapters validate pull-request file API responses before merging, raising GitHub's pagination page size to 100. The GitHub authorization update action now degrades gracefully when fetching affected PR files fails, logging a warning instead of aborting. The webhook event handler moves the linked-repositories lookup earlier and skips work early when no project in the current region is hosted.

Files Change
packages/vcs/src/Adapter/Git/GitHub.php, packages/vcs/src/Adapter/Git/Gitea.php Validate HTTP status and JSON shape of PR-file responses; GitHub perPage 30→100
src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php Try/catch fallback to empty affected-files list with console warning
src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php Region-gated early return and reordered repositories lookup in PR event handling
packages/vcs/phpstan-baseline.neon Baseline count adjustments

Reviewed commit: 17b301e

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

32 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (17)
  • php.appwrite.permissive-write-permission (8)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expand to see previous review

This PR hardens VCS pull-request file fetching with HTTP status and JSON validation, and adds region-aware early gating plus fail-safe error handling in the GitHub webhook and authorization flows.

Key findings

  • 🟡 Fail-open path triggers when PR files fetch fails — Update.php:137

@HarshMN2345

Copy link
Copy Markdown
Member Author

@tenki-reviewer review — the only finding is resolved as not applicable (see thread); please re-review the current head.

@luxor-code-review

luxor-code-review Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 Divergent, unbounded getPullRequestFiles pagination (GitHub.php:764) — The two adapters paginate the same endpoint with divergent termination logic: GitHub::getPullRequestFiles (packages/vcs/src/Adapter/Git/GitHub.php:764) loops while (true) with no page cap, while Gitea::getPullRequestFiles (packages/vcs/src/Adapter/Git/Gitea.php:786) stops at $maxPages = 100 with $limit = 30.

The change raises pagination page sizes, adds error handling around pull-request file fetching, and introduces region-aware filtering in the GitHub events handler so only the owning region deploys resources. It also adjusts the PHPStan baseline for the vcs package.

Files Change
packages/vcs/src/Adapter/Git/GitHub.php, packages/vcs/src/Adapter/Git/Gitea.php Larger pagination page size, is_array guards, and error handling in getPullRequestFiles
src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php Region-aware project filtering and graceful degradation when PR file fetches fail
src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php Fail-open fallback with warning logging on provider errors
packages/vcs/phpstan-baseline.neon Baseline count adjustments

Reviewed commit: 953e42c

@HarshMN2345
HarshMN2345 merged commit 0796307 into main Oct 7, 2026
12 of 14 checks passed
@HarshMN2345

Copy link
Copy Markdown
Member Author

@hansi-codes review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant