2019-11-17 02:27:32 +00:00
|
|
|
const got = require("got");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The defaults here are the same as the API
|
|
|
|
* @see https://developer.github.com/v3/repos/#list-user-repositories
|
|
|
|
* @param {string} username
|
|
|
|
* @param {Object} opts
|
|
|
|
* @param {('all' | 'owner' | 'member')[]} [opts.types]
|
|
|
|
* @param {'created' | 'updated' | 'pushed' | 'full_name' | 'star'} [opts.sort]
|
|
|
|
* @param {'desc' | 'asc'} [opts.order]
|
|
|
|
*/
|
|
|
|
async function getRepos(username, opts = {}) {
|
2020-01-12 09:38:32 +00:00
|
|
|
let tempRepos;
|
|
|
|
let page = 1;
|
|
|
|
let repos = [];
|
2019-11-17 02:27:32 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
const { sort } = opts;
|
|
|
|
const order = opts.order || (sort === "full_name" ? "asc" : "desc");
|
|
|
|
const types = opts.types || [];
|
|
|
|
let type = "all";
|
2019-11-17 02:27:32 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
if (
|
|
|
|
types.includes("all") ||
|
|
|
|
(types.includes("owner") && types.includes("member"))
|
|
|
|
) {
|
|
|
|
type = "all";
|
|
|
|
} else if (types.includes("member")) {
|
|
|
|
type = "member";
|
|
|
|
}
|
2019-11-17 02:27:32 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
do {
|
|
|
|
let requestUrl = `https://api.github.com/users/${username}/repos?per_page=100&page=${page++}&type=${type}`;
|
|
|
|
if (sort && sort !== "star") {
|
|
|
|
requestUrl += `&sort=${sort}&direction=${order}`;
|
|
|
|
}
|
2019-11-17 02:27:32 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
tempRepos = await got(requestUrl);
|
|
|
|
tempRepos = JSON.parse(tempRepos.body);
|
|
|
|
repos = repos.concat(tempRepos);
|
|
|
|
} while (tempRepos.length === 100);
|
2019-11-17 02:27:32 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
if (sort === "star") {
|
|
|
|
repos = repos.sort((a, b) => {
|
|
|
|
if (order === "desc") {
|
|
|
|
return b.stargazers_count - a.stargazers_count;
|
|
|
|
}
|
2020-01-11 16:48:56 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
return a.stargazers_count - b.stargazers_count;
|
|
|
|
});
|
|
|
|
}
|
2020-01-11 16:48:56 +00:00
|
|
|
|
2020-01-12 09:38:32 +00:00
|
|
|
return repos;
|
2019-11-17 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://developer.github.com/v3/users/#get-a-single-user
|
|
|
|
* @param {string} username
|
|
|
|
*/
|
|
|
|
async function getUser(username) {
|
2020-01-12 09:38:32 +00:00
|
|
|
const res = await got(`https://api.github.com/users/${username}`);
|
|
|
|
return JSON.parse(res.body);
|
2019-11-17 02:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2020-01-12 09:38:32 +00:00
|
|
|
getRepos,
|
|
|
|
getUser
|
2019-11-17 02:27:32 +00:00
|
|
|
};
|