using Adaptation.FileHandlers.ConvertExcelToJson; using Adaptation.FileHandlers.json.WorkItems; using Adaptation.Shared; using Adaptation.Shared.Duplicator; using Adaptation.Shared.Methods; using Microsoft.TeamFoundation.WorkItemTracking.WebApi; using Microsoft.VisualStudio.Services.WebApi.Patch; using Microsoft.VisualStudio.Services.WebApi.Patch.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; namespace Adaptation.FileHandlers.json; public class ProcessData : IProcessData { private readonly List _Details; List Shared.Properties.IProcessData.Details => _Details; public ProcessData(IFileRead fileRead, Logistics logistics, List fileInfoCollection, HttpClient httpClient, string basePage, string api, string query, WorkItemTrackingHttpClient workItemTrackingHttpClient, string project) { fileInfoCollection.Clear(); _Details = new List(); Parse(fileRead, logistics, fileInfoCollection, httpClient, basePage, api, query, workItemTrackingHttpClient, project); } string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary reactors) => throw new Exception(string.Concat("See ", nameof(Parse))); Tuple> IProcessData.GetResults(IFileRead fileRead, Logistics logistics, List fileInfoCollection) => new(logistics.Logistics1[0], Array.Empty(), Array.Empty(), fileInfoCollection); internal static List GetDescriptions(JsonElement[] jsonElements) => throw new NotImplementedException(); #nullable enable private static Root GetRoot(HttpClient httpClient, string basePage, string api, int id) { Root result; Task httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, "/workItems/", id)); httpResponseMessageTask.Wait(); if (!httpResponseMessageTask.Result.IsSuccessStatusCode) throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString()); Task streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync(); streamTask.Wait(); if (!streamTask.Result.CanRead) { JsonElement? jsonElement = JsonSerializer.Deserialize(streamTask.Result); if (jsonElement is null) throw new NullReferenceException(nameof(jsonElement)); } Root? root = JsonSerializer.Deserialize(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 fileInfoCollection, HttpClient httpClient, string basePage, string api, string query, WorkItemTrackingHttpClient workItemTrackingHttpClient, string project) { 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(json, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); if (fIBacklogMesaCollection is null || !fIBacklogMesaCollection.Any()) throw new NullReferenceException(); Task httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, query)); httpResponseMessageTask.Wait(); if (!httpResponseMessageTask.Result.IsSuccessStatusCode) throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString()); Task streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync(); streamTask.Wait(); if (!streamTask.Result.CanRead) { JsonElement? jsonElement = JsonSerializer.Deserialize(streamTask.Result); if (jsonElement is null) throw new NullReferenceException(nameof(jsonElement)); } WIQL.Root? root = JsonSerializer.Deserialize(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) { raw = GetRoot(httpClient, basePage, api, workItem.Id); view = new(raw); _Details.Add(view); if (workItem.Id == 308759) break; } 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")); Task workItemTask; workItemTask = workItemTrackingHttpClient.CreateWorkItemAsync(document, project, "Bug"); workItemTask.Wait(); if (workItemTask.Result is null) { } // workItemTask = workItemTrackingHttpClient.UpdateWorkItemAsync(document, project, "Bug"); } }