Shubh Harkawat
Shubh Harkawat

Reputation: 23

How to find the 'm' oldest forks of a repository using GitHub api

I need to obtain the 'm' oldest forks of a given repository, using the GitHub API. To get the 'm' oldest forks I'm trying to obtain the forks sorted from oldest to newest.

Taking the example of "it-cert-automation-practice" repository of google, I've tried the following APIs:

https://api.github.com/repos/google/it-cert-automation-practice/forks?q=sort:created_at
https://api.github.com/repos/google/it-cert-automation-practice/forks?q=sort:oldest
https://api.github.com/repos/google/it-cert-automation-practice/forks?q=sort:updated_at&fork:true&order:asc
https://api.github.com/repos/google/it-cert-automation-practice/forks?q=sort:created_at&fork:true&order:asc

Upvotes: 2

Views: 142

Answers (1)

Ruben Restrepo
Ruben Restrepo

Reputation: 1206

You can use List forks endpoint, take into account the following:

  • Use sort parameter with value oldest
  • If you want to list more than 100 repositories, you will need to handle pagination using the page parameter.

See a working example using Octokit for getting the oldest 40 forks of Node.js repository.

Get oldest forks of GitHub repository                                                                                 View in Fusebit
const owner = 'nodejs';
const repo = 'node';
const repoInfo = await github.request(`GET /repos/${owner}/${repo}`, {
  owner,
  repo,
});

const {forks_count} = repoInfo.data;

// By default it will return 30 results, you can get up to 100 using per_page parameter.
const searchResult = await github.rest.repos.listForks({
  owner,
  repo,
  sort: 'oldest',
  per_page: 40
});

console.log(searchResult.data.map(repo => `${repo.full_name} created at ${repo.created_at}`), `Found ${searchResult.data.length} of ${forks_count} total forks for ${owner}/${repo} repository`);


Upvotes: 3

Related Questions