public void CopyTestPlans() { int i = 1; int planCount = sourceproj.TestPlans.Query("Select * From TestPlan").Count; //delete Test Plans if any existing test plans. //foreach (ITestPlan destinationplan in destinationproj.TestPlans.Query("Select * From TestPlan")) //{ // System.Diagnostics.Debug.WriteLine("Deleting Plan - {0} : {1}", destinationplan.Id, destinationplan.Name); // destinationplan.Delete(DeleteAction.ForceDeletion); ; //} foreach (ITestPlan2 sourceplan in sourceproj.TestPlans.Query("Select * From TestPlan")) { System.Diagnostics.Debug.WriteLine("Plan - {0} : {1}", sourceplan.Id, sourceplan.Name); ITestPlan2 destinationplan = null; if (WorkItemIdMap.Contains(sourceplan.Id)) { var newId = WorkItemIdMap[sourceplan.Id]; destinationplan = (ITestPlan2)destinationproj.TestPlans.Query("SELECT * from TestPlan").Where(a => a.Id == newId).FirstOrDefault();; } if (destinationplan == null) { destinationplan = (ITestPlan2)destinationproj.TestPlans.Create(); destinationplan.Name = sourceplan.Name; destinationplan.Save(); WorkItemIdMap.Map(sourceplan.Id, destinationplan.Id); } destinationplan.Description = sourceplan.Description; destinationplan.StartDate = sourceplan.StartDate; destinationplan.EndDate = sourceplan.EndDate; destinationplan.Status = sourceplan.Status; //drill down to root test suites. if (sourceplan.RootSuite != null && sourceplan.RootSuite.Entries.Count > 0) { CopyTestSuites(sourceplan, destinationplan); } destinationplan.Save(); //progressBar.Dispatcher.BeginInvoke(new Action(delegate() //{ // float progress = (float)i / (float) planCount; // progressBar.Value = ((float)i / (float) planCount) * 100; //})); i++; } }
//Copy all Test suites from source plan to destination plan. private void CopyTestSuites(ITestPlan sourceplan, ITestPlan destinationplan) { ITestSuiteEntryCollection suites = sourceplan.RootSuite.Entries; CopyTestCases(sourceplan.RootSuite, destinationplan.RootSuite); foreach (ITestSuiteEntry suite_entry in suites) { IStaticTestSuite suite = suite_entry.TestSuite as IStaticTestSuite; if (suite != null) { IStaticTestSuite newSuite = null; if (WorkItemIdMap.Contains(suite.Id)) { var newId = WorkItemIdMap[suite.Id]; newSuite = (IStaticTestSuite)destinationproj.TestSuites.Find(newId); } if (newSuite == null) { newSuite = destinationproj.TestSuites.CreateStatic(); newSuite.Title = suite.Title; destinationplan.RootSuite.Entries.Add(newSuite); destinationplan.Save(); WorkItemIdMap.Map(suite.Id, newSuite.Id); } CopyTestCases(suite, newSuite); if (suite.Entries.Count > 0) { CopySubTestSuites(suite, newSuite); } } } }
/* Copy work items to project from work item collection */ public void CopyWorkItems(WorkItemCollection workItemCollection, bool isIncludeHistoryComment, bool isIncludeHistoryLink, bool shouldFixMultilineFields) { //ReadItemMap(sourceProjectName); int i = 1; List <WorkItem> newItems = new List <WorkItem>(); foreach (WorkItem sourceWorkItem in workItemCollection) { if (WorkItemIdMap.Contains(sourceWorkItem.Id)) { //already copied continue; } var map = WorkitemTemplateMap.GetMapping(sourceWorkItem.Type); if (map == null) { logger.InfoFormat("Work Item Type {0} is not mapped", sourceWorkItem.Type.Name); continue; } var fieldMap = WorkitemTemplateMap.GetFieldMapping(sourceWorkItem.Type, map); var newWorkItem = new WorkItem(map); CopyAllfields(sourceWorkItem, fieldMap, newWorkItem); if (shouldFixMultilineFields) { try { foreach (Field sourceField in GetFields(sourceWorkItem)) { var targetField = GetField(GetFields(newWorkItem), sourceField.Name); if (sourceField.FieldDefinition.FieldType == FieldType.PlainText && targetField.FieldDefinition.FieldType == FieldType.Html) { targetField.Value = FixMultilineValue((string)sourceField.Value); } } } catch (Exception ex) { } } if (isIncludeHistoryComment) { CombineHistoryToComment(sourceWorkItem, newWorkItem, isIncludeHistoryLink); } if (ValidateAndTryFix(sourceWorkItem, newWorkItem)) { DownloadAttachment(sourceWorkItem); UploadAttachments(newWorkItem, sourceWorkItem); newWorkItem.Save(); WorkItemIdMap.Map(sourceWorkItem.Id, newWorkItem.Id); updateToLatestStatus(sourceWorkItem, newWorkItem); CreateLinks(new[] { sourceWorkItem }.ToList()); } else { logger.ErrorFormat("Work item {0} could not be saved", sourceWorkItem.Id); } i++; } CreateLinks(newItems); }