diff --git a/release_notes.md b/release_notes.md index 16d551a7..079d2ccc 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1 +1 @@ -* Set environment variable to avoid Get-AzAccessToken breaking change \ No newline at end of file +* [Durable] Add Version property to $Context diff --git a/src/DurableSDK/HistoryEvent.cs b/src/DurableSDK/HistoryEvent.cs index ed16c4f9..9c3d79a7 100644 --- a/src/DurableSDK/HistoryEvent.cs +++ b/src/DurableSDK/HistoryEvent.cs @@ -50,6 +50,9 @@ internal class HistoryEvent [DataMember] public string Name { get; set; } + + [DataMember] + public string Version { get; set; } [DataMember] public string Result { get; set; } diff --git a/src/DurableSDK/OrchestrationContext.cs b/src/DurableSDK/OrchestrationContext.cs index 27f082db..130caf05 100644 --- a/src/DurableSDK/OrchestrationContext.cs +++ b/src/DurableSDK/OrchestrationContext.cs @@ -36,5 +36,20 @@ public class OrchestrationContext internal OrchestrationActionCollector OrchestrationActionCollector { get; } = new OrchestrationActionCollector(); internal object CustomStatus { get; set; } + + private readonly Lazy _version; + + public string Version + { + get + { + return _version.Value; + } + } + + public OrchestrationContext() + { + _version = new Lazy(() => OrchestrationVersionExtractor.GetVersionFromHistory(History)); + } } } diff --git a/src/DurableSDK/OrchestrationVersionExtractor.cs b/src/DurableSDK/OrchestrationVersionExtractor.cs new file mode 100644 index 00000000..332dc4af --- /dev/null +++ b/src/DurableSDK/OrchestrationVersionExtractor.cs @@ -0,0 +1,31 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +namespace Microsoft.Azure.Functions.PowerShellWorker.Durable +{ + using System; + + /// + /// Helper class to extract version information from orchestration context. + /// + internal static class OrchestrationVersionExtractor + { + /// + /// Gets the orchestration version from a collection of history events. + /// + /// The history events to search. + /// The version, or null if not found. + public static string GetVersionFromHistory(HistoryEvent[] historyEvents) + { + if (historyEvents == null) + { + return null; + } + + var executionStartedEvent = Array.Find(historyEvents, e => e.EventType == HistoryEventType.ExecutionStarted); + return executionStartedEvent?.Version; + } + } +} diff --git a/test/Unit/Durable/OrchestrationVersionExtractorTests.cs b/test/Unit/Durable/OrchestrationVersionExtractorTests.cs new file mode 100644 index 00000000..8b3ca082 --- /dev/null +++ b/test/Unit/Durable/OrchestrationVersionExtractorTests.cs @@ -0,0 +1,64 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +namespace Microsoft.Azure.Functions.PowerShellWorker.Test.Durable +{ + using System; + using Microsoft.Azure.Functions.PowerShellWorker.Durable; + using Xunit; + + public class OrchestrationVersionExtractorTests + { + [Fact] + public void GetVersionFromHistory_ReturnsNull_WhenHistoryIsNull() + { + string result = OrchestrationVersionExtractor.GetVersionFromHistory(null); + + Assert.Null(result); + } + + [Fact] + public void GetVersionFromHistory_ReturnsNull_WhenHistoryHasNoExecutionStartedEvent() + { + var historyEvents = new[] + { + new HistoryEvent { EventType = HistoryEventType.OrchestratorStarted }, + new HistoryEvent { EventType = HistoryEventType.TaskScheduled } + }; + + string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents); + + Assert.Null(result); + } + + [Fact] + public void GetVersionFromHistory_ReturnsVersion_WhenExecutionStartedEventExists() + { + var historyEvents = new[] + { + new HistoryEvent { EventType = HistoryEventType.OrchestratorStarted }, + new HistoryEvent { EventType = HistoryEventType.ExecutionStarted, Version = "1.0" }, + }; + + string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents); + + Assert.Equal("1.0", result); + } + + [Fact] + public void GetVersionFromHistory_ReturnsFirstExecutionStartedVersion_WhenMultipleExecutionStartedEventsExist() + { + var historyEvents = new[] + { + new HistoryEvent { EventType = HistoryEventType.ExecutionStarted, Version = "1.0" }, + new HistoryEvent { EventType = HistoryEventType.ExecutionStarted, Version = "2.0" } + }; + + string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents); + + Assert.Equal("1.0", result); + } + } +}