Skip to content

Commit

Permalink
feat: automation context result view reporting and creating
Browse files Browse the repository at this point in the history
  • Loading branch information
gjedlicska committed Oct 26, 2023
1 parent 1ff3245 commit a1831b5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
34 changes: 30 additions & 4 deletions src/speckle_automate/automation_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dataclasses import dataclass, field
from pathlib import Path
import time
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Tuple, Union

import httpx
from gql import gql
Expand Down Expand Up @@ -111,7 +111,7 @@ def receive_version(self) -> Base:

def create_new_version_in_project(
self, root_object: Base, model_name: str, version_message: str = ""
) -> str:
) -> Tuple[str, str]:
"""Save a base model to a new version on the project.
Args:
Expand All @@ -137,9 +137,11 @@ def create_new_version_in_project(
self.automation_run_data.project_id,
model_name,
)
print(branch_create)
if isinstance(branch_create, Exception):
raise branch_create
model_id = branch_create
else:
model_id = branch.id

root_object_id = operations.send(
root_object,
Expand All @@ -159,7 +161,31 @@ def create_new_version_in_project(
raise version_id

self._automation_result.result_versions.append(version_id)
return version_id
return model_id, version_id

def set_context_view(
self,
# f"{model_id}@{version_id} or {model_id} "
resource_ids: Optional[List[str]] = None,
include_source_model_version: bool = True,
) -> None:
link_resources = (
[
f"{self.automation_run_data.model_id}@{self.automation_run_data.version_id}"
]
if include_source_model_version
else []
)
if resource_ids:
link_resources.append(*resource_ids)
if not link_resources:
raise Exception(
"We do not have enough resource ids to compose a context view"
)
self._automation_result.result_view = (
f"{self.automation_run_data.speckle_server_url}/projects"
f"/{self.automation_run_data.project_id}/models/{','.join(link_resources)}"
)

def report_run_status(self) -> None:
"""Report the current run status to the project of this automation."""
Expand Down
36 changes: 35 additions & 1 deletion tests/intergration/speckle_automate/test_automation_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,39 @@ def test_create_version_in_project(
) -> None:
root_object = Base()
root_object.foo = "bar"
version_id = automation_context.create_new_version_in_project(root_object, "foobar")
model_id, version_id = automation_context.create_new_version_in_project(
root_object, "foobar"
)

assert model_id is not None
assert version_id is not None


def test_set_context_view(automation_context: AutomationContext) -> None:
automation_context.set_context_view()

assert automation_context._automation_result.result_view is not None
assert automation_context._automation_result.result_view.endswith(
f"models/{automation_context.automation_run_data.model_id}@{automation_context.automation_run_data.version_id}"
)

automation_context._automation_result.result_view = None

dummy_context = "foo@bar"
automation_context.set_context_view([dummy_context])

assert automation_context._automation_result.result_view is not None
assert automation_context._automation_result.result_view.endswith(
f"models/{automation_context.automation_run_data.model_id}@{automation_context.automation_run_data.version_id},{dummy_context}"
)
automation_context._automation_result.result_view = None

dummy_context = "foo@baz"
automation_context.set_context_view(
[dummy_context], include_source_model_version=False
)

assert automation_context._automation_result.result_view is not None
assert automation_context._automation_result.result_view.endswith(
f"models/{dummy_context}"
)

0 comments on commit a1831b5

Please sign in to comment.