197 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			197 lines
		
	
	
		
			8.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Adaptation.FileHandlers.json.WorkItems;
 | |
| using Adaptation.Shared;
 | |
| using Adaptation.Shared.Duplicator;
 | |
| using Adaptation.Shared.Methods;
 | |
| using log4net;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Collections.ObjectModel;
 | |
| using System.IO;
 | |
| using System.Text.Json;
 | |
| using System.Text.Json.Serialization;
 | |
| 
 | |
| namespace Adaptation.FileHandlers.ADO;
 | |
| 
 | |
| public class ProcessData : IProcessData
 | |
| {
 | |
| 
 | |
|     private readonly List<object> _Details;
 | |
| 
 | |
|     List<object> Shared.Properties.IProcessData.Details => _Details;
 | |
| 
 | |
|     private readonly ILog _Log;
 | |
| 
 | |
|     public ProcessData(IFileRead fileRead, Logistics logistics, string targetFileLocation, string url, List<FileInfo> fileInfoCollection)
 | |
|     {
 | |
|         if (fileRead.IsEAFHosted)
 | |
|         { }
 | |
|         if (url is null)
 | |
|             throw new ArgumentNullException(nameof(url));
 | |
|         _Details = new List<object>();
 | |
|         _Log = LogManager.GetLogger(typeof(ProcessData));
 | |
|         WriteFiles(fileRead, logistics, targetFileLocation, fileInfoCollection);
 | |
|     }
 | |
| 
 | |
|     string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary<string, string> reactors) =>
 | |
|         throw new Exception(string.Concat("See ", nameof(WriteFiles)));
 | |
| 
 | |
|     Tuple<string, Test[], JsonElement[], List<FileInfo>> IProcessData.GetResults(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection) =>
 | |
|         new(logistics.Logistics1[0], Array.Empty<Test>(), Array.Empty<JsonElement>(), fileInfoCollection);
 | |
| 
 | |
| #nullable enable
 | |
| 
 | |
|     internal static List<Description> GetDescriptions(JsonElement[] jsonElements)
 | |
|     {
 | |
|         List<Description> results = new();
 | |
|         Description? description;
 | |
|         JsonSerializerOptions jsonSerializerOptions = new() { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString };
 | |
|         foreach (JsonElement jsonElement in jsonElements)
 | |
|         {
 | |
|             if (jsonElement.ValueKind != JsonValueKind.Object)
 | |
|                 throw new Exception();
 | |
|             description = JsonSerializer.Deserialize<Description>(jsonElement.ToString(), jsonSerializerOptions);
 | |
|             if (description is null)
 | |
|                 continue;
 | |
|             results.Add(description);
 | |
|         }
 | |
|         return results;
 | |
|     }
 | |
| 
 | |
|     private void WriteFiles(IFileRead fileRead, Logistics logistics, string destinationDirectory, List<FileInfo> fileInfoCollection)
 | |
|     {
 | |
|         bool keepRelations = true;
 | |
|         if (!Directory.Exists(destinationDirectory))
 | |
|             _ = Directory.CreateDirectory(destinationDirectory);
 | |
|         string json = File.ReadAllText(logistics.ReportFullPath);
 | |
|         WorkItem[]? workItems = JsonSerializer.Deserialize<WorkItem[]>(json);
 | |
|         if (workItems is null)
 | |
|             throw new Exception(nameof(workItems));
 | |
|         _Details.Add(workItems);
 | |
|         ReadOnlyDictionary<int, Record> keyValuePairs = GetWorkItems(workItems, keepRelations);
 | |
|         WriteFiles(fileRead, destinationDirectory, fileInfoCollection, keyValuePairs);
 | |
|     }
 | |
| 
 | |
|     private static ReadOnlyDictionary<int, Record> GetWorkItems(WorkItem[] workItems, bool keepRelations)
 | |
|     {
 | |
|         ReadOnlyDictionary<int, Record> results;
 | |
|         Dictionary<int, WorkItem> keyValuePairs = new();
 | |
|         foreach (WorkItem workItem in workItems)
 | |
|             keyValuePairs.Add(workItem.Id, workItem);
 | |
|         results = GetKeyValuePairs(new(keyValuePairs), keepRelations);
 | |
|         return results;
 | |
|     }
 | |
| 
 | |
|     private static void WriteFiles(IFileRead fileRead, string destinationDirectory, List<FileInfo> fileInfoCollection, ReadOnlyDictionary<int, Record> keyValuePairs)
 | |
|     {
 | |
|         string old;
 | |
|         string json;
 | |
|         string checkFile;
 | |
|         WorkItem workItem;
 | |
|         string workItemType;
 | |
|         string singletonDirectory;
 | |
|         string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileRead.ReportFullPath);
 | |
|         string rootDirectory = Path.Combine(destinationDirectory, fileNameWithoutExtension);
 | |
|         if (string.IsNullOrEmpty(rootDirectory))
 | |
|             throw new NullReferenceException(nameof(rootDirectory));
 | |
|         foreach (KeyValuePair<int, Record> keyValuePair in keyValuePairs)
 | |
|         {
 | |
|             workItem = keyValuePair.Value.WorkItem;
 | |
|             workItemType = workItem.WorkItemType.Replace(" ", "-");
 | |
|             json = JsonSerializer.Serialize(workItem, WorkItemSourceGenerationContext.Default.WorkItem);
 | |
|             singletonDirectory = Path.Combine(rootDirectory, workItemType, $"{workItem.Id}");
 | |
|             if (!Directory.Exists(singletonDirectory))
 | |
|                 _ = Directory.CreateDirectory(singletonDirectory);
 | |
|             checkFile = Path.Combine(singletonDirectory, $"{workItem.Id}-{workItemType.ToLower()}.json");
 | |
|             old = File.Exists(checkFile) ? File.ReadAllText(checkFile) : string.Empty;
 | |
|             if (old == json)
 | |
|                 continue;
 | |
|             File.WriteAllText(checkFile, json);
 | |
|             if (!fileRead.IsEAFHosted)
 | |
|                 fileInfoCollection.Add(new(checkFile));
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private static ReadOnlyDictionary<int, Record> GetKeyValuePairs(ReadOnlyDictionary<int, WorkItem> keyValuePairs, bool keepRelations)
 | |
|     {
 | |
|         Dictionary<int, Record> results = new();
 | |
|         Record record;
 | |
|         List<bool> nests = new();
 | |
|         WorkItem? parentWorkItem;
 | |
|         ReadOnlyCollection<Record> childRecords;
 | |
|         ReadOnlyCollection<Record> relatedRecords;
 | |
|         ReadOnlyCollection<Record> successorRecords;
 | |
|         foreach (KeyValuePair<int, WorkItem> keyValuePair in keyValuePairs)
 | |
|         {
 | |
|             nests.Clear();
 | |
|             if (keyValuePair.Value.Parent is null)
 | |
|                 parentWorkItem = null;
 | |
|             else
 | |
|                 _ = keyValuePairs.TryGetValue(keyValuePair.Value.Parent.Value, out parentWorkItem);
 | |
|             try
 | |
|             {
 | |
|                 childRecords = Record.GetKeyValuePairs(keyValuePairs, keyValuePair.Value, "Child", nests, keepRelations); // Forward
 | |
|                 relatedRecords = Record.GetKeyValuePairs(keyValuePairs, keyValuePair.Value, "Related", nests, keepRelations); // Related
 | |
|                 successorRecords = Record.GetKeyValuePairs(keyValuePairs, keyValuePair.Value, "Successor", nests, keepRelations); // Forward
 | |
|                 // predecessorRecords = Record.GetKeyValuePairs(keyValuePairs, keyValuePair.Value, "Predecessor", nests, keepRelations); // Reverse
 | |
|                 record = Record.Get(keyValuePair.Value, parentWorkItem, childRecords, relatedRecords, successorRecords, keepRelations);
 | |
|             }
 | |
|             catch (Exception)
 | |
|             {
 | |
|                 Dictionary<string, string>? tag = null;
 | |
|                 record = new(keyValuePair.Value, parentWorkItem, Array.Empty<Record>(), Array.Empty<Record>(), Array.Empty<Record>(), tag);
 | |
|             }
 | |
|             results.Add(keyValuePair.Key, record);
 | |
|         }
 | |
|         return new(results);
 | |
|     }
 | |
| 
 | |
|     private static string GetIndexLines(ReadOnlyCollection<string> frontMatterLines, Record record)
 | |
|     {
 | |
|         List<string> results = new();
 | |
|         results.Clear();
 | |
|         results.AddRange(frontMatterLines);
 | |
|         results.Add(string.Empty);
 | |
|         results.Add($"# {record.WorkItem.Id}");
 | |
|         results.Add(string.Empty);
 | |
|         results.Add("## Backlog");
 | |
|         results.Add(string.Empty);
 | |
|         results.Add("## Todo");
 | |
|         results.Add(string.Empty);
 | |
|         results.Add("## In Progress");
 | |
|         results.Add(string.Empty);
 | |
|         results.Add("## Done");
 | |
|         results.Add(string.Empty);
 | |
|         return string.Join(Environment.NewLine, results);
 | |
|     }
 | |
| 
 | |
|     private static string GetMarkdownLines(string url, Record record, string jsonDirectory, string iterationPathDirectory)
 | |
|     {
 | |
|         List<string> results = new();
 | |
|         string link;
 | |
|         string target;
 | |
|         results.Add($"# {record.WorkItem.Id}");
 | |
|         results.Add(string.Empty);
 | |
|         results.Add($"## {record.WorkItem.Title}");
 | |
|         results.Add(string.Empty);
 | |
|         if (record.Children is not null)
 | |
|         {
 | |
|             foreach (Record r in record.Children)
 | |
|                 results.Add($"- [{r.WorkItem.Id}]({url}{r.WorkItem.Id})");
 | |
|         }
 | |
|         results.Add(string.Empty);
 | |
|         results.Add("```bash");
 | |
|         if (record.Children is not null)
 | |
|         {
 | |
|             foreach (Record r in record.Children)
 | |
|             {
 | |
|                 link = Path.Combine(jsonDirectory, $"{r.WorkItem.Id}-{r.WorkItem.WorkItemType}");
 | |
|                 target = Path.Combine(iterationPathDirectory, r.WorkItem.WorkItemType, $"{r.WorkItem.Id}-{r.WorkItem.WorkItemType}", r.WorkItem.Id.ToString());
 | |
|                 results.Add($"mklink /J \"{link}\" \"{target}\"");
 | |
|             }
 | |
|         }
 | |
|         results.Add("```");
 | |
|         results.Add(string.Empty);
 | |
|         return string.Join(Environment.NewLine, results);
 | |
|     }
 | |
| 
 | |
| } |