text: const { makeRequest, fetch, chunked } = require('./common')
  const config = require('./config.json')
  
  async function lookup(username) {
    const repos = await makeRequest(`https://api.github.com/user/repos`)
    const counts = {}
  
    await chunked(repos, async (repo) => {
      if (repo.owner.login !== username) return
      const pullrequests = await fetch(`https://api.github.com/repos/${repo.full_name}/pulls?state=open`)
  
      pullrequests
        .filter(x => (!x.draft && !x.fork && x.url.indexOf(username) > -1 && x.state === 'open'))
        .forEach(x => {
          if (!counts[repo.full_name]) {
            counts[repo.full_name] = 0
          }
          counts[repo.full_name]++
          console.log('----------')
          console.log(x.title)
          console.log(x.url)
          console.log('----------')
          console.log('\n\n')
        })
    })
  
    console.log(`${username} summary:`)
    let total = 0
    for (let key in counts) {
      total += counts[key]
      console.log(`${key}: ${counts[key]}`)
    }
  
    return {
      username,
      total
    }
  }
  
  async function main () {
    const result = await Promise.all(config.github.orgs.map(username => {
      return lookup(username)
    }))
  
    console.log(result)
  }
  
  module.exports = {
    main
  }
  
  if (require.main === module) {
    main()
  }
  