Skip to content

Commit

Permalink
Merge pull request #45 from nkdAgility/topic/44-update-json-format
Browse files Browse the repository at this point in the history
Topic/44 update json format
  • Loading branch information
MrHinsh authored Jul 10, 2024
2 parents 119b4c1 + 351e9d0 commit f9d27c6
Show file tree
Hide file tree
Showing 5 changed files with 461 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using Microsoft.Azure.Pipelines.WebApi;
using Microsoft.VisualStudio.Services.CircuitBreaker;
using Newtonsoft.Json.Linq;

namespace AzureDevOps.WorkItemClone.ConsoleUI.Commands
{
Expand All @@ -32,8 +33,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, WorkItemClo
AzureDevOpsApi templateApi = CreateAzureDevOpsConnection(config.templateAccessToken, config.templateOrganization, config.templateProject);
AzureDevOpsApi targetApi = CreateAzureDevOpsConnection(config.targetAccessToken, config.targetOrganization, config.targetProject);

List<jsonWorkItem> inputWorkItems = DeserializeWorkItemList(config);

JArray inputWorkItems = DeserializeWorkItemList(config);

// --------------------------------------------------------------
WriteOutSettings(config);
Expand Down Expand Up @@ -273,29 +273,48 @@ await AnsiConsole.Progress()
return 0;
}

private async IAsyncEnumerable<WorkItemToBuild> generateWorkItemsToBuildList(List<jsonWorkItem> jsonWorkItems, List<WorkItemFull> templateWorkItems, WorkItemFull projectItem, string targetTeamProject)
private async IAsyncEnumerable<WorkItemToBuild> generateWorkItemsToBuildList(JArray jsonWorkItems, List<WorkItemFull> templateWorkItems, WorkItemFull projectItem, string targetTeamProject)
{
foreach (var item in jsonWorkItems)
{
WorkItemFull templateWorkItem = null;
if (item.id != null)
if (item["id"] != null)
{
templateWorkItem = templateWorkItems.Find(x => x.id == item.id);
templateWorkItem = templateWorkItems.Find(x => x.id == (int)item["id"]);
}
WorkItemToBuild newItem = new WorkItemToBuild();
newItem.guid = Guid.NewGuid();
newItem.hasComplexRelation = false;
newItem.templateId = item.id;
newItem.templateId = (int)item["id"];
newItem.workItemType = templateWorkItem != null ? templateWorkItem.fields.SystemWorkItemType : "Deliverable";
newItem.fields = new Dictionary<string, string>()
newItem.fields = new Dictionary<string, string>();
newItem.fields.Add("System.Description", templateWorkItem != null ? templateWorkItem.fields.SystemDescription : "");
newItem.fields.Add("Microsoft.VSTS.Common.AcceptanceCriteria", templateWorkItem != null ? templateWorkItem.fields.MicrosoftVSTSCommonAcceptanceCriteria : "");
//{
// { "System.Tags", string.Join(";" , item.tags, item.area, item.fields.product, templateWorkItem != null? templateWorkItem.fields.SystemTags : "") },
// { "System.AreaPath", string.Join("\\", targetTeamProject, item.area)},
//};
var fields = item["fields"].ToObject<Dictionary<string,string>>();
foreach (var field in fields)
{
{ "System.Title", item.fields.title },
{ "Custom.Product", item.fields.product },
{ "System.Tags", string.Join(";" , item.tags, item.area, item.fields.product, templateWorkItem != null? templateWorkItem.fields.SystemTags : "") },
{ "System.AreaPath", string.Join("\\", targetTeamProject, item.area)},
{ "System.Description", templateWorkItem != null? templateWorkItem.fields.SystemDescription: "" },
{ "Microsoft.VSTS.Common.AcceptanceCriteria", templateWorkItem != null? templateWorkItem.fields.MicrosoftVSTSCommonAcceptanceCriteria: "" }
};
switch (field.Key)
{
case "System.AreaPath":
newItem.fields.Add(field.Key, string.Join("\\", targetTeamProject, field.Value));
break;
default:
if (newItem.fields.ContainsKey(field.Key))
{
newItem.fields[field.Key] = field.Value;
}
else
{
newItem.fields.Add(field.Key, field.Value);

}
break;
}
}
newItem.relations = new List<WorkItemToBuildRelation>() { new WorkItemToBuildRelation() { rel = "System.LinkTypes.Hierarchy-Reverse", targetId = projectItem.id } };
yield return newItem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace AzureDevOps.WorkItemClone.ConsoleUI.Commands
{
Expand Down Expand Up @@ -54,12 +55,12 @@ internal int EnsureIntAskIfMissing(int? value, string message = "Provide a valid
return value.Value;
}

private List<jsonWorkItem> DeserializeWorkItemList(string jsonFile)
private JArray DeserializeWorkItemList(string jsonFile)
{
List<jsonWorkItem> configWorkItems;
JArray configWorkItems;
try
{
configWorkItems = JsonConvert.DeserializeObject<List<jsonWorkItem>>(File.ReadAllText(jsonFile));
configWorkItems = JArray.Parse(File.ReadAllText(jsonFile));
}
catch (Exception ex)
{
Expand All @@ -75,7 +76,7 @@ private List<jsonWorkItem> DeserializeWorkItemList(string jsonFile)
return configWorkItems;
}

internal List<jsonWorkItem> DeserializeWorkItemList(WorkItemCloneCommandSettings config)
internal JArray DeserializeWorkItemList(WorkItemCloneCommandSettings config)
{
string CachedRunJson = System.IO.Path.Combine(config.CachePath, config.RunName, "input.json");
if (System.IO.File.Exists(CachedRunJson))
Expand All @@ -93,7 +94,7 @@ internal List<jsonWorkItem> DeserializeWorkItemList(WorkItemCloneCommandSettings
throw new Exception(config.inputJsonFile + " not found.");
}

List<jsonWorkItem> inputWorkItems;
JArray inputWorkItems;
inputWorkItems= DeserializeWorkItemList(config.inputJsonFile);
System.IO.File.WriteAllText(CachedRunJson, JsonConvert.SerializeObject(inputWorkItems, Formatting.Indented));
return inputWorkItems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"Clone": {
"commandName": "Project",
"commandLineArgs": "clone --cachePath ..\\..\\..\\..\\..\\.cache\\ --configFile ..\\..\\..\\..\\..\\.cache\\configuration-test.json --jsonFile ..\\..\\..\\..\\..\\TestData\\new.json --NonInteractive"
"commandLineArgs": "clone --cachePath ..\\..\\..\\..\\..\\.cache\\ --configFile ..\\..\\..\\..\\..\\.cache\\configuration-test.json --jsonFile ..\\..\\..\\..\\..\\TestData\\tst_jsonj_export_v20.json --NonInteractive"
},
"empty": {
"commandName": "Project"
Expand Down
25 changes: 0 additions & 25 deletions AzureDevOps.WorkItemClone/DataContracts/jsonFile.cs

This file was deleted.

Loading

0 comments on commit f9d27c6

Please sign in to comment.