webhooks

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2016 License: MIT Imports: 1 Imported by: 0

README

Library webhooks

![Project status](https://img.shields.io/badge/version-2.0.0-green.svg) [![Build Status](https://semaphoreci.com/api/v1/projects/5b9e2eda-8f8d-40aa-8cb4-e3f6120171fe/587820/badge.svg)](https://semaphoreci.com/joeybloggs/webhooks) [![Coverage Status](https://coveralls.io/repos/go-playground/webhooks/badge.svg?branch=v2&service=github)](https://coveralls.io/github/go-playground/webhooks?branch=v2) [![Go Report Card](https://goreportcard.com/badge/go-playground/webhooks)](https://goreportcard.com/report/go-playground/webhooks) [![GoDoc](https://godoc.org/gopkg.in/go-playground/webhooks.v2?status.svg)](https://godoc.org/gopkg.in/go-playground/webhooks.v2) ![License](https://img.shields.io/dub/l/vibe-d.svg)

Library webhooks allows for easy recieving and parsing of GitHub & Bitbucket Webhook Events

Features:

  • Parses the entire payload, not just a few fields.
  • Fields + Schema directly lines up with webhook posted json

Notes:

  • Github - Currently only accepting json payloads.

Installation

Use go get.

	go get -u gopkg.in/go-playground/webhooks.v2

Then import the validator package into your own code.

import "gopkg.in/go-playground/webhooks.v2"

Usage and documentation

Please see http://godoc.org/gopkg.in/go-playground/webhooks.v2 for detailed usage docs.

Examples:

Multiple Handlers for each event you subscribe to

package main

import (
	"fmt"
	"strconv"

	"gopkg.in/go-playground/webhooks.v2"
	"gopkg.in/go-playground/webhooks.v2/github"
)

const (
	path = "/webhooks"
	port = 3016
)

func main() {

	hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
	hook.RegisterEvents(HandleRelease, github.ReleaseEvent)
	hook.RegisterEvents(HandlePullRequest, github.PullRequestEvent)

	err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
	if err != nil {
		fmt.Println(err)
	}
}

// HandleRelease handles GitHub release events
func HandleRelease(payload interface{}, header webhooks.Header) {

	fmt.Println("Handling Release")

	pl := payload.(github.ReleasePayload)

	// only want to compile on full releases
	if pl.Release.Draft || pl.Release.Prelelease || pl.Release.TargetCommitish != "master" {
		return
	}

	// Do whatever you want from here...
	fmt.Printf("%+v", pl)
}

// HandlePullRequest handles GitHub pull_request events
func HandlePullRequest(payload interface{}, header webhooks.Header) {

	fmt.Println("Handling Pull Request")

	pl := payload.(github.PullRequestPayload)

	// Do whatever you want from here...
	fmt.Printf("%+v", pl)
}

Single receiver for events you subscribe to

package main

import (
	"fmt"
	"strconv"

	"gopkg.in/go-playground/webhooks.v2"
	"gopkg.in/go-playground/webhooks.v2/github"
)

const (
	path = "/webhooks"
	port = 3016
)

func main() {

	hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"})
	hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want

	err := webhooks.Run(hook, ":"+strconv.Itoa(port), path)
	if err != nil {
		fmt.Println(err)
	}
}

// HandleMultiple handles multiple GitHub events
func HandleMultiple(payload interface{}, header webhooks.Header) {

	fmt.Println("Handling Payload..")

	switch payload.(type) {

	case github.ReleasePayload:
		release := payload.(github.ReleasePayload)
		// Do whatever you want from here...
		fmt.Printf("%+v", release)

	case github.PullRequestPayload:
		pullRequest := payload.(github.PullRequestPayload)
		// Do whatever you want from here...
		fmt.Printf("%+v", pullRequest)
	}
}

Contributing

Pull requests for other service like BitBucket are welcome!

There will always be a development branch for each version i.e. v1-development. In order to contribute, please make your pull requests against those branches.

If the changes being proposed or requested are breaking changes, please create an issue, for discussion or create a pull request against the highest development branch for example this package has a v1 and v1-development branch however, there will also be a v2-development branch even though v2 doesn't exist yet.

License

Distributed under MIT License, please see license file in code for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(hook Webhook, addr string, path string) error

Run runs a server

func RunServer

func RunServer(s *http.Server, hook Webhook, path string) error

RunServer runs a custom server.

func RunTLSServer

func RunTLSServer(s *http.Server, hook Webhook, path string) error

RunTLSServer runs a custom server with TLS configuration. NOTE: http.Server Handler will be overridden by this library, just set it to nil. Setting the Certificates can be done in the http.Server.TLSConfig.Certificates see example here: https://gopkg.in/go-playground/webhooks.v1/blob/master/webhooks_test.go#L178

Types

type Header http.Header

Webhook provides http.Header to minimize imports

type ProcessPayloadFunc

type ProcessPayloadFunc func(payload interface{}, header Header)

ProcessPayloadFunc is a common function for payload return values

type Provider

type Provider int

Provider defines the type of webhook

const (
	GitHub Provider = iota
	Bitbucket
)

webhooks available providers

func (Provider) String

func (p Provider) String() string

type Webhook

type Webhook interface {
	Provider() Provider
	ParsePayload(w http.ResponseWriter, r *http.Request)
}

Webhook interface defines a webhook to recieve events

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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