From e504a1b936c6ed153bbd6e63feca1b65ea404d78 Mon Sep 17 00:00:00 2001 From: Richard Murillo Date: Fri, 4 Aug 2017 12:59:50 -0700 Subject: [PATCH] Add paging ability for projects (#166) When there are more than 100 projects in a collection, only 100 are loaded. Resolves #162 --- src/Qwiq.Core.Rest/WorkItemStore.cs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Qwiq.Core.Rest/WorkItemStore.cs b/src/Qwiq.Core.Rest/WorkItemStore.cs index c9f89dbb..c0b663ba 100644 --- a/src/Qwiq.Core.Rest/WorkItemStore.cs +++ b/src/Qwiq.Core.Rest/WorkItemStore.cs @@ -194,9 +194,28 @@ private void Dispose(bool disposing) private IProjectCollection ProjectCollectionFactory() { + const string continuationHeader = "x-ms-continuationtoken"; + + // See https://www.visualstudio.com/en-us/docs/integrate/api/tfs/projects + const ProjectState defaultStateFilter = ProjectState.WellFormed; + const int defaultNumberOfTeamProjects = 100; + + // Use the page size configured by the client if it is higher than the default + // Otherwise, with a default of 50, we would need to make multiple trips to load all the data + var pageSize = Math.Max(defaultNumberOfTeamProjects, Configuration.PageSize); + using (var projectHttpClient = _tfs.Value.GetClient()) { - var projects = (List)projectHttpClient.GetProjects(ProjectState.WellFormed).GetAwaiter().GetResult(); + var projects = new List(pageSize); + + var projectReferences = projectHttpClient.GetProjects(defaultStateFilter, pageSize).GetAwaiter().GetResult(); + projects.AddRange(projectReferences); + while (projectHttpClient.LastResponseContext.Headers.Contains(continuationHeader)) + { + projectReferences = projectHttpClient.GetProjects(defaultStateFilter, pageSize, projects.Count).GetAwaiter().GetResult(); + projects.AddRange(projectReferences); + } + var projects2 = new List(projects.Count + 1); for (var i = 0; i < projects.Count; i++)