Skip to content

Commit

Permalink
Merge pull request #1061 from solidify/feature/preserve-changedby-for…
Browse files Browse the repository at this point in the history
…-wi-links

Preserve changedBy and changedDate for Link additions/deletions
  • Loading branch information
Alexander-Hjelm authored Aug 22, 2024
2 parents d9f3f89 + 580f852 commit 335cd46
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/WorkItemMigrator/WorkItemImport/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,11 @@ private bool ApplyAndSaveLinks(WiRevision rev, WorkItem wi, Settings settings)
continue;
}

if (link.Change == ReferenceChangeType.Added && !_witClientUtils.AddAndSaveLink(link, wi, settings))
if (link.Change == ReferenceChangeType.Added && !_witClientUtils.AddAndSaveLink(link, wi, settings, rev.Author, rev.Time))
{
success = false;
}
else if (link.Change == ReferenceChangeType.Removed && !_witClientUtils.RemoveAndSaveLink(link, wi, settings))
else if (link.Change == ReferenceChangeType.Removed && !_witClientUtils.RemoveAndSaveLink(link, wi, settings, rev.Author, rev.Time))
{
success = false;
}
Expand Down
30 changes: 17 additions & 13 deletions src/WorkItemMigrator/WorkItemImport/WitClient/WitClientUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public bool IsDuplicateWorkItemLink(IEnumerable<WorkItemRelation> links, WorkIte
return true;
}

public bool AddAndSaveLink(WiLink link, WorkItem wi, Settings settings)
public bool AddAndSaveLink(WiLink link, WorkItem wi, Settings settings, string author, DateTime time)
{
if (link == null)
{
Expand Down Expand Up @@ -86,7 +86,7 @@ public bool AddAndSaveLink(WiLink link, WorkItem wi, Settings settings)
if (!IsDuplicateWorkItemLink(wi.Relations, relatedLink))
{
wi.Relations.Add(relatedLink);
AddSingleLinkToWorkItemAndSave(link, wi, targetWorkItem, settings, "Imported link from JIRA");
AddSingleLinkToWorkItemAndSave(link, wi, targetWorkItem, settings, "Imported link from JIRA", author, time);
return true;
}
return false;
Expand All @@ -98,11 +98,11 @@ public bool AddAndSaveLink(WiLink link, WorkItem wi, Settings settings)
{
if (ex2.Message.Contains("TF201036: You cannot add a Child link between work items"))
{
ForceSwapLinkAndSave(link, wi, ex2, settings, Forward, GetWorkItem(link.TargetWiId), "child");
ForceSwapLinkAndSave(link, wi, ex2, settings, Forward, GetWorkItem(link.TargetWiId), "child", author, time);
}
else if (ex2.Message.Contains("TF201036: You cannot add a Parent link between work items"))
{
ForceSwapLinkAndSave(link, wi, ex2, settings, Reverse, GetWorkItem(link.SourceWiId), "parent");
ForceSwapLinkAndSave(link, wi, ex2, settings, Reverse, GetWorkItem(link.SourceWiId), "parent", author, time);
}
else
{
Expand All @@ -122,7 +122,7 @@ public bool AddAndSaveLink(WiLink link, WorkItem wi, Settings settings)

}

private void ForceSwapLinkAndSave(WiLink link, WorkItem wi, Exception ex2, Settings settings, string newLinkType, WorkItem wiTargetCurrent, string parentOrChild)
private void ForceSwapLinkAndSave(WiLink link, WorkItem wi, Exception ex2, Settings settings, string newLinkType, WorkItem wiTargetCurrent, string parentOrChild, string author, DateTime time)
{
Logger.Log(LogLevel.Warning, ex2.Message);
Logger.Log(LogLevel.Warning, "Attempting to fix the above issue by removing the offending link and re-adding the correct link...");
Expand All @@ -140,12 +140,12 @@ private void ForceSwapLinkAndSave(WiLink link, WorkItem wi, Exception ex2, Setti
TargetWiId = int.Parse(relation.Url.Split('/').Last()),
WiType = "System.LinkTypes.Hierarchy-Reverse"
};
RemoveAndSaveLink(linkToRemove, wiTargetCurrent, settings);
RemoveAndSaveLink(linkToRemove, wiTargetCurrent, settings, author, time);

// Add new link again
var matchedRelations = wi.Relations.Where(r => r.Rel == "System.LinkTypes.Hierarchy-" + newLinkType && r.Url.Split('/').Last() == link.TargetWiId.ToString());
wi.Relations.Remove(matchedRelations.First());
linkFixed = AddAndSaveLink(link, wi, settings);
linkFixed = AddAndSaveLink(link, wi, settings, author, time);
break;
}
}
Expand All @@ -161,7 +161,7 @@ private void ForceSwapLinkAndSave(WiLink link, WorkItem wi, Exception ex2, Setti
}
}

public bool RemoveAndSaveLink(WiLink link, WorkItem wi, Settings settings)
public bool RemoveAndSaveLink(WiLink link, WorkItem wi, Settings settings, string author, DateTime time)
{
if (link == null)
{
Expand All @@ -181,7 +181,7 @@ public bool RemoveAndSaveLink(WiLink link, WorkItem wi, Settings settings)
Logger.Log(LogLevel.Warning, $"{link} - cannot identify link to remove for '{wi.Id}'.");
return false;
}
RemoveSingleLinkFromWorkItemAndSave(link, wi, settings);
RemoveSingleLinkFromWorkItemAndSave(link, wi, settings, author, time);
wi.Relations.Remove(linkToRemove);
return true;
}
Expand Down Expand Up @@ -1039,7 +1039,7 @@ private void RemoveSingleAttachmentFromWorkItemAndSave(WiAttachment att, WorkIte
wi.Relations = result.Relations;
}

private void AddSingleLinkToWorkItemAndSave(WiLink link, WorkItem sourceWI, WorkItem targetWI, Settings settings, string comment)
private void AddSingleLinkToWorkItemAndSave(WiLink link, WorkItem sourceWI, WorkItem targetWI, Settings settings, string comment, string changedBy, DateTime changedDate)
{
// Create a patch document for a new work item.
// Specify a relation to the existing work item.
Expand All @@ -1058,7 +1058,9 @@ private void AddSingleLinkToWorkItemAndSave(WiLink link, WorkItem sourceWI, Work
comment
}
}
}
},
JsonPatchDocUtils.CreateJsonFieldPatchOp(Operation.Add, WiFieldReference.ChangedBy, changedBy),
JsonPatchDocUtils.CreateJsonFieldPatchOp(Operation.Add, WiFieldReference.ChangedDate, changedDate)
};

