Skip to content

Commit

Permalink
Resolve #30 by checking for template changes... used next ful day to …
Browse files Browse the repository at this point in the history
…ensure refresh as queries dont have date presision by default.
  • Loading branch information
MrHinsh committed Jul 3, 2024
1 parent 1c2c7a7 commit 279025d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,34 @@ await AnsiConsole.Progress()
var task5 = ctx.AddTask("[bold]Stage 5[/]: Create Output Plan Relations ", false);
var task6 = ctx.AddTask("[bold]Stage 6[/]: Create Work Items", false);
string cacheTemplateWorkItemsFile = $"{config.CachePath}\\templateCache-{config.templateOrganization}-{config.templateProject}.json";
string cacheTemplateWorkItemsFile = $"{config.CachePath}\\templateCache-{config.templateOrganization}-{config.templateProject}-{config.templateParentId}.json";
if (config.ClearCache && System.IO.File.Exists(cacheTemplateWorkItemsFile))
{
System.IO.File.Delete(cacheTemplateWorkItemsFile);
}
task1.MaxValue = 1;
List<WorkItemFull> templateWorkItems;
List<WorkItemFull> templateWorkItems = null;
task1.StartTask();
task2.StartTask();
if (System.IO.File.Exists(cacheTemplateWorkItemsFile))
{
var changedDate = System.IO.File.GetLastWriteTime(cacheTemplateWorkItemsFile).AddDays(1).Date;
//Test Cache
QueryResults fakeItemsFromTemplateQuery;
fakeItemsFromTemplateQuery = await templateApi.GetWiqlQueryResults("Select [System.Id] From WorkItems Where [System.TeamProject] = '@project' AND [System.Parent] = @id AND [System.ChangedDate] > '@changeddate' order by [System.CreatedDate] desc", new Dictionary<string, string>() { { "@id", config.templateParentId.ToString() }, { "@changeddate", changedDate.ToString("yyyy-MM-dd") } });
if (fakeItemsFromTemplateQuery.workItems.Length == 0)
{
AnsiConsole.WriteLine($"Stage 1: Checked template for changes. None Detected. Loading Cache");
// Load from Cache
task1.StartTask();
task1.Increment(1);
task1.Description = task1.Description + " (cache)";
await Task.Delay(250);
Expand All @@ -95,8 +110,10 @@ await AnsiConsole.Progress()
task2.Increment(templateWorkItems.Count);
task2.Description = task2.Description + " (cache)";
AnsiConsole.WriteLine($"Stage 2: Loaded {templateWorkItems.Count()} work items from cache.");
}
}
else
if (templateWorkItems == null)
{
// Get From Server
// --------------------------------------------------------------
Expand All @@ -105,7 +122,7 @@ await AnsiConsole.Progress()
//AnsiConsole.WriteLine("Stage 1: Executing items from Query");
QueryResults fakeItemsFromTemplateQuery;
fakeItemsFromTemplateQuery = await templateApi.GetWiqlQueryResults();
fakeItemsFromTemplateQuery = await templateApi.GetWiqlQueryResults("Select [System.Id] From WorkItems Where [System.TeamProject] = '@project' AND [System.Parent] = @id order by [System.CreatedDate] desc", new Dictionary<string, string>() { { "@id", config.templateParentId.ToString() } });
AnsiConsole.WriteLine($"Stage 1: Query returned {fakeItemsFromTemplateQuery.workItems.Count()} items id's from the template.");
task1.Increment(1);
task1.StopTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ internal abstract class WorkItemCommandBase<TSettings> : AsyncCommand<TSettings>

internal void CombineValuesFromConfigAndSettings(WorkItemCloneCommandSettings settings, WorkItemCloneCommandSettings config)
{
config.NonInteractive = settings.NonInteractive;
config.ClearCache = settings.ClearCache;
config.RunName = settings.RunName != null ? settings.RunName : DateTime.Now.ToString("yyyyyMMddHHmmss");
config.configFile = EnsureConfigFileAskIfMissing(config.configFile = settings.configFile != null ? settings.configFile : config.configFile);
config.inputJsonFile = EnsureJsonFileAskIfMissing(config.inputJsonFile = settings.inputJsonFile != null ? settings.inputJsonFile : config.inputJsonFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"Clone": {
"commandName": "Project",
"commandLineArgs": "clone --RunName Allessandro2 --cachePath ..\\..\\..\\..\\..\\.cache\\ --configFile ..\\..\\..\\..\\..\\.cache\\configuration.json --jsonFile ..\\..\\..\\..\\..\\TestData\\new.json "
"commandLineArgs": "clone --cachePath ..\\..\\..\\..\\..\\.cache\\ --configFile ..\\..\\..\\..\\..\\.cache\\configuration-test.json --jsonFile ..\\..\\..\\..\\..\\TestData\\new.json --NonInteractive"
},
"empty": {
"commandName": "Project"
Expand Down
25 changes: 25 additions & 0 deletions AzureDevOps.WorkItemClone/AzureDevOpsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ public async IAsyncEnumerable<WorkItemFull> GetWorkItemsFullAsync(Workitem[] ite
}
}

public async Task<QueryResults?> GetWiqlQueryResults(string wiqlQuery, Dictionary<string, string> parameters)
{
if (parameters == null)
{
parameters = new Dictionary<string, string>();
}
if (!parameters.ContainsKey("@project"))
{
parameters.Add("@project", _project);
}
if (string.IsNullOrEmpty(wiqlQuery))
{
wiqlQuery = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.TeamProject] = '@project' order by [System.CreatedDate] desc";
}
foreach (var param in parameters)
{
wiqlQuery = wiqlQuery.Replace(param.Key, param.Value);
}
string post = JsonConvert.SerializeObject(new
{
query = wiqlQuery
});
string apiCallUrl = $"https://dev.azure.com/{_account}/_apis/wit/wiql?api-version=7.2-preview.2";
return await GetObjectResult<QueryResults>(apiCallUrl, post);
}

public async Task<QueryResults?> GetWiqlQueryResults()
{
Expand Down

0 comments on commit 279025d

Please sign in to comment.