Skip to content

Commit

Permalink
Update with full validation that no longer craps out for fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHinsh committed Jul 12, 2024
1 parent be4f259 commit c6a9ac6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ private async IAsyncEnumerable<WorkItemToBuild> generateWorkItemsToBuildRelation

}

internal Dictionary<string, bool> foundFields = new Dictionary<string, bool>();

internal Dictionary<string, bool> foundAreaPaths = new Dictionary<string, bool>();
private async Task<bool> ValidateOperations(AzureDevOpsApi targetApi, WorkItemToBuild item, WorkItemAdd itemToAdd)
{
Expand Down Expand Up @@ -446,17 +446,32 @@ private async Task<bool> CheckAreaPathExists(AzureDevOpsApi targetApi, WorkItemT
return valid;
}

internal Dictionary<string, WorkItemFieldList> fieldsForTypes = new Dictionary<string, WorkItemFieldList>();
internal Dictionary<string, bool> foundFields = new Dictionary<string, bool>();
private async Task<bool> CheckFieldExists(AzureDevOpsApi targetApi, WorkItemToBuild item, bool valid, FieldOperation operation)
{
WorkItemFieldList fieldsLookup = null;
if (fieldsForTypes.ContainsKey(item.workItemType))
{
fieldsLookup = fieldsForTypes[item.workItemType];
}
else
{
fieldsLookup = await targetApi.GetFieldsOnWorkItem(item.workItemType);
fieldsForTypes.Add(item.workItemType, fieldsLookup);
}
int idx = operation.path.LastIndexOf('/');
string referenceName = operation.path.Substring(idx + 1);
string uniqueFieldkey = $"{item.workItemType}{operation.path}";

if (foundFields.ContainsKey(uniqueFieldkey))
{
valid = valid && foundFields[uniqueFieldkey];
}
else
{
FieldItem field = await targetApi.GetFieldOnWorkItem(item.workItemType, operation.path);
if (field == null)
var foundField = fieldsLookup.value.FirstOrDefault(x => x.referenceName == referenceName);
if (foundField == null)
{
AnsiConsole.WriteLine($"[VALIDATE] Field {operation.path} does not exist on {item.workItemType} This is required to create a work item.");
valid = false;
Expand Down
10 changes: 9 additions & 1 deletion AzureDevOps.WorkItemClone/AzureDevOpsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ public async Task<NodeClassification> GetNodeClassification(string nodePath)
}
}

public async Task<FieldItem> GetFieldOnWorkItem(string wit, string fieldRefName)
public async Task<WorkItemFieldList> GetFieldsOnWorkItem(string wit)
{
//GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitemtypes/{type}/fields/{field}?api-version=7.2-preview.3
string apiCallUrl = $"https://dev.azure.com/{_account}/{_project}/_apis/wit/workitemtypes/{wit}/fields?api-version=7.1-preview.3";
var result = await GetObjectResult<WorkItemFieldList>(apiCallUrl);
return result.result;

Check warning on line 109 in AzureDevOps.WorkItemClone/AzureDevOpsApi.cs

View workflow job for this annotation

GitHub Actions / Build & Test

Possible null reference return.
}

public async Task<FieldItem> GetFieldOnWorkItem(string wit, string fieldRefName)
{
//GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitemtypes/{type}/fields/{field}?api-version=7.2-preview.3
string apiCallUrl = $"https://dev.azure.com/{_account}/{_project}/_apis/wit/workitemtypes/{wit}{fieldRefName}?api-version=7.1-preview.3";
Expand Down
9 changes: 9 additions & 0 deletions AzureDevOps.WorkItemClone/DataContracts/Fields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ public class FieldItem
public string url { get; set; }
}


public class WorkItemFieldList
{
public int count { get; set; }
public FieldItem[] value { get; set; }
}



}

0 comments on commit c6a9ac6

Please sign in to comment.