Skip to content

Commit

Permalink
Merge pull request #306 from specklesystems/gergo/automateRaise
Browse files Browse the repository at this point in the history
fix(automate_sdk): make sure we throw for failed version create
  • Loading branch information
gjedlicska authored Oct 3, 2023
2 parents 6e8e08a + 67cf41d commit 62912d4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/speckle_automate/automation_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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],
Expand All @@ -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(
Expand Down
11 changes: 7 additions & 4 deletions src/specklepy/api/resources/commit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional
from typing import List, Optional, Union

from gql import gql

Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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:
"""
Expand Down
7 changes: 4 additions & 3 deletions src/specklepy/core/api/resources/commit.py
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 62912d4

Please sign in to comment.