From e7a7b41a8ccb198ed01ad0c9320843ec0c12f156 Mon Sep 17 00:00:00 2001 From: Alexander Hjelm Date: Tue, 17 Oct 2023 06:27:28 +0200 Subject: [PATCH] Correct image links for Acceptance Criteria --- .../Migration.WIContract/WiField.cs | 1 + src/WorkItemMigrator/WorkItemImport/Agent.cs | 14 +++++ .../WitClient/WitClientUtils.cs | 33 ++++++++++++ .../WitClient/WitClientUtilsTests.cs | 51 +++++++++++++++++++ 4 files changed, 99 insertions(+) diff --git a/src/WorkItemMigrator/Migration.WIContract/WiField.cs b/src/WorkItemMigrator/Migration.WIContract/WiField.cs index 2f4db59e..7882d8d3 100644 --- a/src/WorkItemMigrator/Migration.WIContract/WiField.cs +++ b/src/WorkItemMigrator/Migration.WIContract/WiField.cs @@ -37,6 +37,7 @@ public static class WiFieldReference public static string BoardColumn => "System.BoardColumn"; public static string BoardColumnDone => "System.BoardColumnDone"; public static string BoardLane => "System.BoardLane"; + public static string AcceptanceCriteria => "Microsoft.VSTS.Common.AcceptanceCriteria"; } } \ No newline at end of file diff --git a/src/WorkItemMigrator/WorkItemImport/Agent.cs b/src/WorkItemMigrator/WorkItemImport/Agent.cs index 0bce88e8..188572f0 100644 --- a/src/WorkItemMigrator/WorkItemImport/Agent.cs +++ b/src/WorkItemMigrator/WorkItemImport/Agent.cs @@ -94,6 +94,20 @@ public bool ImportRevision(WiRevision rev, WorkItem wi, Settings settings) _witClientUtils.CorrectComment(wi, _context.GetItem(rev.ParentOriginId), rev, _context.Journal.IsAttachmentMigrated); } + if (wi.Fields.ContainsKey(WiFieldReference.AcceptanceCriteria) && !string.IsNullOrEmpty(wi.Fields[WiFieldReference.AcceptanceCriteria].ToString())) + { + Logger.Log(LogLevel.Debug, $"Correcting acceptance criteria on separate revision on '{rev}'."); + + try + { + _witClientUtils.CorrectAcceptanceCriteria(wi, _context.GetItem(rev.ParentOriginId), rev, _context.Journal.IsAttachmentMigrated); + } + catch (Exception ex) + { + Logger.Log(ex, $"Failed to correct acceptance criteria for '{wi.Id}', rev '{rev}'."); + } + } + _witClientUtils.SaveWorkItemAttachments(rev, wi); foreach (string attOriginId in rev.Attachments.Select(wiAtt => wiAtt.AttOriginId)) diff --git a/src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs b/src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs index 7235012a..f89d4dc5 100644 --- a/src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs +++ b/src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs @@ -415,6 +415,39 @@ public bool CorrectDescription(WorkItem wi, WiItem wiItem, WiRevision rev, IsAtt return descUpdated; } + public bool CorrectAcceptanceCriteria(WorkItem wi, WiItem wiItem, WiRevision rev, 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 acceptanceCriteria = wi.Fields[WiFieldReference.AcceptanceCriteria].ToString(); + if (string.IsNullOrWhiteSpace(acceptanceCriteria)) + return false; + + bool updated = false; + + CorrectImagePath(wi, wiItem, rev, ref acceptanceCriteria, ref updated, isAttachmentMigratedDelegate); + + if (updated) + { + wi.Fields[WiFieldReference.AcceptanceCriteria] = acceptanceCriteria; + } + + return updated; + } + public void CorrectComment(WorkItem wi, WiItem wiItem, WiRevision rev, IsAttachmentMigratedDelegate isAttachmentMigratedDelegate) { if (wi == null) diff --git a/src/WorkItemMigrator/tests/Migration.Wi-Import.Tests/WitClient/WitClientUtilsTests.cs b/src/WorkItemMigrator/tests/Migration.Wi-Import.Tests/WitClient/WitClientUtilsTests.cs index 105a75fb..ab6b306c 100644 --- a/src/WorkItemMigrator/tests/Migration.Wi-Import.Tests/WitClient/WitClientUtilsTests.cs +++ b/src/WorkItemMigrator/tests/Migration.Wi-Import.Tests/WitClient/WitClientUtilsTests.cs @@ -741,6 +741,17 @@ public void When_calling_correct_description_with_empty_args_Then_an_exception_i Throws.InstanceOf()); } + [Test] + public void When_calling_correct_acceptance_criteria_with_empty_args_Then_an_exception_is_thrown() + { + MockedWitClientWrapper witClientWrapper = new MockedWitClientWrapper(); + WitClientUtils wiUtils = new WitClientUtils(witClientWrapper); + + Assert.That( + () => wiUtils.CorrectAcceptanceCriteria(null, null, null, MockedIsAttachmentMigratedDelegateTrue), + Throws.InstanceOf()); + } + [Test] public void When_calling_correct_description_for_user_story_Then_description_is_updated_with_correct_image_urls() { @@ -819,6 +830,46 @@ public void When_calling_correct_description_for_bug_Then_repro_steps_is_updated Assert.That(createdWI.Fields[WiFieldReference.ReproSteps], Is.EqualTo(reproStepsAfterTransformation)); } + [Test] + public void When_calling_correct_acceptance_criteria_for_user_story_Then_acceptance_criteria_is_updated_with_correct_image_urls() + { + string acceptanceCriteriaBeforeTransformation = "My description, including file: "; + string acceptanceCriteriaAfterTransformation = "My description, including file: "; + + MockedWitClientWrapper witClientWrapper = new MockedWitClientWrapper(); + WitClientUtils wiUtils = new WitClientUtils(witClientWrapper); + + WorkItem createdWI = wiUtils.CreateWorkItem("User Story"); + createdWI.Fields[WiFieldReference.AcceptanceCriteria] = acceptanceCriteriaBeforeTransformation; + createdWI.Relations.Add(new WorkItemRelation() + { + Rel = "AttachedFile", + Url = "https://example.com/my_image.png", + Attributes = new Dictionary() { + { "comment", "Imported from Jira, original ID: 100" } + } + }); + + WiAttachment att = new WiAttachment(); + att.Change = ReferenceChangeType.Added; + att.FilePath = "C:\\Temp\\workspace\\Attachments\\100\\my_image.png"; + att.AttOriginId = "100"; + + WiRevision revision = new WiRevision(); + revision.Attachments.Add(att); + + WiItem wiItem = new WiItem(); + wiItem.Revisions = new List + { + revision + }; + + wiUtils.CorrectAcceptanceCriteria(createdWI, wiItem, revision, MockedIsAttachmentMigratedDelegateTrue); + + Assert.That(createdWI.Fields[WiFieldReference.AcceptanceCriteria], Is.EqualTo(acceptanceCriteriaAfterTransformation)); + } + + [Test] public void When_calling_apply_attachments_with_empty_args_Then_an_exception_is_thrown() {