Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2693 PageCount doesn't work if the query gets constructed with page number as the first query parameter #2911

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Octokit.Tests/Http/PaginationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using Octokit.Models.Request.Enterprise;
using Xunit;

namespace Octokit.Tests.Http
{
public class PaginationTests
{
public class TheShouldContinueMethod
{
[Fact]
public void HandlesMissingUri()
{
var result = Pagination.ShouldContinue(null, null);
Assert.False(result);
}

[Fact]
public void HandlesIsDone()
{
var uri = new Uri("http://example.com");
var options = new ApiOptionsExtended { IsDone = true };
var result = Pagination.ShouldContinue(uri, options);
Assert.False(result);
}

[Fact]
public void HandlesPageCountPageFirstParam()
{
var uri = new Uri("http://example.com?page=2");
var options = new ApiOptions { StartPage = 1, PageCount = 1 };
var result = Pagination.ShouldContinue(uri, options);
Assert.False(result);
}

[Fact]
public void HandlesPageCountPageNotFirstParam()
{
var uri = new Uri("http://example.com?page_size=100&page=2");
var options = new ApiOptions { StartPage = 1, PageCount = 1 };
var result = Pagination.ShouldContinue(uri, options);
Assert.False(result);
}
}
}
}
11 changes: 8 additions & 3 deletions Octokit/Helpers/Pagination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ internal static bool ShouldContinue(
{
var allValues = ToQueryStringDictionary(uri);

string pageValue;
if (allValues.TryGetValue("page", out pageValue))
if (allValues.TryGetValue("page", out var pageValue))
{
var startPage = options.StartPage ?? 1;
var pageCount = options.PageCount.Value;
Expand All @@ -61,8 +60,14 @@ internal static bool ShouldContinue(
static Dictionary<string, string> ToQueryStringDictionary(Uri uri)
{
return uri.Query.Split('&')
.Select(keyValue =>
.Select((keyValue, i) =>
{
if (i == 0)
{
// Trim the leading '?' character from the first key-value pair
keyValue = keyValue.Substring(1);
}

var indexOf = keyValue.IndexOf('=');
if (indexOf > 0)
{
Expand Down
Loading