Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions launchable/commands/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from launchable.utils.env_keys import TOKEN_KEY
from launchable.utils.tracking import Tracking, TrackingClient

from ..utils.authentication import get_org_workspace
from ..utils.authentication import get_org_workspace, ensure_org_workspace
from ..utils.click import emoji
from ..utils.commands import Command
from ..utils.http_client import DEFAULT_BASE_URL
Expand Down Expand Up @@ -88,18 +88,15 @@ def verify(context: click.core.Context):
click.echo("Java command: " + repr(java))
click.echo("launchable version: " + repr(version))

if org is None or workspace is None:
msg = (
"Could not identify Launchable organization/workspace. "
"Please confirm if you set LAUNCHABLE_TOKEN or LAUNCHABLE_ORGANIZATION and LAUNCHABLE_WORKSPACE "
"environment variables"
)
# raise an error here after we print out the basic diagnostics if LAUNCHABLE_TOKEN is not set.
try:
ensure_org_workspace()
except click.UsageError as e:
tracking_client.send_error_event(
event_name=Tracking.ErrorEvent.INTERNAL_CLI_ERROR,
stack_trace=msg
stack_trace=e.message
)
raise click.UsageError(
click.style(msg, fg="red"))
raise e

try:
res = client.request("get", "verification")
Expand Down
10 changes: 6 additions & 4 deletions launchable/utils/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@


def get_org_workspace():
'''
Returns (org,ws) tuple from LAUNCHABLE_TOKEN, or (None,None) if not found.
Use ensure_org_workspace() if this is supposed to be an error condition
'''
token = os.getenv(TOKEN_KEY)
if token:
try:
_, user, _ = token.split(":", 2)
org, workspace = user.split("/", 1)
return org, workspace
except ValueError:
return None, None
raise click.UsageError(click.style("Invalid value in LAUNCHABLE_TOKEN environment variable.", fg="red"))

return os.getenv(ORGANIZATION_KEY), os.getenv(WORKSPACE_KEY)

Expand All @@ -25,9 +29,7 @@ def ensure_org_workspace() -> Tuple[str, str]:
if org is None or workspace is None:
raise click.UsageError(
click.style(
"Could not identify Launchable organization/workspace. "
"Please confirm if you set LAUNCHABLE_TOKEN or LAUNCHABLE_ORGANIZATION and "
"LAUNCHABLE_WORKSPACE environment variables",
"LAUNCHABLE_TOKEN environment variable not set. Nor the LAUNCHABLE_ORGANIZATION and LAUNCHABLE_WORKSPACE combo if you are using https://help.launchableinc.com/sending-data-to-launchable/using-the-launchable-cli/getting-started/migration-to-github-oidc-auth/)", # noqa: E501
fg="red"))
return org, workspace

Expand Down
10 changes: 2 additions & 8 deletions launchable/utils/launchable_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from launchable.utils.tracking import Tracking, TrackingClient # type: ignore

from ..app import Application
from .authentication import get_org_workspace
from .authentication import get_org_workspace, ensure_org_workspace
from .env_keys import REPORT_ERROR_KEY


Expand All @@ -23,13 +23,7 @@ def __init__(self, tracking_client: Optional[TrackingClient] = None, base_url: s
app=app
)
self.tracking_client = tracking_client
self.organization, self.workspace = get_org_workspace()
if self.organization is None or self.workspace is None:
raise ValueError(
"Could not identify a Launchable organization/workspace. "
"Confirm that you set LAUNCHABLE_TOKEN "
"(or LAUNCHABLE_ORGANIZATION and LAUNCHABLE_WORKSPACE) environment variable(s)\n"
"See https://help.launchableinc.com/getting-started#setting-your-api-key")
self.organization, self.workspace = ensure_org_workspace()
self._workspace_state_cache: Optional[Dict[str, Union[str, bool]]] = None

def request(
Expand Down
6 changes: 0 additions & 6 deletions tests/utils/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ def test_get_org_workspace_no_environment_variables(self):
self.assertIsNone(org)
self.assertIsNone(workspace)

@mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": "invalid"})
def test_get_org_workspace_invalid_LAUNCHABLE_TOKEN(self):
org, workspace = get_org_workspace()
self.assertIsNone(org)
self.assertIsNone(workspace)

@mock.patch.dict(os.environ,
{"LAUNCHABLE_TOKEN": "v1:launchableinc/test:token"})
def test_get_org_workspace_valid_LAUNCHABLE_TOKEN(self):
Expand Down
Loading