Skip to content

fix(vcs): reindex provider pull request ids after removing one - #13530

Merged
HarshMN2345 merged 5 commits into
mainfrom
fix/vcs-pull-request-ids-reindex
Sep 8, 2026
Merged

HarshMN2345 merged 5 commits into
mainfrom
fix/vcs-pull-request-ids-reindex

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Closes #11741

What does this PR do?

array_diff() filters by value but keeps the original keys, so untracking a pull request that is not the last entry leaves a gapped array.

The consequence is worse than malformed storage. providerPullRequestIds is declared array => true in app/config/collections/platform.php, and the structure validator rejects any value that is not a list:

if (!\is_array($value) || !\array_is_list($value)) {
    $this->message = 'Attribute "'.$key.'" must be an array';

So closing an external contribution whose id sits anywhere but at the end throws, the webhook 500s, and the pull request is never untracked. Closing the last entry happens to work, which is why this went unnoticed.

Three call sites persist the result and all three are fixed:

  • VCS/Http/GitHub/Events/Create.php — the one reported
  • VCS/Http/Gitea/Events/Create.php
  • Vcs/RepositoryPullRequestCleanup.php — shared by the GitLab and Bitbucket handlers

Same bug elsewhere

Sweeping every key-preserving filter whose result is persisted or serialized turned up one more, in a different service: Teams/Http/Memberships/XList.php drops memberships with an empty userId through array_filter(), which also preserves keys. Response::dynamic() writes each entry back to its original index rather than appending, so the gap survives into the response and a member with an empty userId in any but the last position turns the memberships list into a JSON object — breaking list deserialization in every SDK.

Call sites where the filtered value is only used locally (in_array, empty, count, iteration) were checked and left alone.

Test Plan

  • tests/unit/Vcs/RepositoryPullRequestCleanupTest::testRemoveReindexesRemainingPullRequestIds removes a non-tail ID and asserts the persisted value is [2, 3].
  • tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest::testClosePullRequestRemovesAuthorization creates two external pull requests through the local Gitea API and authorizes both through Appwrite. It closes and reopens the first, checks that it can be authorized again, and verifies the second remains authorized (duplicate authorization returns 409).

Validated locally against the isolated Gitea fixture and real Appwrite API: the E2E passes with this fix and fails with HTTP 409 on reauthorization when only the Gitea array_values() change is removed. Existing cleanup unit tests, Pint, and the focused Rector dry run also pass.

array_diff() keeps the original keys, so untracking a pull request that is
not the last entry leaves a gapped array. providerPullRequestIds is a
declared array attribute, and the structure validator rejects any value that
is not a list, so closing such a pull request threw instead of untracking it.

Reindex at the three call sites that persist the result: the GitHub and Gitea
event handlers, and the shared cleanup used by GitLab and Bitbucket.

Teams membership listing had the same defect through array_filter(), which
also preserves keys. Response::dynamic() writes each entry back to its
original index, so a member with an empty userId in any but the last position
turned the memberships list into a JSON object.
@greptile-apps

greptile-apps Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR prevents key-preserving PHP filters from producing gapped arrays where list-shaped data is required.

  • Reindexes provider pull request IDs after removals in the GitHub, Gitea, GitLab, and Bitbucket cleanup paths.
  • Reindexes filtered team memberships before response serialization.
  • Adds focused unit coverage and an end-to-end Gitea regression test.
  • Updates the Gitea test to wait for asynchronous fork creation before continuing.

Confidence Score: 5/5

The PR appears safe to merge; the list reindexing fixes are correctly applied and the previously reported test race is resolved.

No actionable defects or outstanding previous findings remain. The production changes preserve retained values and ordering while restoring list-shaped arrays, and the regression tests cover non-tail removal.

Important Files Changed

Filename Overview
src/Appwrite/Platform/Modules/Teams/Http/Memberships/XList.php Reindexes filtered memberships so the API response remains a JSON list.
src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php Reindexes stored pull request IDs after removing a closed GitHub pull request.
src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php Reindexes stored pull request IDs after removing a closed Gitea pull request.
src/Appwrite/Vcs/RepositoryPullRequestCleanup.php Reindexes pull request IDs in the shared GitLab and Bitbucket cleanup path.
tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php Covers non-tail pull request removal and reliably waits for asynchronous fork availability.
tests/unit/Vcs/RepositoryPullRequestCleanupTest.php Verifies that removing a non-tail pull request ID persists a contiguous list.

Reviews (5): Last reviewed commit: "Merge branch 'main' into fix/vcs-pull-re..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/vcs-pull-request-ids-reindex (after).

Metric Before After Change
🚀 Requests/sec 264.74 251.31 🔴 -5.1%
⏱️ Latency P50 65.16 ms 69.75 ms 🔴 +7%
⏱️ Latency P95 156.44 ms 162.44 ms ⚪ +3.8%
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 69.75 162.44 15,903 251.31 +6
Account 143.6 268.64 837 13.91 +29.28
TablesDB 67.18 118.32 8,649 139.91 +7.43
Storage 64.93 139.26 4,185 69.39 +12.36
Functions 103.67 195.15 2,232 37.56 +6.09

Top API waits (after)

API request Max wait (ms)
account.name.update 406.96
account.prefs.update 405.39
storage.files.preview 367.83
functions.variables.update 341.05
functions.create 327.95

@Meldiron Meldiron left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm, lets see if we can add e2e test too

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.

Missing array_values() after array_diff() in VCS GitHub PR cleanup causes malformed array storage

2 participants