<commit_before>var concat = require('concat-stream')
var http = require('http')
var fs = require('fs')
var url = require('url')

var prStatus = require('./prcheck.js')
var collabStatus = require('./collabcheck.js')
var mergePr = require('./merge.js')

module.exports = function(onHook) {
  var server = http.createServer(handler)

  function handler(req, res) {
    console.log(req.method, req.url)
    
    // when RR gets a push from email on collab
    if (req.url === '/push') {
      return handleHook(req, res)
    }
    
    // when git-it verifies user made a pr
    if (req.url.match('/pr')) {
      var queryURL = url.parse(req.url, true)
      var username = queryURL.query.username
      return prStatus(username, function(err, pr) {
        checkPR(res, err, pr)
      })
    }
    
    // when a pr is made to patchwork repo  
    if (req.url.match('/orderin')) {
      return getPR(req, res)
    }

    // when git-it verifies user added RR as collab
    if (req.url.match('/collab')) {
      var queryURL = url.parse(req.url, true)
      var username = queryURL.query.username
      return collabStatus(username, function(err, collab) {
        checkCollab(res, err, collab)
      })
    }
  
  // when anything else goes to reporobot.jlord.us
  res.statusCode = 404
  res.setHeader('content-type', 'application/json')
  res.end(JSON.stringify({
    error: 404,
    message: 'not_found'
  }, true, 2))
}

  function handleHook(req, res) {
    req.pipe(concat(function(buff) {
      var hookObj = JSON.parse(buff)
      if (onHook) onHook(hookObj, req)
    }))
    res.statusCode = 200
    res.end("Thank you.")
  }
  
  function getPR(req, res) {
    req.pipe(concat(function(buff) {
      console.log(["buffstring", buff.toString()])
      var pullreq = JSON.parse(buff)
      mergePr(pullreq, req)
      res.statusCode = 200
      res.setHeader('content-type', 'application/json')
      res.end()
    }))
  }

  function checkPR(res, err, pr) {
    if (err) {
      console.log(err)
      res.statusCode = 500
      res.end(JSON.stringify({error: err}))
      return
    }
    res.statusCode = 200
    res.setHeader('content-type', 'application/json')
    res.end(JSON.stringify({
      pr: pr
    }, true, 2))
  }

  function checkCollab(res, err, collab) {
    if (err) {
      console.log(err)
      res.statusCode = 500
      res.end(JSON.stringify({error: err}))
      return
    }
    res.statusCode = 200
    res.setHeader('content-type', 'application/json')
    res.end(JSON.stringify({
      collab: collab
    }, true, 2))
  }
  
  function mergedPr(res, err) {<commit_msg>tired<commit_after>var concat = require('concat-stream')
var http = require('http')
var fs = require('fs')
var url = require('url')

var prStatus = require('./prcheck.js')
var collabStatus = require('./collabcheck.js')
var mergePr = require('./merge.js')

module.exports = function(onHook) {
  var server = http.createServer(handler)

  function handler(req, res) {
    console.log(req.method, req.url)
    
    // when RR gets a push from email on collab
    if (req.url === '/push') {
      return handleHook(req, res)
    }
    
    // when git-it verifies user made a pr
    if (req.url.match('/pr')) {
      var queryURL = url.parse(req.url, true)
      var username = queryURL.query.username
      return prStatus(username, function(err, pr) {
        checkPR(res, err, pr)
      })
    }
    
    // when a pr is made to patchwork repo  
    if (req.url.match('/orderin')) {
      return getPR(req, res)
    }

    // when git-it verifies user added RR as collab
    if (req.url.match('/collab')) {
      var queryURL = url.parse(req.url, true)
      var username = queryURL.query.username
      return collabStatus(username, function(err, collab) {
        checkCollab(res, err, collab)
      })
    }
  
  // when anything else goes to reporobot.jlord.us
  res.statusCode = 404
  res.setHeader('content-type', 'application/json')
  res.end(JSON.stringify({
    error: 404,
    message: 'not_found'
  }, true, 2))
}

  function handleHook(req, res) {
    req.pipe(concat(function(buff) {
      var hookObj = JSON.parse(buff)
      if (onHook) onHook(hookObj, req)
    }))
    res.statusCode = 200
    res.end("Thank you.")
  }
  
  function getPR(req, res) {
    req.pipe(concat(function(buff) {
      console.log(["buffstring", buff.toString()])
      var pullreq = JSON.parse(buff)
      mergePr(pullreq, req)
      res.statusCode = 200
      res.setHeader('content-type', 'application/json')
      res.end()
    }))
  }

  function checkPR(res, err, pr) {
    if (err) {
      console.log(err)
      res.statusCode = 500
      res.end(JSON.stringify({error: err}))
      return
    }
    res.statusCode = 200
    // res.setHeader('content-type', 'application/json')
    res.end(JSON.stringify({
      pr: pr
    }, true, 2))
  }

  function checkCollab(res, err, collab) {
    if (err) {
      console.log(err)
      res.statusCode = 500
      res.end(JSON.stringify({error: err}))
      return
    }
    res.statusCode = 200
    res.setHeader('content-type', 'application/json')
    res.end(JSON.stringify({
      collab: collab
    }, true, 2))
  }
  
  function mergedPr(res, err) {
{'fraction_non_alphanumeric': 0.14546853954204347, 'fraction_numerical': 0.01039060996728882, 'mean_word_length': 2.550546448087432, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '543643', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1734, 'n_words': 398}