ghooks

package module
v0.0.0-...-b369388 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2018 License: MIT Imports: 9 Imported by: 0

README

ghooks -- golang github hooks handler

Notice

This is a heavily refactored version of https://github.com/Konboi/ghooks. It includes several code refactors and simplifications.

About ghooks

ghooks is github hooks receiver. inspired by GitHub::Hooks::Receiver, octoks

Install

go get github.com/erikh/ghooks

Usage

// sample.go
package main

import (
    "fmt"
    "log"

    "github.com/erikh/ghooks"
)


func main() {
    port := 8080
    hooks := ghooks.NewServer(port)

    hooks.On("push", pushHandler)
    hooks.On("pull_request", pullRequestHandler)
    hooks.Run()
}

func pushHandler(payload interface{}) {
    fmt.Println("puuuuush")
}

func pullRequestHandler(payload interface{}) {
    fmt.Println("pull_request")
}
go run sample.go
curl -H "X-GitHub-Event: push" -d '{"hoge":"fuga"}' http://localhost:8080
> puuuuush

Documentation

Overview

Package ghooks is a github hooks receiver in golang with net/http.

Inspired by GitHub::Hooks::Receiver (https://github.com/Songmu/Github-Hooks-Receiver), and octoks (https://github.com/hisaichi5518/octoks)

Install

go get github.com/erikh/ghooks

Usage

// sample.go
package main

import (
		"fmt"
		"log"

		"github.com/erikh/ghooks"
)

func main() {
		port := 8080
		hooks := ghooks.NewServer(port)

		hooks.On("push", pushHandler)
		hooks.On("pull_request", pullRequestHandler)
		hooks.Run()
}

func pushHandler(payload interface{}) {
		fmt.Println("puuuuush")
}

func pullRequestHandler(payload interface{}) {
		fmt.Println("pull_request")
}

After starting this server:

curl -H "X-GitHub-Event: push" -d '{"hoge":"fuga"}' http://localhost:8080
> puuuuush

Index

Constants

View Source
const (
	// Version is the version of the library
	Version = 0.2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

type Hook struct {
	Event string
	Func  func(payload interface{})
}

Hook is the type of hook event being processed.

type Hooks

type Hooks []Hook

Hooks is a list of Hook.

type Server

type Server struct {
	Secret string
}

Server is the hander pipeline for serving github hooks. One can instantiate a server

func NewServer

func NewServer() *Server

NewServer creates a new *Server.

func (*Server) Handler

func (s *Server) Handler(w http.ResponseWriter, req *http.Request)

Handler is the primary handler returned by the server. You can leverage it with the http library like so:

s := NewServer()
http.HandleFunc("/", s.Handler)
http.ListenAndServe(":2222", http.DefaultServeMux)

func (*Server) On

func (s *Server) On(name string, handler func(payload interface{}))

On is the primary registration mechanism for handlers. Handlers must accept interface{} and process the resulting data (typically some form of map[string]interface{}) in its handler after receiving the hook.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL