Skip to content

Commit

Permalink
Merge pull request #890 from solidify/feature/correct-image-links-for…
Browse files Browse the repository at this point in the history
…-acceptance-criteria

Correct image links for Acceptance Criteria
  • Loading branch information
Alexander-Hjelm authored Oct 18, 2023
2 parents e831745 + 1dbad99 commit ffedacb
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/WorkItemMigrator/Migration.WIContract/WiField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
}
14 changes: 14 additions & 0 deletions src/WorkItemMigrator/WorkItemImport/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
33 changes: 33 additions & 0 deletions src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string, bool> 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<string, string, bool> isAttachmentMigratedDelegate)
{
if (wi == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,17 @@ public void When_calling_correct_description_with_empty_args_Then_an_exception_i
Throws.InstanceOf<ArgumentException>());
}

[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<ArgumentException>());
}

[Test]
public void When_calling_correct_description_for_user_story_Then_description_is_updated_with_correct_image_urls()
{
Expand Down Expand Up @@ -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: <img src=\"C:\\Temp\\workspace\\Attachments\\100\\my_image.png\">";
string acceptanceCriteriaAfterTransformation = "My description, including file: <img src=\"https://example.com/my_image.png\">";

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<string, object>() {
{ "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<WiRevision>
{
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()
{
Expand Down

0 comments on commit ffedacb

Please sign in to comment.