Skip to content

Commit

Permalink
Fix #658 : Pager not working properly
Browse files Browse the repository at this point in the history
Since the removal of "X-Total-Pages" and "X-Total" headers, all paginations use the kaminari counter.
In this mode, kaminari counter is reset to -1 during the call of the last page. This part is correct.
Then when we call the `current()` method of the pager to get the current page. This method call the same `page` method than the `next` method. In this method the pager check if there is element before checking if we call for the current page. It throws NoSuchElementException erroneously.

To fix, I change the check sequence. First I check ifthe user call the current page. If so, we return the corresponding items. If not we check if there is such elements and call the API.
  • Loading branch information
jabby committed Jun 7, 2021
1 parent db8f925 commit 4e9f7a6
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/gitlab4j/api/Pager.java
Expand Up @@ -306,12 +306,6 @@ public List<T> current() throws GitLabApiException {
*/
public List<T> page(int pageNumber) {

if (pageNumber > totalPages && pageNumber > kaminariNextPage) {
throw new NoSuchElementException();
} else if (pageNumber < 1) {
throw new NoSuchElementException();
}

if (currentPage == 0 && pageNumber == 1) {
currentPage = 1;
return (currentItems);
Expand All @@ -321,6 +315,12 @@ public List<T> page(int pageNumber) {
return (currentItems);
}

if (pageNumber > totalPages && pageNumber > kaminariNextPage) {
throw new NoSuchElementException();
} else if (pageNumber < 1) {
throw new NoSuchElementException();
}

try {

setPageParam(pageNumber);
Expand Down

2 comments on commit 4e9f7a6

@LiuTing0729
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right,which means I can only use hasNext() to determine if there has a next page? If I want to know the exact number of the total size, use pager.all().size(), but it's implementation is a loop ,sad! Maybe I should give up display the total number.

@jabby
Copy link
Collaborator Author

@jabby jabby commented on 4e9f7a6 Sep 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LiuTing0729 I didn't try but maybe you can use

/**
* Get a list of contributors from a project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/repository/contributors</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a List containing the contributors for the specified project ID
* @throws GitLabApiException if any exception occurs
*/
public List<Contributor> getContributors(Object projectIdOrPath) throws GitLabApiException {
return (getContributors(projectIdOrPath, getDefaultPerPage()).all());
}
and sum all commits of each contributor. I don't know if the number of commits is the one for the default branch of the repository or if it's for all branches.

Please sign in to comment.