#!/usr/bin/env node
import Promise from 'bluebird';
import { GitHubClient } from 'opencollective-jobs';
import models from '../../server/models';
const _ = require('lodash'); //eslint-disable-line import/no-commonjs

Promise.longStackTraces();
const client = GitHubClient({logLevel: 'verbose'});
const { log } = client; // repurpose the logger
const { Collective } = models;

Collective.findAll({
  attributes: [
    'id',
    'name',
    'settings',
    'data'
  ], 
  where: {
    type: "COLLECTIVE"
  }
})
  .tap(collectives => {
    log.verbose('collectives', `Found ${collectives.length} collective(s) to inspect`);
  })
  .each(collective => {
    const org = _.get(collective, 'settings.githubOrg');
    const repoLink = _.get(collective, 'settings.githubRepo');
    if (!org && !repoLink) {
      log.warn(collective.name, `No GitHub org or repo associated`);
      return;
    }
    let fetchPromise;

    if (org) {
      fetchPromise = client.contributorsInOrg({orgs: [org]})
        .get(org)
        .then(repos => {
          const data = {};
          data.contributorData = _(repos)
              .map('contributors')
              .reduce((acc, contributions) => {
                _.each(contributions, (count, user) => {
                  acc[user] = (acc[user] || 0) + count;
                });
                return acc;
              }, {});
          data.repoData = _.mapValues(repos, repo => _.omit(repo, 'contributors'));
          data.stars = _(repos)
              .map('stars')
              .reduce((sum, n) => {
                return sum + n;
              }, 0)
          return data;
        });
    } else {
      const split = repoLink.split('/');
      if (split.length !== 2) {
        log.warn(collective.name, 'Incorrect format of githubRepo');
        return;
      }
      const options = {
        user: split[0],
        repo: split[1]
      }
      fetchPromise = client.contributorsForRepo(options)
        .then(data => {
          const contributorData = {};
          data.map(dataEntry => contributorData[dataEntry.user] = dataEntry.contributions);
          return {contributorData}
        });
    }

    return fetchPromise
      .then(data => {
        collective.data = _.assign(collective.data || {}, {
          githubContributors: data.contributorData,
          repos: data.repoData
        });
        return collective.save();
      })
      .then(() => {
        log.info(collective.name,
          `Successfully updated contribution data`);
      })
      .catch(err => {
        log.error(collective.name, err.stack);
      });
  })
  .finally(() => {
    log.verbose('collectives', 'Done.');
    process.exit();
  });

@@ -373,99 +373,302 @@
 s;%0A%0A
-Collective.findAll(%7B%0A  attributes: %5B%0A    'id',%0A    'name',%0A    'settings',%0A    'data'%0A  %5D, 
+// TODO: As number of collectives grow, we need to consider fetching 100 at a time,%0A// otherwise, we end up fetching a lot of data.%0A// Note: Picking attributes doesn't work with Histories table because Histories %0A// ends up recording on the attributes that were pulled out%0A%0ACollective.findAll(%7B
 %0A  w
@@ -2379,33 +2379,39 @@
 ise%0A      .then(
-d
+githubD
 ata =%3E %7B%0A       
@@ -2417,24 +2417,18 @@
   co
-llective.
+nst 
 data =
- 
 _.as
@@ -2487,17 +2487,23 @@
 butors: 
-d
+githubD
 ata.cont
@@ -2532,17 +2532,23 @@
  repos: 
-d
+githubD
 ata.repo
@@ -2594,13 +2594,23 @@
 ive.
-save(
+update(%7B data %7D
 );%0A 

fix slug issue (#1110)