From 7f2d49c735bd02b7e4f72ecf8afc9976699edde8 Mon Sep 17 00:00:00 2001 From: Alexander Hjelm Date: Tue, 28 Nov 2023 21:27:05 +0100 Subject: [PATCH] Add support for images in custom rendered fields --- src/WorkItemMigrator/WorkItemImport/Agent.cs | 28 ++++++++++++++++ .../WorkItemImport/ImportCommandLine.cs | 3 +- .../WorkItemImport/Settings.cs | 5 ++- .../WitClient/WitClientUtils.cs | 33 +++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/WorkItemMigrator/WorkItemImport/Agent.cs b/src/WorkItemMigrator/WorkItemImport/Agent.cs index 188572f0..7d74e7d3 100644 --- a/src/WorkItemMigrator/WorkItemImport/Agent.cs +++ b/src/WorkItemMigrator/WorkItemImport/Agent.cs @@ -128,6 +128,34 @@ public bool ImportRevision(WiRevision rev, WorkItem wi, Settings settings) { Logger.Log(ex, $"Failed to correct description for '{wi.Id}', rev '{rev}'."); } + + // Correct other HTMl fields than description + foreach (var field in settings.FieldMap.Fields) + { + if ( + field.Mapper == "MapRendered" + && (field.For == "All" || field.For.Split(',').Contains(wi.Fields[WiFieldReference.WorkItemType])) + && (field.NotFor == null || !field.NotFor.Split(',').Contains(wi.Fields[WiFieldReference.WorkItemType])) + && wi.Fields.ContainsKey(field.Target) + && field.Target != WiFieldReference.Description + ) + { + try + { + _witClientUtils.CorrectRenderedField( + wi, + _context.GetItem(rev.ParentOriginId), + rev, + field.Target, + _context.Journal.IsAttachmentMigrated + ); + } + catch (Exception ex) + { + Logger.Log(ex, $"Failed to correct description for '{wi.Id}', rev '{rev}'."); + } + } + } } // rev with a commit won't have meaningful information, skip saving fields diff --git a/src/WorkItemMigrator/WorkItemImport/ImportCommandLine.cs b/src/WorkItemMigrator/WorkItemImport/ImportCommandLine.cs index 388fb67a..ab4fa96c 100644 --- a/src/WorkItemMigrator/WorkItemImport/ImportCommandLine.cs +++ b/src/WorkItemMigrator/WorkItemImport/ImportCommandLine.cs @@ -90,7 +90,8 @@ private bool ExecuteMigration(CommandOption token, CommandOption url, CommandOpt IgnoreFailedLinks = config.IgnoreFailedLinks, ProcessTemplate = config.ProcessTemplate, IncludeLinkComments = config.IncludeLinkComments, - IncludeCommits = config.IncludeCommits + IncludeCommits = config.IncludeCommits, + FieldMap = config.FieldMap }; // initialize Azure DevOps/TFS connection. Creates/fetches project, fills area and iteration caches. diff --git a/src/WorkItemMigrator/WorkItemImport/Settings.cs b/src/WorkItemMigrator/WorkItemImport/Settings.cs index 168ed5d5..6c5d268c 100644 --- a/src/WorkItemMigrator/WorkItemImport/Settings.cs +++ b/src/WorkItemMigrator/WorkItemImport/Settings.cs @@ -1,4 +1,6 @@ -namespace WorkItemImport +using Migration.Common.Config; + +namespace WorkItemImport { public class Settings { @@ -18,5 +20,6 @@ public Settings(string account, string project, string pat) public string ProcessTemplate { get; internal set; } public bool IncludeLinkComments { get; internal set; } public bool IncludeCommits { get; internal set; } + public FieldMap FieldMap { get; internal set; } } } \ No newline at end of file diff --git a/src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs b/src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs index 8733d903..a107dc3f 100644 --- a/src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs +++ b/src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs @@ -463,6 +463,39 @@ public bool CorrectDescription(WorkItem wi, WiItem wiItem, WiRevision rev, IsAtt return descUpdated; } + public bool CorrectRenderedField(WorkItem wi, WiItem wiItem, WiRevision rev, string fieldRef, IsAttachmentMigratedDelegate isAttachmentMigratedDelegate) + { + if (wi == null) + { + throw new ArgumentException(nameof(wi)); + } + + if (wiItem == null) + { + throw new ArgumentException(nameof(wiItem)); + } + + if (rev == null) + { + throw new ArgumentException(nameof(rev)); + } + + string fieldValue = wi.Fields[fieldRef].ToString(); + if (string.IsNullOrWhiteSpace(fieldValue)) + return false; + + bool updated = false; + + CorrectImagePath(wi, wiItem, rev, ref fieldValue, ref updated, isAttachmentMigratedDelegate); + + if (updated) + { + wi.Fields[fieldRef] = fieldValue; + } + + return updated; + } + public bool CorrectAcceptanceCriteria(WorkItem wi, WiItem wiItem, WiRevision rev, IsAttachmentMigratedDelegate isAttachmentMigratedDelegate) { if (wi == null)