Skip to content
Snippets Groups Projects
Verified Commit 83382efc authored by Jordi Gómez's avatar Jordi Gómez
Browse files

refactor: moving getAllPullRequestComments to GithubHelper

parent 05104147
No related branches found
No related tags found
No related merge requests found
Pipeline #71757 failed
......@@ -167,36 +167,4 @@ public interface GithubAPI {
@Path("installation/repositories")
public Response getInstallationRepositories(@BeanParam BaseAPIParameters params, @HeaderParam(HttpHeaders.AUTHORIZATION) String bearer);
/**
* Recursively fetches all comments from a pull request by handling pagination manually.
*
* @param bearer GitHub bearer token
* @param apiVersion GitHub API version
* @param org organization name
* @param repo repository name
* @param prNumber pull request number
* @return List of all comments from the pull request
*/
default List<GithubPullRequestComment> getAllPullRequestComments(String bearer, String apiVersion, String org, String repo,
int prNumber) {
List<GithubPullRequestComment> allComments = new ArrayList<>();
int page = 1;
int perPage = 100; // GitHub's maximum items per page
// Given that there's no pagination in the response, we need to query until we get an empty response, that would mean that we've
// reached the end
while (true) {
RestResponse<List<GithubPullRequestComment>> response = getComments(bearer, apiVersion, org, repo, prNumber, perPage, page);
List<GithubPullRequestComment> comments = response.getEntity();
if (Objects.isNull(comments) || comments.isEmpty()) {
break;
}
allComments.addAll(comments);
page++;
}
return allComments;
}
}
......@@ -57,6 +57,7 @@ import org.eclipsefoundation.persistence.model.RDBMSQuery;
import org.eclipsefoundation.persistence.service.FilterService;
import org.eclipsefoundation.utils.helper.DateTimeHelper;
import org.eclipsefoundation.utils.helper.TransformationHelper;
import org.jboss.resteasy.reactive.RestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.quarkus.qute.Location;
......@@ -481,6 +482,40 @@ public class GithubHelper {
.build());
}
/**
* Recursively fetches all comments from a pull request by handling pagination manually.
*
* @param bearer GitHub bearer token
* @param apiVersion GitHub API version
* @param org organization name
* @param repo repository name
* @param prNumber pull request number
* @return List of all comments from the pull request
*/
private List<GithubPullRequestComment> getAllPullRequestComments(String bearer, String apiVersion, String org, String repo,
int prNumber) {
List<GithubPullRequestComment> allComments = new ArrayList<>();
int page = 1;
int perPage = 100; // GitHub's maximum items per page
// Given that there's no pagination in the response, we need to query until we get an empty response, that would mean that we've
// reached the end
while (true) {
RestResponse<List<GithubPullRequestComment>> response = ghApi.getComments(bearer, apiVersion, org, repo, prNumber, perPage, page);
List<GithubPullRequestComment> comments = response.getEntity();
if (Objects.isNull(comments) || comments.isEmpty()) {
break;
}
allComments.addAll(comments);
page++;
}
return allComments;
}
/**
* This method posts a comment to the pull request detailing the validation errors and mentioning the usernames that need to take
* action.
......@@ -503,8 +538,7 @@ public class GithubHelper {
Integer pullRequestNumber = request.getPullRequest().getNumber();
// Get existing comments using pagination
List<GithubPullRequestComment> comments = ghApi
.getAllPullRequestComments(ghBearerString, apiVersion, login, repositoryName, pullRequestNumber);
List<GithubPullRequestComment> comments = getAllPullRequestComments(ghBearerString, apiVersion, login, repositoryName, pullRequestNumber);
Set<String> nonMentionedUsers = usernames
.stream()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment