Update API

This commit is contained in:
2023-04-23 11:03:48 -07:00
parent 4e2cb5d696
commit 7d4b0383e9
3 changed files with 111 additions and 31 deletions

View File

@ -126,6 +126,58 @@ public class ProcessData : IProcessData
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 static void Update(HttpClient httpClient, string basePage, string api, string query, HttpContent httpContent)
{
Task<HttpResponseMessage> httpResponseMessageTask = httpClient.PatchAsync(string.Concat(basePage, api, query), httpContent);
httpResponseMessageTask.Wait();
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
Task<string> stringTask = httpResponseMessageTask.Result.Content.ReadAsStringAsync();
stringTask.Wait();
}
private static void UpdateIds(HttpClient httpClient, string basePage, string api, string[] checkFiles)
{
int? idIndex;
string[] lines;
string[] segments;
string stringPayload;
HttpContent httpContent;
foreach (string checkFile in checkFiles)
{
idIndex = null;
lines = File.ReadAllLines(checkFile);
if (lines.Length < 1)
continue;
segments = lines.First().Split(',');
for (int i = 0; i < segments.Length; i++)
{
if (segments[i] == "ID")
{
idIndex = i;
break;
}
}
if (idIndex is null)
continue;
for (int i = 1; i < lines.Length; i++)
{
segments = lines[i].Split(',');
if (segments.Length < idIndex.Value)
continue;
var payload = new
{
op = "replace",
path = "/fields/System.IterationPath",
value = "Mesa_FI"
};
stringPayload = JsonSerializer.Serialize(payload);
httpContent = new StringContent($"[{stringPayload}]", Encoding.UTF8, "application/json-patch+json");
Update(httpClient, basePage, api, $"/workitems/{segments[idIndex.Value].Replace("\"", string.Empty)}?api-version=1.0", httpContent);
}
}
}
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)
@ -138,37 +190,46 @@ public class ProcessData : IProcessData
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;
Dictionary<string, FIBacklogMesa> keyToFIBacklogMesa = GetFIBacklogMesaCollection(json, keySection);
int count = keyToFIBacklogMesa.Count;
Value[] extra = GetExtra(keySection, workItems, keyToFIBacklogMesa);
if (count != extra.Length)
{ }
if (count != keyToFIBacklogMesa.Count)
{ }
_Details.AddRange(workItems);
foreach (KeyValuePair<string, FIBacklogMesa> keyValuePair in keyToFIBacklogMesa)
string? directory = Path.GetDirectoryName(fileRead.ReportFullPath);
if (directory is null)
throw new Exception();
string[] checkFiles = Directory.GetFiles(directory, "*.csv", SearchOption.TopDirectoryOnly);
if (checkFiles.Any())
UpdateIds(httpClient, basePage, api, checkFiles);
else
{
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;
string ids = GetIds(httpClient, basePage, api, query);
Value[] workItems = GetWorkItems(httpClient, basePage, api, ids);
Task<Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem> workItemTask;
Dictionary<string, FIBacklogMesa> keyToFIBacklogMesa = GetFIBacklogMesaCollection(json, keySection);
int count = keyToFIBacklogMesa.Count;
Value[] extra = GetExtra(keySection, workItems, keyToFIBacklogMesa);
if (count != extra.Length)
{ }
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.IterationPath", project);
AddPatch(document, "/fields/System.Title", keyValuePair.Key);
AddPatch(document, "/fields/System.Description", description);
// 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;
}
}
}