const _ = require('lodash')

const dbs = require('../../../lib/dbs')
const GithubQueue = require('../../../lib/github-queue')
const statsd = require('../../../lib/statsd')

const { createDocs } = require('../../../lib/repository-docs')

module.exports = async function ({ installation, repositories_added }) {
  const { repositories: reposDb } = await dbs()
  if (!repositories_added.length) return

  const repositories = await Promise.mapSeries(repositories_added, doc => {
    const [owner, repo] = doc.full_name.split('/')
    return GithubQueue(installation.id).read(github => github.repos.get({ owner, repo }))
  })

  statsd.increment('repositories', repositories.length)

  const repoDocs = await createDocs({
    repositories,
    accountId: String(installation.account.id)
  })

  // saving installation repos to db
  await reposDb.bulkDocs(repoDocs)

  // scheduling create-initial-branch jobs
  return _(repoDocs)
    .map(repository => ({
      data: {
        name: 'create-initial-branch',
        repositoryId: repository._id,
        accountId: repository.accountId
      }
    }))
    .value()
}

@@ -20,16 +20,46 @@
 lodash')
+%0Aconst Log = require('gk-log')
 %0A%0Aconst 
@@ -364,16 +364,22 @@
  reposDb
+, logs
  %7D = awa
@@ -393,46 +393,293 @@
 )%0A  
-if (!repositories_added.length)
+const log = Log(%7BlogsDb: logs, accountId: installation.account.id, repoSlug: null, context: 'integration-installation-repositories-added'%7D)%0A  log.info('started', %7B repositories_added %7D)%0A  if (!repositories_added.length) %7B%0A    log.warn('exited: no repositories selected')%0A   
  return
+%0A  %7D
 %0A%0A  
@@ -899,16 +899,64 @@
 )%0A  %7D)%0A%0A
+  log.info('added repositories', repositories)%0A%0A
   statsd
@@ -1186,16 +1186,16 @@
 oDocs)%0A%0A
-
   // sch
@@ -1229,16 +1229,68 @@
 ch jobs%0A
+  log.success('starting create-initial-branch job')%0A
   return

add logging for added job