Skip to content

fix(proxy): bind branch-pinned domains to a deployment - #13795

Merged
HarshMN2345 merged 11 commits into
mainfrom
fix/rebind-branch-scoped-rules
Sep 22, 2026
Merged

HarshMN2345 merged 11 commits into
mainfrom
fix/rebind-branch-scoped-rules

Conversation

@HarshMN2345

Copy link
Copy Markdown
Member

Problem

A proxy rule pinned to a git branch could stay bound to no deployment forever. Its domain then answered 404 on every request, while the console showed it as Verified with a working Open URL link, and redeploying never helped.

Three separate gaps produced that state. Each commit closes one.

1. Activation ignored branch-pinned rules. PATCH /v1/functions/:functionId/deployment and its Sites twin matched only rules whose deploymentVcsProviderBranch was empty, so activating a deployment never repointed a branch-pinned rule. The build worker was already fixed this way; these two were not.

2. Functions had no branch rebind at all — this is the root cause. activate() runs only when a deployment is activated, and a push to a branch other than the production one never is. So for functions, a rule pinned to any non-production branch was never bound by anything. Sites do not have this problem: finalize() calls Base::activateBranchPreviewRule() on every successful site build, activated or not, and that call is gated on $collection === 'sites'.

3. A rule could be born pointing at the wrong branch. Proxy/Http/Rules/{Function,Site}/Create.php adopted whatever deployment the resource currently served, regardless of branch, so a rule pinned to dev could start life on a main build.

Changes

  • Functions/Http/Functions/Deployment/Update.php, Sites/Http/Sites/Deployment/Update.php — match the deployment's own branch alongside the empty value, mirroring Functions/Workers/Jobs.php.
  • Functions/Workers/Jobs.php — new activateBranchRule() beside activate(), called from finalize() for builds activate() does not handle. The guard is $deployment->getAttribute('activate') !== true, so the two paths are exactly disjoint and no rule is written or announced twice. Template deployments are excluded because they reuse providerBranch for a resolved ref, tags included — the same exclusion activateBranchPreviewRule() makes.
  • Proxy/Http/Rules/{Function,Site}/Create.php — a branch-pinned rule looks up that branch's newest ready deployment; ordering copied from Jobs::updateLatestDeployment. findOne returns an empty document on a miss, so an unbound rule stays unbound rather than adopting a stranger's build.

The two Create.php edits are duplicated inline rather than extracted, per AGENTS.md.

Tests

tests/unit/Platform/Modules/Functions/Workers/ActivateRulesTest.php gains two methods, using the suite's existing platformDatabaseCapturing() and queryFor() helpers and its named fake:

  • a branch rebind targets only that branch — not ['', $branch], which would duplicate activate()'s work;
  • a deployment with no installationId is skipped, which is what keeps a template's resolved ref from repointing a branch rule.

Both were mutation-checked: widening the query to ['', $branch] fails the first, and dropping the installationId guard fails the second. tests/e2e/Services/Proxy/ProxyBase.php gains one assertion in each of the two existing branch-rule tests, asserting through the public API that a rule pinned to a branch with no build of its own stays unbound.

Locally: ActivateRulesTest 5/5, tests/unit/Platform 309 tests with one pre-existing Swoole\Http\Response environment error unrelated to these files. Pint passes on every changed file.

Notes

Appwrite Cloud overrides activate() without calling parent::, so it does not inherit commit 1 — that half is tracked separately. Rules already stuck unbound are not repaired by this PR; they bind on the next successful build of their branch, and anything older needs a backfill.

Activating a deployment matched only rules whose deploymentVcsProviderBranch
was empty, so a branch-pinned rule kept pointing at whatever it already held
and a freshly created one kept pointing at nothing.

Match the deployment's own branch alongside the empty value, as the build
worker already does.
activate() runs only when a deployment is activated, and a push to a branch
other than the production one never is. A rule pinned to such a branch was
therefore never bound to anything, and its domain answered 404 for as long
as it existed. Sites already avoid this through activateBranchPreviewRule.

