Skip to content

Commit b1b2723

Browse files
committed
fix: page extraction regex issue
1 parent 5bf7037 commit b1b2723

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@
227227
<version>2.2</version>
228228
<scope>test</scope>
229229
</dependency>
230+
<dependency>
231+
<groupId>com.github.npathai</groupId>
232+
<artifactId>hamcrest-optional</artifactId>
233+
<version>2.0.0</version>
234+
<scope>test</scope>
235+
</dependency>
230236
<dependency>
231237
<groupId>org.junit.jupiter</groupId>
232238
<artifactId>junit-jupiter-engine</artifactId>

src/main/java/com/spotify/github/v3/clients/GithubPage.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.spotify.github.http.ImmutablePagination;
3131
import com.spotify.github.http.Link;
3232
import com.spotify.github.http.Pagination;
33-
import java.net.URI;
3433
import java.util.Iterator;
3534
import java.util.List;
3635
import java.util.Map;
@@ -54,7 +53,7 @@ public class GithubPage<T> implements AsyncPage<T> {
5453
private final TypeReference<List<T>> typeReference;
5554
private final int itemsPerPage;
5655

57-
private static String formatPath(final String path, final int itemsPerPage) {
56+
protected static String formatPath(final String path, final int itemsPerPage) {
5857
try {
5958
URIBuilder uriBuilder = new URIBuilder(path);
6059
if (uriBuilder.getQueryParams().stream().anyMatch(p -> p.getName().equals("per_page"))) {
@@ -199,12 +198,13 @@ private CompletableFuture<Map<String, Link>> linkMapAsync() {
199198
.collect(toMap(link -> link.rel().get(), identity())));
200199
}
201200

202-
private Optional<Integer> pageNumberFromUri(final String uri) {
203-
Pattern pageInQueryPattern = Pattern.compile("page=(\\d+)");
204-
Matcher matcher = pageInQueryPattern.matcher(URI.create(uri).getQuery());
201+
protected static Optional<Integer> pageNumberFromUri(final String uri) {
202+
Pattern pageInQueryPattern = Pattern.compile("(^|\\?|&)page=(?<page>\\d+)", Pattern.CASE_INSENSITIVE);
205203
try {
206-
return Optional.of(Integer.parseInt(matcher.group(1)));
207-
} catch (NumberFormatException e) {
204+
String query = new URIBuilder(uri).build().getQuery();
205+
Matcher matcher = pageInQueryPattern.matcher(query);
206+
return matcher.find() ? Optional.of(Integer.parseInt(matcher.group("page"))) : Optional.empty();
207+
} catch (Exception e) {
208208
return Optional.empty();
209209
}
210210
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2025 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.clients;
22+
23+
import static org.hamcrest.core.Is.is;
24+
25+
import java.net.URI;
26+
import org.junit.jupiter.api.Test;
27+
import static org.hamcrest.MatcherAssert.assertThat;
28+
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresentAndIs;
29+
import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty;
30+
31+
public class GitHubPageTest {
32+
private static final String MOCK_GITHUB_HOST = "bogus.host";
33+
private static final URI MOCK_GITHUB_URI = URI.create(String.format("http://%s/api/v3/", MOCK_GITHUB_HOST));
34+
35+
@Test
36+
public void testFormatPathWithPerPage() {
37+
assertThat(GithubPage.formatPath("/commits?page=2", 3), is("/commits?page=2&per_page=3"));
38+
assertThat(GithubPage.formatPath("NOT_A_CORRECT PATH ", 3), is("NOT_A_CORRECT PATH "));
39+
assertThat(GithubPage.formatPath("/commits", 3), is("/commits?per_page=3"));
40+
assertThat(GithubPage.formatPath("/commits?page=2&per_page=7", 3), is("/commits?page=2&per_page=7"));
41+
}
42+
43+
@Test
44+
public void testPageNumberFromURI() {
45+
assertThat(GithubPage.pageNumberFromUri(MOCK_GITHUB_URI.resolve("/commits?page=5").toString()), isPresentAndIs(5));
46+
assertThat(GithubPage.pageNumberFromUri(MOCK_GITHUB_URI.resolve("/commits").toString()), isEmpty());
47+
assertThat(GithubPage.pageNumberFromUri(MOCK_GITHUB_URI.resolve("commits").toString()), isEmpty());
48+
assertThat(GithubPage.pageNumberFromUri("/commits?page=2"), isPresentAndIs(2));
49+
assertThat(GithubPage.pageNumberFromUri("/commits?per_page=4&page=2"), isPresentAndIs(2));
50+
assertThat(GithubPage.pageNumberFromUri("NOT_A_CORRECT PATH "), isEmpty());
51+
assertThat(GithubPage.pageNumberFromUri("/commits"), isEmpty());
52+
}
53+
}

src/test/java/com/spotify/github/v3/clients/PullRequestClientTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@
2121
package com.spotify.github.v3.clients;
2222

2323
import static com.google.common.io.Resources.getResource;
24-
import static com.spotify.github.MockHelper.createMockHttpResponse;
2524
import static com.spotify.github.MockHelper.createMockResponse;
26-
import static com.spotify.github.v3.UserTest.assertUser;
27-
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
2825
import static java.lang.String.format;
2926
import static java.nio.charset.Charset.defaultCharset;
3027
import static java.util.concurrent.CompletableFuture.completedFuture;
31-
import static java.util.concurrent.CompletableFuture.completedStage;
3228
import static java.util.stream.Collectors.toList;
3329
import static org.hamcrest.MatcherAssert.assertThat;
3430
import static org.hamcrest.core.Is.is;
@@ -45,7 +41,6 @@
4541
import com.spotify.github.async.AsyncPage;
4642
import com.spotify.github.http.HttpResponse;
4743
import com.spotify.github.jackson.Json;
48-
import com.spotify.github.v3.User;
4944
import com.spotify.github.v3.exceptions.RequestNotOkException;
5045
import com.spotify.github.v3.git.FileItem;
5146
import com.spotify.github.v3.git.ImmutableFileItem;
@@ -63,7 +58,6 @@
6358
import java.net.URI;
6459
import java.util.List;
6560
import java.util.concurrent.CompletableFuture;
66-
import java.util.concurrent.CompletionStage;
6761
import java.util.concurrent.ExecutionException;
6862
import okhttp3.Call;
6963
import okhttp3.Callback;
@@ -80,7 +74,7 @@
8074

8175
public class PullRequestClientTest {
8276
private static final String MOCK_GITHUB_HOST = "bogus.host";
83-
private static final URI MOCK_GITHUB_URI = URI.create(String.format("http://%s/", MOCK_GITHUB_HOST));
77+
private static final URI MOCK_GITHUB_URI = URI.create(String.format("http://%s/api/v3", MOCK_GITHUB_HOST));
8478
private static final URI MOCK_GITHUB_URI_GQL = MOCK_GITHUB_URI.resolve("/graphql");
8579

8680
private static final String PR_CHANGED_FILES_TEMPLATE = "/repos/%s/%s/pulls/%s/files";

0 commit comments

Comments
 (0)