I'm working on a Java program to retrieve pull requests from a GitHub repository within a specified date range using OkHttp and the GitHub API. However, I'm encountering issues where the code doesn't seem to filter pull requests correctly based on the date range.
I'm using OkHttp to make a GET request to the GitHub API to fetch pull requests from a specific repository.
I want to filter the pull requests based on their creation date, and I'm using the 'created' parameter in the API URL to specify the date range. The dates are correctly formatted in ISO 8601 format ('yyyy-MM-dd').
Despite setting the date range, the API response doesn't seem to be filtering the pull requests as expected. I'm still receiving pull requests outside the specified date range.
package ConsumeGithubAPI;
import com.google.gson.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class ConsumeGithubAPI {
public static void main(String[] args) {
JsonArray pullRequests = getPullRequests("-Owner-", "-reponame-", "2022-06-01", "2022-07-10");
String formattedOutput = formatOutput(pullRequests);
System.out.println(formattedOutput);
}
public static JsonArray getPullRequests(String owner, String repositoryName, String startDate, String endDate) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.github.com/repos/" + owner + "/" + repositoryName + "/pulls" +
"?state=all&per_page=100&page=1" +
"&since=" + startDate + "T00:00:00Z" +
"&until=" + endDate + "T23:59:59Z")
.addHeader("Authorization", "-token-")
.build();
JsonArray modifiedPRs = new JsonArray();
try {
Response response = client.newCall(request).execute();
String responseBody = response.body().string();
JsonArray pullRequests = JsonParser.parseString(responseBody).getAsJsonArray();
for (JsonElement pR : pullRequests) {
JsonObject prObject = pR.getAsJsonObject();
JsonObject prData = new JsonObject();
prData.addProperty("id", prObject.get("id").getAsBigInteger());
prData.addProperty("user", prObject.get("user").getAsJsonObject().get("login").getAsString());
prData.addProperty("title", prObject.get("title").getAsString());
prData.addProperty("state", prObject.get("state").getAsString());
prData.addProperty("created_at", prObject.get("created_at").getAsString());
modifiedPRs.add(prData);
}
response.close();
return modifiedPRs;
} catch (IOException e) {
e.printStackTrace();
}
return modifiedPRs;
}
private static String formatOutput(JsonArray jsonArray) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(jsonArray);
}
}
What I've tried:
I've checked the date formatting to ensure it's correct. I've verified that the repository exists and contains pull requests within the date range. I've reviewed the GitHub API documentation to confirm the correct parameter usage.
The request
List pull requestsdoes not provide the query parameterssinceanduntil. That's why it is returning pull requests that don't fall within the given range. The only query parameters allowed are:state,head,base,sort,direction,per_pageandpage. See the documentation below:https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests
I think you might be mixing up the request with
List repositories for the authenticated user, which instead accepts the query parameterssinceandafterand allows you to filter repositories for their update date.https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repositories-for-the-authenticated-user
It seems from the documentation that there are no requests that allow you to filter pull requests by their creation date. The only thing you could do is get the pull requests of a repository and then filter them manually.
Your code could look something like this:
I've also added a full example with the properties file on my GitHub.
https://github.com/daniele-aveta/GitHubAPIGetModifiedPRs