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

RorschachActor.scala « server « rorschach « getbootstrap « com « scala « main « src - github.com/twbs/rorschach.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 745e62195caf31c748528c8f1be27ad186823fa9 (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
46
47
48
49
package com.getbootstrap.rorschach.server

import akka.actor.ActorRef
import spray.routing._
import spray.http._


class RorschachActor(protected val pullRequestEventHandler: ActorRef) extends ActorWithLogging with HttpService {
  import GitHubPullRequestWebHooksDirectives.authenticatedPullRequestEvent

  private val settings = Settings(context.system)
  override def actorRefFactory = context
  override def receive = runRoute(theOnlyRoute)

  val theOnlyRoute =
    pathPrefix("rorschach") { pathEndOrSingleSlash {
      get {
        complete(StatusCodes.OK, "Hi! Rorschach is online.")
      } ~
      post {
        headerValueByName("X-Github-Event") { githubEvent =>
          githubEvent match {
            case "ping" => {
              log.info("Successfully received GitHub webhook ping.")
              complete(StatusCodes.OK)
            }
            case "pull_request" => {
              authenticatedPullRequestEvent(settings.WebHookSecretKey.toArray) { event =>
                event.getAction match {
                  case "opened" | "synchronize" => {
                    val pr = event.getPullRequest
                    if (pr.getState == "open") {
                      pullRequestEventHandler ! pr
                      complete(StatusCodes.OK)
                    }
                    else {
                      complete(StatusCodes.OK, s"Ignoring event about closed pull request #${pr.getId}")
                    }
                  }
                  case _ => complete(StatusCodes.OK, "Ignoring irrelevant action")
                }
              }
            }
            case _ => complete(StatusCodes.BadRequest, "Unexpected event type")
          }
        }
      }
    }}
}