Skip to content

Commit 5184e80

Browse files
authored
Refactor FieldToFieldMap for validation and logging (#2997)
Refactored the `InternalExecute` method by introducing a new `IsValid` method to centralize validation logic, improving maintainability and readability. Enhanced logging for both skipped and successful field mappings, providing detailed context for debugging. Added support for default values when source fields are null or empty. Introduced new namespaces and static imports to support additional functionality. Improved error handling with detailed warnings for validation failures. Cleaned up code for better readability and maintainability.
2 parents 2e0bf79 + 616ed84 commit 5184e80

File tree

1 file changed

+49
-2
lines changed
  • src/MigrationTools.Clients.TfsObjectModel/Tools/FieldMappingTool/FieldMaps

1 file changed

+49
-2
lines changed

src/MigrationTools.Clients.TfsObjectModel/Tools/FieldMappingTool/FieldMaps/FieldToFieldMap.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
using System;
22
using System.Windows.Forms;
33
using Microsoft.Extensions.Logging;
4+
using Microsoft.IdentityModel.Tokens;
45
using Microsoft.TeamFoundation.WorkItemTracking.Client;
6+
using MigrationTools.DataContracts.Pipelines;
57
using MigrationTools.Tools;
68
using MigrationTools.Tools.Infrastructure;
9+
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
10+
using static Microsoft.TeamFoundation.Client.CommandLine.Options;
711

812
namespace MigrationTools.FieldMaps.AzureDevops.ObjectModel;
913

@@ -37,10 +41,13 @@ public override void Configure(IFieldMapOptions config)
3741

3842
internal override void InternalExecute(WorkItem source, WorkItem target)
3943
{
40-
if (!source.Fields.Contains(Config.sourceField) || !target.Fields.Contains(Config.targetField))
44+
if (!IsValid(source, target))
4145
{
46+
Log.LogWarning("FieldToFieldMap: [SKIPPED] Field mapping from '{SourceField}' to '{TargetField}' was skipped due to validation failures | Source WorkItem: {SourceId} -> Target WorkItem: {TargetId} | Mode: {FieldMapMode}",
47+
Config.sourceField, Config.targetField, source.Id, target.Id, Config.fieldMapMode);
4248
return;
4349
}
50+
4451
string value;
4552
switch (Config.fieldMapMode)
4653
{
@@ -53,11 +60,51 @@ internal override void InternalExecute(WorkItem source, WorkItem target)
5360
default:
5461
throw new ArgumentOutOfRangeException();
5562
}
63+
5664
if (string.IsNullOrEmpty(value) && Config.defaultValue is not null)
5765
{
5866
value = Config.defaultValue;
5967
}
68+
6069
target.Fields[Config.targetField].Value = value;
61-
Log.LogDebug("FieldToFieldMap: [UPDATE] field mapped {0}:{1} to {2}:{3} as {4}", source.Id, Config.sourceField, target.Id, Config.targetField, Config.fieldMapMode.ToString());
70+
Log.LogDebug("FieldToFieldMap: [UPDATE] Successfully mapped field {SourceField} to {TargetField} with value '{Value}' using mode {FieldMapMode} | Source WorkItem: {SourceId} -> Target WorkItem: {TargetId}",
71+
Config.sourceField, Config.targetField, value, Config.fieldMapMode, source.Id, target.Id);
72+
}
73+
74+
private bool IsValid(WorkItem source, WorkItem target)
75+
{
76+
bool valid = true;
77+
switch (Config.fieldMapMode)
78+
{
79+
case FieldMapMode.SourceToTarget:
80+
if (!source.Fields.Contains(Config.sourceField))
81+
{
82+
Log.LogWarning("FieldToFieldMap: [VALIDATION FAILED] Source field '{SourceField}' does not exist on source WorkItem {SourceId}. Please verify the field name is correct and exists in the source work item type. Available fields can be checked in Azure DevOps work item customization.",
83+
Config.sourceField, source.Id);
84+
valid = false;
85+
}
86+
if (!target.Fields.Contains(Config.targetField))
87+
{
88+
Log.LogWarning("FieldToFieldMap: [VALIDATION FAILED] Target field '{TargetField}' does not exist on target WorkItem {TargetId}. Please verify the field name is correct and exists in the target work item type. You may need to add this field to the target work item type or update your field mapping configuration.",
89+
Config.targetField, target.Id);
90+
valid = false;
91+
}
92+
break;
93+
case FieldMapMode.TargetToTarget:
94+
if (!target.Fields.Contains(Config.sourceField))
95+
{
96+
Log.LogWarning("FieldToFieldMap: [VALIDATION FAILED] Source field '{SourceField}' does not exist on target WorkItem {TargetId}. In TargetToTarget mode, both source and target fields must exist on the target work item. Please verify the source field name is correct.",
97+
Config.sourceField, target.Id);
98+
valid = false;
99+
}
100+
if (!target.Fields.Contains(Config.targetField))
101+
{
102+
Log.LogWarning("FieldToFieldMap: [VALIDATION FAILED] Target field '{TargetField}' does not exist on target WorkItem {TargetId}. In TargetToTarget mode, both source and target fields must exist on the target work item. Please verify the target field name is correct.",
103+
Config.targetField, target.Id);
104+
valid = false;
105+
}
106+
break;
107+
}
108+
return valid;
62109
}
63110
}

0 commit comments

Comments
 (0)