Skip to content

Commit

Permalink
WEB-961 Update Automate.SDK to match new AutomationRunData shape (#…
Browse files Browse the repository at this point in the history
…3434)

* update schema, add types for trigger

* update `AutomationContext` methods

* update gql in `ReportRunStatus`

* comments

* remove commented code

* correctness, cleanliness

* fix test compilation, skip tests for now

---------

Co-authored-by: Alan Rynne <[email protected]>
Co-authored-by: Gergő Jedlicska <[email protected]>
  • Loading branch information
3 people authored May 24, 2024
1 parent 7b3f646 commit b2f9f49
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 79 deletions.
139 changes: 80 additions & 59 deletions Automate/Speckle.Automate.Sdk/AutomationContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using GraphQL;
using Speckle.Automate.Sdk.Schema;
using Speckle.Automate.Sdk.Schema.Triggers;
using Speckle.Core.Api;
using Speckle.Core.Credentials;
using Speckle.Core.Logging;
Expand Down Expand Up @@ -75,9 +76,14 @@ public static async Task<AutomationContext> Initialize(string automationRunData,
/// <exception cref="SpeckleException">Throws if commit object is null.</exception>
public async Task<Base> ReceiveVersion()
{
Commit? commit = await SpeckleClient
.CommitGet(AutomationRunData.ProjectId, AutomationRunData.VersionId)
.ConfigureAwait(false);
// TODO: this is a quick hack to keep implementation consistency. Move to proper receive many versions
if (AutomationRunData.Triggers.First() is not VersionCreationTrigger trigger)
{
throw new SpeckleException("Processed automation run data without any triggers");
}
var versionId = trigger.Payload.VersionId;

Commit? commit = await SpeckleClient.CommitGet(AutomationRunData.ProjectId, versionId).ConfigureAwait(false);
Base? commitRootObject = await Operations
.Receive(commit.referencedObject, _serverTransport, _memoryTransport)
.ConfigureAwait(false);
Expand All @@ -86,9 +92,7 @@ public async Task<Base> ReceiveVersion()
throw new SpeckleException("Commit root object was null");
}

Console.WriteLine(
$"It took {Elapsed.TotalSeconds} seconds to receive the speckle version {AutomationRunData.VersionId}"
);
Console.WriteLine($"It took {Elapsed.TotalSeconds} seconds to receive the speckle version {versionId}");
return commitRootObject;
}

Expand All @@ -103,26 +107,54 @@ public async Task<Base> ReceiveVersion()
/// The reason is to prevent circular run loop in automation. </exception>
public async Task<string> CreateNewVersionInProject(Base rootObject, string modelName, string versionMessage = "")
{
if (modelName == AutomationRunData.BranchName)
{
throw new ArgumentException(
$"The target model: {modelName} cannot match the model that triggered this automation: {AutomationRunData.ModelId}/{AutomationRunData.BranchName}",
nameof(modelName)
);
}

string rootObjectId = await Operations
.Send(rootObject, new List<ITransport> { _serverTransport, _memoryTransport })
.ConfigureAwait(false);

Branch branch = await SpeckleClient.BranchGet(AutomationRunData.ProjectId, modelName).ConfigureAwait(false);

if (branch is null)
{
// Create the branch with the specified name
await SpeckleClient
.BranchCreate(new BranchCreateInput() { streamId = AutomationRunData.ProjectId, name = modelName })
.ConfigureAwait(false);
}
else
{
// Confirm target branch is not the same as source branch
if (branch.id == null)
{
throw new SpeckleException("Cannot use the branch without its id");
}

foreach (var trigger in AutomationRunData.Triggers)
{
switch (trigger)
{
case VersionCreationTrigger versionCreationTrigger:
{
if (versionCreationTrigger.Payload.ModelId == branch.id)
{
throw new SpeckleException(
$$"""
The target model: {{modelName}} cannot match the model
that triggered this automation:
{{versionCreationTrigger.Payload.ModelId}}
"""
);
}
continue;
}
default:
{
// TODO: How should we handle unknown trigger types?
continue;
}
}
}
}

string rootObjectId = await Operations
.Send(rootObject, new List<ITransport> { _serverTransport, _memoryTransport })
.ConfigureAwait(false);

string versionId = await SpeckleClient
.CommitCreate(
new CommitCreateInput
Expand Down Expand Up @@ -154,7 +186,21 @@ public void SetContextView(List<string>? resourceIds = null, bool includeSourceM
List<string> linkResources = new();
if (includeSourceModelVersion)
{
linkResources.Add($@"{AutomationRunData.ModelId}@{AutomationRunData.VersionId}");
foreach (var trigger in AutomationRunData.Triggers)
{
switch (trigger)
{
case VersionCreationTrigger versionCreationTrigger:
{
linkResources.Add($@"{versionCreationTrigger.Payload.ModelId}@{versionCreationTrigger.Payload.VersionId}");
break;
}
default:
{
throw new SpeckleException($"Could not link resource specified by {trigger.TriggerType} trigger");
}
}
}
}

if (resourceIds is not null)
Expand Down Expand Up @@ -189,54 +235,29 @@ public async Task ReportRunStatus()
{
Query =
@"
mutation ReportFunctionRunStatus(
$automationId: String!,
$automationRevisionId: String!,
$automationRunId: String!,
$versionId: String!,
$functionId: String!,
$functionName: String!,
$functionLogo: String,
$runStatus: AutomationRunStatus!
$elapsed: Float!
$resultVersionIds: [String!]!
mutation AutomateFunctionRunStatusReport(
$functionRunId: String!
$status: AutomateRunStatus!
$statusMessage: String
$objectResults: JSONObject
$results: JSONObject
$contextView: String
){
automationMutations {
functionRunStatusReport(input: {
automationId: $automationId
automationRevisionId: $automationRevisionId
automationRunId: $automationRunId
versionId: $versionId
functionRuns: [{
functionId: $functionId,
functionName: $functionName,
functionLogo: $functionLogo,
status: $runStatus,
elapsed: $elapsed,
resultVersionIds: $resultVersionIds,
statusMessage: $statusMessage,
results: $objectResults,
}]
automateFunctionRunStatusReport(input: {
functionRunId: $functionRunId
status: $status
statusMessage: $statusMessage
contextView: $contextView
results: $results
})
}
}
",
Variables = new
{
automationId = AutomationRunData.AutomationId,
automationRevisionId = AutomationRunData.AutomationRevisionId,
automationRunId = AutomationRunData.AutomationRunId,
versionId = AutomationRunData.VersionId,
functionId = AutomationRunData.FunctionId,
functionName = AutomationRunData.FunctionName,
functionLogo = AutomationRunData.FunctionLogo,
runStatus = RunStatus,
functionRunId = AutomationRunData.FunctionRunId,
status = RunStatus,
statusMessage = AutomationResult.StatusMessage,
elapsed = Elapsed.TotalSeconds,
resultVersionIds = AutomationResult.ResultVersions,
objectResults,
contextView = ContextView,
results = objectResults,
}
};
await SpeckleClient.ExecuteGraphQLRequest<Dictionary<string, object>>(request).ConfigureAwait(false);
Expand Down
14 changes: 5 additions & 9 deletions Automate/Speckle.Automate.Sdk/Schema/AutomationRunData.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
using Speckle.Automate.Sdk.Schema.Triggers;

namespace Speckle.Automate.Sdk.Schema;

///<summary>
///Values of the project, model and automation that triggered this function run.
/// Values of the project, model and automation that triggered this function run.
///</summary>
public struct AutomationRunData
{
public string ProjectId { get; set; }
public string ModelId { get; set; }
public string BranchName { get; set; }
public string VersionId { get; set; }
public string SpeckleServerUrl { get; set; }
public string AutomationId { get; set; }
public string AutomationRevisionId { get; set; }
public string AutomationRunId { get; set; }
public string FunctionId { get; set; }
public string FunctionRelease { get; set; }
public string FunctionName { get; set; }
public string? FunctionLogo { get; set; }
public string FunctionRunId { get; set; }
public List<AutomationRunTriggerBase> Triggers { get; set; }
}
2 changes: 1 addition & 1 deletion Automate/Speckle.Automate.Sdk/Schema/ObjectResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Speckle.Automate.Sdk.Schema;

public struct ObjectResults
{
public readonly string Version => "1.0.0";
public readonly int Version => 1;
public ObjectResultValues Values { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Speckle.Automate.Sdk.Schema.Triggers;

public class AutomationRunTriggerBase
{
public string TriggerType { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Speckle.Automate.Sdk.Schema.Triggers;

/// <summary>
/// Represents a single version creation trigger for the automation run.
/// </summary>
public class VersionCreationTrigger : AutomationRunTriggerBase
{
public VersionCreationTriggerPayload Payload { get; set; }

public VersionCreationTrigger(string modelId, string versionId)
{
TriggerType = "versionCreation";
Payload = new VersionCreationTriggerPayload() { ModelId = modelId, VersionId = versionId };
}
}

/// <summary>
/// Represents the version creation trigger payload.
/// </summary>
public class VersionCreationTriggerPayload
{
public string ModelId { get; set; }
public string VersionId { get; set; }
}
Loading

0 comments on commit b2f9f49

Please sign in to comment.