public bool PrepareAndUpdateTarget()

in src/VstsRestAPI/WorkItemAndTracking/ImportWorkItems.cs [109:262]


        public bool PrepareAndUpdateTarget(string workItemType, string workImport, string projectName, string selectedTemplate, string userAssignment)
        {
            try
            {
                workImport = workImport.Replace("$ProjectName$", projectName);
                ImportWorkItemModel.WorkItems fetchedWIs = JsonConvert.DeserializeObject<ImportWorkItemModel.WorkItems>(workImport);

                if (fetchedWIs.count > 0)
                {
                    if (workItemType.ToLower() == "epic" || workItemType.ToLower() == "feature")
                    {
                        // commenting this to fix the work item ordering issue with Azure Sentinel template - if affecting other templates, undo the same 
                        // fetchedWIs.value = fetchedWIs.value.OrderBy(x => x.id).ToArray();
                    }
                    foreach (ImportWorkItemModel.Value newWI in fetchedWIs.value)
                    {
                        newWI.fields.SystemCreatedDate = DateTime.Now.AddDays(-3);
                        Dictionary<string, object> dicWIFields = new Dictionary<string, object>();
                        string assignToUser = string.Empty;
                        if (listAssignToUsers.Count > 0)
                        {
                            assignToUser = listAssignToUsers[new Random().Next(0, listAssignToUsers.Count)] ?? string.Empty;
                        }

                        //Test cases have different fields compared to other items like bug, Epics, etc.                     
                        if ((workItemType == "Test Case"))
                        {
                            //replacing null values with Empty strngs; creation fails if the fields are null
                            if (newWI.fields.MicrosoftVSTSTCMParameters == null)
                            {
                                newWI.fields.MicrosoftVSTSTCMParameters = string.Empty;
                            }

                            if (newWI.fields.MicrosoftVSTSTCMSteps == null)
                            {
                                newWI.fields.MicrosoftVSTSTCMSteps = string.Empty;
                            }

                            if (newWI.fields.MicrosoftVSTSTCMLocalDataSource == null)
                            {
                                newWI.fields.MicrosoftVSTSTCMLocalDataSource = string.Empty;
                            }

                            dicWIFields.Add("/fields/System.Title", newWI.fields.SystemTitle);
                            dicWIFields.Add("/fields/System.State", newWI.fields.SystemState);
                            dicWIFields.Add("/fields/System.Reason", newWI.fields.SystemReason);
                            dicWIFields.Add("/fields/Microsoft.VSTS.Common.Priority", newWI.fields.MicrosoftVSTSCommonPriority);
                            dicWIFields.Add("/fields/Microsoft.VSTS.TCM.Steps", newWI.fields.MicrosoftVSTSTCMSteps);
                            dicWIFields.Add("/fields/Microsoft.VSTS.TCM.Parameters", newWI.fields.MicrosoftVSTSTCMParameters);
                            dicWIFields.Add("/fields/Microsoft.VSTS.TCM.LocalDataSource", newWI.fields.MicrosoftVSTSTCMLocalDataSource);
                            dicWIFields.Add("/fields/Microsoft.VSTS.TCM.AutomationStatus", newWI.fields.MicrosoftVSTSTCMAutomationStatus);

                            if (newWI.fields.MicrosoftVSTSCommonAcceptanceCriteria != null)
                            {
                                dicWIFields.Add("/fields/Microsoft.VSTS.Common.AcceptanceCriteria", newWI.fields.MicrosoftVSTSCommonAcceptanceCriteria);
                            }

                            if (newWI.fields.SystemTags != null)
                            {
                                dicWIFields.Add("/fields/System.Tags", newWI.fields.SystemTags);
                            }

                            dicWIFields.Add("/fields/Microsoft.VSTS.Scheduling.RemainingWork", newWI.fields.MicrosoftVSTSSchedulingRemainingWork);

                        }
                        else
                        {
                            string iterationPath = projectName;
                            string boardRowField = string.Empty;

                            if (newWI.fields.SystemIterationPath.Contains("\\"))
                            {
                                iterationPath = string.Format(@"{0}\{1}", projectName, newWI.fields.SystemIterationPath.Split('\\')[1]);

                            }

                            if (!string.IsNullOrWhiteSpace(boardRowFieldName))
                            {
                                boardRowField = string.Format("/fields/{0}", boardRowFieldName);
                            }

                            if (newWI.fields.SystemDescription == null)
                            {
                                newWI.fields.SystemDescription = newWI.fields.SystemTitle;
                            }

                            if (string.IsNullOrEmpty(newWI.fields.SystemBoardLane))
                            {
                                newWI.fields.SystemBoardLane = string.Empty;
                            }

                            dicWIFields.Add("/fields/System.Title", newWI.fields.SystemTitle);
                            if (userAssignment.ToLower() != "any")
                            {
                                if (newWI.fields.SystemState == "Done")
                                {
                                    dicWIFields.Add("/fields/System.AssignedTo", assignToUser);
                                }
                            }
                            else
                            {
                                dicWIFields.Add("/fields/System.AssignedTo", assignToUser);
                            }

                            string areaPath = newWI.fields.SystemAreaPath ?? projectName;
                            string[] areaPathSlpit = areaPath.Split('\\');
                            areaPathSlpit[0] = projectName;
                            areaPath = string.Join("\\", areaPathSlpit);
                            dicWIFields.Add("/fields/System.AreaPath", areaPath);
                            dicWIFields.Add("/fields/System.Description", newWI.fields.SystemDescription);
                            dicWIFields.Add("/fields/System.State", newWI.fields.SystemState);
                            dicWIFields.Add("/fields/System.Reason", newWI.fields.SystemReason);
                            dicWIFields.Add("/fields/Microsoft.VSTS.Common.Priority", newWI.fields.MicrosoftVSTSCommonPriority);
                            dicWIFields.Add("/fields/System.IterationPath", iterationPath);
                            dicWIFields.Add("/fields/Microsoft.VSTS.Scheduling.RemainingWork", newWI.fields.MicrosoftVSTSSchedulingRemainingWork);
                            dicWIFields.Add("/fields/Microsoft.VSTS.Scheduling.Effort", newWI.fields.MicrosoftVSTSSchedulingEffort);

                            if (newWI.fields.MicrosoftVSTSCommonAcceptanceCriteria != null)
                            {
                                dicWIFields.Add("/fields/Microsoft.VSTS.Common.AcceptanceCriteria", newWI.fields.MicrosoftVSTSCommonAcceptanceCriteria);
                            }

                            if (newWI.fields.SystemTags != null)
                            {
                                dicWIFields.Add("/fields/System.Tags", newWI.fields.SystemTags);
                            }

                            if (newWI.fields.MicrosoftVSTSTCMParameters != null)
                            {
                                dicWIFields.Add("/fields/Microsoft.VSTS.TCM.Parameters", newWI.fields.MicrosoftVSTSTCMParameters);
                            }

                            if (newWI.fields.MicrosoftVSTSTCMSteps != null)
                            {
                                dicWIFields.Add("/fields/Microsoft.VSTS.TCM.Steps", newWI.fields.MicrosoftVSTSTCMSteps);
                            }

                            if (!string.IsNullOrWhiteSpace(boardRowField))
                            {
                                dicWIFields.Add(boardRowField, newWI.fields.SystemBoardLane);
                            }
                        }
                        UpdateWorkIteminTarget(workItemType, newWI.id.ToString(), projectName, dicWIFields);
                    }

                    return true;
                }
            }
            catch (Exception ex)
            {
                logger.Debug(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + ex.Message + "\t" + "\n" + ex.StackTrace + "\n");
            }
            return false;
        }