webhook

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

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

Go to latest
Published: Feb 11, 2020 License: MIT Imports: 10 Imported by: 0

README

github-webhook-handler

github-webhook-handler is parse webhook from github and dispatch event to your event handler for GitHub Apps.

Install

go get -u github.com/wreulicke/github-webhook-handler

Usage

	var appID int64 = 0 // your app id
	bs, err := ioutil.ReadFile("your-key.pem")
	if err != nil {
		return nil
	}
	key, err := jwt.ParseRSAPrivateKeyFromPEM(bs)
	if err != nil {
		return nil
	}
	r := http.NewServeMux()
	webhookHandler := webhook.New()
	appsTransport := ghinstallation.NewAppsTransportFromPrivateKey(http.DefaultTransport, appID, key),
	webhookHandler.On("issues", func(i *webhook.Installation, e *github.IssuesEvent) error {
		itr := ghinstallation.NewFromAppsTransport(appsTransport, i.Id)
		client := github.NewClient(&http.Client{Transport: itr})
		commentBody := e.Issue.GetTitle() + ":" + e.Issue.GetState()
		comment := &github.IssueComment{Body: &commentBody}
		_, _, err := client.Issues.CreateComment(context.Background(), *e.Repo.Owner.Login, *e.Repo.Name, *e.Issue.Number, comment)
		return err
	})
	r.Handle("/", webhookHandler)
	server := http.Server{
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
		Handler:      r,
		Addr:         ":8080",
	}
	log.Printf("Server is started. Go to http://localhost:%d", 8080)
	if err := server.ListenAndServe(); err != nil {
		return err
	}

You can see fully example in here

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate

func Validate(handler interface{}) error

Types

type Handlers

type Handlers struct {
	AppId int64

	ParseWebhook func(eventType string, bs []byte) (interface{}, error)
	// contains filtered or unexported fields
}

func New

func New() *Handlers

func (*Handlers) Handle

func (h *Handlers) Handle(ctx context.Context, eventType string, bs []byte) error

func (*Handlers) On

func (h *Handlers) On(eventType string, handler interface{}) error

func (*Handlers) ParseInstallationWebHook

func (h *Handlers) ParseInstallationWebHook(eventType string, bytes []byte) (*Installation, interface{}, error)

func (*Handlers) ServeHTTP

func (h *Handlers) ServeHTTP(res http.ResponseWriter, req *http.Request)

type Installation

type Installation struct {
	Id int64 `json:"id"`
}

type InstallationPart

type InstallationPart struct {
	Installation *Installation `json:"installation"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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