<commit_before>const express = require('express')
<<<<<<< HEAD
const logger = require('winston')
const Github = require('github')
const Router = express.Router
const router = new Router()
const config = require('../../../../../config')
const Bugreport = require('../../../../../models/Bugreport')

const github = new Github({
=======
const router = express.Router({
  mergeParams: true
})
const RateLimit = require('express-rate-limit')
const github = new (require('github'))({
>>>>>>> 8443436522a7d831ab5a55f717a25926164685a8
  debug: false,
  protocol: 'https',
  host: 'api.github.com',
  pathPrefix: '',
  timeout: 5000,
  headers: {
    'user-agent': 'STUCO-Backend-Server'
  },
  followRedirects: false,
  includePreview: true
})

github.authenticate({type: 'token', token: config.github.access_token})

// NOTE: We may need to add permissions to only allow certain users to submit BugreportSchema
// NOTE: We also may want to add a quota for certain roles, (student is 5 reports per hour, tester is unlimmited, etc)
/**
  * @api {post} /api/v1/bugreport Submit a bug report
  * @apiVersion 1.0.0
  * @apiName Submit
  * @apiGroup Bugreport
  * @apiDescription Submit a bug report to bugreport database and github issues
  *
  * @apiUse AuthParam
  *
  * @apiParam {String="crash","ui","login","event","other"} bugtype The type or category of bug
  * @apiParam {String{1..512}} summary Title for the bug occuring
  * @apiParam {String{16..4096}} description Description of the bug
  * @apiParam {String} [syslogs] System logs at the time of the bug
  * @apiParam {String} [applogs] Application logs associated with the bug
  *
  * @apiSuccess {String} submitter User ID of the submitter
  * @apiSuccess {Boolean} closed Whether or not the issue has been handled
  * @apiSuccess {String} summary Submitted title of the bugreport
  * @apiSuccess {String} description Submitted description of the bugreport
  * @apiSuccess {String} bugtype Submitted type of the bugreport
*/
router.post('/', (req, res) => {
  if (req.body.bugtype == null || req.body.summary == null || req.body.summary.trim() === '' || req.body.description == null || req.body.description === '') {
    return res.error('Body Parameters Not Met')
  } else {
    // NOTE: We may possibly want to store reports locally as well as setup a webhook to update them from github as they are closed, edited, or labeled
    new Bugreport({
      submitter: req.user.uid,
      bugtype: req.body.bugtype,
      summary: req.body.summary,
      description: req.body.description,
      syslogs: req.body.syslogs,
      applogs: req.body.applogs
    }).save().then((dbBug) => {
      // return res.json(dbBug.pretty())
      // TODO: Do checking for duplicate bugs then create issue
      github.issues.create({
        owner: 'RSCodingClub',
        repo: 'STUCO-Backend',
        title: dbBug.summary,
        body: dbBug.description.toString().trim(),
        labels: ['bug', dbBug.bugtype]
      }).then((issue) => {
        logger.info('Created Github bug report.')
        return res.json(dbBug.pretty())
      }).catch((githubError) => {
        logger.error(githubError, {context: 'githubError'})
        return res.error()
      })
    }).catch((dbError) => {
      logger.error(dbError, {context: 'dbError'})
      return res.error()
    })
  }
})

<<<<<<< HEAD
module.exports = router
=======
router.use(new RateLimit({
  message: 'Too many bug reports submitted too fast.  Wait a few minutes before trying again.',
  headers: true,
  windowMs: 15 * 60 * 1000, // 15 minute window
  max: 25,
  delayAfter: 1,
  delayMs: (15 * 60 * 1000) / (25 - 1) // Delay requests as to never reach the max
}))
>>>>>>> 8443436522a7d831ab5a55f717a25926164685a8<commit_msg>Manual merge of bugreport router.<commit_after>const express = require('express')
const logger = require('winston')
const Github = require('github')
const Router = express.Router
const router = new Router()
const config = require('../../../../../config')
const Bugreport = require('../../../../../models/Bugreport')

const RateLimit = require('express-rate-limit')
const github = new Github({
  debug: false,
  protocol: 'https',
  host: 'api.github.com',
  pathPrefix: '',
  timeout: 5000,
  headers: {
    'user-agent': 'STUCO-Backend-Server'
  },
  followRedirects: false,
  includePreview: true
})

github.authenticate({type: 'token', token: config.github.access_token})

// NOTE: We may need to add permissions to only allow certain users to submit BugreportSchema
// NOTE: We also may want to add a quota for certain roles, (student is 5 reports per hour, tester is unlimmited, etc)
/**
  * @api {post} /api/v1/bugreport Submit a bug report
  * @apiVersion 1.0.0
  * @apiName Submit
  * @apiGroup Bugreport
  * @apiDescription Submit a bug report to bugreport database and github issues
  *
  * @apiUse AuthParam
  *
  * @apiParam {String="crash","ui","login","event","other"} bugtype The type or category of bug
  * @apiParam {String{1..512}} summary Title for the bug occuring
  * @apiParam {String{16..4096}} description Description of the bug
  * @apiParam {String} [syslogs] System logs at the time of the bug
  * @apiParam {String} [applogs] Application logs associated with the bug
  *
  * @apiSuccess {String} submitter User ID of the submitter
  * @apiSuccess {Boolean} closed Whether or not the issue has been handled
  * @apiSuccess {String} summary Submitted title of the bugreport
  * @apiSuccess {String} description Submitted description of the bugreport
  * @apiSuccess {String} bugtype Submitted type of the bugreport
*/
router.post('/', (req, res) => {
  if (req.body.bugtype == null || req.body.summary == null || req.body.summary.trim() === '' || req.body.description == null || req.body.description === '') {
    return res.error('Body Parameters Not Met')
  } else {
    // NOTE: We may possibly want to store reports locally as well as setup a webhook to update them from github as they are closed, edited, or labeled
    new Bugreport({
      submitter: req.user.uid,
      bugtype: req.body.bugtype,
      summary: req.body.summary,
      description: req.body.description,
      syslogs: req.body.syslogs,
      applogs: req.body.applogs
    }).save().then((dbBug) => {
      // TODO: Do checking for duplicate bugs then create issue
      github.issues.create({
        owner: 'RSCodingClub',
        repo: 'STUCO-Backend',
        title: dbBug.summary,
        body: dbBug.description.toString().trim(),
        labels: ['bug', dbBug.bugtype]
      }).then((issue) => {
        logger.info('Created Github bug report.')
        return res.json(dbBug.pretty())
      }).catch((githubError) => {
        logger.error(githubError, {context: 'githubError'})
        return res.error()
      })
    }).catch((dbError) => {
      logger.error(dbError, {context: 'dbError'})
      return res.error()
    })
  }
})

router.use(new RateLimit({
  message: 'Too many bug reports submitted too fast.  Wait a few minutes before trying again.',
  headers: true,
  windowMs: 15 * 60 * 1000, // 15 minute window
  max: 25,
  delayAfter: 1,
  delayMs: (15 * 60 * 1000) / (25 - 1) // Delay requests as to never reach the max
}))

module.exports = router
{'fraction_non_alphanumeric': 0.13997775924381428, 'fraction_numerical': 0.020850708924103418, 'mean_word_length': 3.400611620795107, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, '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': '1542131', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2151, 'n_words': 708}