3

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?

St. John Johnson
  • 6,590
  • 7
  • 35
  • 56

3 Answers3

3

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).

Andy
  • 7,885
  • 5
  • 55
  • 61
Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
  • Hmm, this isn't documented on the github api. I never knew you could use the repository ID. – St. John Johnson Apr 05 '15 at 00:53
  • 1
    Yeah. They have had many an argument over whether or not it should be documented. There's similar functionality for users too. Either way, it's moderately well known and I'm pretty sure it's supported fairly well too. I maintain a library of bindings and am comfortable enough supporting this as part of it (which only means something if you know how conservative I am with features). – Ian Stapleton Cordasco Apr 05 '15 at 14:20
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.

Andy
  • 7,885
  • 5
  • 55
  • 61
0

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.

scas
  • 221
  • 2
  • 9
  • Is there anyway to lookup by ID instead? – St. John Johnson Mar 30 '15 at 16:18
  • 1
    Not via the API. Another hack: - Do a GET request to the normal repository; e.g. http://github.com/username/reponame; - if you receive the status code 200, then the repository was not renamed. If you receive 301, then it was renamed and you parse the response to get the new URL for it. It is up to you which method you want to use. If you have a better one, please share it. – scas Mar 30 '15 at 16:42
  • Accessing the UI is a little harder as I cannot use the user's access_token to do it. I would need to do that as it could be a private repository. – St. John Johnson Mar 30 '15 at 18:30
  • I see. I don't have any other ideas for now. If you still are in need of this, you can shoot a question to the support people at Github. I found them to be extremely helpful :) – scas Mar 30 '15 at 20:29