Skip to content

Instantly share code, notes, and snippets.

@alestrunda
Created March 7, 2021 21:09
Show Gist options
  • Save alestrunda/aee21ca688624cf7693a0b9f56096b8d to your computer and use it in GitHub Desktop.
Save alestrunda/aee21ca688624cf7693a0b9f56096b8d to your computer and use it in GitHub Desktop.
Get GitHub repos count
/*
Description: gets total count of your GitHub repos - public and private (you will need access token for that)
Requirements:
- get your GitHub access token
- make sure axios package is installed
Example: await getReposCnt()
*/
const axios = require("axios");
const TOKEN = "your_github_access_token";
const MAX_PAGE = 10;
const getRepos = (page) =>
axios
.get(`https://api.github.com/user/repos?page=${page}`, {
headers: {
Authorization: `token ${TOKEN}`,
},
})
.then((res) => res.data);
const getReposCnt = async () => {
let total = 0;
for (let page = 1; page <= MAX_PAGE; page++) {
const repos_cnt = (await getRepos(page)).length;
if (!repos_cnt) break;
total += repos_cnt;
}
return total;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment