Skip to content

Instantly share code, notes, and snippets.

@gabmontes
Last active May 28, 2018 03:03
Show Gist options
  • Select an option

  • Save gabmontes/fcf54132230ea17ea02a3f4962bfb5f5 to your computer and use it in GitHub Desktop.

Select an option

Save gabmontes/fcf54132230ea17ea02a3f4962bfb5f5 to your computer and use it in GitHub Desktop.
Automagically obtain your top starred repos as an Markdow table
const GitHubApi = require('@octokit/rest');
function pick(props) {
return function (object) {
const result = {}
Object.keys(object).filter(prop => props.includes(prop)).forEach(function (prop) {
result[prop] = object[prop]
})
return result
}
}
function sortDesc(prop) {
return function (a, b) {
return b[prop] - a[prop]
}
}
const github = new GitHubApi({
version: '3.0.0'
});
const config = {
username: 'gabmontes',
per_page: 100,
min_stars: 5
}
function reducePromise(get, test, reduce, init) {
return get(init).then(function (result) {
const accum = reduce(init, result)
return test(result) ? reducePromise(get, test, reduce, accum) : accum
})
}
function getMyPublicRepos() {
const { username, per_page } = config
return reducePromise(
({ page }) => github.repos.getForUser({ username, per_page, page }),
repos => repos.headers.link && repos.headers.link.split(',').filter(link => link.includes('rel="next"')).length,
(acc, repos) => ({ repos: acc.repos.concat(repos.data), page: acc.page += 1 }),
{ repos: [], page: 1 }
).then(({ repos }) => repos)
}
function filterReposData(repos) {
const { min_stars } = config
return repos
.filter(repo => !repo.fork && repo.stargazers_count >= min_stars)
.map(pick(['name', 'stargazers_count', 'url']))
.sort(sortDesc('name'))
.sort(sortDesc('stargazers_count'))
}
function formatAsTable(repos) {
return repos.reduce(function (table, repo) {
return table + `[${repo.name}](${repo.url}) | ${repo.stargazers_count}\n`
}, 'Name | Stars\n--- | ---\n')
}
getMyPublicRepos().then(filterReposData).then(formatAsTable).then(function (table) {
console.log(table)
process.exit(0);
}).catch(function (err) {
console.log('Execution failed: %s', err.message);
process.exit(1);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment