<commit_before>var Heroku = require("heroku");
var GitAPI = require('github');
var colors = require("colors");

var heroToken = process.env.HEROKU_TOKEN;
var gitToken = process.env.GITHUB_TOKEN;
//Place token in place process.env.TOKEN or set environmental tokens

// MAP TO DESIRED JSON FILE
var heroRepoNames = require('./heroku_github_namemap.json');
/*Map heroku apps to github repos in the file above like so
{
  "Heroku-App-Name": "Github-Repo-Name",
  "HerokuApp2": "MatchingGitHubApp"
}
*/

// SET OPTIONS TO DESIRED OUPUT: ORG OR USER
var options = {user: "Cjones90"};
//  {user: "username"}

// var options = {org: "NAQ"};
//  {org: "orgName"}



///// NOTHING BELOW NEEDS TO BE CONFIGURED //////

var heroku = new Heroku.Heroku({key: heroToken});
var github = new GitAPI({  version: "3.0.0"  });

github.authenticate({
    type: "oauth",
    token: gitToken
});

String.prototype.padRight = function (len) {
  if(this.length < len) {
    return String(this + (Array(len - this.length + 1).join(' ')))
  }
  else {
    return String(this);
  }
}

///TODO implement error catching

function getGit (options, callback) {
  var gitRepos = [];
  if(options.user) {
    getRepos = github.repos.getFromUser;
  }
  else if (options.org) {
    getRepos = github.repos.getFromOrg;
  }
  else {
    console.log("Please be sure to enter the appropriate options");
    return;
  }
  getRepos(options, function (err, repos) {
    repos.forEach(function (repo) {
      var commitArr = [];
      github.repos.getCommits({user: repo.owner.login, repo: repo.name}, function(err, results) {
        results.forEach(function(elem) {
          commitArr.push(elem.sha.slice(0,7));
        })
        github.repos.getBranches({user: repo.owner.login, repo: repo.name}, function (err, branches) {
          branches.forEach(function (branch) {
            if(branch.name === 'master') {
              gitRepos[gitRepos.length] = {name: repo.name, commit: branch.commit.sha.slice(0,7),
                branchCount: branches.length-1, commitArr: commitArr};
              if(gitRepos.length === repos.length) {
                callback(null, gitRepos)
              }
            }
          })
        })
      })
    })
  })
}

// TODO do get_release and get_app at same time, pushing into array if doesnt exist
// or mapping either .commit or .dyno if it doesnt exist, once both functions complete
// call callback
function getHeroku(callback) {
  var herokuRepos = [];
  heroku.get_apps(function (err, apps) {
    for(var i in apps) {
      (function (i) {
        heroku.get_releases(apps[i].name, function (err, releaseRes) {
          heroku.get_app(apps[i].name, function (err, appRes) {
            herokuRepos[herokuRepos.length] = {name: apps[i].name, commit: releaseRes[releaseRes.length-1].commit, dynos: appRes.dynos}
            if(herokuRepos.length === apps.length) {
              callback(null, herokuRepos);
            }
          })
        })
      })(i);
    }
  })
}

function getRepos(userFlag, git, heroku, cb) {
  var REPO = {
    gitArr: [],
    heroArr: []
  }
  git(userFlag, function (err, gitRepos) {
    gitRepos.forEach(function (repo) {
      REPO.gitArr[REPO.gitArr.length] = repo;
      if(REPO.gitArr.length === gitRepos.length) {
          cb(REPO);
      }
    })
  })

  heroku(function (err, herokuRepos) {
    herokuRepos.forEach(function (repo) {
      REPO.heroArr[REPO.heroArr.length] = repo;
      if(REPO.heroArr.length === herokuRepos.length) {
        cb(REPO);
      }
    });
  });

}

function padRepoLength(repos) {
  var padRepo = {
    MAX_LENGTH: 0,
    str: ''
  }
  for(p in repos) {
    if(repos[p].length > padRepo.MAX_LENGTH) {
      padRepo.MAX_LENGTH = repos[p].length;
    }
  }
  for(var i = 0; i < (padRepo.MAX_LENGTH/2)-2; i++) {
    padRepo.str += "-";
  }
  padRepo.str += 'Repo'
  for(var j = 0; j < (padRepo.MAX_LENGTH/2)-2; j++) {
    padRepo.str += "-";
  }
  return padRepo;
}


////////////^^^ FUNCTIONS//////// CALLS

getRepos(options, getGit, getHeroku, function (repos) {
  //Result contains all repos from git/heroku as an object in the form of:
  //repos = {gitArr: [{}, {} ...], heroArr: [{}, {} ...]}
  //gitArr = [{name: "RepoName", commit: "7 dig Sha Number"}, {}, {} ...]
  //heroArr = [{name: "RepoName", commit: "7 dig Sha Number"}, {}, {} ...]
  if(repos.gitArr.length && repos.heroArr.length) {
    var maxRepo = padRepoLength(heroRepoNames);
    console.log(); //// Buffer
    console.log(maxRepo.str+" ||  ---- Git ---  ||  -- Heroku --  || -- Dynos --");
    repos.gitArr.forEach(function (gitRepo) {
      var heroRepo = repos.heroArr.filter(function (elem) {
        return gitRepo.commitArr.some(function (elem2) {
          return elem2 === elem.commit;
        })
      })
      if(heroRepo.length) {
        var heroRepo = heroRepo[0];
        var str = gitRepo.name.padRight(maxRepo.MAX_LENGTH)+" ||  Sha: "+ gitRepo.commit+"  ||  Sha: "+ heroRepo.commit+"  ||  Dynos: "+ heroRepo.dynos+ "  === ";
        var flag = gitRepo.commit === heroRepo.commit ? str.green+"Match".padRight(12).green : str.red+"OUT OF DATE".padRight(12).red;
        flag = (gitRepo.branchCount > 0) ? flag+String("== "+gitRepo.branchCount).yellow+" branch(es)".yellow : flag;
        console.log(flag);
      }
    })
  }
});<commit_msg>remove dependency on json file<commit_after>var Heroku = require("heroku");
var GitAPI = require('github');
var colors = require("colors");

var heroToken = process.env.HEROKU_TOKEN;
var gitToken = process.env.GITHUB_TOKEN;
//Place token in place process.env.TOKEN or set environmental tokens


// SET OPTIONS TO DESIRED OUPUT: ORG OR USER
//  Examples:
//  {user: "username"}
//  {org: "orgName"}


//My current uses:
// var options = {user: "Cjones90"};
var options = {org: "NAQ"};


///// NOTHING BELOW NEEDS TO BE CONFIGURED //////
// TODO implement error catching
// TODO optimize calls
// TODO create API for npm/node use
// TODO allow params be passed in via CLI eg.
//    node hero*.js user:Cjones90
//    node hero*.js org:NAQ
// TODO remove change to String.prototype
// TODO modulize
// TODO better documentation


var heroku = new Heroku.Heroku({key: heroToken});
var github = new GitAPI({  version: "3.0.0"  });

github.authenticate({
    type: "oauth",
    token: gitToken
});

String.prototype.padRight = function (len) {
  if(this.length < len) {
    return String(this + (Array(len - this.length + 1).join(' ')))
  }
  else {
    return String(this);
  }
}

///TODO implement error catching

function getGit (options, callback) {
  var gitRepos = [];
  if(options.user) {
    getRepos = github.repos.getFromUser;
  }
  else if (options.org) {
    getRepos = github.repos.getFromOrg;
  }
  else {
    console.log("Please be sure to enter the appropriate options");
    return;
  }
  getRepos(options, function (err, repos) {
    repos.forEach(function (repo) {
      var commitArr = [];
      github.repos.getCommits({
        user: repo.owner.login,
        repo: repo.name
        }, function(err, results) {
        results.forEach(function(elem) {
          commitArr.push(elem.sha.slice(0,7));
        })
        github.repos.getBranches({
          user: repo.owner.login,
          repo: repo.name
          }, function (err, branches) {
          branches.forEach(function (branch) {
            if(branch.name === 'master') {
              gitRepos[gitRepos.length] = {
                name: repo.name,
                commit: branch.commit.sha.slice(0,7),
                branchCount: branches.length-1,
                commitArr: commitArr
              };
              if(gitRepos.length === repos.length) {
                callback(null, gitRepos)
              }
            }
          })
        })
      })
    })
  })
}


function getHeroku(callback) {
  var herokuRepos = [];
  heroku.get_apps(function (err, apps) {
    for(var i in apps) {
      (function (i) {
        heroku.get_releases(apps[i].name, function (err, releaseRes) {
          heroku.get_ps(apps[i].name, function (err, dyno) {
            herokuRepos[herokuRepos.length] = {
              name: apps[i].name,
              commit: releaseRes[releaseRes.length-1].commit,
              dynos: apps[i].dynos,
              dynoSize: dyno[0].size
            }
            if(herokuRepos.length === apps.length) {
              callback(null, herokuRepos);
            }
          })
        })
      })(i);
    }
  })
}

function getRepos(userFlag, git, heroku, cb) {
  var REPO = {
    gitArr: [],
    heroArr: []
  }
  git(userFlag, function (err, gitRepos) {
    gitRepos.forEach(function (repo) {
      REPO.gitArr[REPO.gitArr.length] = repo;
      if(REPO.gitArr.length === gitRepos.length) {
          cb(REPO);
      }
    })
  })

  heroku(function (err, herokuRepos) {
    herokuRepos.forEach(function (repo) {
      REPO.heroArr[REPO.heroArr.length] = repo;
      if(REPO.heroArr.length === herokuRepos.length) {
        cb(REPO);
      }
    });
  });
}

function padRepoLength(repos) {
  var padRepo = {
    MAX_LENGTH: 0,
    str: ''
  }
  for(p in repos) {
    if(repos[p].heroRepo.name.length > padRepo.MAX_LENGTH) {
      padRepo.MAX_LENGTH = repos[p].heroRepo.name.length;
    }
  }
  var minus = !(padRepo.MAX_LENGTH % 2) ? 2 : 3;
  for(var i = 0; i < (padRepo.MAX_LENGTH/2)-2; i++) {
    padRepo.str += "-";
  }
  padRepo.str += 'Live'
  for(var j = 0; j < (padRepo.MAX_LENGTH/2)-minus; j++) {
    padRepo.str += "-";
  }
  return padRepo;
}


////////////^^^ FUNCTIONS//////// CALLS

getRepos(options, getGit, getHeroku, function (repos) {
  //Result contains all repos from git/heroku as an object in the form of:
  //repos = {gitArr: [{}, {} ...], heroArr: [{}, {} ...]}
  //gitArr = [{name: "RepoName", commit: "7 dig Sha Number"}, {}, {} ...]
  //heroArr = [{name: "RepoName", commit: "7 dig Sha Number"}, {}, {} ...]
  if(repos.gitArr.length && repos.heroArr.length) {
    var matchedArr = [];
    repos.gitArr.forEach(function (gitRepo) {
      var heroRepo = repos.heroArr.filter(function (elem) {
        return gitRepo.commitArr.some(function (elem2) {
          return elem2 === elem.commit;
        })
      })
      if(heroRepo.length) {
        var heroRepo = heroRepo[0];
        matchedArr[matchedArr.length] = {"gitRepo": gitRepo, "heroRepo": heroRepo};
      }
    })
    var maxRepo = padRepoLength(matchedArr);
    console.log(); //// Buffer
    console.log(maxRepo.str+" ||  ---- Git ---  ||  -- Heroku --  ||  --- Dynos ---");
    matchedArr.forEach(function(elem) {
      var str = elem.heroRepo.name.padRight(maxRepo.MAX_LENGTH)+
          " ||  Sha: "+ elem.gitRepo.commit+"  ||  Sha: "+ elem.heroRepo.commit+
          "  ||  #: "+ elem.heroRepo.dynos+ " Size: "+String(elem.heroRepo.dynoSize).padRight(2)+"  === ";
      var flag = elem.gitRepo.commit === elem.heroRepo.commit ?
          str.green+"Match".padRight(12).green :
          str.red+"OUT OF DATE".padRight(12).red;
      flag = (elem.gitRepo.branchCount > 0) ?
          flag+String("== "+elem.gitRepo.branchCount).yellow+" branch(es)".yellow :
          flag;
      console.log(flag);
    })
  }
});
{'fraction_non_alphanumeric': 0.16551165511655117, 'fraction_numerical': 0.005850058500585006, 'mean_word_length': 2.4127764127764126, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2437352', 'n_tokens_mistral': 3840, 'n_tokens_neox': 3791, 'n_words': 785}