ghhook is a Go library that makes working with Github webhooks. This library assumes the wehooks are delivered via AWS APIGateway.
At its core you register an event name with a function that's run whenever webhook with that event name is received.
// Listen to "pull_request" event and return with 200 status.
ghhook.EventHandler(ghhook.PullRequestEvent, func(e interface{}) (*ghhook.Response, error) {
pr, _ := e.(*github.PullRequestEvent)
return &ghhook.Response{
Body: fmt.Sprintf("%s", *pr.Action),
StatusCode: 200,
}, nil
})
The input function has to be interface func(e interface{}) (*ghhook.Response, error)
. e
maps to event struct that's specific to the webhook. ghhook uses the webhook event definitions from google/go-github. See here for list of all the available events and their respective mapping.
DefaultHandler is the start point which receives the webhook and runs the registered functions for the webhook. It's possible to use it as a lambda function out of the box.
lambda.Start(ghhook.DefaultHandler)
package main
import (
"fmt"
"github.com/WalkerAndCoBrandsInc/ghhook"
"github.com/aws/aws-lambda-go/lambda"
"github.com/google/go-github/github"
)
func main() {
ghhook.EventHandler(ghhook.PullRequestEvent, func(e interface{}) (*ghhook.Response, error) {
pr, _ := e.(*github.PullRequestEvent)
return &ghhook.Response{
Body: fmt.Sprintf("%s", *pr.Action),
StatusCode: 200,
}, nil
})
// important, this should be the last line since it blocks
lambda.Start(ghhook.DefaultHandler)
}