Skip to content

Commit

Permalink
Merge pull request #2 from muselab-d2x/feature/jobs
Browse files Browse the repository at this point in the history
Feature/jobs
  • Loading branch information
jlantz authored Mar 5, 2024
2 parents d1913d5 + 4255bdb commit 63d3c50
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/d2x_cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def get_oauth_device_flow_token():
console.print(
f"[bold green]Successfully authorized OAuth token ({access_token[:7]}...)[/bold green]"
)
device_token["expires_at"] = datetime.now().isoformat() + device_token.get("expires_in")
device_token["expires_at"] = int(datetime.now().timestamp()) + device_token.get("expires_in")
return json.dumps(device_token)


Expand All @@ -87,7 +87,7 @@ def validate_service(options: dict, keychain) -> dict:
token = json.loads(options["token"])

# Refresh the token if it's expired or expires in the next 30 minutes
if token.get("expires_at") <= datetime.now.isoformat() - 1800:
if token.get("expires_at") <= datetime.now().timestamp() - 1800:
oauth = OAuth2Session(OAUTH_DEVICE_APP["client_id"], token={})
token_url = f"https://{ AUTH0_DOMAIN }/oauth/token"
refresh_token = token.get("refresh_token")
Expand All @@ -100,7 +100,7 @@ def validate_service(options: dict, keychain) -> dict:
if resp.status_code != 200:
raise Exception(f"Failed to refresh token: {resp.json()}")
new_token = resp.json()
new_token["expires_at"] = datetime.now().isoformat() + new_token.get("expires_in")
new_token["expires_at"] = datetime.now().timestamp() + new_token.get("expires_in")
token = new_token

resp = requests.get(
Expand Down
28 changes: 27 additions & 1 deletion src/d2x_cli/commands/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from rich.panel import Panel
from rich.progress import Progress
from rich.status import Status
from rich.table import Table
from cumulusci.core.config import FlowConfig, TaskConfig
from cumulusci.core.config.scratch_org_config import SfdxOrgConfig, ScratchOrgConfig
from cumulusci.core.config.project_config import BaseProjectConfig
Expand Down Expand Up @@ -450,7 +451,7 @@ def create(
if task:
runtime.project_config.config["flows"]["d2x_single_task"] = {
"description": "A flow with a single task for use with D2X Cloud jobs",
"steps": [{1: {"task": task}}],
"steps": {1: {"task": task}},
}
flow = "d2x_single_task"

Expand All @@ -461,6 +462,17 @@ def create(

d2x = get_d2x_api_client(runtime)

repo_id = None
for repo in d2x.list(D2XApiObjects.GithubRepo):
if repo["org"]["name"] == runtime.project_config.repo_owner and repo["name"] == runtime.project_config.repo_name:
repo_id = repo["id"]
break

if not repo_id:
raise click.UsageError(
f"GitHub repo '{runtime.project_config.repo_owner}/{runtime.project_config.repo_name}' not found in D2X Cloud. Please make sure you have installed the D2X Cloud GitHub Application to the repo."
)

# Look up org user
org_user_id = None
if org_user:
Expand Down Expand Up @@ -516,6 +528,8 @@ def create(
job_data = {
"plan_version_id": plan_version_id,
"org_user_id": org_user,
"ref": runtime.project_config.repo_commit,
"repo_id": repo_id,
"steps": json.dumps(steps),
"scratch_create_request_id": scratch_create_request_id,
}
Expand Down Expand Up @@ -868,6 +882,18 @@ def log(runtime, job_id):
asyncio.run(log_async(runtime, job_id))


@job.command(name="steps", help="List the steps in a job")
@click.argument("job_id")
@pass_runtime(require_project=False, require_keychain=True)
def steps(runtime, job_id):
d2x = get_d2x_api_client(runtime)
job = d2x.read(D2XApiObjects.Job, job_id)
if not job:
raise click.UsageError(f"Job {job_id} not found in D2X Cloud")
steps = json.loads(job["steps"])
table = display_job_summary(steps)
click.echo(table)

async def log_async(runtime, job_id):
d2x_service = runtime.project_config.keychain.get_service("d2x")
websocket_uri = await convert_url_to_websocket(
Expand Down
4 changes: 2 additions & 2 deletions src/d2x_cli/commands/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,18 @@ def service_info(runtime, service_type, service_name, print_json):
Use --json to include the full value of sensitive attributes, such as a token or secret.
"""
try:
console = Console()
service_config = runtime.keychain.get_service(service_type, service_name)
if print_json:
print_config = {
k: make_jsonable(v) for k, v in service_config.config.items()
}
console.print(json.dumps(print_config))
print(json.dumps(print_config))
return
sensitive_attributes = get_sensitive_service_attributes(runtime, service_type)
service_data = get_service_data(service_config, sensitive_attributes)
default_service = runtime.keychain.get_default_service_name(service_type)
service_name = default_service if not service_name else service_name
console = Console()
console.print(CliTable(service_data, title=f"{service_type}:{service_name}"))
except ServiceNotConfigured:
click.echo(
Expand Down

0 comments on commit 63d3c50

Please sign in to comment.