From 34f0e9dc5102c37818427fb0dde68183315c73e8 Mon Sep 17 00:00:00 2001
From: Zeid <zeid@mozilla.com>
Date: Fri, 24 Jan 2025 11:50:13 -0500
Subject: [PATCH] LandoLinkEventListener: add new Lando URL functionality (bug
 1943388)

- add new config parameter for new Lando URL
- update event listener to choose URL based on repo projects
---
 .../config/PhabricatorLandoConfigOptions.php  |  6 +++
 .../lando/events/LandoLinkEventListener.php   | 43 +++++++++++++++++--
 2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/moz-extensions/src/lando/config/PhabricatorLandoConfigOptions.php b/moz-extensions/src/lando/config/PhabricatorLandoConfigOptions.php
index 57b04d03ea..1ba25c90f9 100644
--- a/moz-extensions/src/lando/config/PhabricatorLandoConfigOptions.php
+++ b/moz-extensions/src/lando/config/PhabricatorLandoConfigOptions.php
@@ -26,6 +26,12 @@ public function getOptions() {
         'string',
         '')
         ->setDescription(pht('Full URL for the Lando UI server. '.
+                             'Set to an empty string to disable the link on the Revision page.')),
+      $this->newOption(
+        'lando.url',
+        'string',
+        '')
+        ->setDescription(pht('Full URL for the new Lando server. '.
                              'Set to an empty string to disable the link on the Revision page.'))
     );
   }
diff --git a/moz-extensions/src/lando/events/LandoLinkEventListener.php b/moz-extensions/src/lando/events/LandoLinkEventListener.php
index 4f9c6b1be6..017e5488a8 100644
--- a/moz-extensions/src/lando/events/LandoLinkEventListener.php
+++ b/moz-extensions/src/lando/events/LandoLinkEventListener.php
@@ -26,8 +26,10 @@ private function handleActionEvent($event) {
       return;
     }
 
-    $lando_uri = PhabricatorEnv::getEnvConfig('lando-ui.url');
-    if (!$lando_uri) {
+    $legacy_lando_uri = PhabricatorEnv::getEnvConfig('lando-ui.url');
+    $new_lando_uri = PhabricatorEnv::getEnvConfig('lando.url');
+
+    if (!$legacy_lando_uri && !$new_lando_uri) {
       return;
     }
 
@@ -36,7 +38,42 @@ private function handleActionEvent($event) {
       return;
     }
 
-    // View stack in Lando Action
+
+    // Get repository projects, and determine if it uses the new Lando.
+    // If a repository is associated with the "lando" project, it is treated
+    // as a repo that uses the modern Lando. Otherwise, it is treated as using
+    // the legacy Lando.
+    $repository_phid = $object->getRepositoryPHID();
+    $repository = id(new PhabricatorRepositoryQuery())
+      ->setViewer(PhabricatorUser::getOmnipotentUser())
+      ->needProjectPHIDs(true)
+      ->withPHIDs(array($repository_phid))
+      ->executeOne();
+
+    $repository_project_phids = $repository->getProjectPHIDs();
+
+    if ($repository_project_phids) {
+        $projects = id(new PhabricatorProjectQuery())
+          ->setViewer(PhabricatorUser::getOmnipotentUser())
+          ->withPHIDs($repository_project_phids)
+          ->execute();
+
+        $lando_project = id(new PhabricatorProjectQuery())
+          ->setViewer(PhabricatorUser::getOmnipotentUser())
+          ->withNames(array("lando"))
+          ->executeOne();
+
+        $uses_new_lando = in_array($lando_project, $projects);
+    } else {
+        $uses_new_lando = false;
+    }
+
+    if ($uses_new_lando) {
+        $lando_uri = $new_lando_uri;
+    } else {
+        $lando_uri = $legacy_lando_uri;
+    }
+ 
     $lando_stack_uri = (string) id(new PhutilURI($lando_uri))
       ->setPath('/D' . $object->getID() . '/');