St. John Johnson
St. John Johnson

Reputation: 6660

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

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?

Upvotes: 3

Views: 851

Answers (3)

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28747

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

Upvotes: 3

Andy
Andy

Reputation: 8650

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.

Upvotes: 2

scas
scas

Reputation: 229

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.

Upvotes: 0

Related Questions