gpt4 book ai didi

Github Api - 如何跟踪移动的存储库/项目?

转载 作者:行者123 更新时间:2023-12-04 18:05:41 24 4
gpt4 key购买 nike

我正在构建的应用程序遇到问题,该应用程序通过 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
6to5/6to5 重命名为 babel/babel 为例。正如您所指出的,对 https://api.github.com/repos/6to5/6to5 的请求将返回 404,但我们可以通过将 application/vnd.github.quicksilver-preview+json 作为 Accept header 传递来预览即将推出的 repo 重定向功能:

$ curl -i -H "Accept: application/vnd.github.quicksilver-preview+json" https://api.github.com/repos/6to5/6to5
这将返回一个 301 和一个带有 url 键的响应,其值是我们正在寻找的存储库的链接:
{
"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 为例。它的 repo id24560307 。您可以使用它来调用 /repositories/:id 端点(例如 https://api.github.com/repositories/24560307 ),并且只要它仍然存在就可以保证获得该存储库。
以下是我与 GitHub Support 交流的记录以供引用:

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

关于Github Api - 如何跟踪移动的存储库/项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28863131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com