Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm having trouble with an application I'm building that keeps track of some github repos via github API. When I manually move or rename a project (on github) that my app is tracking, I lose it (by getting a 404 when I call the old api url). I do notice that when I navigate via browser to my old project url I get a 301 and am redirected to the correct page. Is there an easy way I can find out the new location of the project via api? Perhaps by searching projects by "id" attribute or something? Thanks!

example: old: github.com/api/v3/repos/group/app1 renamed to: github.com/api/v3/repos/group/app2

when I call github.com/api/v3/repos/group/app2 I get a 404 when I navigate to github.com/group/app1 I receive a 301 and am redirected to github.com/group/app2

Cheers.

like image 426
ResrieC Avatar asked Mar 04 '15 19:03

ResrieC


People also ask

How do I see my GitHub repository activity?

On GitHub.com, navigate to the main page of the repository. Under your repository name, click Insights.

What is GitHub pulse?

Pulse will show you who has been actively committing and what has changed in a project's default branch. Pulse is also a great way to see new and merged pull requests, open and closed issues, and unresolved discussions. You can find the link to the left of the nav bar.

How do I see stats on GitHub?

Github readme stats allows you to simply add a markdown image link and it will show you realtime stats for your github account. And the cool thing is since it's just an image you can embed it anywhere even on Dev.to posts!


1 Answers

I've run across this exact issue and was able to get some really useful information from Github Support.

Currently, the API does not redirect on repository or user renames like github.com does, but as of 17 April 2015, you can preview repository redirects.

Take 6to5/6to5, which was renamed to babel/babel, as an example. As you've noted, a request to https://api.github.com/repos/6to5/6to5 will return a 404, but we can preview the upcoming repo redirect feature by passing application/vnd.github.quicksilver-preview+json as the Accept header:

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

This returns a 301 and a response with a url key whose value is a link to the repo we are looking for:

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

Alternatively, you can use the unique id attribute (which never changes) to track a repository (or user) regardless of name changes.

Let's take babel/babel née 6to5/6to5 as the example again. It's repo id is 24560307. You can use this to make a call to the /repositories/:id endpoint (e.g., https://api.github.com/repositories/24560307) and be guaranteed to get that repo as long as it still exists.

Below is a transcript of my exchange with GitHub Support for reference:

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

like image 183
O-I Avatar answered Nov 15 '22 06:11

O-I