257 lines
11 KiB
C#
257 lines
11 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.Linq;
|
|
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));
|
|
fileInfoCollection.Clear();
|
|
_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);
|
|
WriteFileStructure(destinationDirectory, keyValuePairs);
|
|
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 WriteFileStructure(string destinationDirectory, ReadOnlyDictionary<int, Record> keyValuePairs)
|
|
{
|
|
ReadOnlyCollection<string> collection = GetDirectories(destinationDirectory, keyValuePairs);
|
|
foreach (string directory in collection)
|
|
{
|
|
if (directory.Length > 222)
|
|
continue;
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
}
|
|
}
|
|
|
|
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}-{workItemType}", $"{workItem.Id}");
|
|
if (!Directory.Exists(singletonDirectory))
|
|
_ = Directory.CreateDirectory(singletonDirectory);
|
|
checkFile = Path.Combine(singletonDirectory, ".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)
|
|
{
|
|
record = new(keyValuePair.Value, parentWorkItem, Array.Empty<Record>(), Array.Empty<Record>(), Array.Empty<Record>());
|
|
}
|
|
results.Add(keyValuePair.Key, record);
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
private static ReadOnlyCollection<string> GetDirectories(string destinationDirectory, ReadOnlyDictionary<int, Record> keyValuePairs)
|
|
{
|
|
List<string> results = new();
|
|
Record record;
|
|
string directory;
|
|
List<bool> nests = new();
|
|
ReadOnlyCollection<string> childrenDirectories;
|
|
string dateDirectory = Path.Combine(destinationDirectory, "_", DateTime.Now.ToString("yyyy-MM-dd"));
|
|
foreach (KeyValuePair<int, Record> keyValuePair in keyValuePairs)
|
|
{
|
|
record = keyValuePair.Value;
|
|
if (record.Parent is not null && (record.WorkItem.Parent is null || record.Parent.Id != record.WorkItem.Parent.Value))
|
|
continue;
|
|
if (record.Parent is not null)
|
|
continue;
|
|
// if (record.WorkItem.Id == 110730)
|
|
// continue;
|
|
// if (record.WorkItem.Id == 110732)
|
|
// continue;
|
|
nests.Clear();
|
|
directory = Path.Combine(dateDirectory, $"{record.WorkItem.WorkItemType.Substring(0, 1)}-{record.WorkItem.Id}-{record.WorkItem.Title.Trim().Substring(0, 1)}");
|
|
childrenDirectories = GetChildrenDirectories(keyValuePairs, nests, directory, record);
|
|
results.AddRange(childrenDirectories);
|
|
}
|
|
return new(results.Distinct().ToArray());
|
|
}
|
|
|
|
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);
|
|
foreach (Record r in record.Children)
|
|
results.Add($"- [{r.WorkItem.Id}]({url}{r.WorkItem.Id})");
|
|
results.Add(string.Empty);
|
|
results.Add("```bash");
|
|
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);
|
|
}
|
|
|
|
private static ReadOnlyCollection<string> GetChildrenDirectories(ReadOnlyDictionary<int, Record> keyValuePairs, List<bool> nests, string parentDirectory, Record record)
|
|
{
|
|
List<string> results = new();
|
|
nests.Add(true);
|
|
string directory;
|
|
Record? childRecord;
|
|
ReadOnlyCollection<string> childrenDirectories;
|
|
foreach (Record r in record.Children)
|
|
{
|
|
// if (record.WorkItem.Id == 110730)
|
|
// continue;
|
|
// if (record.WorkItem.Id == 110732)
|
|
// continue;
|
|
directory = Path.Combine(parentDirectory, $"{r.WorkItem.WorkItemType.Substring(0, 1)}-{r.WorkItem.Id}-{r.WorkItem.Title.Trim().Substring(0, 1)}");
|
|
results.Add(directory);
|
|
if (!keyValuePairs.TryGetValue(r.WorkItem.Id, out childRecord))
|
|
continue;
|
|
if (nests.Count > 99)
|
|
break;
|
|
childrenDirectories = GetChildrenDirectories(keyValuePairs, nests, directory, childRecord);
|
|
results.AddRange(childrenDirectories);
|
|
}
|
|
return new(results);
|
|
}
|
|
|
|
} |