2023-01-24 08:45:13 -07:00

120 lines
6.0 KiB
C#

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<object> _Details;
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)
{
fileInfoCollection.Clear();
_Details = new List<object>();
Parse(fileRead, logistics, fileInfoCollection, httpClient, basePage, api, query, workItemTrackingHttpClient, project);
}
string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary<string, string> reactors)
=> throw new Exception(string.Concat("See ", nameof(Parse)));
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);
internal static List<Description> GetDescriptions(JsonElement[] jsonElements) => throw new NotImplementedException();
#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)
{
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())
throw new NullReferenceException();
Task<HttpResponseMessage> httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, query));
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));
}
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)
{
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<Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem> workItemTask;
workItemTask = workItemTrackingHttpClient.CreateWorkItemAsync(document, project, "Bug");
workItemTask.Wait();
if (workItemTask.Result is null)
{ }
// workItemTask = workItemTrackingHttpClient.UpdateWorkItemAsync(document, project, "Bug");
}
}