Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GitHubWebHooksDirectives.scala « server « savage « getbootstrap « com « scala « main « src - github.com/twbs/savage.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0fc01a94d3ccff90f94559a1863c283c85c3aa00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.getbootstrap.savage.server

import scala.util.{Success, Failure, Try}
import akka.event.LoggingAdapter
import spray.json._
import spray.routing.{Directive1, ValidationRejection}
import spray.routing.directives.{BasicDirectives, RouteDirectives, HeaderDirectives}
import org.eclipse.egit.github.core.event.PullRequestPayload
import org.eclipse.egit.github.core.client.GsonUtils
import com.getbootstrap.savage.github.{GitHubJsonProtocol,IssueOrCommentEvent}
import com.getbootstrap.savage.github.event.Event

trait GitHubWebHooksDirectives {
  import RouteDirectives.reject
  import BasicDirectives.provide
  import HeaderDirectives.headerValueByName
  import HubSignatureDirectives.stringEntityMatchingHubSignature
  import GitHubJsonProtocol._

  def authenticatedPullRequestEvent(secretKey: Array[Byte]): Directive1[PullRequestPayload] = stringEntityMatchingHubSignature(secretKey).flatMap{ entityJsonString =>
    Try { GsonUtils.fromJson(entityJsonString, classOf[PullRequestPayload]) } match {
      case Failure(exc) => reject(ValidationRejection("JSON was either malformed or did not match expected schema!"))
      case Success(payload) => provide(payload)
    }
  }

  def authenticatedIssueOrCommentEvent(secretKey: Array[Byte]): Directive1[IssueOrCommentEvent] = stringEntityMatchingHubSignature(secretKey).flatMap{ entityJsonString =>
    Try{ entityJsonString.parseJson.convertTo[IssueOrCommentEvent] } match {
      case Failure(err) => reject(ValidationRejection("JSON either malformed or does not match expected schema!"))
      case Success(event) => provide(event)
    }
  }

  def gitHubEvent(log: LoggingAdapter): Directive1[Event] = headerValueByName("X-Github-Event").flatMap{ eventName => {
    Event(eventName) match {
      case None => {
        log.error(s"Received unknown GitHub event: ${eventName}")
        reject(ValidationRejection(s"Unrecognized GitHub event: ${eventName}"))
      }
      case Some(event) => provide(event)
    }
  }}
}

object GitHubWebHooksDirectives extends GitHubWebHooksDirectives