Skip to content

Instantly share code, notes, and snippets.

@Gaafar
Created November 20, 2016 08:39
Show Gist options
  • Save Gaafar/4fea3d7a8e7769a12e065b481a7cca94 to your computer and use it in GitHub Desktop.
Save Gaafar/4fea3d7a8e7769a12e065b481a7cca94 to your computer and use it in GitHub Desktop.
Get Github repos with contributors
const bb = require('bluebird');
const axios = require('axios');
const f = require('lodash/fp');
const autheader = 'token xxx';
const fetch = (page, continuePredicate, accumulator = [] ) =>
axios.get('https://api.github.com/orgs/:org/repos', {
params: {
page,
per_page: 100
},
headers: {
Authorization: autheader
},
}).then(response => {
if (continuePredicate(response)){
accumulator.push(response)
return fetch(page + 1, continuePredicate, accumulator)
} else {
return accumulator;
}
})
const continuePredicate = response => response.data.length > 1
fetch(1, continuePredicate).then((acc) => {
console.log('done', acc.map(x => x.data.length));
const repos = f.flatMap(response => response.data, acc)
console.log(repos.length);
return bb.map(repos, repo =>
axios.request({
url: repo.contributors_url,
headers: {
'Authorization': autheader
},
})
.then(({data: contributors}) => Object.assign(repo, {contributors}))
)
.then(
f.flow(
f.orderBy(["updated_at"], ["desc"]),
f.map(repo => Object.assign(repo,
f.flow(
f.map(({login, contributions}) => [login, contributions]),
f.fromPairs
) (repo.contributors)
)),
f.each(repo => delete repo.contributors)
)
)
})
.then(x => { console.log(JSON.stringify(x, 0, 4)) })
.catch(err => {
console.log(err);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment