github

package module
v0.0.0-...-3a13de5 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2017 License: MIT Imports: 11 Imported by: 0

README

ghwh

Simple abstraction of github webhook functionality. Currently, probably not useful for much aside from the projects I'm using it in.

import (
    "fmt"
    "net/http"

    "github.com/sirupsen/logrus"
    "github.com/jmervine/ghwh"
)

func main() {
    logger := logrus.New().WithFields("package", "main")

    // Assumes configuration is set via the Environment
    github.Init(logger)

    // Fetch a file
    data, err := github.Fetch("README.md")
    if err != nil {
        logger.Fatal(err)
    }

    fmt.Println(string(data))

    // Listen for a webook
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        webhook := &github.WebhookPayload{}

        defer r.Body.Close()
        err := webhook.Decode(r.Body)

        if err == io.EOF {
            logger.Error(errors.New("Github webhook payload empty"))
            return
        }

        if err != nil {
            logger.Error(err)
            return
        }

        modified, err = webhook.Validate("modified-file.txt")
        if err != nil {
            if err == github.InvalidBranchError() {
                w.WriteHeader(404)
            } else {
                w.WriteHeader(500)
            }
            fmt.Fprintf(w, err.Error())
        }

        fmt.Fprintf(w, "webhook recieved")
    })
}

Documentation

Index

Constants

View Source
const AcceptHeader = "application/vnd.github.v3.raw"

Variables

View Source
var (
	Config github
)

Functions

func Fetch

func Fetch(file string) (body []byte, err error)

Fetch fetches the file from the github branch and repo as per the confiugration.

func Init

func Init(log *logrus.Entry)

func InvalidBranchError

func InvalidBranchError() error

Types

type WebhookPayload

type WebhookPayload struct {
	Ref        string `json:"ref"`
	HeadCommit struct {
		Modified []string `json:"modified"`
	} `json:"head_commit"`
	Repository struct {
		FullName string `json:"full_name"`
	} `json:"repository"`
}

WebhookPayload describes the incoming json payload from the Github webhook.

func (*WebhookPayload) Decode

func (w *WebhookPayload) Decode(reader io.Reader) error

Decode decodes the incoming payload in to a WebhookPayload.

func (*WebhookPayload) Validate

func (w *WebhookPayload) Validate(usersFile string) (bool, error)

Validate ensure that the payload's repo, branch and file all match what's configured to be valid.

Jump to

Keyboard shortcuts

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