diff options
author | Chris Rebert <code@rebertia.com> | 2015-03-24 08:44:29 +0300 |
---|---|---|
committer | Chris Rebert <code@rebertia.com> | 2015-03-24 08:44:29 +0300 |
commit | da96b788483ea34b6035190c8358b4ed5bec1fb9 (patch) | |
tree | d9853595169e6715716be286508bfaab72f25aaf | |
parent | ec4f128467c3a070de73c42701e07688a3134513 (diff) |
add github.event.Event & refactor
3 files changed, 137 insertions, 8 deletions
diff --git a/src/main/scala/com/getbootstrap/savage/github/event/Event.scala b/src/main/scala/com/getbootstrap/savage/github/event/Event.scala new file mode 100644 index 0000000..53a51a0 --- /dev/null +++ b/src/main/scala/com/getbootstrap/savage/github/event/Event.scala @@ -0,0 +1,116 @@ +package com.getbootstrap.savage.github.event + +object Event { + def apply(name: String): Option[Event] = { + name match { + case CommitComment.Name => Some(CommitComment) + case Create.Name => Some(Create) + case Delete.Name => Some(Delete) + case Deployment.Name => Some(Deployment) + case DeploymentStatus.Name => Some(DeploymentStatus) + case Download.Name => Some(Download) + case Follow.Name => Some(Follow) + case Fork.Name => Some(Fork) + case ForkApply.Name => Some(ForkApply) + case Gist.Name => Some(Gist) + case Gollum.Name => Some(Gollum) + case IssueComment.Name => Some(IssueComment) + case Issues.Name => Some(Issues) + case Member.Name => Some(Member) + case Membership.Name => Some(Membership) + case PageBuild.Name => Some(PageBuild) + case Ping.Name => Some(Ping) + case Public.Name => Some(Public) + case PullRequest.Name => Some(PullRequest) + case PullRequestReviewComment.Name => Some(PullRequestReviewComment) + case Push.Name => Some(Push) + case Release.Name => Some(Release) + case Repository.Name => Some(Repository) + case Status.Name => Some(Status) + case TeamAdd.Name => Some(TeamAdd) + case Watch.Name => Some(Watch) + case _ => None + } + } +} +sealed trait Event { + def Name: String +} +object CommitComment extends Event { + override val Name = "commit_comment" +} +object Create extends Event { + override val Name = "create" +} +object Delete extends Event { + override val Name = "delete" +} +object Deployment extends Event { + override val Name = "deployment" +} +object DeploymentStatus extends Event { + override val Name = "deployment_status" +} +object Download extends Event { + override val Name = "download" +} +object Follow extends Event { + override val Name = "follow" +} +object Fork extends Event { + override val Name = "fork" +} +object ForkApply extends Event { + override val Name = "fork_apply" +} +object Gist extends Event { + override val Name = "gist" +} +object Gollum extends Event { + override val Name = "gollum" +} +object IssueComment extends Event { + override val Name = "issue_comment" +} +object Issues extends Event { + override val Name = "issues" +} +object Member extends Event { + override val Name = "member" +} +object Membership extends Event { + override val Name = "membership" +} +object PageBuild extends Event { + override val Name = "page_build" +} +object Ping extends Event { + override val Name = "ping" +} +object Public extends Event { + override val Name = "public" +} +object PullRequest extends Event { + override val Name = "pull_request" +} +object PullRequestReviewComment extends Event { + override val Name = "pull_request_review_comment" +} +object Push extends Event { + override val Name = "push" +} +object Release extends Event { + override val Name = "release" +} +object Repository extends Event { + override val Name = "repository" +} +object Status extends Event { + override val Name = "status" +} +object TeamAdd extends Event { + override val Name = "team_add" +} +object Watch extends Event { + override val Name = "watch" +}
\ No newline at end of file diff --git a/src/main/scala/com/getbootstrap/savage/server/GitHubWebHooksDirectives.scala b/src/main/scala/com/getbootstrap/savage/server/GitHubWebHooksDirectives.scala index 9ef26ee..0fc01a9 100644 --- a/src/main/scala/com/getbootstrap/savage/server/GitHubWebHooksDirectives.scala +++ b/src/main/scala/com/getbootstrap/savage/server/GitHubWebHooksDirectives.scala @@ -1,16 +1,19 @@ 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} +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._ @@ -27,6 +30,16 @@ trait GitHubWebHooksDirectives { 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 diff --git a/src/main/scala/com/getbootstrap/savage/server/SavageWebService.scala b/src/main/scala/com/getbootstrap/savage/server/SavageWebService.scala index 511dc3d..091f295 100644 --- a/src/main/scala/com/getbootstrap/savage/server/SavageWebService.scala +++ b/src/main/scala/com/getbootstrap/savage/server/SavageWebService.scala @@ -5,7 +5,7 @@ import akka.actor.ActorRef import spray.routing._ import spray.http._ import com.getbootstrap.savage.PullRequestBuildResult -import com.getbootstrap.savage.github.{BranchDeletionRequest, PullRequestNumber, commit_status, pr_action} +import com.getbootstrap.savage.github.{BranchDeletionRequest, PullRequestNumber, commit_status, pr_action, event=>events} import com.getbootstrap.savage.github.util._ class SavageWebService( @@ -14,7 +14,7 @@ class SavageWebService( protected val branchDeleter: ActorRef, protected val statusSetter: ActorRef ) extends ActorWithLogging with HttpService { - import GitHubWebHooksDirectives.{authenticatedPullRequestEvent,authenticatedIssueOrCommentEvent} + import GitHubWebHooksDirectives.{gitHubEvent,authenticatedPullRequestEvent,authenticatedIssueOrCommentEvent} import TravisWebHookDirectives.authenticatedTravisEvent private val settings = Settings(context.system) @@ -31,19 +31,19 @@ class SavageWebService( path("github") { pathEndOrSingleSlash { post { - headerValueByName("X-Github-Event") { githubEvent => - githubEvent match { - case "ping" => { + gitHubEvent(log) { ghEvent => + ghEvent match { + case events.Ping => { log.info("Successfully received GitHub webhook ping.") complete(StatusCodes.OK) } - case "issue_comment" => { + case events.IssueComment => { authenticatedIssueOrCommentEvent(settings.GitHubWebHookSecretKey.toArray) { event => { pullRequestEventHandler ! event complete(StatusCodes.OK) }} } - case "pull_request" => { + case events.PullRequest => { authenticatedPullRequestEvent(settings.GitHubWebHookSecretKey.toArray) { event => event.action match { case pr_action.Opened | pr_action.Synchronize => { |