Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: go-playground/webhooks
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.7.0
Choose a base ref
...
head repository: go-playground/webhooks
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3
Choose a head ref

Commits on Feb 22, 2018

  1. Copy the full SHA
    0c4911f View commit details

Commits on Feb 23, 2018

  1. Merge pull request #25 from lukepatrick/v3

    Update bitbucket uuid check to allow a non-specified uuid, similar to…
    Dean Karn authored Feb 23, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    c271ec3 View commit details
  2. Update README.md

    Dean Karn authored Feb 23, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    b6e930d View commit details

Commits on Mar 10, 2018

  1. Copy the full SHA
    ea44f69 View commit details

Commits on Mar 19, 2018

  1. add gogs support

    奶爸 committed Mar 19, 2018
    Copy the full SHA
    2aa5fdc View commit details
  2. edit import

    奶爸 committed Mar 19, 2018
    Copy the full SHA
    1c3914e View commit details
  3. ignore IDEA

    奶爸 committed Mar 19, 2018
    Copy the full SHA
    fc20b2a View commit details
  4. [fix] Gogs signature calc

    奶爸 committed Mar 19, 2018
    Copy the full SHA
    493e94d View commit details
  5. Merge pull request #26 from kuiro5/add-requested-reviewers

    Add RequestedReviewers to PullRequestPayload
    Dean Karn authored Mar 19, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1b6492c View commit details

Commits on Mar 20, 2018

  1. [fix]Gogs sign verify

    奶爸 committed Mar 20, 2018
    Copy the full SHA
    d4d9692 View commit details
  2. Copy the full SHA
    1253715 View commit details

Commits on Mar 21, 2018

  1. Copy the full SHA
    6d73143 View commit details
  2. Merge pull request #27 from lukepatrick/v3

    fix(bitbucket) typo in Event
    Dean Karn authored Mar 21, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    32331e6 View commit details
  3. Update README.md

    Dean Karn authored Mar 21, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    3ad83a8 View commit details
  4. Update README.md

    Dean Karn authored Mar 21, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f2acbcd View commit details

Commits on Mar 23, 2018

  1. Copy the full SHA
    a831111 View commit details

Commits on Apr 2, 2018

  1. Copy the full SHA
    9e4b7fa View commit details

Commits on Apr 8, 2018

  1. Merge pull request #28 from rtnpro/parse-github-pull-request-labels

    Parse Github pull request labels from pull request payload.
    Dean Karn authored Apr 8, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8db8abb View commit details
  2. Update README.md

    Dean Karn authored Apr 8, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    5580947 View commit details

Commits on Apr 11, 2018

  1. remove space

    奶爸 committed Apr 11, 2018
    Copy the full SHA
    b26a00f View commit details
  2. merge

    奶爸 committed Apr 11, 2018
    Copy the full SHA
    0ebe523 View commit details

Commits on May 7, 2018

  1. add gogs support

    奶爸 committed May 7, 2018
    Copy the full SHA
    22713e3 View commit details
  2. godmt

    奶爸 committed May 7, 2018
    Copy the full SHA
    c9c9d98 View commit details

Commits on May 8, 2018

  1. fix merge issue

    奶爸 committed May 8, 2018
    Copy the full SHA
    3e1bb69 View commit details
  2. Merge pull request #29 from naiba/v3

    Add gogs support
    Dean Karn authored May 8, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    16896cd View commit details
  3. update REAME + travis.yml

    Dean Karn committed May 8, 2018
    Copy the full SHA
    be944ed View commit details

Commits on Jun 28, 2018

  1. Copy the full SHA
    2c541a7 View commit details
  2. Merge pull request #33 from kishorenc/fix_deployment_payload

    Deployment Payload fields should be of type string.
    Dean Karn authored Jun 28, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    84dc839 View commit details
  3. Update README.md

    Dean Karn authored Jun 28, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8ffb2ff View commit details
Showing with 226 additions and 28 deletions.
  1. +1 −0 .gitignore
  2. +2 −3 .travis.yml
  3. +2 −2 README.md
  4. +9 −5 bitbucket/bitbucket.go
  5. +42 −18 github/payload.go
  6. +167 −0 gogs/gogs.go
  7. +3 −0 webhooks.go
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
# Folders
_obj
_test
.idea

# Architecture specific extensions/prefixes
*.[568vq]
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: go
go:
- 1.7.6
- 1.8.3
- 1.10.2
- tip
matrix:
allow_failures:
@@ -34,6 +33,6 @@ script:
- go test -race

