Github Api - 如何跟踪移动的 repos/projects?

Github Api - how to keep track of moved repos/projects?

我在构建一个应用程序时遇到问题,该应用程序通过 github API 跟踪一些 github 存储库。当我手动移动或重命名我的应用正在跟踪的项目(在 github 上)时,我丢失了它(当我调用旧的 api url 时得到 404)。我确实注意到,当我通过浏览器导航到我的旧项目 url 时,我收到 301 并被重定向到正确的页面。有没有一种简单的方法可以通过 api 找到项目的新位置?也许通过 "id" 属性或其他方式搜索项目?谢谢!

示例: 旧:github.com/api/v3/repos/group/app1 重命名为:github.com/api/v3/repos/group/app2

当我打电话给 github.com/api/v3/repos/group/app2 时,我得到了 404 当我导航到 github.com/group/app1 时,我收到 301 并被重定向到 github.com/group/app2

干杯。

我已经 运行 了解了这个 issue 并且能够从 Github 支持人员那里获得一些非常有用的信息。

目前,API 不会像 github.com 那样在存储库上重定向或用户重命名,但从 2015 年 4 月 17 日起,您可以 preview repository redirects

以重命名为babel/babel6to5/6to5为例。正如您所指出的,对 https://api.github.com/repos/6to5/6to5 的请求将 return 404,但我们可以通过将 application/vnd.github.quicksilver-preview+json 作为 Accept header:

$ curl -i -H "Accept: application/vnd.github.quicksilver-preview+json" https://api.github.com/repos/6to5/6to5

这个 return 是一个 301 和一个带有 url 键的响应,其值为 link 我们正在寻找的回购:

{
  "message": "Moved Permanently",
  "url": "https://api.github.com/repositories/24560307",
  "documentation_url": "https://developer.github.com/v3/#http-redirects"
}

或者,您可以使用唯一的 id 属性(永远不会更改)来跟踪存储库(或用户),无论名称是否更改。

我们还是以babel/babel née 6to5/6to5为例。它的回购 id24560307。您可以使用它来调用 /repositories/:id 端点(例如,https://api.github.com/repositories/24560307)并保证在该回购仍然存在的情况下获得它。

以下是我与GitHub的交流记录,支持参考:

Detecting a renamed repo

Hi.

Is there any way to detect a renamed repository using the Github API?

For example, 6to5/6to5 was recently renamed to babel/babel. Running curl -i https://github.com/6to5/6to5 returns a 301 with a Location field that points to the new location. However, running curl -i https://api.github.com/repos/6to5/6to5 returns a 404.

Is there anyway to get that Location field with the API? If not, would making a HEAD request not using the API be a valid workaround, or is there a better way to achieve my goal? I'm trying to work through this issue on codetriage/codetriage (https://github.com/codetriage/codetriage/issues/228).

Thanks for any help you provide.

All the best, O-I


Hi O-I,

You're right -- the API doesn't redirect on repository or username renames like github.com does. It might be something we will provide in the future (I agree it would be useful), but I can't make any promises about that. Thanks for expressing interest!

For now, there's a way you can handle renames in your API scripts and applications -- instead of using the name of the repository to make API requests, you can use the ID of the repository (which will not change when the repository is renamed or transferred).

For example, the ID of github/linguist is 1725199:

https://api.github.com/repos/github/linguist

Now that you know that, you can fetch the repository by ID:

https://api.github.com/repositories/1725199

If you haven't been tracking the ID of repositories and all you have is the previous name, then making a HEAD request to github.com to see where you'll be redirected is the only workaround I can think of at the moment.

Hope this helps and let us know if you have any other questions.

Cheers, Ivan


Hi O-I,

I thought you might be interested to know that we'll soon be providing redirects in the API for renamed repositories. You can preview the redirect functionality now, and we'll be enabling it for all requests in the coming months. You can read more about it in today's post on https://developer.github.com:

https://developer.github.com/changes/2015-04-17-preview-repository-redirects/

If you try it out, we'd love to have your feedback on whether this functionality meets your needs.

All the best, Jason