github :: HookListener :: defaultinit
# A nitcorn listener for Github hooks.
abstract class HookListener
# Api client used to perform Github API requests.
var api: GithubAPI
# Host to listen.
var host: String
# Port to listen.
var port: Int
# VirtualHost listened
private var vh: VirtualHost is noinit
init do
vh = new VirtualHost("{host}:{port}")
vh.routes.add new Route("/", new HookAction(self))
end
# Verbosity level (0: quiet, 1: debug).
# Default: 0
var verbosity = 0
# Print `message` if `lvl` <= `verbosity`
fun message(lvl: Int, message: String) do
if lvl <= verbosity then print message
end
# Start listening and responding to event.
fun listen do
message(1, "Hook listening on {host}:{port}")
var factory = new HttpFactory.and_libevent
factory.config.virtual_hosts.add vh
factory.run
end
# How to build events from received json objects.
fun event_factory(kind: String, json: String): nullable GithubEvent do
if kind == "commit_comment" then
return api.deserialize(json).as(CommitCommentEvent)
else if kind == "create" then
return api.deserialize(json).as(CreateEvent)
else if kind == "delete" then
return api.deserialize(json).as(DeleteEvent)
else if kind == "deployment" then
return api.deserialize(json).as(DeploymentEvent)
else if kind == "deployment_status" then
return api.deserialize(json).as(DeploymentStatusEvent)
else if kind == "fork" then
return api.deserialize(json).as(ForkEvent)
else if kind == "issues" then
return api.deserialize(json).as(IssuesEvent)
else if kind == "issue_comment" then
return api.deserialize(json).as(IssueCommentEvent)
else if kind == "member" then
return api.deserialize(json).as(MemberEvent)
else if kind == "pull_request" then
return api.deserialize(json).as(PullRequestEvent)
else if kind == "pull_request_review_comment" then
return api.deserialize(json).as(PullRequestPullCommentEvent)
else if kind == "push" then
return api.deserialize(json).as(PushEvent)
else if kind == "status" then
return api.deserialize(json).as(StatusEvent)
end
return null
end
# What to do when we receive an event from a hook?
fun apply_event(event: GithubEvent) is abstract
end
lib/github/hooks.nit:53,1--124,3