private WorkItemClassificationNode UpdateWithChildNode(ref WorkItemClassificationNode parent, string childName, TreeNodeStructureType childType) { var children = parent.Children?.ToList() ?? new List <WorkItemClassificationNode>(); children.Add(new WorkItemClassificationNode() { Name = childName, StructureType = childType }); parent.Children = children.ToArray(); parent.HasChildren = true; return(parent); }
private void CreateAndSaveClassificationNodes(IOrderedEnumerable <KeyValuePair <int, List <string> > > groups, TreeNodeStructureType nodeType) { var azureClassificationNodes = new List <WorkItemClassificationNode>(); var projectId = projectClient.GetProject(ProjectName).Result.Id; foreach (var nodeGroup in groups) { if (nodeGroup.Key == 0) { // Root nodes foreach (var rootArea in nodeGroup.Value) { azureClassificationNodes.Add(new WorkItemClassificationNode() { Name = rootArea, StructureType = nodeType }); } } else { // These are child nodes foreach (var childArea in nodeGroup.Value) { // Split the path and start finding out where to put the area var splitPath = childArea.Split('\\'); var pathEnd = splitPath[splitPath.Length - 1]; var pathStart = splitPath[0]; var rootArea = azureClassificationNodes.SingleOrDefault(x => x.Name == pathStart); if (rootArea == null) { Logger.Warning($"{GetType()} Root area not found, creating it anyway {pathStart}"); azureClassificationNodes.Add(new WorkItemClassificationNode() { Name = pathStart, StructureType = nodeType }); rootArea = azureClassificationNodes.SingleOrDefault(x => x.Name == pathStart); } for (int i = 1; i < splitPath.Length - 1; i++) { rootArea = rootArea.Children.SingleOrDefault(x => x.Name == splitPath[i]); if (rootArea == null) { var previousRoot = azureClassificationNodes.SingleOrDefault(x => x.Name == splitPath[i - 1]); Logger.Warning($"{GetType()} Child area not found, creating {splitPath[i]} under {previousRoot?.Name}"); try { UpdateWithChildNode(ref previousRoot, splitPath[i], nodeType); rootArea = previousRoot.Children.SingleOrDefault(x => x.Name == splitPath[i]); } catch (Exception e) { Logger.Error($"{GetType()} Failed creating child area, error message below:{Environment.NewLine}{e.Message}{Environment.NewLine}{e.StackTrace}"); Logger.Error($"{GetType()} Try to create the area manually in Azure and run the export again"); } } } UpdateWithChildNode(ref rootArea, pathEnd, nodeType); } } } foreach (var classificationNode in azureClassificationNodes) { var treeStructureGroup = TreeStructureGroup.Iterations; if (nodeType == TreeNodeStructureType.Area) { treeStructureGroup = TreeStructureGroup.Areas; } SaveClassificationNodesRecursive(projectId, classificationNode, treeStructureGroup); } }