Skip to content

Commit

Permalink
✨ (configuration.json): enable WorkItemTypeMappingEnricher and update…
Browse files Browse the repository at this point in the history
… mappings

♻️ (ProcessorContainer): refactor processor loading to use lazy initialization
✅ (ProcessorContainerTests): update tests to use ServiceProviderHelper for processor creation
🔧 (ServiceCollectionExtensions): remove redundant ProcessorContainer registration
📝 (MigrationTools.xml): update generated documentation with new commit details

Enable the WorkItemTypeMappingEnricher to map "User Story" to "Product Backlog Item" for better alignment with target system terminology. Refactor ProcessorContainer to use lazy initialization for improved performance and resource management. Update tests to use a helper method for creating service providers, ensuring consistency and reducing code duplication. Remove redundant ProcessorContainer registration from ServiceCollectionExtensions to avoid unnecessary singleton instances. Update generated documentation to reflect the latest commit details.
  • Loading branch information
MrHinsh committed Aug 15, 2024
1 parent 8825f80 commit 2b4b2e7
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 50 deletions.
16 changes: 2 additions & 14 deletions configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
},
"CommonEnrichers": {
"WorkItemTypeMappingEnricher": {
"Enabled": false,
"Enabled": true,
"Mappings": {
"Source Work Item Type Name": "Target Work Item Type Name"
"User Story": "Product Backlog Item"
}
},
"StringManipulatorEnricher": {
Expand Down Expand Up @@ -111,18 +111,6 @@
"UpdateTeamSettings": true,
"MigrateTeamCapacities": true,
"Teams": null
},
"TfsUserMappingEnricher": {
"Enabled": true,
"UserMappingFile": "C:\\temp\\userExport.json",
"IdentityFieldsToCheck": [
"System.AssignedTo",
"System.ChangedBy",
"System.CreatedBy",
"Microsoft.VSTS.Common.ActivatedBy",
"Microsoft.VSTS.Common.ResolvedBy",
"Microsoft.VSTS.Common.ClosedBy"
]
}
},
"Processors": [
Expand Down
12 changes: 6 additions & 6 deletions docs/Reference/Generated/MigrationTools.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using MigrationTools._EngineV1.Configuration;
using MigrationTools._EngineV1.Containers;
using MigrationTools.Processors;
using MigrationTools.Tests;

namespace MigrationTools.Engine.Containers.Tests
{
Expand All @@ -23,10 +24,7 @@ private IOptions<ProcessorContainerOptions> CreateProcessorContainerOptions()

private IServiceProvider CreateServiceProvider()
{
ServiceCollection sc = new ServiceCollection();
sc.AddTransient<SimpleProcessorMock>();
IServiceProvider sp = sc.BuildServiceProvider();
return sp;
return ServiceProviderHelper.GetWorkItemMigrationProcessor();
}

[TestMethod(), TestCategory("L0")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
using MigrationTools._EngineV1.Configuration;
using MigrationTools.Endpoints;
using MigrationTools.Enrichers;
using MigrationTools.Processors;

namespace MigrationTools.Engine.Containers.Tests
{
public class SimpleProcessorMock : _EngineV1.Containers.IProcessor
public class SimpleProcessorMock : _EngineV1.Containers.IProcessor, MigrationTools.Processors.IProcessor
{
public string Name => "TestSimpleContext";

public ProcessingStatus Status => ProcessingStatus.None;

public ProcessorType Type => ProcessorType.Legacy;

public IEndpoint Source => throw new System.NotImplementedException();

public IEndpoint Target => throw new System.NotImplementedException();

public bool SupportsProcessorEnrichers => throw new System.NotImplementedException();

public ProcessorEnricherContainer ProcessorEnrichers => throw new System.NotImplementedException();

public void Configure(IProcessorConfig config)
{
}
Expand Down
3 changes: 1 addition & 2 deletions src/MigrationTools.Tests/ServiceProviderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ internal static ServiceProvider GetWorkItemMigrationProcessor()
services.AddMigrationToolServicesForUnitTests();
services.AddMigrationToolServices(configuration);

// Containers
services.AddSingleton<ProcessorContainer>();

services.AddSingleton<ITelemetryLogger, TelemetryClientAdapter>();

Expand All @@ -38,6 +36,7 @@ internal static ServiceProvider GetWorkItemMigrationProcessor()
services.AddSingleton<IMigrationToolVersionInfo, FakeMigrationToolVersionInfo>();
services.AddSingleton<IMigrationToolVersion, FakeMigrationToolVersion>();
services.AddTransient<SimpleFieldMapMock>();
services.AddTransient<SimpleProcessorMock>();

return services.BuildServiceProvider();
}
Expand Down
31 changes: 15 additions & 16 deletions src/MigrationTools/Processors/ProcessorContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public class ProcessorContainer
private ILogger<ProcessorContainer> _logger;
private ProcessorContainerOptions _Options;

private List<IProcessor> processors;
private readonly Lazy<List<_EngineV1.Containers.IProcessor>> _processorsLazy;

public int Count { get { return processors.Count; } }
public int Count { get { return _processorsLazy.Value.Count; } }

public ReadOnlyCollection<IProcessor> Processors
public ReadOnlyCollection<_EngineV1.Containers.IProcessor> Processors
{
get
{
return new ReadOnlyCollection<IProcessor>(processors);
return new ReadOnlyCollection<_EngineV1.Containers.IProcessor>(_processorsLazy.Value);
}
}

Expand All @@ -39,37 +39,35 @@ public ProcessorContainer(IOptions<ProcessorContainerOptions> options, IServiceP
_services = services;
_logger = logger;
_Options = options.Value;
LoadProcessorsfromOptions(_Options);
// Initialize the lazy processor list
_processorsLazy = new Lazy<List<_EngineV1.Containers.IProcessor>>(() => LoadProcessorsfromOptions(_Options));
}

private void LoadProcessorsfromOptions(ProcessorContainerOptions options)
private List<_EngineV1.Containers.IProcessor> LoadProcessorsfromOptions(ProcessorContainerOptions options)
{
var processors = new List<_EngineV1.Containers.IProcessor>();
if (options.Processors != null)
{
var enabledProcessors = options.Processors.Where(x => x.Enabled).ToList();
_logger.LogInformation("ProcessorContainer: Of {ProcessorCount} configured Processors only {EnabledProcessorCount} are enabled", options.Processors.Count, enabledProcessors.Count);
var allTypes = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => !a.IsDynamic)
.SelectMany(a => a.GetTypes()).ToList();
var allTypes = AppDomain.CurrentDomain.GetMigrationToolsTypes().WithInterface<_EngineV1.Containers.IProcessor>().ToList();

foreach (IProcessorConfig processorConfig in enabledProcessors)
{
if (processorConfig.IsProcessorCompatible(enabledProcessors))
{
_logger.LogInformation("ProcessorContainer: Adding Processor {ProcessorName}", processorConfig.Processor);
string typePattern = $"VstsSyncMigrator.Engine.{processorConfig.Processor}";


Type type = allTypes
.FirstOrDefault(t => t.Name.Equals(processorConfig.Processor) || t.FullName.Equals(typePattern));
.FirstOrDefault(t => t.Name.Equals(processorConfig.Processor));

if (type == null)
{
_logger.LogError("Type " + typePattern + " not found.", typePattern);
throw new Exception("Type " + typePattern + " not found.");
_logger.LogError("Type " + processorConfig.Processor + " not found.", processorConfig.Processor);
throw new Exception("Type " + processorConfig.Processor + " not found.");
}

IProcessor pc = (IProcessor)_services.GetRequiredService(type);
pc.Configure(processorConfig);
_EngineV1.Containers.IProcessor pc = (_EngineV1.Containers.IProcessor)ActivatorUtilities.CreateInstance(_services, type);
processors.Add(pc);
}
else
Expand All @@ -80,6 +78,7 @@ private void LoadProcessorsfromOptions(ProcessorContainerOptions options)
}
}
}
return processors;
}
}
}
4 changes: 0 additions & 4 deletions src/MigrationTools/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ public static void AddMigrationToolServicesLegacy(this IServiceCollection contex
// Services
context.AddSingleton<ITelemetryLogger, TelemetryClientAdapter>();



// Containers
context.AddSingleton<ProcessorContainer>();
//Engine
context.AddSingleton<IMigrationEngine, MigrationEngine>();
}
Expand Down
8 changes: 6 additions & 2 deletions src/MigrationTools/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ public static partial class TypeExtensions
{
public static IEnumerable<Type> WithInterface<TInterface>(this IEnumerable<Type> types)
{
var interfaceType = typeof(TInterface);
return types.Where(type => interfaceType.IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract);
return types.Where(type => typeof(TInterface).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract);
}

public static IEnumerable<Type> WithInterface<TInterface, TInterface2>(this IEnumerable<Type> types)
{
return types.Where(type => (typeof(TInterface).IsAssignableFrom(type) || typeof(TInterface2).IsAssignableFrom(type)) && !type.IsInterface && !type.IsAbstract);
}

public static IEnumerable<Type> WithConfigurationSectionName(this IEnumerable<Type> types)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class WorkItemMigrationContext : TfsMigrationProcessorBase
private List<string> _itemsInError;


public WorkItemMigrationContext(IMigrationEngine engine,
public WorkItemMigrationContext(IOptions<WorkItemMigrationConfig> processorConfig,IMigrationEngine engine,
IServiceProvider services,
ITelemetryLogger telemetry,
ILogger<WorkItemMigrationContext> logger,
Expand All @@ -77,6 +77,7 @@ public WorkItemMigrationContext(IMigrationEngine engine,
StaticEnrichers staticEnrichers)
: base(engine, tfsStaticEnrichers, staticEnrichers, services, telemetry, logger)
{
_config = processorConfig.Value;
_telemetry = telemetry;
_engineConfig = engineConfig.Value;
contextLog = Serilog.Log.ForContext<WorkItemMigrationContext>();
Expand Down

0 comments on commit 2b4b2e7

Please sign in to comment.