Inititial Commit

This commit is contained in:
2023-04-22 21:42:57 -07:00
parent 660b89c129
commit c7995ffd4f
58 changed files with 1312 additions and 905 deletions

View File

@ -11,6 +11,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
@ -23,11 +24,11 @@ public class ProcessData : IProcessData
List<object> Shared.Properties.IProcessData.Details => _Details;
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, HttpClient httpClient, string basePage, string api, string query, WorkItemTrackingHttpClient workItemTrackingHttpClient, string project)
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, HttpClient httpClient, string basePage, string api, string query, WorkItemTrackingHttpClient workItemTrackingHttpClient, string project, string json)
{
fileInfoCollection.Clear();
_Details = new List<object>();
Parse(fileRead, logistics, fileInfoCollection, httpClient, basePage, api, query, workItemTrackingHttpClient, project);
Parse(fileRead, logistics, fileInfoCollection, httpClient, basePage, api, query, workItemTrackingHttpClient, project, json);
}
string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary<string, string> reactors)
@ -40,45 +41,33 @@ public class ProcessData : IProcessData
#nullable enable
private static Root GetRoot(HttpClient httpClient, string basePage, string api, int id)
{
Root result;
Task<HttpResponseMessage> httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, "/workItems/", id));
httpResponseMessageTask.Wait();
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
streamTask.Wait();
if (!streamTask.Result.CanRead)
{
JsonElement? jsonElement = JsonSerializer.Deserialize<JsonElement>(streamTask.Result);
if (jsonElement is null)
throw new NullReferenceException(nameof(jsonElement));
}
Root? root = JsonSerializer.Deserialize<Root>(streamTask.Result, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
streamTask.Result.Dispose();
if (root is null || root.Fields is null)
throw new NullReferenceException(nameof(root));
result = root;
return result;
}
private static void AddPatch(JsonPatchDocument document, string path, object value) => document.Add(new JsonPatchOperation { From = null, Operation = Operation.Add, Path = path, Value = value });
private void Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, HttpClient httpClient, string basePage, string api, string query, WorkItemTrackingHttpClient workItemTrackingHttpClient, string project)
private static Dictionary<string, FIBacklogMesa> GetFIBacklogMesaCollection(string json, string keySection)
{
if (fileRead is null)
throw new NullReferenceException();
if (logistics is null)
throw new NullReferenceException();
if (fileInfoCollection is null)
throw new NullReferenceException();
Root raw;
ViewModels.WorkItem view;
string json = File.ReadAllText(logistics.ReportFullPath);
FIBacklogMesa[]? fIBacklogMesaCollection = JsonSerializer.Deserialize<FIBacklogMesa[]>(json, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
if (fIBacklogMesaCollection is null || !fIBacklogMesaCollection.Any())
Dictionary<string, FIBacklogMesa> results = new();
string key;
FIBacklogMesa[]? fiBacklogMesaCollection;
fiBacklogMesaCollection = JsonSerializer.Deserialize<FIBacklogMesa[]>(json, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
if (fiBacklogMesaCollection is null || !fiBacklogMesaCollection.Any())
throw new NullReferenceException();
foreach (FIBacklogMesa fIBacklogMesa in fiBacklogMesaCollection)
{
if (string.IsNullOrEmpty(fIBacklogMesa.Requestor))
continue;
if (string.IsNullOrEmpty(fIBacklogMesa.Req))
continue;
key = $"{fIBacklogMesa.Requestor.Split(' ').First()}{keySection}{fIBacklogMesa.Req}";
if (results.ContainsKey(key))
continue;
results.Add(key, fIBacklogMesa);
}
return results;
}
private static string GetIds(HttpClient httpClient, string basePage, string api, string query)
{
StringBuilder result = new();
Task<HttpResponseMessage> httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, query));
httpResponseMessageTask.Wait();
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
@ -86,35 +75,101 @@ public class ProcessData : IProcessData
Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
streamTask.Wait();
if (!streamTask.Result.CanRead)
{
JsonElement? jsonElement = JsonSerializer.Deserialize<JsonElement>(streamTask.Result);
if (jsonElement is null)
throw new NullReferenceException(nameof(jsonElement));
}
throw new NullReferenceException(nameof(streamTask));
WIQL.Root? root = JsonSerializer.Deserialize<WIQL.Root>(streamTask.Result, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
streamTask.Result.Dispose();
if (root is null || root.WorkItems is null)
throw new NullReferenceException(nameof(root));
foreach (WIQL.WorkItem workItem in root.WorkItems)
_ = result.Append(workItem.Id).Append(',');
if (result.Length > 0)
_ = result.Remove(result.Length - 1, 1);
return result.ToString();
}
private static Value[] GetWorkItems(HttpClient httpClient, string basePage, string api, string ids)
{
Value[]? results;
// List<Value> results = new();
// Value? value;
Task<HttpResponseMessage> httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, $"/workitems?ids={ids}"));
httpResponseMessageTask.Wait();
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
streamTask.Wait();
if (!streamTask.Result.CanRead)
throw new NullReferenceException(nameof(streamTask));
JsonElement? jsonElement = JsonSerializer.Deserialize<JsonElement>(streamTask.Result);
if (jsonElement is null || jsonElement.Value.ValueKind != JsonValueKind.Object)
throw new NullReferenceException(nameof(jsonElement));
results = JsonSerializer.Deserialize<Value[]>(jsonElement.Value.EnumerateObject().Last().Value);
if (results is null || !results.Any())
throw new NullReferenceException(nameof(results));
return results;
}
private static Value[] GetExtra(string keySection, IReadOnlyList<Value> workItems, Dictionary<string, FIBacklogMesa> keyToFIBacklogMesa)
{
List<Value> results = new();
foreach (Value value in workItems)
{
raw = GetRoot(httpClient, basePage, api, workItem.Id);
view = new(raw);
_Details.Add(view);
if (workItem.Id == 308759)
break;
if (!value.Fields.SystemTitle.Contains(keySection))
continue;
if (keyToFIBacklogMesa.Remove(value.Fields.SystemTitle))
continue;
results.Add(value);
}
JsonPatchDocument document = new();
AddPatch(document, "/fields/System.Title", "Title");
AddPatch(document, "/fields/System.Description", "Description");
AddPatch(document, "/fields/System.AssignedTo", "Mike.Phares@infineon.com");
AddPatch(document, "/fields/System.AreaPath", string.Concat(project, @"\OI"));
AddPatch(document, "/fields/System.IterationPath", string.Concat(project, @"\CMP"));
return results.ToArray();
}
private static string GetDescription(FIBacklogMesa fiBacklogMesa, DateTime dateTime) =>
$"Req:{fiBacklogMesa.Req}; Submitted:{(dateTime == DateTime.MinValue ? fiBacklogMesa.Submitted : dateTime.ToString("d-MMM-yy"))}; Requestor:{fiBacklogMesa.Requestor}; AssignedTo:{fiBacklogMesa.AssignedTo}; SecondResource:{fiBacklogMesa.SecondResource}; Systems:{fiBacklogMesa.SystemS}; ";
private void Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, HttpClient httpClient, string basePage, string api, string query, WorkItemTrackingHttpClient workItemTrackingHttpClient, string project, string json)
{
if (fileRead is null)
throw new NullReferenceException();
if (logistics is null)
throw new NullReferenceException();
if (fileInfoCollection is null)
throw new NullReferenceException();
int counter = 0;
DateTime dateTime;
string description;
string keySection = " UAT for ";
string ids = GetIds(httpClient, basePage, api, query);
Value[] workItems = GetWorkItems(httpClient, basePage, api, ids);
Task<Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem> workItemTask;
workItemTask = workItemTrackingHttpClient.CreateWorkItemAsync(document, project, "Bug");
workItemTask.Wait();
if (workItemTask.Result is null)
Dictionary<string, FIBacklogMesa> keyToFIBacklogMesa = GetFIBacklogMesaCollection(json, keySection);
int count = keyToFIBacklogMesa.Count;
Value[] extra = GetExtra(keySection, workItems, keyToFIBacklogMesa);
if (count != extra.Length)
{ }
// workItemTask = workItemTrackingHttpClient.UpdateWorkItemAsync(document, project, "Bug");
if (count != keyToFIBacklogMesa.Count)
{ }
_Details.AddRange(workItems);
foreach (KeyValuePair<string, FIBacklogMesa> keyValuePair in keyToFIBacklogMesa)
{
if (counter > 5)
break;
JsonPatchDocument document = new();
if (!DateTime.TryParse(keyValuePair.Value.Submitted, out dateTime))
dateTime = DateTime.MinValue;
AddPatch(document, "/fields/System.AreaPath", project);
description = GetDescription(keyValuePair.Value, dateTime);
AddPatch(document, "/fields/System.Title", keyValuePair.Key);
AddPatch(document, "/fields/System.Description", description);
AddPatch(document, "/fields/System.IterationPath", $"{project}\\(Default)");
// AddPatch(document, "/fields/System.AssignedTo", "Mike.Phares@infineon.com");
if (!fileRead.IsEAFHosted)
continue;
workItemTask = workItemTrackingHttpClient.CreateWorkItemAsync(document, project, "Task");
workItemTask.Wait();
counter++;
if (workItemTask.Result is null)
continue;
}
}
}