|
2 | 2 | import json |
3 | 3 | import runpy |
4 | 4 | import webbrowser |
5 | | -from datetime import datetime |
6 | 5 | from urllib.parse import urlencode, urlparse |
7 | 6 |
|
8 | 7 | import click |
9 | 8 | from rich.console import Console |
10 | 9 |
|
11 | 10 | from cumulusci.cli.ui import CliTable, SimpleSalesforceUIHelpers |
12 | 11 | from cumulusci.core.config import OrgConfig, ScratchOrgConfig |
13 | | -from cumulusci.core.config.sfdx_org_config import SfdxOrgConfig |
14 | | -from cumulusci.core.exceptions import OrgNotFound |
| 12 | +from cumulusci.core.exceptions import CumulusCIException, OrgNotFound |
| 13 | +from cumulusci.core.org_import import ( |
| 14 | + import_sfdx_org_to_keychain, |
| 15 | + calculate_org_days as _core_calculate_org_days, |
| 16 | +) |
15 | 17 | from cumulusci.oauth.client import ( |
16 | 18 | PROD_LOGIN_URL, |
17 | 19 | SANDBOX_LOGIN_URL, |
18 | 20 | OAuth2Client, |
19 | 21 | OAuth2ClientConfig, |
20 | 22 | ) |
21 | 23 | from cumulusci.salesforce_api.utils import get_simple_salesforce_connection |
22 | | -from cumulusci.utils import parse_api_datetime |
23 | 24 | from typing import Optional |
24 | 25 |
|
25 | | -import click |
26 | | - |
27 | 26 | from cumulusci.utils.clariti import ( |
28 | 27 | ClaritiError, |
29 | 28 | build_default_org_name, |
@@ -341,61 +340,28 @@ def org_import( |
341 | 340 | ) |
342 | 341 | if not org_name: |
343 | 342 | raise click.UsageError("Please specify ORGNAME or --org ORGNAME.") |
344 | | - # Import the org from the SFDX keychain as an SfdxOrgConfig |
345 | | - # The `sfdx` key ensures we can reload using the right class. |
346 | | - org_config = SfdxOrgConfig( |
347 | | - {"username": username_or_alias, "sfdx": True}, |
348 | | - org_name, |
349 | | - runtime.keychain, |
350 | | - global_org=False, |
351 | | - ) |
352 | | - |
353 | | - # Determine if we received a locally-created scratch org |
354 | | - # or some other org (which we'll treat as persistent) |
355 | | - |
356 | | - info = org_config.sfdx_info |
357 | | - if info.get("created_date"): |
358 | | - # This is a locally-created scratch org. |
359 | | - # Re-import accordingly. |
360 | | - org_config = ScratchOrgConfig( |
361 | | - {"username": username_or_alias}, |
362 | | - org_name, |
| 343 | + try: |
| 344 | + org_config = import_sfdx_org_to_keychain( |
363 | 345 | runtime.keychain, |
| 346 | + username_or_alias, |
| 347 | + org_name, |
364 | 348 | global_org=False, |
365 | 349 | ) |
366 | | - org_config._sfdx_info = info |
367 | | - # Set `created` so we don't try to rebuild it. |
368 | | - org_config.config["created"] = True |
369 | | - |
370 | | - org_config.config["days"] = calculate_org_days(info) |
371 | | - org_config.config["date_created"] = parse_api_datetime(info["created_date"]) |
| 350 | + except CumulusCIException as err: |
| 351 | + raise click.ClickException(str(err)) from err |
372 | 352 |
|
373 | | - org_config.save() |
374 | | - click.echo( |
375 | | - "Imported scratch org: {org_id}, username: {username}".format( |
376 | | - **org_config.sfdx_info |
377 | | - ) |
378 | | - ) |
379 | | - else: |
380 | | - # This is either a persistent org or a scratch org imported into the |
381 | | - # sfdx keychain via OAuth login. |
382 | | - org_config.populate_expiration_date() |
383 | | - org_config.save() |
384 | | - click.echo( |
385 | | - "Imported org: {org_id}, username: {username}".format( |
386 | | - **org_config.sfdx_info |
387 | | - ) |
388 | | - ) |
| 353 | + message = ( |
| 354 | + "Imported scratch org: {org_id}, username: {username}" |
| 355 | + if getattr(org_config, "scratch", False) |
| 356 | + else "Imported org: {org_id}, username: {username}" |
| 357 | + ) |
| 358 | + click.echo(message.format(**org_config.sfdx_info)) |
389 | 359 |
|
390 | 360 |
|
391 | 361 | def calculate_org_days(info): |
392 | | - """Returns the difference in days between created_date (ISO 8601), |
393 | | - and expiration_date (%Y-%m-%d)""" |
394 | | - if not info.get("created_date") or not info.get("expiration_date"): |
395 | | - return 1 |
396 | | - created_date = parse_api_datetime(info["created_date"]).date() |
397 | | - expires_date = datetime.strptime(info["expiration_date"], "%Y-%m-%d").date() |
398 | | - return abs((expires_date - created_date).days) |
| 362 | + """Backwards-compatible shim for legacy imports.""" |
| 363 | + |
| 364 | + return _core_calculate_org_days(info) |
399 | 365 |
|
400 | 366 |
|
401 | 367 | @org.command(name="info", help="Display information for a connected org") |
|
0 commit comments