From 5459af4ec0c1e97b4edb0deebc42285592aed70b Mon Sep 17 00:00:00 2001 From: Jonathon Broughton Date: Sat, 22 Apr 2023 20:15:14 +0100 Subject: [PATCH] Paginated queries where Branches are retrieved - introducing a hardcoded limit as a single variable --- .../DesktopUI2/ViewModels/StreamViewModel.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/DesktopUI2/DesktopUI2/ViewModels/StreamViewModel.cs b/DesktopUI2/DesktopUI2/ViewModels/StreamViewModel.cs index 8f4ed7a001..b6131094b3 100644 --- a/DesktopUI2/DesktopUI2/ViewModels/StreamViewModel.cs +++ b/DesktopUI2/DesktopUI2/ViewModels/StreamViewModel.cs @@ -160,6 +160,9 @@ public int SelectedTab internal Client Client { get; } + /// The limit of branches to be displayed in the stream, Given a limit to prevent runaway Branch DOS attacks + const int BranchLimit = 500; + public void GoBack() { PreviewOn = false; @@ -696,9 +699,9 @@ internal async void GetBranchesAndRestoreState() AvailableFilters = new List(Bindings.GetSelectionFilters().Select(x => new FilterViewModel(x))); SelectedFilter = AvailableFilters[0]; + // Get all branches created in the stream, not limited to the server limits string nextCursor = null; var branchCount = 0; - const int branchLimit = 500; do { @@ -706,7 +709,7 @@ internal async void GetBranchesAndRestoreState() Branches.AddRange(branchResponse.items); nextCursor = branchResponse.cursor; branchCount += branchResponse.items.Count; - } while (!string.IsNullOrEmpty(nextCursor) && branchCount <= branchLimit); + } while (!string.IsNullOrEmpty(nextCursor) && branchCount <= BranchLimit); var index = Branches.FindIndex(x => x.name == StreamState.BranchName); if (index != -1) @@ -758,7 +761,7 @@ private void GetReport() Report = report; //do not switch to report tab automatically - //if (HasReportItems) + //if (HasReportItems) //{ // // activate report tab // SelectedTab = 4; @@ -860,7 +863,18 @@ private void UpdateStreamState() private async Task GetBranches() { var prevBranchName = SelectedBranch != null ? SelectedBranch.Branch.name : StreamState.BranchName; - Branches = await Client.StreamGetBranches(Stream.id, 1000, 0); + + // Get all branches created in the stream, not limited to the server limits + string nextCursor = null; + var branchCount = 0; + + do + { + var branchResponse = await Client.PagedStreamGetBranches(Stream.id, 100, 0, nextCursor).ConfigureAwait(false); + Branches.AddRange(branchResponse.items); + nextCursor = branchResponse.cursor; + branchCount += branchResponse.items.Count; + } while (!string.IsNullOrEmpty(nextCursor) && branchCount <= BranchLimit); var index = Branches.FindIndex(x => x.name == prevBranchName); if (index != -1) @@ -1610,6 +1624,5 @@ public void Dispose() Client?.Dispose(); } #endregion - } }