Skip to content

Commit

Permalink
Bugfix for exceptions when migrating revisions with changed type and …
Browse files Browse the repository at this point in the history
…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) <koz1sf@bosch.com>
  • Loading branch information
jivkoko and Kostadinov Zhivko (BD PDL-BG1) authored Nov 29, 2023
1 parent fe76296 commit 2c5de98
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,6 @@ private WorkItemData ReplayRevisions(List<RevisionItem> 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;

Expand All @@ -658,16 +657,11 @@ private WorkItemData ReplayRevisions(List<RevisionItem> 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)
Expand All @@ -691,16 +685,18 @@ private WorkItemData ReplayRevisions(List<RevisionItem> 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;
string token = Engine.Target.Config.AsTeamProjectConfig().PersonalAccessToken;
VssConnection connection = new VssConnection(collectionUri, new VssBasicCredential(string.Empty, token));
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
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()
Expand Down Expand Up @@ -734,9 +730,16 @@ private WorkItemData ReplayRevisions(List<RevisionItem> 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);

Expand All @@ -748,7 +751,8 @@ private WorkItemData ReplayRevisions(List<RevisionItem> 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];
}
Expand Down

0 comments on commit 2c5de98

Please sign in to comment.