<commit_before>package com.getbootstrap.lmvtfy.server

import java.nio.charset.Charset
import akka.actor.Actor
import spray.routing._
import spray.http._
import spray.json._
import com.getbootstrap.lmvtfy.github._

class LmvtfyActor extends Actor with Lmvtfy {
  def actorRefFactory = context

  // TODO: timeout handling
  def receive = runRoute(theOnlyRoute)
}


trait Lmvtfy extends HttpService {
  import GitHubJsonProtocol._

  val theOnlyRoute =
    path("lmvtfy") {
      post {
        headerValueByName("X-Github-Event") { githubEvent =>
          headerValueByName("X-Hub-Signature") { hmacHex =>
            val hmacBytes = javax.xml.bind.DatatypeConverter.parseHexBinary(hmacHex)
            entity(as[Array[Byte]]) { rawBody =>
              val FOOBARDDDDD = Array(1,2,3,4,5).map{_.toByte} // FIXME
              val hmac = new HmacSha1(mac = hmacBytes, secretKey = FOOBARDDDDD, data = rawBody)
              if (false && !hmac.isValid) {// FIXME
                complete(StatusCodes.Forbidden, "HMAC verification failed!")
              }
              else {
                formField("payload") { payload =>
                  System.out.println("RAW JSON:", payload)
                  githubEvent match {
                    case "issues" | "issue_comment" => {
                      val event = payload.parseJson.convertTo[IssueOrCommentEvent]
                      event.action match {
                        case "opened" | "created" => {
                          val comment = event.comment.getOrElse(event.issue)
                          val markdown = comment.body
                          val username = comment.user.login
                          System.out.println("EVENT: ", event.action)
                          System.out.println("USERNAME: ", username)
                          System.out.println("COMMENT BODY: ", markdown)
                          // FIXME: DO ACTUAL WORK
                          complete(StatusCodes.OK)
                        }
                        case _ => complete(StatusCodes.OK, "Ignoring irrelevant action")
                      }
                    }
                    case _ => complete(StatusCodes.BadRequest, "Unexpected event type")
                  }
                }
              }
            }
          }
        }
      }
    }
}
<commit_msg>add minimal GitHub WebHook ping handling<commit_after>package com.getbootstrap.lmvtfy.server

import java.nio.charset.Charset
import akka.actor.Actor
import spray.routing._
import spray.http._
import spray.json._
import com.getbootstrap.lmvtfy.github._

class LmvtfyActor extends Actor with Lmvtfy {
  def actorRefFactory = context

  // TODO: timeout handling
  def receive = runRoute(theOnlyRoute)
}


trait Lmvtfy extends HttpService {
  import GitHubJsonProtocol._

  val theOnlyRoute =
    path("lmvtfy") {
      post {
        headerValueByName("X-Github-Event") { githubEvent =>
          headerValueByName("X-Hub-Signature") { hmacHex =>
            val hmacBytes = javax.xml.bind.DatatypeConverter.parseHexBinary(hmacHex)
            entity(as[Array[Byte]]) { rawBody =>
              val FOOBARDDDDD = Array(1,2,3,4,5).map{_.toByte} // FIXME
              val hmac = new HmacSha1(mac = hmacBytes, secretKey = FOOBARDDDDD, data = rawBody)
              if (false && !hmac.isValid) {// FIXME
                complete(StatusCodes.Forbidden, "HMAC verification failed!")
              }
              else {
                formField("payload") { payload =>
                  System.out.println("RAW JSON:", payload)
                  githubEvent match {
                    case "ping" => {
                      System.out.println("Pong.")
                      complete(StatusCodes.OK)
                    }
                    case "issues" | "issue_comment" => {
                      val event = payload.parseJson.convertTo[IssueOrCommentEvent]
                      event.action match {
                        case "opened" | "created" => {
                          val comment = event.comment.getOrElse(event.issue)
                          val markdown = comment.body
                          val username = comment.user.login
                          System.out.println("EVENT: ", event.action)
                          System.out.println("USERNAME: ", username)
                          System.out.println("COMMENT BODY: ", markdown)
                          // FIXME: DO ACTUAL WORK
                          complete(StatusCodes.OK)
                        }
                        case _ => complete(StatusCodes.OK, "Ignoring irrelevant action")
                      }
                    }
                    case _ => complete(StatusCodes.BadRequest, "Unexpected event type")
                  }
                }
              }
            }
          }
        }
      }
    }
}

{'fraction_non_alphanumeric': 0.09449143800288838, 'fraction_numerical': 0.0024757582009490406, 'mean_word_length': 1.4497220818595251, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3834902', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1248, 'n_words': 247}