Skip to content

Instantly share code, notes, and snippets.

@shajanjp
Created March 24, 2022 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shajanjp/b95c5fc2c92bf7c91912510062637acb to your computer and use it in GitHub Desktop.
Save shajanjp/b95c5fc2c92bf7c91912510062637acb to your computer and use it in GitHub Desktop.
Search most recently updated forks of a github repository
const axios = require("axios").default;
(async () => {
async function getForkByPage(repoUrl, page) {
console.log(`Fetching page ${page}`);
return axios
.get(`${repoUrl}/forks?page=${page}`)
.then((res) => {
return res.data || [];
})
.catch((error) => {
return [];
});
}
async function getForksSortedByUpdatedDate(repoUrl) {
const repos = [];
let nextPageAvailable = true;
let page = 1;
while (nextPageAvailable) {
const forks = await getForkByPage(repoUrl, page);
repos.push(
...forks.map((f) => ({ url: f.url, updatedAt: f.updated_at }))
);
if (!forks.length) {
nextPageAvailable = false;
}
page++;
}
return repos.sort((a, b) => new Date(b.updatedAt) - new Date(a.updatedAt));
}
const sortedForks = await getForksSortedByUpdatedDate(`https://api.github.com/repos/statping/statping`);
console.log(sortedForks);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment