<commit_before>express = require "express"
md      = require "markdown"
async   = require "async"
_       = require "underscore"
nconf   = require "nconf"
moment  = require "moment"

stylus  = require "stylus"
nib     = require "nib"

github  = require "octonode"
gitty   = require "gitty"

GitHubApi = require "github"
ghRepos   = new GitHubApi({version: "3.0.0"}).repos


redis = require "redis"
rc    = redis.createClient()


app = module.exports = express()


nconf
  .argv()
  .env()
  .file({file: __dirname + "/configs/" + app.settings.env + ".config.json"})
  .defaults({"NODE_ENV": "development"})


github.auth.config({
  username: nconf.get("GIHUB_ADMIN_USERNAME")
  password: <PASSWORD>("<PASSWORD>")
}).login ["user", "repo", "gist"], (err, id, token) ->
  if err or not token
    throw new Error("Cannot connect to GitHub")
  nconf.set "GIHUB_ADMIN_TOKEN", token  



createRepo = (repo, username, callback) ->
  createGitHubRepo repo, username, (err, repo) ->
    if err
      callback err
      return
    createGitRepo repo, callback  


createGitHubRepo = (repo, username, callback) ->
  ghAdmin = github.client nconf.get "GIHUB_ADMIN_TOKEN"
  ghAdmin.post "/orgs/communities/repos", repo, (err, status, repo) ->
    if err
      callback err
      return
    ghAdmin.post "/orgs/communities/teams", {name: "#{repo.name}-admins", permission: "admin", repo_names:["communities/#{repo.name}"]}, (err, status, team) ->
      if err
        callback err
        return
      ghAdmin.put "/teams/#{team.id}/members/#{username}", {}, (err, status, resp) ->
        if err
          callback err
          return   
        ghAdmin.post "/orgs/communities/teams", {name: "#{repo.name}-members", permission: "push", repo_names:["communities/#{repo.name}"]}, (err, status, team) ->
          if err
            callback err
            return
          hook = {
            "name": "web",
            "active": true,
            "config": {
              "url": "http://gitcommunities.com/webhook"
            }
          }  
          ghAdmin.post "/repos/communities/#{repo.name}/hooks", hook, (err) ->
            if err
              callback err
              return
            callback undefined, repo    


createGitRepo = (repo, callback) ->
  {name} = repo
  gitty.create name, repo.description, __dirname + "/repos", (err, data) ->
    # gitty.add __dirname + "/repos/#{name}", ["LICENSE"], (err, data) ->
    #   console.log "added file", err, data
    #   if err
    #     callback err
    #     return
    gitty.commit __dirname + "/repos/#{name}", "initial", (err, data) ->
      gitty.remote.add __dirname + "/repos/#{name}", "origin", "https://github.com/communities/#{name}.git", (err, data) ->
        console.log "origin was added", err, data
        gitty.push __dirname + "/repos/#{name}", "origin", "master", (err, data) ->
          if err
            callback err
            return
          callback undefined, data  


passport = require "passport"

GitHubStrategy = require("passport-github").Strategy

passport.serializeUser (user, done) -> done null, user
passport.deserializeUser (obj, done) -> done null, obj



passport.use new GitHubStrategy {
    clientID: nconf.get("GITHUB_CLIENT_ID"),
    clientSecret: nconf.get("GITHUB_CLIENT_SECRET"),
    callbackURL: nconf.get("GITHUB_CALLBACK_URL")
  },
  (accessToken, refreshToken, profile, done) ->

    process.nextTick ->
      profile.accessToken = accessToken
      profile.avatar = profile._json.avatar_url
      return done null, profile



# stylus compile function
compile = (str, path) ->
  return stylus(str)
    .define("url", stylus.url({ paths: [__dirname + "/public"] }))
    .set("filename", path)
    .set("warn", true)
    .set("compress", false)
    .use(nib())

app.configure ->
  # stylus middleware
  app.use stylus.middleware
    src    : __dirname + "/styls"  # styl files should be placed inside this folder
    dest   : __dirname + "/public" # CSS files will be complied to public directory
    compile: compile    # compile function
  app.set "views", __dirname + "/views"
  app.set "view engine", "jade"
  app.set "view options", {layout: false}
  app.use express.favicon __dirname + "/public/favicon.ico"
  app.use express.logger()
  app.use express.cookieParser()
  app.use express.bodyParser()
  app.use express.methodOverride()
  app.use express.session { secret: "session-secret-key" }


  app.use passport.initialize()
  app.use passport.session()
  app.use app.router 
  app.use express.static __dirname + "/public"


app.get "/auth",
  passport.authenticate("github", scope: "repo"),
  (req, res) ->


app.get "/logout", (req, res) ->
  req.logout()
  res.redirect "/"

app.get "/auth/callback", 
  passport.authenticate("github", { failureRedirect: "/login" }),
  (req, res) ->
    res.redirect "/"




app.get "/api/communities", (req, res) ->
  getCommunities (err, repos) ->
    if err
      res.send 500, { error: "API call failed" }
      return
    res.json repos


getCommunities = (callback) ->
  rc.hgetall "communities", (err, hash) ->
    repos = (JSON.parse(repo) for name, repo of hash)
    if repos.length > 0
      callback undefined, repos
    else  
      ghRepos.getFromOrg {org: "communities"}, (err, repos) ->
        repos = _.filter repos, (repo) -> repo.name != "gitcommunities.com"
        communitiesFetchFuncs = []
        _.each repos, (repo) ->
          communitiesFetchFuncs.push async.apply getCommunity, repo.name
        async.parallel communitiesFetchFuncs, (err, repos) ->
          if err
            callback err
            return
          hash = {}  
          _.each repos, (repo) -> hash[repo.name] = JSON.stringify(repo)  
          rc.hmset "communities", hash, callback

app.get "/api/communities/:community", (req, res) ->
  community = req.params.community
  getCommunity community, (err, community) ->
    if err
      res.send 500, { error: "API call failed" }
      return
    res.json community 


getCommunity = (community, callback) ->
  ghRepos.get {user: "communities", repo: community}, (err, repo) ->
    if err
      callback err
      return
    worker =
      topics: async.apply getTopics, community<commit_msg>minor updates<commit_after>express = require "express"
md      = require "markdown"
async   = require "async"
_       = require "underscore"
nconf   = require "nconf"
moment  = require "moment"

stylus  = require "stylus"
nib     = require "nib"

github  = require "octonode"
gitty   = require "gitty"

GitHubApi = require "github"
ghRepos   = new GitHubApi({version: "3.0.0"}).repos


redis = require "redis"
rc    = redis.createClient()


app = module.exports = express()


nconf
  .argv()
  .env()
  .file({file: __dirname + "/configs/" + app.settings.env + ".config.json"})
  .defaults({"NODE_ENV": "development"})


github.auth.config({
  username: nconf.get("GIHUB_ADMIN_USERNAME")
  password: <PASSWORD>("<PASSWORD>")
}).login ["user", "repo", "gist"], (err, id, token) ->
  if err or not token
    throw new Error("Cannot connect to GitHub")
  nconf.set "GIHUB_ADMIN_TOKEN", token  



createRepo = (repo, username, callback) ->
  createGitHubRepo repo, username, (err, repo) ->
    if err
      callback err
      return
    createGitRepo repo, callback  


createGitHubRepo = (repo, username, callback) ->
  ghAdmin = github.client nconf.get "GIHUB_ADMIN_TOKEN"
  ghAdmin.post "/orgs/communities/repos", repo, (err, status, repo) ->
    if err
      callback err
      return
    ghAdmin.post "/orgs/communities/teams", {name: "#{repo.name}-admins", permission: "admin", repo_names:["communities/#{repo.name}"]}, (err, status, team) ->
      if err
        callback err
        return
      ghAdmin.put "/teams/#{team.id}/members/#{username}", {}, (err, status, resp) ->
        if err
          callback err
          return   
        ghAdmin.post "/orgs/communities/teams", {name: "#{repo.name}-members", permission: "push", repo_names:["communities/#{repo.name}"]}, (err, status, team) ->
          if err
            callback err
            return
          hook = {
            "name": "web",
            "active": true,
            "config": {
              "url": "http://gitcommunities.com/webhook"
            }
          }  
          ghAdmin.post "/repos/communities/#{repo.name}/hooks", hook, (err) ->
            if err
              callback err
              return
            callback undefined, repo    


createGitRepo = (repo, callback) ->
  {name} = repo
  gitty.create name, repo.description, __dirname + "/repos", (err, data) ->
    gitty.add __dirname + "/repos/#{name}", ["LICENSE"], (err, data) ->
      gitty.commit __dirname + "/repos/#{name}", "initial", (err, data) ->
        gitty.remote.add __dirname + "/repos/#{name}", "origin", "https://github.com/communities/#{name}.git", (err, data) ->
          console.log "origin was added", err, data
          gitty.push __dirname + "/repos/#{name}", "origin", "master", (err, data) ->
            callback undefined, data  


passport = require "passport"

GitHubStrategy = require("passport-github").Strategy

passport.serializeUser (user, done) -> done null, user
passport.deserializeUser (obj, done) -> done null, obj



passport.use new GitHubStrategy {
    clientID: nconf.get("GITHUB_CLIENT_ID"),
    clientSecret: nconf.get("GITHUB_CLIENT_SECRET"),
    callbackURL: nconf.get("GITHUB_CALLBACK_URL")
  },
  (accessToken, refreshToken, profile, done) ->

    process.nextTick ->
      profile.accessToken = accessToken
      profile.avatar = profile._json.avatar_url
      return done null, profile



# stylus compile function
compile = (str, path) ->
  return stylus(str)
    .define("url", stylus.url({ paths: [__dirname + "/public"] }))
    .set("filename", path)
    .set("warn", true)
    .set("compress", false)
    .use(nib())

app.configure ->
  # stylus middleware
  app.use stylus.middleware
    src    : __dirname + "/styls"  # styl files should be placed inside this folder
    dest   : __dirname + "/public" # CSS files will be complied to public directory
    compile: compile    # compile function
  app.set "views", __dirname + "/views"
  app.set "view engine", "jade"
  app.set "view options", {layout: false}
  app.use express.favicon __dirname + "/public/favicon.ico"
  app.use express.logger()
  app.use express.cookieParser()
  app.use express.bodyParser()
  app.use express.methodOverride()
  app.use express.session { secret: "session-secret-key" }


  app.use passport.initialize()
  app.use passport.session()
  app.use app.router 
  app.use express.static __dirname + "/public"


app.get "/auth",
  passport.authenticate("github", scope: "repo"),
  (req, res) ->


app.get "/logout", (req, res) ->
  req.logout()
  res.redirect "/"

app.get "/auth/callback", 
  passport.authenticate("github", { failureRedirect: "/login" }),
  (req, res) ->
    res.redirect "/"




app.get "/api/communities", (req, res) ->
  getCommunities (err, repos) ->
    if err
      res.send 500, { error: "API call failed" }
      return
    res.json repos


getCommunities = (callback) ->
  rc.hgetall "communities", (err, hash) ->
    repos = (JSON.parse(repo) for name, repo of hash)
    if repos.length > 0
      callback undefined, repos
    else  
      ghRepos.getFromOrg {org: "communities"}, (err, repos) ->
        repos = _.filter repos, (repo) -> repo.name != "gitcommunities.com"
        communitiesFetchFuncs = []
        _.each repos, (repo) ->
          communitiesFetchFuncs.push async.apply getCommunity, repo.name
        async.parallel communitiesFetchFuncs, (err, repos) ->
          if err
            callback err
            return
          hash = {}  
          _.each repos, (repo) -> hash[repo.name] = JSON.stringify(repo)  
          rc.hmset "communities", hash, (err) ->
            callback err, repos

app.get "/api/communities/:community", (req, res) ->
  community = req.params.community
  getCommunity community, (err, community) ->
    if err
      res.send 500, { error: "API call failed" }
      return
    res.json community 


getCommunity = (community, callback) ->
  ghRepos.get {user: "communities", repo: community}, (err, repo) ->
    if err
      callback err
      return
    worker =
      topics: async.apply getTopics, community
{'fraction_non_alphanumeric': 0.14589442815249268, 'fraction_numerical': 0.0016291951775822744, 'mean_word_length': 2.686786786786787, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 84, 'https://': 2, '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': '860915', 'n_tokens_mistral': 4008, 'n_tokens_neox': 3843, 'n_words': 998}