after_success: |
[ $TRAVIS_GO_VERSION = 1.8.3 ] &&
[ $TRAVIS_GO_VERSION = 1.10.2 ] &&
overalls -project="github.com/go-playground/webhooks" -covermode=count -ignore=.git,examples -debug &&
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Library webhooks
================
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">![Project status](https://img.shields.io/badge/version-3.6.0-green.svg)
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">![Project status](https://img.shields.io/badge/version-3.13.0-green.svg)
[![Build Status](https://travis-ci.org/go-playground/webhooks.svg?branch=v3)](https://travis-ci.org/go-playground/webhooks)
[![Coverage Status](https://coveralls.io/repos/go-playground/webhooks/badge.svg?branch=v3&service=github)](https://coveralls.io/github/go-playground/webhooks?branch=v3)
[![Go Report Card](https://goreportcard.com/badge/go-playground/webhooks)](https://goreportcard.com/report/go-playground/webhooks)
@@ -24,7 +24,7 @@ Installation
Use go get.

```shell
go get -u gopkg.in/go-playground/webhooks.v3
go get -u gopkg.in/go-playground/webhooks.v3
```

Then import the package into your own code.
14 changes: 9 additions & 5 deletions bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ const (
PullRequestDeclinedEvent Event = "pullrequest:rejected"
PullRequestCommentCreatedEvent Event = "pullrequest:comment_created"
PullRequestCommentUpdatedEvent Event = "pullrequest:comment_updated"
PullRequestCommentDeletedEvent Event = "pull_request:comment_deleted"
PullRequestCommentDeletedEvent Event = "pullrequest:comment_deleted"
)

// New creates and returns a WebHook instance denoted by the Provider type
@@ -80,10 +80,14 @@ func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
}
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Hook-UUID:%s", uuid))

if uuid != hook.uuid {
webhooks.DefaultLog.Error(fmt.Sprintf("X-Hook-UUID does not match configured uuid of %s", hook.uuid))
http.Error(w, "403 Forbidden - X-Hook-UUID does not match", http.StatusForbidden)
return
if len(hook.uuid) > 0 {
if uuid != hook.uuid {
webhooks.DefaultLog.Error(fmt.Sprintf("X-Hook-UUID %s does not match configured uuid of %s", uuid, hook.uuid))
http.Error(w, "403 Forbidden - X-Hook-UUID does not match", http.StatusForbidden)
return
}
} else {
webhooks.DefaultLog.Debug("hook uuid not defined - recommend setting for improved security")
}

event := r.Header.Get("X-Event-Key")
60 changes: 42 additions & 18 deletions github/payload.go
Original file line number Diff line number Diff line change
@@ -381,8 +381,7 @@ type DeploymentPayload struct {
Sha string `json:"sha"`
Ref string `json:"ref"`
Task string `json:"task"`
Payload struct {
} `json:"payload"`
Payload string `json:"payload"`
Environment string `json:"environment"`
Description *string `json:"description"`
Creator struct {
@@ -555,8 +554,7 @@ type DeploymentStatusPayload struct {
Sha string `json:"sha"`
Ref string `json:"ref"`
Task string `json:"task"`
Payload struct {
} `json:"payload"`
Payload string `json:"payload"`
Environment string `json:"environment"`
Description *string `json:"description"`
Creator struct {
@@ -2759,20 +2757,46 @@ type PullRequestPayload struct {
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
} `json:"user"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt *time.Time `json:"closed_at"`
MergedAt *time.Time `json:"merged_at"`
MergeCommitSha *string `json:"merge_commit_sha"`
Assignee *Assignee `json:"assignee"`
Milestone *Milestone `json:"milestone"`
CommitsURL string `json:"commits_url"`
ReviewCommentsURL string `json:"review_comments_url"`
ReviewCommentURL string `json:"review_comment_url"`
CommentsURL string `json:"comments_url"`
StatusesURL string `json:"statuses_url"`
Head struct {
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt *time.Time `json:"closed_at"`
MergedAt *time.Time `json:"merged_at"`
MergeCommitSha *string `json:"merge_commit_sha"`
Assignee *Assignee `json:"assignee"`
Milestone *Milestone `json:"milestone"`
CommitsURL string `json:"commits_url"`
ReviewCommentsURL string `json:"review_comments_url"`
ReviewCommentURL string `json:"review_comment_url"`
CommentsURL string `json:"comments_url"`
StatusesURL string `json:"statuses_url"`
RequestedReviewers []struct {
Login string `json:"login"`
ID int `json:"id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
} `json:"requested_reviewers,omitempty"`
Labels []struct {
ID int64 `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Color string `json:"color"`
Default bool `json:"default"`
} `json:"labels"`
Head struct {
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
167 changes: 167 additions & 0 deletions gogs/gogs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package gogs

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"crypto/hmac"
"crypto/sha256"
"encoding/hex"
client "github.com/gogits/go-gogs-client"
"gopkg.in/go-playground/webhooks.v3"
)

// Webhook instance contains all methods needed to process events
type Webhook struct {
provider webhooks.Provider
secret string
eventFuncs map[Event]webhooks.ProcessPayloadFunc
}

// Config defines the configuration to create a new Gogs Webhook instance
type Config struct {
Secret string
}

// Event defines a Gogs hook event type
type Event string

// Gogs hook types
const (
CreateEvent Event = "create"
DeleteEvent Event = "delete"
ForkEvent Event = "fork"
PushEvent Event = "push"
IssuesEvent Event = "issues"
IssueCommentEvent Event = "issue_comment"
PullRequestEvent Event = "pull_request"
ReleaseEvent Event = "release"
)

// New creates and returns a WebHook instance denoted by the Provider type
func New(config *Config) *Webhook {
return &Webhook{
provider: webhooks.Gogs,
secret: config.Secret,
eventFuncs: map[Event]webhooks.ProcessPayloadFunc{},
}
}

// Provider returns the current hooks provider ID
func (hook Webhook) Provider() webhooks.Provider {
return hook.provider
}

// RegisterEvents registers the function to call when the specified event(s) are encountered
func (hook Webhook) RegisterEvents(fn webhooks.ProcessPayloadFunc, events ...Event) {

for _, event := range events {
hook.eventFuncs[event] = fn
}
}

// ParsePayload parses and verifies the payload and fires off the mapped function, if it exists.
func (hook Webhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
webhooks.DefaultLog.Info("Parsing Payload...")

event := r.Header.Get("X-Gogs-Event")
if len(event) == 0 {
webhooks.DefaultLog.Error("Missing X-Gogs-Event Header")
http.Error(w, "400 Bad Request - Missing X-Gogs-Event Header", http.StatusBadRequest)
return
}
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Gogs-Event:%s", event))

gogsEvent := Event(event)

fn, ok := hook.eventFuncs[gogsEvent]
// if no event registered
if !ok {
webhooks.DefaultLog.Info(fmt.Sprintf("Webhook Event %s not registered, it is recommended to setup only events in gogs that will be registered in the webhook to avoid unnecessary traffic and reduce potential attack vectors.", event))
return
}

payload, err := ioutil.ReadAll(r.Body)
if err != nil || len(payload) == 0 {
webhooks.DefaultLog.Error("Issue reading Payload")
http.Error(w, "Issue reading Payload", http.StatusInternalServerError)
return
}
webhooks.DefaultLog.Debug(fmt.Sprintf("Payload:%s", string(payload)))

// If we have a Secret set, we should check the MAC
if len(hook.secret) > 0 {
webhooks.DefaultLog.Info("Checking secret")
signature := r.Header.Get("X-Gogs-Signature")
if len(signature) == 0 {
webhooks.DefaultLog.Error("Missing X-Gogs-Signature required for HMAC verification")
http.Error(w, "403 Forbidden - Missing X-Gogs-Signature required for HMAC verification", http.StatusForbidden)
return
}
webhooks.DefaultLog.Debug(fmt.Sprintf("X-Gogs-Signature:%s", signature))

mac := hmac.New(sha256.New, []byte(hook.secret))
mac.Write(payload)

expectedMAC := hex.EncodeToString(mac.Sum(nil))

if !hmac.Equal([]byte(signature), []byte(expectedMAC)) {
webhooks.DefaultLog.Debug(string(payload))
http.Error(w, "403 Forbidden - HMAC verification failed", http.StatusForbidden)
return
}
}

// Make headers available to ProcessPayloadFunc as a webhooks type
hd := webhooks.Header(r.Header)

switch gogsEvent {
case CreateEvent:
var pe client.CreatePayload
json.Unmarshal([]byte(payload), &pe)
hook.runProcessPayloadFunc(fn, pe, hd)

case ReleaseEvent:
var re client.ReleasePayload
json.Unmarshal([]byte(payload), &re)
hook.runProcessPayloadFunc(fn, re, hd)

case PushEvent:
var pe client.PushPayload
json.Unmarshal([]byte(payload), &pe)
hook.runProcessPayloadFunc(fn, pe, hd)

case DeleteEvent:
var de client.DeletePayload
json.Unmarshal([]byte(payload), &de)
hook.runProcessPayloadFunc(fn, de, hd)

case ForkEvent:
var fe client.ForkPayload
json.Unmarshal([]byte(payload), &fe)
hook.runProcessPayloadFunc(fn, fe, hd)

case IssuesEvent:
var ie client.IssuesPayload
json.Unmarshal([]byte(payload), &ie)
hook.runProcessPayloadFunc(fn, ie, hd)

case IssueCommentEvent:
var ice client.IssueCommentPayload
json.Unmarshal([]byte(payload), &ice)
hook.runProcessPayloadFunc(fn, ice, hd)

case PullRequestEvent:
var pre client.PullRequestPayload
json.Unmarshal([]byte(payload), &pre)
hook.runProcessPayloadFunc(fn, pre, hd)
}
}

func (hook Webhook) runProcessPayloadFunc(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
go func(fn webhooks.ProcessPayloadFunc, results interface{}, header webhooks.Header) {
fn(results, header)
}(fn, results, header)
}
3 changes: 3 additions & 0 deletions webhooks.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ func (p Provider) String() string {
return "Bitbucket"
case GitLab:
return "GitLab"
case Gogs:
return "Gogs"
default:
return "Unknown"
}
@@ -29,6 +31,7 @@ const (
GitHub Provider = iota
Bitbucket
GitLab
Gogs
)

// Webhook interface defines a webhook to receive events