From 2c5de98077fbf8ca85cc10bd3121bad97a35f310 Mon Sep 17 00:00:00 2001 From: jivkoko <48054017+jivkoko@users.noreply.github.com> Date: Wed, 29 Nov 2023 13:33:45 +0200 Subject: [PATCH] Bugfix for exceptions when migrating revisions with changed type and the configuration is set to "SkipToFinalRevisedWorkItemType": true. Removed usage of "SkipToFinalRevisedWorkItemType" confuguration. Check for Work Item ID > 0 before updating added. (#1756 ) (#1761) Co-authored-by: Kostadinov Zhivko (BD PDL-BG1) --- .../WorkItemMigrationContext.cs | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/VstsSyncMigrator.Core/Execution/MigrationContext/WorkItemMigrationContext.cs b/src/VstsSyncMigrator.Core/Execution/MigrationContext/WorkItemMigrationContext.cs index cdb74b970..366947fdf 100644 --- a/src/VstsSyncMigrator.Core/Execution/MigrationContext/WorkItemMigrationContext.cs +++ b/src/VstsSyncMigrator.Core/Execution/MigrationContext/WorkItemMigrationContext.cs @@ -649,7 +649,6 @@ private WorkItemData ReplayRevisions(List revisionsToMigrate, Work //If work item hasn't been created yet, create a shell if (targetWorkItem == null) { - var skipToFinalRevisedWorkItemType = _config.SkipToFinalRevisedWorkItemType; var finalDestType = revisionsToMigrate.Last().Type; var targetType = revisionsToMigrate.First().Type; @@ -658,16 +657,11 @@ private WorkItemData ReplayRevisions(List revisionsToMigrate, Work TraceWriteLine(LogEventLevel.Information, $"WorkItem has changed type at one of the revisions, from {targetType} to {finalDestType}"); } - if (skipToFinalRevisedWorkItemType) - { - targetType = finalDestType; - } - if (Engine.TypeDefinitionMaps.Items.ContainsKey(targetType)) { targetType = Engine.TypeDefinitionMaps.Items[targetType].Map(); } - targetWorkItem = CreateWorkItem_Shell(Engine.Target.WorkItems.Project, sourceWorkItem, skipToFinalRevisedWorkItemType ? finalDestType : targetType); + targetWorkItem = CreateWorkItem_Shell(Engine.Target.WorkItems.Project, sourceWorkItem, targetType); } if (_config.AttachRevisionHistory) @@ -691,8 +685,10 @@ private WorkItemData ReplayRevisions(List revisionsToMigrate, Work destType = Engine.TypeDefinitionMaps.Items[destType].Map(); } bool typeChange = (destType != targetWorkItem.Type); - - if (typeChange) + + int workItemId = Int32.Parse(targetWorkItem.Id); + + if (typeChange && workItemId > 0) { ValidatePatTokenRequirement(); Uri collectionUri = Engine.Target.Config.AsTeamProjectConfig().Collection; @@ -700,7 +696,7 @@ private WorkItemData ReplayRevisions(List revisionsToMigrate, Work VssConnection connection = new VssConnection(collectionUri, new VssBasicCredential(string.Empty, token)); WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient(); JsonPatchDocument patchDocument = new JsonPatchDocument(); - DateTime changedDate = ((DateTime) currentRevisionWorkItem.Fields["System.ChangedDate"].Value).AddMilliseconds(-3); + DateTime changedDate = ((DateTime)currentRevisionWorkItem.Fields["System.ChangedDate"].Value).AddMilliseconds(-3); patchDocument.Add( new JsonPatchOperation() @@ -734,9 +730,16 @@ private WorkItemData ReplayRevisions(List revisionsToMigrate, Work Value = changedDate } ); - int id = Int32.Parse(targetWorkItem.Id); - var result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, id, bypassRules:true).Result; - targetWorkItem = Engine.Target.WorkItems.GetWorkItem(id); + patchDocument.Add( + new JsonPatchOperation() + { + Operation = Operation.Add, + Path = "/fields/System.ChangedBy", + Value = currentRevisionWorkItem.Fields["System.ChangedBy"].Value.ToString() + } + ); + var result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, workItemId, bypassRules: true).Result; + targetWorkItem = Engine.Target.WorkItems.GetWorkItem(workItemId); } PopulateWorkItem(currentRevisionWorkItem, targetWorkItem, destType); @@ -748,7 +751,8 @@ private WorkItemData ReplayRevisions(List revisionsToMigrate, Work if (f.AllowedValues.Count > 0) { targetWorkItem.ToWorkItem().Fields[f.Name].Value = f.AllowedValues[0]; - } else if (f.FieldDefinition.AllowedValues.Count > 0) + } + else if (f.FieldDefinition.AllowedValues.Count > 0) { targetWorkItem.ToWorkItem().Fields[f.Name].Value = f.FieldDefinition.AllowedValues[0]; }