<commit_before>fs = require 'fs'
cons = require 'consolidate'

validate = (req, res, next) ->
  if req.session.user?
    next()
  else
    req.session.error = 'Not Authenticated'
    res.redirect '/login'

module.exports = (robot) ->
  app = robot.router
  env = process.env
  team = env.HUBOT_SLACK_TEAM or ''

  robot.brain.on 'loaded', ->
    robot.brain.data.ingressAgents ?= []

  app.engine 'html', cons.hogan
  app.set 'view engine', 'html'
  app.set 'views', "#{__dirname}/views"

  app.post '/login', (req, res) ->
    if req.body.user?.kind is 'plus#person'
      req.session.user = req.body.user
      res.send 200
    else
      res.send 401

  app.get '/login', (req, res) ->
    if req.session.user?
      res.redirect '/apply'
    else
      res.render 'login'

  app.get '/apply', validate, (req, res) ->
    user = req.session.user
    viewData =
      team: team
      title: env.HUBOT_INGRESS_INVITE_TITLE or "You're almost there!"
      description: env.HUBOT_INGRESS_INVITE_DESC or 'Please fill out
 the form below and upload your verification screenshot to complete your
 application. We have attempted to determine your agent name automatically,
 please check that this information is correct. Your application will be
 reviewed as quickly as possible and your invitation will be sent to the email
 address below if you are approved.'
      fullName: user.displayName
      givenName: user.name.givenName
      nickname: user.nickname
      email: user.emails[0].value

    res.render 'apply', viewData

  app.post '/apply', validate, (req, res) ->
    user = req.session.user
    fileTemp = req.files.screenshot.path
    filename = "images/#{fileTemp.split('/').pop()}." + req.files.screenshot.name.split('.').pop()
    imageUrl = env.HUBOT_BASE_URL.replace /\/$/, "/#{filename}"

    fs.rename fileTemp, "#{__dirname}/public/#{filename}", (err) ->
      if err?
        res.send 500
      viewData =
        team: team
        fullName: user.displayName
        email: user.emails[0].value
        imagePath: filename

      res.render 'thanks', viewData

      user.agentName = req.body.agentName.slice 0, 32
      user.community = req.body.community.slice 0, 140
      user.comments = req.body.comments.slice 0, 140
<commit_msg>handle no HUBOT_BASE_URL env var<commit_after>fs = require 'fs'
cons = require 'consolidate'

validate = (req, res, next) ->
  if req.session.user?
    next()
  else
    req.session.error = 'Not Authenticated'
    res.redirect '/login'

module.exports = (robot) ->
  app = robot.router
  env = process.env
  team = env.HUBOT_SLACK_TEAM or ''
  url = env.HUBOT_BASE_URL or 'http://please-set-HUBOT_BASE_URL/'

  robot.brain.on 'loaded', ->
    robot.brain.data.ingressAgents ?= []

  app.engine 'html', cons.hogan
  app.set 'view engine', 'html'
  app.set 'views', "#{__dirname}/views"

  app.post '/login', (req, res) ->
    if req.body.user?.kind is 'plus#person'
      req.session.user = req.body.user
      res.send 200
    else
      res.send 401

  app.get '/login', (req, res) ->
    if req.session.user?
      res.redirect '/apply'
    else
      res.render 'login'

  app.get '/apply', validate, (req, res) ->
    user = req.session.user
    viewData =
      team: team
      title: env.HUBOT_INGRESS_INVITE_TITLE or "You're almost there!"
      description: env.HUBOT_INGRESS_INVITE_DESC or 'Please fill out
 the form below and upload your verification screenshot to complete your
 application. We have attempted to determine your agent name automatically,
 please check that this information is correct. Your application will be
 reviewed as quickly as possible and your invitation will be sent to the email
 address below if you are approved.'
      fullName: user.displayName
      givenName: user.name.givenName
      nickname: user.nickname
      email: user.emails[0].value

    res.render 'apply', viewData

  app.post '/apply', validate, (req, res) ->
    user = req.session.user
    fileTemp = req.files.screenshot.path
    filename = "images/#{fileTemp.split('/').pop()}." + req.files.screenshot.name.split('.').pop()
    imageUrl = url.replace /\/$/, "/#{filename}"

    fs.rename fileTemp, "#{__dirname}/public/#{filename}", (err) ->
      if err?
        res.send 500
      viewData =
        team: team
        fullName: user.displayName
        email: user.emails[0].value
        imagePath: filename

      res.render 'thanks', viewData

      user.agentName = req.body.agentName.slice 0, 32
      user.community = req.body.community.slice 0, 140
      user.comments = req.body.comments.slice 0, 140

{'fraction_non_alphanumeric': 0.12554585152838427, 'fraction_numerical': 0.00960698689956332, 'mean_word_length': 2.8079800498753116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '886801', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1453, 'n_words': 406}