Skip to content

Commit fdae325

Browse files
authored
Implement work item type validator processor (#2899)
This PR follows my previous PR #2894 that implements work item type validator tool. I also implemented the porcessor, which uses tool and just validates work item types. This validation is run automatically during work item migrations, but I need (and hope it is useful for others) just to validate things ad do not continue even if everything is OK. I was whinking implementing it as separate processor, or just some boolean option in `TfsWorkItemMigrationProcessor` and chose separate processor. I think it is cleaner because it is simple and it is very clear what is this processor for. The processor supports just one option `StopIfValidationFails`. If `true`, the migration process will stop if the validation fails – this is the default. If set to `false`, the migration process (other processors) will continue to run. The processor configuration is: ``` json { "ProcessorType": "TfsWorkItemTypeValidatorProcessor", "Enabled": true, "SourceName": "Source", "TargetName": "Target", "StopIfValidationFails": true }, ```
2 parents 8238730 + 7c9b711 commit fdae325

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.Extensions.Logging;
5+
using Microsoft.Extensions.Options;
6+
using Microsoft.TeamFoundation.WorkItemTracking.Client;
7+
using MigrationTools.Enrichers;
8+
using MigrationTools.Processors.Infrastructure;
9+
using MigrationTools.Tools;
10+
11+
namespace MigrationTools.Processors
12+
{
13+
/// <summary>
14+
/// Work item type validation processor. Basically it just runs the <see cref="TfsWorkItemTypeValidatorTool"/>
15+
/// to validate work item types. The validation is run always, even if the tool iself is disabled.
16+
/// Neither this processor, nor the tool do not perform any changes to the source or target system.
17+
/// </summary>
18+
public class TfsWorkItemTypeValidatorProcessor : TfsProcessor
19+
{
20+
public TfsWorkItemTypeValidatorProcessor(
21+
IOptions<TfsWorkItemTypeValidatorProcessorOptions> options,
22+
TfsCommonTools tfsCommonTools,
23+
ProcessorEnricherContainer processorEnrichers,
24+
IServiceProvider services,
25+
ITelemetryLogger telemetry,
26+
ILogger<TfsWorkItemTypeValidatorProcessor> logger)
27+
: base(options, tfsCommonTools, processorEnrichers, services, telemetry, logger)
28+
{
29+
}
30+
31+
public new TfsWorkItemTypeValidatorProcessorOptions Options => (TfsWorkItemTypeValidatorProcessorOptions)base.Options;
32+
33+
protected override void InternalExecute()
34+
{
35+
if (!CommonTools.WorkItemTypeValidatorTool.Enabled)
36+
{
37+
Log.LogInformation("Work item type validation tool is disabled, but this processor will still"
38+
+ " run the validation. No changes are done to source or target system.");
39+
}
40+
List<WorkItemType> sourceWits = Source.WorkItems.Project
41+
.ToProject()
42+
.WorkItemTypes
43+
.Cast<WorkItemType>()
44+
.ToList();
45+
List<WorkItemType> targetWits = Target.WorkItems.Project
46+
.ToProject()
47+
.WorkItemTypes
48+
.Cast<WorkItemType>()
49+
.ToList();
50+
bool validationResult = CommonTools.WorkItemTypeValidatorTool
51+
.ValidateWorkItemTypes(sourceWits, targetWits, Target.Options.ReflectedWorkItemIdField);
52+
if (Options.StopIfValidationFails && !validationResult)
53+
{
54+
Log.LogInformation($"'{nameof(Options.StopIfValidationFails)}' is set to 'true', so migration process will stop now.");
55+
Environment.Exit(-1);
56+
}
57+
}
58+
}
59+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using MigrationTools.Processors.Infrastructure;
2+
3+
namespace MigrationTools.Processors
4+
{
5+
/// <summary>
6+
/// Options for processor <see cref="TfsWorkItemTypeValidatorProcessor"/>.
7+
/// </summary>
8+
public class TfsWorkItemTypeValidatorProcessorOptions : ProcessorOptions
9+
{
10+
/// <summary>
11+
/// If set to <see langword="true"/>, migration process will stop if there are some validation errors.
12+
/// If set to <see langword="false"/>, migration process will continue, for example to support some other validation
13+
/// processors.
14+
/// Default value is <see langword="true"/>.
15+
/// </summary>
16+
public bool StopIfValidationFails { get; set; } = true;
17+
}
18+
}

0 commit comments

Comments
 (0)