You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OkHttp在Java中按日期范围拉取GitHub PR时遇过滤问题

解决GitHub API按创建日期过滤Pull Requests的参数错误问题

你代码里的核心问题是使用了GitHub Pulls API不支持的since和until参数,导致无法按创建日期范围筛选PR。要实现按创建日期过滤,需要改用GitHub搜索API,并使用正确的搜索语法指定日期范围。

正确的参数用法

GitHub搜索API中,针对PR的过滤需要结合以下条件:

  • repo:{owner}/{repo}:指定目标仓库
  • type:pr:限定为Pull Request
  • created:{startDate}..{endDate}:指定创建日期范围(支持ISO 8601格式,也可以用>=/<=来明确边界)

修改后的代码

package ConsumeGithubAPI;

import com.google.gson.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

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();
        
        // 构建搜索查询条件,注意URL编码
        String query = String.format("repo:%s/%s type:pr created:%sT00:00:00Z..%sT23:59:59Z",
                owner, repositoryName, startDate, endDate);
        String encodedQuery = URLEncoder.encode(query, StandardCharsets.UTF_8);

        Request request = new Request.Builder()
                .url("https://api.github.com/search/issues?q=" + encodedQuery + "&per_page=100&page=1")
                .addHeader("Authorization", "-token-")
                .build();
        
        JsonArray modifiedPRs = new JsonArray();
        try {
            Response response = client.newCall(request).execute();
            String responseBody = response.body().string();

            JsonObject responseObject = JsonParser.parseString(responseBody).getAsJsonObject();
            JsonArray pullRequests = responseObject.getAsJsonArray("items"); // 搜索API返回的结果在items数组中

            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);
    }
}

关键修改点

  1. 切换API端点:从/repos/{owner}/{repo}/pulls改为搜索API的/search/issues,因为前者不支持日期范围过滤;
  2. 构建正确的查询字符串:用repo、type:pr和created范围条件组合查询,并对查询字符串做URL编码,避免特殊字符问题;
  3. 解析响应结构:搜索API的返回结果包含在items数组中,需要调整解析逻辑。

补充说明

  • 如果需要更精确的日期边界,可以把created:{start}..{end}拆分为created:>=xxx和created:<=xxx,用+连接(URL编码后为%2B);
  • GitHub搜索API有请求频率限制,使用认证token可以提升限额;
  • 若需要获取超过100条结果,需要分页处理(递增page参数)。

内容的提问来源于stack exchange,提问作者Lungelo Thabethe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 21:55:37