diff --git a/src/speckle_automate/automation_context.py b/src/speckle_automate/automation_context.py index ff9082fd..d293392b 100644 --- a/src/speckle_automate/automation_context.py +++ b/src/speckle_automate/automation_context.py @@ -8,9 +8,11 @@ from gql import gql from specklepy.api import operations from specklepy.api.client import SpeckleClient +from specklepy.core.api.models import Branch from specklepy.objects import Base from specklepy.transports.memory import MemoryTransport from specklepy.transports.server import ServerTransport +from specklepy.logging.exceptions import SpeckleException from speckle_automate.schema import ( AutomateBase, @@ -118,6 +120,10 @@ def create_new_version_in_project( f" that triggered this automation: {self.automation_run_data.model_id}" ) + branch = self._get_model(model_id) + if not branch.name: + raise ValueError(f"The model {model_id} has no name.") + root_object_id = operations.send( root_object, [self._server_transport, self._memory_transport], @@ -131,8 +137,30 @@ def create_new_version_in_project( message=version_message, source_application="SpeckleAutomate", ) + + if isinstance(version_id, SpeckleException): + raise version_id + self._automation_result.result_versions.append(version_id) + def _get_model(self, model_id: str) -> Branch: + query = gql( + """ + query ProjectModel($projectId: String!, $modelId: String!){ + project(id: $projectId) { + model(id: $modelId) { + name + id + description + } + } + } + """ + ) + params = {"projectId": self.automation_run_data.project_id, "modelId": model_id} + response = self.speckle_client.httpclient.execute(query, params) + return Branch.model_validate(response["project"]["model"]) + def report_run_status(self) -> None: """Report the current run status to the project of this automation.""" query = gql( diff --git a/src/specklepy/api/resources/commit.py b/src/specklepy/api/resources/commit.py index 0f69bf75..8a0bc6ae 100644 --- a/src/specklepy/api/resources/commit.py +++ b/src/specklepy/api/resources/commit.py @@ -1,4 +1,4 @@ -from typing import List, Optional +from typing import List, Optional, Union from gql import gql @@ -7,6 +7,7 @@ from specklepy.logging import metrics from specklepy.core.api.resources.commit import Resource as CoreResource +from specklepy.logging.exceptions import SpeckleException class Resource(CoreResource): @@ -55,8 +56,8 @@ def create( branch_name: str = "main", message: str = "", source_application: str = "python", - parents: List[str] = None, - ) -> str: + parents: Optional[List[str]] = None, + ) -> Union[str, SpeckleException]: """ Creates a commit on a branch @@ -76,7 +77,9 @@ def create( str -- the id of the created commit """ metrics.track(metrics.SDK, self.account, {"name": "Commit Create"}) - return super().create(stream_id, object_id, branch_name, message, source_application, parents) + return super().create( + stream_id, object_id, branch_name, message, source_application, parents + ) def update(self, stream_id: str, commit_id: str, message: str) -> bool: """ diff --git a/src/specklepy/core/api/resources/commit.py b/src/specklepy/core/api/resources/commit.py index 0f691515..4a076b1c 100644 --- a/src/specklepy/core/api/resources/commit.py +++ b/src/specklepy/core/api/resources/commit.py @@ -1,9 +1,10 @@ -from typing import List, Optional +from typing import List, Optional, Union from gql import gql from specklepy.core.api.models import Commit from specklepy.core.api.resource import ResourceBase +from specklepy.logging.exceptions import SpeckleException NAME = "commit" @@ -106,8 +107,8 @@ def create( branch_name: str = "main", message: str = "", source_application: str = "python", - parents: List[str] = None, - ) -> str: + parents: Optional[List[str]] = None, + ) -> Union[str, SpeckleException]: """ Creates a commit on a branch