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 subTest suites from source root test suite to destination plan's root test suites. private void CopyTestCases(IStaticTestSuite sourcesuite, IStaticTestSuite destinationsuite) { ITestSuiteEntryCollection suiteentrys = sourcesuite.TestCases; destinationsuite.Entries.RemoveCases(destinationsuite.Entries.OfType <ITestCase>()); foreach (ITestSuiteEntry testcase in suiteentrys) { try { //check whether testcase exists in new work items(closed work items may not be created again). if (!WorkItemIdMap.Contains(testcase.TestCase.WorkItem.Id)) { continue; } int newWorkItemID = WorkItemIdMap[testcase.TestCase.WorkItem.Id]; ITestCase tc = destinationproj.TestCases.Find(newWorkItemID); destinationsuite.Entries.Add(tc); bool updateTestCase = false; TestActionCollection testActionCollection = tc.Actions; foreach (var item in testActionCollection) { var sharedStepRef = item as ISharedStepReference; if (sharedStepRef != null) { int newSharedStepId = (int)WorkItemIdMap[sharedStepRef.SharedStepId]; //GetNewSharedStepId(testCase.Id, sharedStepRef.SharedStepId); if (0 != newSharedStepId) { sharedStepRef.SharedStepId = newSharedStepId; updateTestCase = true; } } } if (updateTestCase) { Console.WriteLine(); Console.WriteLine("Test case with Id: {0} updated", tc.Id); tc.Save(); } } catch (Exception) { logger.Info("Error retrieving Test case " + testcase.TestCase.WorkItem.Id + ": " + testcase.Title); } } }
//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); } } } }
//private void ReadItemMap(string sourceProjectName) //{ // string filaPath = String.Format(@"Map\ID_map_{0}_to_{1}.txt", sourceProjectName, destinationProject.Name); // itemMap = new Hashtable(); // string line; // if (File.Exists(filaPath)) // { // System.IO.StreamReader file = new System.IO.StreamReader(filaPath); // while ((line = file.ReadLine()) != null) // { // if (line.Contains("Source ID|Target ID")) // { // continue; // } // string[] idMap = line.Split(new char[] { '|' }); // if (idMap[0].Trim() != "" && idMap[1].Trim() != "") // { // itemMap.Map(Convert.ToInt32(idMap[0].Trim()), Convert.ToInt32(idMap[1].Trim())); // } // } // file.Close(); // } //} /* Set links between workitems */ private void CreateLinks(List <WorkItem> workItemCollection) { List <int> linkedWorkItemList = new List <int>(); //WorkItemCollection targetWorkItemCollection = GetWorkItemCollection(); foreach (WorkItem workItem in workItemCollection) { WorkItemLinkCollection links = workItem.WorkItemLinks; if (links.Count > 0) { int newWorkItemID = (int)WorkItemIdMap[workItem.Id]; WorkItem newWorkItem = targetWorkItemStore.GetWorkItem(newWorkItemID); foreach (WorkItemLink link in links) { try { WorkItem targetItem = sourceWorkItemStore.GetWorkItem(link.TargetId); if (WorkItemIdMap.Contains(link.TargetId) && targetItem != null) { int targetWorkItemID = 0; if (WorkItemIdMap.Contains(link.TargetId)) { targetWorkItemID = (int)WorkItemIdMap[link.TargetId]; } //if the link is not already created(check if target id is not in list) if (!linkedWorkItemList.Contains(link.TargetId)) { try { WorkItemLinkTypeEnd linkTypeEnd = targetWorkItemStore.WorkItemLinkTypes.LinkTypeEnds[link.LinkTypeEnd.Name]; newWorkItem.Links.Add(new RelatedLink(linkTypeEnd, targetWorkItemID)); ArrayList array = newWorkItem.Validate(); if (array.Count == 0) { newWorkItem.Save(); } else { logger.Info("WorkItem Validation failed at link setup for work item: " + workItem.Id); } } catch (Exception ex) { logger.ErrorFormat("Error occured when crearting link for work item: {0} target item: {1}", workItem.Id, link.TargetId); logger.Error("Error detail", ex); } } } else { logger.Info("Link is not created for work item: " + workItem.Id + " - target item: " + link.TargetId + " does not exist"); } } catch (Exception) { logger.Warn("Link is not created for work item: " + workItem.Id + " - target item: " + link.TargetId + " is not in Source TFS or you do not have permission to access"); } } //add the work item to list if the links are processed linkedWorkItemList.Add(workItem.Id); } } }
/* 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); }