How to find the new location of a repository after a rename

旧街凉风 提交于 2019-12-19 04:09:44

问题


If a user renames a repository from foo/bar.git to foo/baz.git via GitHub's UI, how can I detect this via the API?

Currently, I get a 404 if I call the API like this:

GET /repos/foo/bar

How can I find the new repository name?


回答1:


So GitHub does not expose renames through the API.

UPDATE: that is no longer true and this answer is out of date; the API has exposed renames since July 2015.

Given the constraints of your question (i.e., just the repository name has changed, not the user/organization) you could rely on the repository ID.

If you have information about the repo before it was renamed, you should have the id which is returned by the API. If you do then to have a resilient access to the repository, you just need to do

GET /repositories/1234

And you'll always get the repository, regardless of whether the name changes (assuming you still have access to it).

A better example, is to do

GET https://api.github.com/repositories/1234

(or whatever your GitHub enterprise instance is).




回答2:


The other answers are out of date; As of July 21, 2015, the API does expose renames, and when you GET /repos/foo/bar you should receive

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

Then if you GET /repositories/<numeric id> the response will contain

{
  full_name: 'foo/baz'
  ...
}

Additionally, you should now be able to GET /repos/foo/baz and receive the same response.

If you're trying to access a private repository make sure you have the "Full control of private repositories" scope selected on the personal access token you use, or you will confusingly get a 404 when you request GET /repos/foo/baz or GET /repos/<numeric id>, even though GET /repos/foo/bar (the old name) will get you the "Moved Permanently" response above.




回答3:


I think it is not possible via the API if you use the name.

Some hack is to retrieve all repositories of that user and then store the ID of each repository and its name. You do this always if you know the repository changes its name often. Then you get the name of that repository by ID, and you will not have the issue with renaming.



来源:https://stackoverflow.com/questions/28778568/how-to-find-the-new-location-of-a-repository-after-a-rename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!