Skip to content

Instantly share code, notes, and snippets.

@bfncs
Last active August 14, 2017 20:21
Show Gist options
  • Save bfncs/a393e410e7fec51c3d1c72e0bc1f9a30 to your computer and use it in GitHub Desktop.
Save bfncs/a393e410e7fec51c3d1c72e0bc1f9a30 to your computer and use it in GitHub Desktop.
const request = require('request-promise-native');
const Set = require('immutable').Set;
const parseLinkHeader = require('parse-link-header');
const fetchRepos = uri =>
request({
uri,
json: true,
resolveWithFullResponse: true,
headers: { 'User-Agent': 'ghmatch' },
})
.then(({ headers, body: repos }) => ({
repos,
links: parseLinkHeader(headers['link']),
}))
.then(
({ links, repos }) =>
links.next
? fetchRepos(links.next.url).then(nextRepos =>
repos.concat(nextRepos)
)
: repos
);
const fetchStarredRepos = username =>
fetchRepos(`https://api.github.com/users/${username}/starred`);
const reposToSetOfFullNames = repos =>
Set(repos.map(repo => repo['full_name']));
const getMatchingRepos = (user1, user2) =>
Promise.all([fetchStarredRepos(user1), fetchStarredRepos(user2)])
.then(results => results.map(reposToSetOfFullNames))
.then(([repos1, repos2]) => repos1.intersect(repos2));
getMatchingRepos('bfncs', 'derveloper').then(console.log).catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment