5 Commits

Author SHA1 Message Date
Claire Kuang 213221e7d5 improved comparer and reporting
build and deploy Speckle functions / publish-automate-function-version (push) Has been cancelled
2024-01-31 20:58:13 +00:00
Claire Kuang f2cb5c30be Update AutomateFunction.cs
build and deploy Speckle functions / publish-automate-function-version (push) Has been cancelled
2024-01-31 13:54:40 +00:00
Claire Kuang 4ec42cde84 server and remote transport fixes
build and deploy Speckle functions / publish-automate-function-version (push) Has been cancelled
2024-01-31 12:59:57 +00:00
Claire Kuang 86e4759531 updates to use 2.18 prerelease nugets
build and deploy Speckle functions / publish-automate-function-version (push) Has been cancelled
2024-01-31 11:54:23 +00:00
Claire Kuang e3806f3fb4 marked tolerance as required input
build and deploy Speckle functions / publish-automate-function-version (push) Has been cancelled
2024-01-31 11:29:00 +00:00
3 changed files with 117 additions and 48 deletions
+114 -45
View File
@@ -2,6 +2,7 @@ using Objects;
using Speckle.Automate.Sdk; using Speckle.Automate.Sdk;
using Speckle.Automate.Sdk.Schema; using Speckle.Automate.Sdk.Schema;
using Speckle.Core.Api; using Speckle.Core.Api;
using Speckle.Core.Credentials;
using Speckle.Core.Models; using Speckle.Core.Models;
using Speckle.Core.Models.Extensions; using Speckle.Core.Models.Extensions;
using Speckle.Core.Transports; using Speckle.Core.Transports;
@@ -37,6 +38,8 @@ static class AutomateFunction
throw new Exception("Release branch has no commits"); throw new Exception("Release branch has no commits");
} }
var tolerance = functionInputs.Tolerance;
Console.WriteLine($"Comparing {testBranchName} against {releaseBranchName}"); Console.WriteLine($"Comparing {testBranchName} against {releaseBranchName}");
// get the test and release commits // get the test and release commits
@@ -44,19 +47,25 @@ static class AutomateFunction
Base? testingCommitObject = await automationContext.ReceiveVersion(); Base? testingCommitObject = await automationContext.ReceiveVersion();
Console.WriteLine("Received test version: " + testingCommitObject); Console.WriteLine("Received test version: " + testingCommitObject);
Console.WriteLine("Receiving release version"); Console.WriteLine("Receiving release version");
using ServerTransport transport = ServerTransport serverTransport = new ServerTransport(
new( automationContext.SpeckleClient.Account,
automationContext.SpeckleClient.Account, automationContext.AutomationRunData.ProjectId
automationContext.AutomationRunData.ProjectId );
);
Base? releaseCommitObject = await Operations Base? releaseCommitObject = await Operations
.Receive(releaseCommit.referencedObject, transport) .Receive(
(
await automationContext.SpeckleClient
.CommitGet(automationContext.AutomationRunData.ProjectId, releaseCommit.id)
.ConfigureAwait(continueOnCapturedContext: false)
).referencedObject,
serverTransport,
new MemoryTransport()
)
.ConfigureAwait(continueOnCapturedContext: false); .ConfigureAwait(continueOnCapturedContext: false);
if (releaseCommitObject == null) if (releaseCommitObject == null)
{ {
throw new Exception("Commit root object was null"); throw new Exception("Commit root object was null");
} }
Console.WriteLine("Received release version: " + releaseCommitObject); Console.WriteLine("Received release version: " + releaseCommitObject);
// flatten both commits // flatten both commits
@@ -65,7 +74,10 @@ static class AutomateFunction
var releaseCommitObjectsDict = new Dictionary<string, Base>(); var releaseCommitObjectsDict = new Dictionary<string, Base>();
foreach (var releaseObject in releaseCommitObjects) foreach (var releaseObject in releaseCommitObjects)
{ {
if (!releaseCommitObjectsDict.ContainsKey(releaseObject.applicationId)) if (
releaseObject.applicationId != null
&& !releaseCommitObjectsDict.ContainsKey(releaseObject.applicationId)
)
{ {
releaseCommitObjectsDict.Add(releaseObject.applicationId, releaseObject); releaseCommitObjectsDict.Add(releaseObject.applicationId, releaseObject);
} }
@@ -77,12 +89,15 @@ static class AutomateFunction
Console.WriteLine($"Found {testCommitObjects.Count()} objects in release version"); Console.WriteLine($"Found {testCommitObjects.Count()} objects in release version");
// compare objects // compare objects
int addedCount = 0;
int modifiedCount = 0;
int unchangedCount = 0; int unchangedCount = 0;
var addedList = new List<Tuple<string, string>>();
var modifiedList = new List<Tuple<string, string, string>>();
foreach (Base testObject in testCommitObjects) foreach (Base testObject in testCommitObjects)
{ {
if (releaseCommitObjectsDict.ContainsKey(testObject.applicationId)) if (
testObject.applicationId != null
&& releaseCommitObjectsDict.ContainsKey(testObject.applicationId)
)
{ {
Base releaseObject = releaseCommitObjectsDict[testObject.applicationId]; Base releaseObject = releaseCommitObjectsDict[testObject.applicationId];
@@ -94,30 +109,28 @@ static class AutomateFunction
// if ids are different, find property differences // if ids are different, find property differences
else else
{ {
modifiedCount++;
var diffDictionary = new Dictionary<string, string>(); var diffDictionary = new Dictionary<string, string>();
Dictionary<string, object> releaseObjectPropDict = releaseObject.GetMembers(); Dictionary<string, object?> releaseObjectPropDict =
Dictionary<string, object> testObjectPropDict = testObject.GetMembers(); releaseObject.GetMembers();
Dictionary<string, object?> testObjectPropDict = testObject.GetMembers();
foreach (var entry in testObjectPropDict) foreach (var entry in testObjectPropDict)
{ {
if (releaseObjectPropDict.ContainsKey(entry.Key)) if (releaseObjectPropDict.ContainsKey(entry.Key))
{ {
bool changed = false;
try try
{ {
changed = entry.Value != releaseObjectPropDict[entry.Key]; bool changed = !Equals(entry.Value, releaseObjectPropDict[entry.Key]);
} if (changed)
catch { }
if (changed)
{
string diff =
$"Property ({entry.Key}) changed from ({releaseObjectPropDict[entry.Key]}) to ({entry.Value})";
if (!diffDictionary.ContainsKey(entry.Key))
{ {
diffDictionary.Add(entry.Key, diff); string diff =
$"Property ({entry.Key}) changed from ({releaseObjectPropDict[entry.Key]}) to ({entry.Value})";
if (!diffDictionary.ContainsKey(entry.Key))
{
diffDictionary.Add(entry.Key, diff);
}
} }
} }
catch { }
releaseObjectPropDict.Remove(entry.Key); releaseObjectPropDict.Remove(entry.Key);
} }
else else
@@ -144,14 +157,17 @@ static class AutomateFunction
var sb = new System.Text.StringBuilder(); var sb = new System.Text.StringBuilder();
foreach (var entry in diffDictionary) foreach (var entry in diffDictionary)
{ {
sb.AppendLine($"{entry.Key}: {entry.Value}"); sb.AppendLine($"{entry.Key}: {entry.Value}. ");
} }
automationContext.AttachWarningToObjects( modifiedList.Add(
MODIFIED, new Tuple<string, string, string>(
new List<string>() { testObject.id }, testObject.id,
sb.ToString() testObject.speckle_type,
sb.ToString()
)
); );
//automationContext.AttachWarningToObjects( MODIFIED, new List<string>() { testObject.id }, sb.ToString());
} }
} }
@@ -159,35 +175,88 @@ static class AutomateFunction
} }
else else
{ {
addedCount++; //automationContext.AttachInfoToObjects(ADDED, new List<string>() { testObject.id });
automationContext.AttachInfoToObjects( addedList.Add(
ADDED, new Tuple<string, string>(testObject.id, testObject.speckle_type)
new List<string>() { testObject.id }
); );
} }
} }
// if there are any remaining release commit objects, this indicates missing objects in the test run. // if there are any remaining release commit objects, this indicates missing objects in the test run.
// mark run as failed and list missing object details. var deletedList = new List<Tuple<string, string>>();
if (releaseCommitObjectsDict.Keys.Count > 0) foreach (var entry in releaseCommitObjectsDict)
{
deletedList.Add(new Tuple<string, string>(entry.Key, entry.Value.speckle_type));
}
// mark run failed if there are any added, modified, or deleted objects and report
if (addedList.Count + deletedList.Count + modifiedList.Count > 0)
{ {
automationContext.MarkRunFailed( automationContext.MarkRunFailed(
$"Missing {releaseCommitObjectsDict.Keys.Count} objects compared to the release commit." $"Run failed due to {addedList.Count} ADDED, {modifiedList.Count} MODIFIED, and {deletedList.Count} DELETED objects compared to the release commit."
); );
foreach (var missingObject in releaseCommitObjectsDict) addedList.ForEach(
{ o => Console.WriteLine($"ADDED object: id( {o.Item1} ), type( {o.Item2} )")
Console.WriteLine( );
$"Missing object info: id( {missingObject.Value.id} ), applicationId( {missingObject.Key} ), type( {missingObject.Value.speckle_type} )" deletedList.ForEach(
); o => Console.WriteLine($"DELETED object: id( {o.Item1} ), type( {o.Item2} )")
} );
modifiedList.ForEach(
o =>
Console.WriteLine(
$"MODIFIED object: id( {o.Item1} ), type( {o.Item2} ), changed props:( {o.Item3} )"
)
);
} }
// mark run as succeeded, noting any changed objects and added objects
else else
{ {
automationContext.MarkRunSuccess( automationContext.MarkRunSuccess(
$"Run passed with {addedCount} new objects, {modifiedCount} objects, and {unchangedCount} unchanged objects." $"Run passed with {unchangedCount} unchanged objects."
); );
} }
} }
public static bool Equals<T>(T a, T b)
{
switch (a)
{
case Base o:
return b is Base bBase ? o.id == bBase.id : false;
case List<object> aList:
if (b is List<object> bList && aList.Count == bList.Count)
{
for (int i = 0; i < aList.Count; i++)
{
if (!Equals(aList[i], bList[i]))
{
return false;
}
}
return true;
}
return false;
case Dictionary<string, object> aDictionary:
if (
b is Dictionary<string, object> bDictionary
&& aDictionary.Count == bDictionary.Count
)
{
foreach (var entry in aDictionary)
{
if (
!bDictionary.ContainsKey(entry.Key)
|| !Equals(entry.Value, bDictionary[entry.Key])
)
{
return false;
}
}
return true;
}
return false;
default:
return EqualityComparer<T>.Default.Equals(a, b);
}
}
} }
@@ -7,7 +7,7 @@ using System.ComponentModel.DataAnnotations;
/// are valid and match the required schema. /// are valid and match the required schema.
struct FunctionInputs struct FunctionInputs
{ {
//[Required] [Required]
public double Tolerance; public double Tolerance;
//public string Exclusions; //public string Exclusions;
@@ -8,7 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Speckle.Automate.Sdk" Version="2.17.0-automate3" /> <PackageReference Include="Speckle.Automate.Sdk" Version="2.18.0-fileInput" />
<PackageReference Include="Speckle.Objects" Version="2.17.0-automate3" /> <PackageReference Include="Speckle.Objects" Version="2.18.0-fileInput" />
</ItemGroup> </ItemGroup>
</Project> </Project>