diff --git a/src/uipath/_cli/__init__.py b/src/uipath/_cli/__init__.py index 5f1992db..b30c9798 100644 --- a/src/uipath/_cli/__init__.py +++ b/src/uipath/_cli/__init__.py @@ -10,6 +10,7 @@ from .cli_pack import pack as pack # type: ignore from .cli_publish import publish as publish # type: ignore from .cli_run import run as run # type: ignore +from .cli_app import app as app # type: ignore @click.group(invoke_without_command=True) @@ -52,3 +53,4 @@ def cli(lv: bool, v: bool) -> None: cli.add_command(run) cli.add_command(deploy) cli.add_command(auth) +cli.add_command(app) diff --git a/src/uipath/_cli/_templates/metadata.json.template b/src/uipath/_cli/_templates/metadata.json.template new file mode 100644 index 00000000..b490f2b8 --- /dev/null +++ b/src/uipath/_cli/_templates/metadata.json.template @@ -0,0 +1,33 @@ +{ + "inputs": [ + { + "name": "Sample Property", + "key": "12401570-0b26-4f66-9534-03b87733038b", + "required": false, + "type": "System.String", + "typeNamespace": "system", + "isList": false, + "collectionDataType": null, + "customType": null, + "properties": [], + "version": 1 + } + ], + "outcomes": [ + { + "name": "Submit", + "key": "ca561305-8feb-41fe-a455-00b960749898", + "required": false, + "type": "System.String", + "typeNamespace": "system", + "isList": false, + "collectionDataType": null, + "customType": null, + "properties": [], + "version": 1 + } + ], + "version": 1, + "outputs": [], + "inOuts": [] +} \ No newline at end of file diff --git a/src/uipath/_cli/cli_app.py b/src/uipath/_cli/cli_app.py new file mode 100644 index 00000000..139137c2 --- /dev/null +++ b/src/uipath/_cli/cli_app.py @@ -0,0 +1,85 @@ +import os +import click +import json +from typing import Optional, TYPE_CHECKING +import requests +from dotenv import load_dotenv +from ._auth._utils import update_env_file + +if TYPE_CHECKING: + pass + +def get_env_vars(): + base_url = os.environ.get("UIPATH_URL") + tenant_id = os.environ.get("UIPATH_TENANT_ID") + token = os.environ.get("UIPATH_ACCESS_TOKEN") + package_key = os.environ.get("UIPATH_PACKAGE_KEY") + package_version = os.environ.get("UIPATH_PACKAGE_VERSION") + + + if not all([base_url, tenant_id, token, package_key, package_version]): + click.echo( + "Missing required environment variables. Please check your .env file contains:" + ) + click.echo("UIPATH_URL, UIPATH_TENANT_ID, UIPATH_ACCESS_TOKEN, UIPATH_PACKAGE_KEY") + raise click.Abort("Missing environment variables") + + return [base_url, tenant_id, token, package_key, package_version] + +@click.command() +@click.option("--action", is_flag=True, help="Create an action app") +@click.option("--suffix", type=str, help="The suffix to add to the app name") +def app(action: bool = False, suffix: str = "") -> None: + """ + Creates a new UiPath App. (Placeholder) + """ + # Get base url, token, package key from env variables, make that a function + base_url, tenant_id, token, package_key, package_version = get_env_vars() + + index = base_url.rfind("/") + tenant_name = base_url[index+1:] + base_url = base_url[:index] + url = f"{base_url}/apps_/default/api/v1/default/models/tenants/{tenant_id}/publish/extenal/apps" + + click.echo(f"Base URL: {base_url}") + click.echo(f"Tenant Name: {tenant_name}") + click.echo(f"URL: {url}") + + headers = {"Authorization": f"Bearer {token}"} + + # Construct path relative to the current working directory + current_dir = os.getcwd() + metadata_path = os.path.join(current_dir, "metadata.json") + + # Read schema from metadata.json + try: + with open(metadata_path, 'r') as f: + schema_data = json.load(f) + except FileNotFoundError: + click.echo(f"Error: metadata.json not found at {metadata_path}") + raise click.Abort() + except json.JSONDecodeError: + click.echo(f"Error: Could not decode JSON from {metadata_path}") + raise click.Abort() + + payload = { + "packageName": package_key, + "title": f"{package_key}{suffix}", + "tenantId": tenant_id, + "tenantName": tenant_name, + "packageVersion": package_version, + "context": { + "appUsageType": "1" if action else "0" + }, + "schema": schema_data + } + + response = requests.post(url, headers=headers, json=payload) + if response.status_code == 200: + click.echo("App Created successfully!") + click.echo(response.json()) + else: + click.echo(f"Failed to create app. Status code: {response.status_code}") + click.echo(f"Request: {url}") + click.echo(f"Response: {response.json()}") + diff --git a/src/uipath/_cli/cli_publish.py b/src/uipath/_cli/cli_publish.py index b11f480d..27278a81 100644 --- a/src/uipath/_cli/cli_publish.py +++ b/src/uipath/_cli/cli_publish.py @@ -1,9 +1,11 @@ # type: ignore import os +import json import click import requests from dotenv import load_dotenv +from ._auth._utils import update_env_file load_dotenv() @@ -108,6 +110,24 @@ def publish(feed): if response.status_code == 200: click.echo("Package published successfully!") + try: + response_data = response.json() + body = response_data.get("value")[0].get("Body") + if body: + try: + body_data = json.loads(body) + package_key = body_data.get("Id") + package_version = body_data.get("Version") + os.environ["UIPATH_PACKAGE_KEY"] = package_key + os.environ["UIPATH_PACKAGE_VERSION"] = package_version + update_env_file({"UIPATH_PACKAGE_KEY": package_key}) + update_env_file({"UIPATH_PACKAGE_VERSION": package_version}) + except Exception as e: + click.echo(f"Error parsing response JSON: {e}") + else: + click.echo("Response format unexpected or 'value' list is empty.") + except Exception as e: + click.echo(f"Error parsing response JSON: {e}") else: click.echo(f"Failed to publish package. Status code: {response.status_code}") click.echo(f"Response: {response.text}") diff --git a/src/uipath/_cli/middlewares.py b/src/uipath/_cli/middlewares.py index 26e3eaad..a609f823 100644 --- a/src/uipath/_cli/middlewares.py +++ b/src/uipath/_cli/middlewares.py @@ -24,6 +24,7 @@ class Middlewares: "pack": [], "publish": [], "run": [], + "app": [], } _plugins_loaded = False