Repoint those rules from finalize() instead, for builds activate() does not
handle, so the two paths stay disjoint. Template deployments are excluded:
they reuse providerBranch for a resolved ref, tags included.
Creating a branch-pinned rule adopted whatever deployment the resource
currently served, so the domain could be born pointing at a build from a
different branch and stay there until that branch was next deployed.

Look up the branch's newest ready deployment instead, and leave the rule
unbound when the branch has none.
@greptile-apps

greptile-apps Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge because no outstanding or newly introduced correctness issue remains.

Summary

This PR ensures branch-pinned proxy domains bind only to ready deployments from their own VCS branch and are rebound after successful function builds or explicit activation.

  • Prevents branch-pinned rules from adopting manual or template deployments.
  • Adds function branch-rule rebinding for successful builds that are not activated.
  • Updates function and site activation paths to include matching branch-pinned rules only for genuine VCS deployments.
  • Adds API-level coverage for rule creation and function deployment rebinding.

Reviews (7) · Last reviewed commit: "Merge branch 'main' into fix/rebind-bran..."

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/rebind-branch-scoped-rules (after).

Metric Before After Change
🚀 Requests/sec 197.34 201.42 ⚪ +2.1%
⏱️ Latency P50 88.47 ms 86.8 ms ⚪ -1.9%
⏱️ Latency P95 206.23 ms 199.4 ms ⚪ -3.3%
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 86.8 199.4 12,540 201.42 -6.83
Account 162.03 308.95 660 11.29 -14.12
TablesDB 85.09 159.03 6,820 112.25 -5.74
Storage 77.09 163.89 3,300 55.82 -2.88
Functions 124.36 243.09 1,760 30.49 -8.86

Top API waits (after)

API request Max wait (ms)
tablesdb.rows.delete 483.15
functions.variables.update 453.52
functions.create 434.73
tablesdb.rows.update 410.9
account.name.update 403.4

HarshMN2345 and others added 7 commits September 21, 2026 16:46
A template deployment stores its resolved ref — branch, tag or commit — in
providerBranch and carries no installationId. Every place that matched a
branch therefore treated a template whose ref happened to equal a real
branch name as a build of that branch, and could bind or repoint that
branch's domain to template content.

Require a genuine installation wherever providerBranch decides a match:
rule creation, both manual activation paths, and the build worker.

Cover the rebind end to end in the Gitea suite as well. Every unit test
reaches activateBranchRule() directly, so deleting the call site in
finalize() left them all green; the new deployment there is never
activated, which is the only state that reaches it.
updateEmptyManualRule() repoints every manual rule for the resource whose
deploymentId is empty, with no predicate on the branch. That was near
harmless while a rule was born holding whatever the resource served, but a
branch-pinned rule created before its branch has built is now born empty by
design, so the next template deploy or VCS redeploy would claim it and
serve another ref's build from it.

Scope it to branch-agnostic rules, as the sibling repoint in this class
already scopes itself to one branch.

Spell the branch check in activateBranchRule() the same way as the other
call sites while here: empty() also rejects the branch named "0", which git
allows, and that rule would never have been rebound.
Widening the rule query in the activate-deployment actions had no coverage:
the unit tests reach the worker, which carries its own copy of the guard,
and every existing caller of the route activates an uploaded deployment,
whose branch is empty and whose query is therefore unchanged.

Activate the branch's build at the end of the Gitea journey and assert the
pinned rule follows it, which the route used to skip.

Name the arrange helper after the operation it performs, name the template
case after the state it describes rather than the field it reads, and say
what the Proxy fixture actually holds: an uploaded deployment with no
branch at all, not a build of some other branch.
@HarshMN2345
HarshMN2345 merged commit cb9c866 into main Sep 22, 2026
7 checks passed
@HarshMN2345
HarshMN2345 deleted the fix/rebind-branch-scoped-rules branch September 22, 2026 11:03
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.

2 participants