if (sourceWI.Id.HasValue)
Expand All @@ -1069,7 +1071,7 @@ private void AddSingleLinkToWorkItemAndSave(WiLink link, WorkItem sourceWI, Work
Logger.Log(LogLevel.Info, $"Updated new work item Id:{sourceWI.Id} with link to work item ID:{targetWI.Id}");
}

private void RemoveSingleLinkFromWorkItemAndSave(WiLink link, WorkItem sourceWI, Settings settings)
private void RemoveSingleLinkFromWorkItemAndSave(WiLink link, WorkItem sourceWI, Settings settings, string changedBy, DateTime changedDate)
{
WorkItemRelation rel = sourceWI.Relations.SingleOrDefault(a =>
a.Rel == link.WiType
Expand All @@ -1091,7 +1093,9 @@ private void RemoveSingleLinkFromWorkItemAndSave(WiLink link, WorkItem sourceWI,
{
Operation = Operation.Remove,
Path = "/relations/"+relIndex
}
},
JsonPatchDocUtils.CreateJsonFieldPatchOp(Operation.Add, WiFieldReference.ChangedBy, changedBy),
JsonPatchDocUtils.CreateJsonFieldPatchOp(Operation.Add, WiFieldReference.ChangedDate, changedDate)
};

if (sourceWI.Id.HasValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ public void When_calling_add_link_with_empty_args_Then_an_exception_is_thrown()
WitClientUtils wiUtils = new WitClientUtils(witClientWrapper);

Assert.That(
() => wiUtils.AddAndSaveLink(null, null, null),
() => wiUtils.AddAndSaveLink(null, null, null, null, DateTime.Now),
Throws.InstanceOf<ArgumentException>());
}

Expand All @@ -615,7 +615,7 @@ public void When_calling_add_link_with_valid_args_Then_a_link_is_added()
Change = ReferenceChangeType.Added
};

wiUtils.AddAndSaveLink(link, createdWI, settings);
wiUtils.AddAndSaveLink(link, createdWI, settings, "author", DateTime.Now);

WorkItemRelation rel = createdWI.Relations[0];

Expand Down Expand Up @@ -659,7 +659,7 @@ public void When_calling_add_link_with_valid_args_and_an_attachment_is_present_o
Change = ReferenceChangeType.Added
};

wiUtils.AddAndSaveLink(link, createdWI, settings);
wiUtils.AddAndSaveLink(link, createdWI, settings, "author", DateTime.Now);

WorkItemRelation rel = createdWI.Relations.Where(rl => rl.Rel != "AttachedFile").Single();

Expand Down Expand Up @@ -703,7 +703,7 @@ public void When_calling_add_link_with_valid_args_and_an_attachment_is_present_o
Change = ReferenceChangeType.Added
};

wiUtils.AddAndSaveLink(link, createdWI, settings);
wiUtils.AddAndSaveLink(link, createdWI, settings, "author", DateTime.Now);

WorkItemRelation rel = createdWI.Relations[0];

Expand All @@ -721,7 +721,7 @@ public void When_calling_remove_link_with_empty_args_Then_an_exception_is_thrown
WitClientUtils wiUtils = new WitClientUtils(witClientWrapper);

Assert.That(
() => wiUtils.RemoveAndSaveLink(null, null, null),
() => wiUtils.RemoveAndSaveLink(null, null, null, null, DateTime.Now),
Throws.InstanceOf<ArgumentException>());
}

Expand All @@ -740,7 +740,7 @@ public void When_calling_remove_link_with_no_link_added_Then_false_is_returned()
WiType = "System.LinkTypes.Hierarchy-Forward"
};

bool result = wiUtils.RemoveAndSaveLink(link, createdWI, settings);
bool result = wiUtils.RemoveAndSaveLink(link, createdWI, settings, "author", DateTime.Now);

Assert.That(result, Is.EqualTo(false));
}
Expand All @@ -766,9 +766,9 @@ public void When_calling_remove_link_with_link_added_Then_link_is_removed()
Change = ReferenceChangeType.Added
};

wiUtils.AddAndSaveLink(link, createdWI, settings);
wiUtils.AddAndSaveLink(link, createdWI, settings, "author", DateTime.Now);

bool result = wiUtils.RemoveAndSaveLink(link, createdWI, settings);
bool result = wiUtils.RemoveAndSaveLink(link, createdWI, settings, "author", DateTime.Now);

Assert.Multiple(() =>
{
Expand Down

0 comments on commit 335cd46

Please sign in to comment.