Last active
February 21, 2017 09:21
-
-
Save oshimayoan/02a91eaef0be474f5a5070028cbcdaff to your computer and use it in GitHub Desktop.
Fetching github users, orgs, and repos and print it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
// import fetch from 'node-fetch'; | |
async function fetchUsers(userNames: Array<string>): Promise<*> { | |
console.log('Start fetching'); | |
console.log('--------------'); | |
for (let userName of userNames) { | |
let orgs: Array<Object> = await fetch(`https://api.github.com/users/${userName}/orgs`).then((res: Response) => res.json()); // eslint-disable-line | |
console.log('Fetching user..'); | |
let orgsList = new Set(); | |
for (let org of orgs) { | |
orgsList.add(org.login); | |
usersOrgs.set(userName, orgsList); | |
if (!orgsRepos.has(org.login)) { | |
let repos: Array<Object> = await fetch(`https://api.github.com/orgs/${org.login}/repos`).then((res: Response) => res.json()); // eslint-disable-line | |
console.log('Fetching orgs..'); | |
orgsRepos.set(org.login, repos.length); | |
} | |
} | |
} | |
console.log('Fetching done'); | |
console.log('--------------'); | |
return 1; | |
} | |
async function showUsers(userNames: Array<string>) { | |
let result = await fetchUsers(userNames); // eslint-disable-line | |
console.log('Start writing'); | |
console.log('--------------'); | |
usersOrgs.forEach((orgs: Set<string>, userName: string) => { | |
console.log(`${userName} member of ${orgs.size} orgs:`); | |
orgs.forEach((orgName: string) => { | |
console.log(`>> ${orgName} has ${orgsRepos.get(orgName)}`); | |
}); | |
}); | |
console.log('--------------'); | |
console.log('Writing done.'); | |
} | |
let userNames = ['mjackson', 'vjeux', 'jordwalke']; | |
// let userNames = ['mjackson']; | |
let usersOrgs: Map<string, Set<string>> = new Map(); | |
let orgsRepos: Map<string, number> = new Map(); | |
showUsers(userNames); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment