<commit_before>const express = require('express')
const axios = require('axios')
const Sniffr = require('sniffr')

const config = require('../config')
const controllerUtils = require('../lib/controller-utils')

const router = express.Router()

function capitalize (string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

function postToZen (ticket) {
  return axios.post(config.zenUrl, { ticket },
    {
      auth: {
        username: `${config.zenEmail}/token`,
        password: <PASSWORD>
      }
    }
  )
}

function getBug (req, res) {
  const data = (req.body && req.body.length > 0) ? req.body : {}

  const sniffr = new Sniffr()
  sniffr.sniff(req.headers['user-agent'])
  data.browser = `${capitalize(sniffr.browser.name)} ${sniffr.browser.version[0]}.${sniffr.browser.version[1]} - ${capitalize(sniffr.os.name)} ${sniffr.os.version[0]}.${sniffr.os.version[1]}`
  data.csrfToken = controllerUtils.genCSRF(req, res)
  res.render('support/bug', {data, errors: req.errors})
}

function checkForm (req) {
  const errors = {}
  if (!!req.body.email && !req.body.email.match(/.*@.*\..*/)) {
    errors.email = 'A valid email address is required'
  }

  if (!req.body.title) {
    errors.title = 'Your feedback needs a title'
  }

  if (!req.body.type) {
    errors.type = 'You need to choose between raising a bug and leaving feedback'
  }
  return errors
}

function postBug (req, res) {
  const errors = checkForm(req)
  if (Object.keys(errors).length > 0) {
    req.errors = errors
    return getBug(req, res)
  }
  const ticket = {
    requester: {
      email: (req.body.email && req.body.email.length > 0) ? req.body.email : 'Anonymous'
    },
    subject: req.body.title,
    comment: {
      body: (req.body.description && req.body.description.length > 0) ? req.body.description : 'N/A'
    },
    custom_fields: [
      {id: config.zenBrowser, value: req.body.browser},
      {id: config.zenService, value: 'datahub_export'}
    ],
    tags: [req.body.type]
  }
  return postToZen(ticket)
    .then(({data}) => {
      req.flash('success-message', `Created new bug, reference number ${data.ticket.id}`)
      res.redirect('/support/thank')
    })
}

function thank (req, res) {
  res.render('support/thank')
}

router.get('/bug', getBug)
router.post('/bug', postBug)
router.get('/thank', thank)

module.exports = { router }<commit_msg>Removed email address from ticket object if not present<commit_after>const express = require('express')
const axios = require('axios')
const Sniffr = require('sniffr')

const config = require('../config')
const controllerUtils = require('../lib/controller-utils')

const router = express.Router()

function capitalize (string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

function postToZen (ticket) {
  return axios.post(config.zenUrl, { ticket },
    {
      auth: {
        username: `${config.zenEmail}/token`,
        password: <PASSWORD>
      }
    }
  )
}

function getBug (req, res) {
  const data = (req.body && req.body.length > 0) ? req.body : {}

  const sniffr = new Sniffr()
  sniffr.sniff(req.headers['user-agent'])
  data.browser = `${capitalize(sniffr.browser.name)} ${sniffr.browser.version[0]}.${sniffr.browser.version[1]} - ${capitalize(sniffr.os.name)} ${sniffr.os.version[0]}.${sniffr.os.version[1]}`
  data.csrfToken = controllerUtils.genCSRF(req, res)
  res.render('support/bug', {data, errors: req.errors})
}

function checkForm (req) {
  const errors = {}
  if (!!req.body.email && !req.body.email.match(/.*@.*\..*/)) {
    errors.email = 'A valid email address is required'
  }

  if (!req.body.title) {
    errors.title = 'Your feedback needs a title'
  }

  if (!req.body.type) {
    errors.type = 'You need to choose between raising a bug and leaving feedback'
  }
  return errors
}

function postBug (req, res) {
  const errors = checkForm(req)
  if (Object.keys(errors).length > 0) {
    req.errors = errors
    return getBug(req, res)
  }
  const ticket = {
    requester: {
      name: '<NAME>'
    },
    subject: req.body.title,
    comment: {
      body: (req.body.description && req.body.description.length > 0) ? req.body.description : 'N/A'
    },
    custom_fields: [
      {id: config.zenBrowser, value: req.body.browser},
      {id: config.zenService, value: 'datahub_export'}
    ],
    tags: [req.body.type]
  }
  if (req.body.email && req.body.email.length > 0) {
    ticket.requester.email = req.body.email
  }
  return postToZen(ticket)
    .then(({data}) => {
      req.flash('success-message', `Created new bug, reference number ${data.ticket.id}`)
      res.redirect('/support/thank')
    }).catch(function (error) { res.render('support/bug', {}, {errors: {message: error.message}}) })
}

function thank (req, res) {
  res.render('support/thank')
}

router.get('/bug', getBug)
router.post('/bug', postBug)
router.get('/thank', thank)

module.exports = { router }
{'fraction_non_alphanumeric': 0.17340686274509803, 'fraction_numerical': 0.004084967320261438, 'mean_word_length': 3.3723214285714285, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 16, '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': '5921163', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1674, 'n_words': 354}