Skip to content

Commit 50bee62

Browse files
ethanwharrislantiga
authored andcommitted
[App] Update app URLs to latest format (#16568)
(cherry picked from commit 0ec93f2)
1 parent c62778e commit 50bee62

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

src/lightning_app/runners/cloud.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from functools import partial
2525
from pathlib import Path
2626
from typing import Any, Dict, List, Optional, Tuple, Union
27+
from urllib.parse import quote
2728

2829
import click
2930
import rich
@@ -201,7 +202,7 @@ def open(self, name: str, cluster_id: Optional[str] = None):
201202
)
202203

203204
if "PYTEST_CURRENT_TEST" not in os.environ:
204-
click.launch(self._get_app_url(run_instance, "code", needs_credits))
205+
click.launch(self._get_app_url(project, run_instance, "code", needs_credits))
205206

206207
except ApiException as e:
207208
logger.error(e.body)
@@ -320,7 +321,9 @@ def dispatch(
320321

321322
# TODO: Remove testing dependency, but this would open a tab for each test...
322323
if open_ui and "PYTEST_CURRENT_TEST" not in os.environ:
323-
click.launch(self._get_app_url(run_instance, "logs" if run.is_headless else "web-ui", needs_credits))
324+
click.launch(
325+
self._get_app_url(project, run_instance, "logs" if run.is_headless else "web-ui", needs_credits)
326+
)
324327
except ApiException as e:
325328
logger.error(e.body)
326329
sys.exit(1)
@@ -915,10 +918,27 @@ def _print_specs(run_body: CloudspaceIdRunsBody, print_format: str) -> None:
915918

916919
def _get_app_url(
917920
self,
921+
project: V1Membership,
918922
run_instance: Externalv1LightningappInstance,
919923
tab: str,
920924
need_credits: bool = False,
921925
) -> str:
922926
user = self.backend.client.auth_service_get_user()
923927
action = "?action=add_credits" if need_credits else ""
924-
return f"{get_lightning_cloud_url()}/{user.username}/apps/{run_instance.id}/{tab}{action}"
928+
if user.features.project_selector:
929+
paths = [
930+
user.username,
931+
project.name,
932+
"apps",
933+
run_instance.name,
934+
tab,
935+
]
936+
else:
937+
paths = [
938+
user.username,
939+
"apps",
940+
run_instance.id,
941+
tab,
942+
]
943+
path = quote("/".join([path.replace(" ", "_").replace("/", "~") for path in paths]))
944+
return f"{get_lightning_cloud_url()}/{path}{action}"

tests/tests_app/runners/test_cloud.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,25 +1968,47 @@ def run(self):
19681968

19691969

19701970
@pytest.mark.parametrize(
1971-
"run_instance, tab, lightning_cloud_url, expected_url",
1971+
"project, run_instance, user, tab, lightning_cloud_url, expected_url",
19721972
[
1973+
# Old style
19731974
(
1975+
V1Membership(),
19741976
Externalv1LightningappInstance(id="test-app-id"),
1977+
V1GetUserResponse(username="tester", features=V1UserFeatures()),
19751978
"logs",
1976-
"https://b975913c4b22eca5f0f9e8eff4c4b1c315340a0d.staging.lightning.ai",
1977-
"https://b975913c4b22eca5f0f9e8eff4c4b1c315340a0d.staging.lightning.ai/tester/apps/test-app-id/logs",
1979+
"https://lightning.ai",
1980+
"https://lightning.ai/tester/apps/test-app-id/logs",
19781981
),
19791982
(
1983+
V1Membership(),
19801984
Externalv1LightningappInstance(id="test-app-id"),
1985+
V1GetUserResponse(username="tester", features=V1UserFeatures()),
19811986
"logs",
19821987
"http://localhost:9800",
19831988
"http://localhost:9800/tester/apps/test-app-id/logs",
19841989
),
1990+
# New style
1991+
(
1992+
V1Membership(name="tester's project"),
1993+
Externalv1LightningappInstance(name="test/app"),
1994+
V1GetUserResponse(username="tester", features=V1UserFeatures(project_selector=True)),
1995+
"logs",
1996+
"https://lightning.ai",
1997+
"https://lightning.ai/tester/tester%27s_project/apps/test~app/logs",
1998+
),
1999+
(
2000+
V1Membership(name="tester's project"),
2001+
Externalv1LightningappInstance(name="test/app"),
2002+
V1GetUserResponse(username="tester", features=V1UserFeatures(project_selector=True)),
2003+
"logs",
2004+
"http://localhost:9800",
2005+
"http://localhost:9800/tester/tester%27s_project/apps/test~app/logs",
2006+
),
19852007
],
19862008
)
1987-
def test_get_app_url(monkeypatch, run_instance, tab, lightning_cloud_url, expected_url):
2009+
def test_get_app_url(monkeypatch, project, run_instance, user, tab, lightning_cloud_url, expected_url):
19882010
mock_client = mock.MagicMock()
1989-
mock_client.auth_service_get_user.return_value = V1GetUserResponse(username="tester")
2011+
mock_client.auth_service_get_user.return_value = user
19902012
cloud_backend = mock.MagicMock(client=mock_client)
19912013
monkeypatch.setattr(backends, "CloudBackend", mock.MagicMock(return_value=cloud_backend))
19922014

@@ -1995,4 +2017,4 @@ def test_get_app_url(monkeypatch, run_instance, tab, lightning_cloud_url, expect
19952017
with mock.patch(
19962018
"lightning_app.runners.cloud.get_lightning_cloud_url", mock.MagicMock(return_value=lightning_cloud_url)
19972019
):
1998-
assert runtime._get_app_url(run_instance, tab) == expected_url
2020+
assert runtime._get_app_url(project, run_instance, tab) == expected_url

0 commit comments

Comments
 (0)