Skip to content

Commit

Permalink
Update to dynamic field import from control file and template (#64)
Browse files Browse the repository at this point in the history
🐛 (WorkItemCloneCommand.cs): fix field access to use dictionary indexing

♻️ (WorkItemCloneCommand.cs): rename variables for clarity and
consistency

The changes fix the way fields are accessed in the `projectItem` object
by using dictionary indexing (e.g.,
`projectItem.fields["System.Title"]`) instead of direct property access.
This ensures compatibility with the data structure. Additionally,
variable names are updated for better clarity and consistency, such as
renaming `item` to `controlItem` in the `generateWorkItemsToBuildList`
method. This improves code readability and maintainability.

📝 (Resources): add tst_jsonj_export_v20.json to Resources

Introduce a new JSON file, `tst_jsonj_export_v20.json`, to the Resources
directory. This file contains a list of work items with various fields
such as `System.AreaPath`, `System.Tags`, `System.Title`,
`Custom.Product`, `Microsoft.VSTS.Scheduling.Effort`, and
`Custom.TRA_Milestone`.

The addition of this file is intended to provide a sample dataset for
testing and development purposes, ensuring that the application can
handle and process work item data correctly.

✨ (data.json): add new tasks and milestones for engineering group

New tasks and milestones are added to the data.json file to reflect the
latest project requirements and scheduling efforts. This update ensures
that all relevant tasks are tracked and managed effectively, improving
project oversight and resource allocation.

♻️ (WorkItem.cs): change fields property type to Dictionary<string,
object> for flexibility

The fields property is changed from a custom Fields type to a
Dictionary<string, object>. This change allows for more flexibility in
handling various field types and structures that may be encountered in
different work items.
  • Loading branch information
MrHinsh authored Jul 25, 2024
2 parents ca4ac09 + b339983 commit 0bc3778
Show file tree
Hide file tree
Showing 7 changed files with 472 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ await AnsiConsole.Progress()
Dictionary<string, string> queryParameters = new Dictionary<string, string>()
{
{ "@projectID", projectItem.id.ToString() },
{ "@projectTitle", projectItem.fields.SystemTitle },
{ "@projectTags", projectItem.fields.SystemTags },
{ "@projectTitle", projectItem.fields["System.Title"].ToString() },
{ "@projectTags", projectItem.fields.ContainsKey("System.Tags") ? projectItem.fields["System.Tags"].ToString() : "" },
{ "@RunName", config.RunName }
};
var query = await targetApi.CreateProjectQuery(config.targetQueryTitle, config.targetQueryFolder, config.targetQuery, queryParameters);
Expand All @@ -250,29 +250,24 @@ await AnsiConsole.Progress()
return 0;
}


private async IAsyncEnumerable<WorkItemToBuild> generateWorkItemsToBuildList(JArray jsonWorkItems, List<WorkItemFull> templateWorkItems, WorkItemFull projectItem, string targetTeamProject)
{
foreach (var item in jsonWorkItems)
foreach (var controlItem in jsonWorkItems)
{
WorkItemFull templateWorkItem = null;
int jsonItemTemplateId = 0;
if (int.TryParse(item["id"].Value<string>(), out jsonItemTemplateId))
if (int.TryParse(controlItem["id"].Value<string>(), out jsonItemTemplateId))
{
templateWorkItem = templateWorkItems.Find(x => x.id == jsonItemTemplateId);
}
WorkItemToBuild newItem = new WorkItemToBuild();
newItem.guid = Guid.NewGuid();
newItem.hasComplexRelation = false;
newItem.templateId = jsonItemTemplateId;
newItem.workItemType = templateWorkItem != null ? templateWorkItem.fields.SystemWorkItemType : "Deliverable";
newItem.workItemType = templateWorkItem != null ? templateWorkItem.fields["System.WorkItemType"].ToString() : "Deliverable";
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>>();
var fields = controlItem["fields"].ToObject<Dictionary<string, string>>();
foreach (var field in fields)
{
switch (field.Key)
Expand All @@ -281,14 +276,15 @@ private async IAsyncEnumerable<WorkItemToBuild> generateWorkItemsToBuildList(JAr
newItem.fields.Add(field.Key, string.Join("\\", targetTeamProject, field.Value));
break;
default:
if (newItem.fields.ContainsKey(field.Key))
if (templateWorkItem != null && templateWorkItem.fields.ContainsKey(field.Key) && (field.Value.Contains("${valuefromtemplate}") || field.Value.Contains("${fromtemplate}")))
{
newItem.fields[field.Key] = field.Value;
/// Add the value from the template
newItem.fields.Add(field.Key, templateWorkItem.fields[field.Key].ToString());
}
else
{
/// add value from control file
newItem.fields.Add(field.Key, field.Value);

}
break;
}
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\\tst_jsonj_export_v20.json --NonInteractive"
"commandLineArgs": "clone --cachePath ..\\..\\..\\..\\..\\.cache\\ --configFile ..\\..\\..\\..\\..\\.cache\\configuration-test.json --jsonFile ..\\..\\..\\..\\..\\TestData\\tst_jsonj_export_v20-lite.json --NonInteractive"
},
"empty": {
"commandName": "Project"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"id": 213928,
"fields": {
"System.AreaPath": "Engineering Group\\ECH Group\\ECH TPL 1",
"System.Tags": "Customer Document",
"System.Title": "Technical specification",
"Custom.Product": "CC",
"Microsoft.VSTS.Scheduling.Effort": 12,
"Custom.TRA_Milestone": "E0.1",
"System.Description": "${fromtemplate}",
"Microsoft.VSTS.Common.AcceptanceCriteria": "${fromtemplate}"
}
}
]
Loading

0 comments on commit 0bc3778

Please sign in to comment.