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

org.kohsuke.github API仓库路径错误问题咨询

关于org.kohsuke.github包获取仓库时URL错误的问题

问题描述

使用org.kohsuke.github包时遇到仓库未找到的错误,以下是包内两个相关方法的源码:

getRepository方法

public GHRepository getRepository(String name) throws IOException {
  String[] tokens = name.split("/");
  if (tokens.length != 2) {
    throw new IllegalArgumentException("Repository name must be in format owner/repo");
  } else {
    return GHRepository.read(this, tokens[0], tokens[1]);
  }
}

read方法

static GHRepository read(GitHub root, String owner, String name) throws IOException {
  return (GHRepository)((Requester)root.createRequest().withUrlPath("/repos/" + owner + '/' + name, new String[0])).fetch(GHRepository.class);
}

已将root设置为https://github.com,此时API构建的请求URL为https://github.com/repos/OWNER/REPOSITORY_NAME,运行时抛出仓库未找到错误。手动移除URL中的/repos部分后,https://github.com/OWNER/REPOSITORY_NAME是正确的仓库路径。

请教:

  • 是否是本人操作有误?
  • 此前该包可正常使用,是否是GitHub的URL结构发生了变化?

解答

1. 操作错误分析

核心问题是混淆了GitHub网页URL与GitHub REST API的URL规范:

  • https://github.com/OWNER/REPOSITORY_NAME是仓库的前端网页访问路径
  • org.kohsuke.github包是调用GitHub的REST API,API的仓库查询路径必须遵循https://api.github.com/repos/OWNER/REPOSITORY_NAME的格式

你错误地将root配置为网页域名https://github.com,但该包默认应该指向GitHub API的专属域名https://api.github.com。当root设为网页域名后,拼接/repos/...会生成无效的网页路径,自然无法找到仓库。

2. GitHub URL结构是否变更?

GitHub的API与网页URL结构近期没有发生会导致此问题的变更。之前包能正常使用,大概率是之前你正确配置了API域名,或者旧版本包会自动处理域名转换,而当前误修改了root的配置。

解决方法

将root的地址修改为https://api.github.com,而非https://github.com。此时拼接后的URLhttps://api.github.com/repos/OWNER/REPOSITORY_NAME符合GitHub API规范,即可正常获取仓库信息。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 04:48:25