GithubHelp home page GithubHelp logo

go-playground / webhooks Goto Github PK

View Code? Open in Web Editor NEW
930.0 21.0 232.0 552 KB

:fishing_pole_and_fish: Webhook receiver for GitHub, Bitbucket, GitLab, Gogs

License: MIT License

Go 99.97% Makefile 0.03%
webhooks bitbucket github gitlab

webhooks's Introduction

Library webhooks

Project status Test Coverage Status Go Report Card GoDoc License

Library webhooks allows for easy receiving and parsing of GitHub, Bitbucket, GitLab, Docker Hub, Gogs and Azure DevOps Webhook Events

Features:

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

Notes:

  • Currently only accepting json payloads.

Installation

Use go get.

go get -u github.com/go-playground/webhooks/v6

Then import the package into your own code.

import "github.com/go-playground/webhooks/v6"

Usage and Documentation

Please see http://godoc.org/github.com/go-playground/webhooks/v6 for detailed usage docs.

Examples:
package main

import (
	"fmt"

	"net/http"

	"github.com/go-playground/webhooks/v6/github"
)

const (
	path = "/webhooks"
)

func main() {
	hook, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecret...?"))

	http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
		payload, err := hook.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
		if err != nil {
			if err == github.ErrEventNotFound {
				// ok event wasn't one of the ones asked to be parsed
			}
		}
		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)
		}
	})
	http.ListenAndServe(":3000", nil)
}

Contributing

Pull requests for other services are welcome!

If the changes being proposed or requested are breaking changes, please create an issue for discussion.

License

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

webhooks's People

Contributors

alexmukho avatar alrs avatar ammario avatar aritas1 avatar artsmvch avatar binkkatal avatar bnfinet avatar deankarn avatar drmagpie avatar emaincourt avatar exileed avatar fish-nullify avatar gergu avatar gsaraf avatar hatstand avatar huhudev-git avatar iladin avatar lukepatrick avatar maguro avatar neiser avatar pieterlexis avatar robinlieb avatar sumitpantheon avatar toshik1978 avatar tulir avatar veziak avatar wbagdon avatar xnok avatar yinxulai avatar zrochler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webhooks's Issues

[Feature Request] Add gitea support and v6 version

Hello @deankarn I'm interested in use this wonderful lib to some of our projects. But we are currently using gitea and I would like to see merged this PR #81 Any plans to merge?
About version , I've seen latest v6.0.0-beta.2 from january. Any plans to release v6.0.0-final version ?

[gitlab] Webhook Event Note Hook not registered

Hi

I'm currently using the following dependency:

PROJECT                               CONSTRAINT     VERSION        REVISION  LATEST   PKGS USED
gopkg.in/go-playground/webhooks.v3    v3.11.0        v3.11.0        5580947   v3.11.0  2  

I've noticed some of the events formerly handled by functions registered as: IssuesEvents no longer seem to be properly handled. More specifically: When someone reacts to an issue, it no longer seems to create the same hook from gitlab -> It's become some sort of (NotesEvents?).

I opened this repository at tag: v3.13.0 and started searching for this event type in https://github.com/go-playground/webhooks/blob/v3.13.0/gitlab/gitlab.go#L28-L38 but couldn't find it.

Is it possible I'm missing something?

Seems like gitlab now has this type of hook:
notesevent

When I created this application it seems like the notesEvents didn't exist and it was shipped along with the IssuesEvents?

Gitlab now sends X-Gitlab-Event: System Hook for all events

It seems gitlab 12.7.5 sends the header X-Gitlab-Event: System Hook for push events in stead of "Push Hook".

I have set X-Gitlab-Event:Push Hook in the ingress in front of Argo CD, which uses dex, which uses webhooks v5 (of some version) and that made the push hooks work just fine, so that makes me think that gitlab simply changed the header value.

Is the reliance on the header wrong or is gitlab broken?

Github Organization Webhook for Package V2 API

Hello. :3

Lately, there exists a Github organization webhook called "Package v2s". It can come in handy because it allows triggering events when container images are pushed to the Github Container Registry.

Bildschirmfoto 2021-01-04 um 16 59 15

I assume that the webhook is pretty new because it lacks documentation, but I think it would be more elegant to add it to this project before I start to build something custom on my own.

Would a PR adding this webhook generally be welcome? If yes, I am willing to contribute this addition.

Thanks and regards!

Support for multiple webhooks on the same server

Hey everyone, I needed support for listening to multiple webhooks on the same server so I wrote an example implementation for bitbucket, which seems to work okay.

#40

It's not complete or necessarily correct, and it doesn't have tests (yet), but please let me know your thoughts

String fields are not populated

It seems like no values that are mapped to string is available.

I am using the following:

case github.PushPayload:
		push := payload.(github.PushPayload)

		fmt.Print(push.Repository.URL) // Nothing
		fmt.Print(push.Before) // Actual value False

I checked my payload. The values I'm looking for actually does exist.

Let me know if you need more details.

Gitlab MergeRequestEvents

It looks like MergeRequestEvents is represented as MergerRequestEvents -- I was wondering if the double "r" was intentional?

I can do a PR to fix but wanted to check first.

Position in commit notes no longer int64

Position in commit notes are now an object. There are some additional position fields.

"change_position": {
    "base_sha": null,
    "start_sha": null,
    "head_sha": null,
    "old_path": null,
    "new_path": null,
    "position_type": "text",
    "old_line": null,
    "new_line": null
},
"original_position": {
    "base_sha": "068bca55a1606f8deda95713d043daae579123d0",
    "start_sha": "0883f2037428b19cac8bb5ac3823924dff0062e3",
    "head_sha": "130d7c3903d7d015219482fe5ad1b00110e76c4b",
    "old_path": "dataflow/src/main/java/com/nianticproject/lexicon/wb/transforms/ParseSessionTrackingProtos.java",
    "new_path": "dataflow/src/main/java/com/nianticproject/lexicon/wb/transforms/ParseSessionTrackingProtos.java",
    "position_type": "text",
    "old_line": null,
    "new_line": 50
},
"position": {
    "base_sha": "068bca55a1606f8deda95713d043daae579123d0",
    "start_sha": "0883f2037428b19cac8bb5ac3823924dff0062e3",
    "head_sha": "130d7c3903d7d015219482fe5ad1b00110e76c4b",
    "old_path": "dataflow/src/main/java/com/nianticproject/lexicon/wb/transforms/ParseSessionTrackingProtos.java",
    "new_path": "dataflow/src/main/java/com/nianticproject/lexicon/wb/transforms/ParseSessionTrackingProtos.java",
    "position_type": "text",
    "old_line": null,
    "new_line": 50
},

Payload Not Populating

Seems to be similar #8. My payload does not seem to be populating properly. I'm using the PullRequestEvent, specifically the review_requested Action. I also noticed that the payload was missing the attribute requested_reviewers (https://developer.github.com/v3/activity/events/types/#pullrequestevent)

Hook

func main() {
	hook := github.New(&github.Config{Secret: os.Getenv("GITHUB_SECRET")})
	hook.RegisterEvents(HandlePullRequestEvent, github.PullRequestEvent)
	port := os.Getenv("PORT")

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

Handling the Event

pullRequestPayload := payload.(github.PullRequestPayload)
fmt.Printf("%+v", pullRequestPayload)            // Seen below
fmt.Println(pullRequestPayload.Action)           // Empty

Payload

{Action: Number:0 PullRequest:{URL: ID:0 HTMLURL: DiffURL: PatchURL: IssueURL: Number:0 State: Locked:false Title: User:{Login: ID:0 AvatarURL: GravatarID: URL: HTMLURL: FollowersURL: FollowingURL: GistsURL: StarredURL: SubscriptionsURL: OrganizationsURL: ReposURL: EventsURL: ReceivedEventsURL: Type: SiteAdmin:false} Body: CreatedAt:0001-01-01 00:00:00 +0000 UTC UpdatedAt:0001-01-01 00:00:00 +0000 UTC ClosedAt:<nil> MergedAt:<nil> MergeCommitSha:<nil> Assignee:<nil> Milestone:<nil> CommitsURL: ReviewCommentsURL: ReviewCommentURL: CommentsURL: StatusesURL: Head:{Label: Ref: Sha: User:{Login: ID:0 AvatarURL: GravatarID: URL: HTMLURL: FollowersURL: FollowingURL: GistsURL: StarredURL: SubscriptionsURL: OrganizationsURL: ReposURL: EventsURL: ReceivedEventsURL: Type: SiteAdmin:false} Repo:{ID:0 Name: FullName: Owner:{Login: ID:0 AvatarURL: GravatarID: URL: HTMLURL: FollowersURL: FollowingURL: GistsURL: StarredURL: SubscriptionsURL: OrganizationsURL: ReposURL: EventsURL: ReceivedEventsURL: Type: SiteAdmin:false} Private:false HTMLURL: Description: Fork:false URL: ForksURL: KeysURL: CollaboratorsURL: TeamsURL: HooksURL: IssueEventsURL: EventsURL: AssigneesURL: BranchesURL: TagsURL: BlobsURL: GitTagsURL: GitRefsURL: TreesURL: StatusesURL: LanguagesURL: StargazersURL: ContributorsURL: SubscribersURL: SubscriptionURL: CommitsURL: GitCommitsURL: CommentsURL: IssueCommentURL: ContentsURL: CompareURL: MergesURL: ArchiveURL: DownloadsURL: IssuesURL: PullsURL: MilestonesURL: NotificationsURL: LabelsURL: ReleasesURL: CreatedAt:0001-01-01 00:00:00 +0000 UTC UpdatedAt:0001-01-01 00:00:00 +0000 UTC PushedAt:0001-01-01 00:00:00 +0000 UTC GitURL: SSHURL: CloneURL: SvnURL: Homepage:<nil> Size:0 StargazersCount:0 WatchersCount:0 Language:<nil> HasIssues:false HasDownloads:false HasWiki:false HasPages:false ForksCount:0 MirrorURL:<nil> OpenIssuesCount:0 Forks:0 OpenIssues:0 Watchers:0 DefaultBranch:}} Base:{Label: Ref: Sha: User:{Login: ID:0 AvatarURL: GravatarID: URL: HTMLURL: FollowersURL: FollowingURL: GistsURL: StarredURL: SubscriptionsURL: OrganizationsURL: ReposURL: EventsURL: ReceivedEventsURL: Type: SiteAdmin:false} Repo:{ID:0 Name: FullName: Owner:{Login: ID:0 AvatarURL: GravatarID: URL: HTMLURL: FollowersURL: FollowingURL: GistsURL: StarredURL: SubscriptionsURL: OrganizationsURL: ReposURL: EventsURL: ReceivedEventsURL: Type: SiteAdmin:false} Private:false HTMLURL: Description: Fork:false URL: ForksURL: KeysURL: CollaboratorsURL: TeamsURL: HooksURL: IssueEventsURL: EventsURL: AssigneesURL: BranchesURL: TagsURL: BlobsURL: GitTagsURL: GitRefsURL: TreesURL: StatusesURL: LanguagesURL: StargazersURL: ContributorsURL: SubscribersURL: SubscriptionURL: CommitsURL: GitCommitsURL: CommentsURL: IssueCommentURL: ContentsURL: CompareURL: MergesURL: ArchiveURL: DownloadsURL: IssuesURL: PullsURL: MilestonesURL: NotificationsURL: LabelsURL: ReleasesURL: CreatedAt:0001-01-01 00:00:00 +0000 UTC UpdatedAt:0001-01-01 00:00:00 +0000 UTC PushedAt:0001-01-01 00:00:00 +0000 UTC GitURL: SSHURL: CloneURL: SvnURL: Homepage:<nil> Size:0 StargazersCount:0 WatchersCount:0 Language:<nil> HasIssues:false HasDownloads:false HasWiki:false HasPages:false ForksCount:0 MirrorURL:<nil> OpenIssuesCount:0 Forks:0 OpenIssues:0 Watchers:0 DefaultBranch:}} Links:{Self:{Href:} HTML:{Href:} Issue:{Href:} Comments:{Href:} ReviewComments:{Href:} ReviewComment:{Href:} Commits:{Href:} Statuses:{Href:}} Merged:false Mergeable:<nil> MergeableState: MergedBy:<nil> Comments:0 ReviewComments:0 Commits:0 Additions:0 Deletions:0 ChangedFiles:0} Label:{ID:0 URL: Name: Color: Default:false} Repository:{ID:0 Name: FullName: Owner:{Login: ID:0 AvatarURL: GravatarID: URL: HTMLURL: FollowersURL: FollowingURL: GistsURL: StarredURL: SubscriptionsURL: OrganizationsURL: ReposURL: EventsURL: ReceivedEventsURL: Type: SiteAdmin:false} Private:false HTMLURL: Description: Fork:false URL: ForksURL: KeysURL: CollaboratorsURL: TeamsURL: HooksURL: IssueEventsURL: EventsURL: AssigneesURL: BranchesURL: TagsURL: BlobsURL: GitTagsURL: GitRefsURL: TreesURL: StatusesURL: LanguagesURL: StargazersURL: ContributorsURL: SubscribersURL: SubscriptionURL: CommitsURL: GitCommitsURL: CommentsURL: IssueCommentURL: ContentsURL: CompareURL: MergesURL: ArchiveURL: DownloadsURL: IssuesURL: PullsURL: MilestonesURL: NotificationsURL: LabelsURL: ReleasesURL: CreatedAt:0001-01-01 00:00:00 +0000 UTC UpdatedAt:0001-01-01 00:00:00 +0000 UTC PushedAt:0001-01-01 00:00:00 +0000 UTC GitURL: SSHURL: CloneURL: SvnURL: Homepage:<nil> Size:0 StargazersCount:0 WatchersCount:0 Language:<nil> HasIssues:false HasDownloads:false HasWiki:false HasPages:false ForksCount:0 MirrorURL:<nil> OpenIssuesCount:0 Forks:0 OpenIssues:0 Watchers:0 DefaultBranch:} Sender:{Login: ID:0 AvatarURL: GravatarID: URL: HTMLURL: FollowersURL: FollowingURL: GistsURL: StarredURL: SubscriptionsURL: OrganizationsURL: ReposURL: EventsURL: ReceivedEventsURL: Type: SiteAdmin:false} Installation:{ID:0}}

Thanks for your help!

GitLab 12.3 incompatibility with pipeline event

GitLab 12.3 added "source" field to "object_attributes". But it's not as other source fields - it's a simple string. https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/32373
So, webhook now can't unmarshal pipeline event structure.

json: cannot unmarshal string into Go struct field ObjectAttributes.object_attributes.source of type gitlab.Source

Looks like it's not trivial changes, because ObjectAttributes structure uses everywhere... That's why I can't simply create PR. May be the only way is to create separate ObjectAtrributesPipeline structure, but looks like it should be maintainer decision.

logging

It would be great if this project supports logging; it helps a lot when debugging.

For now I've used github.com/gorilla/handlers and modified Run in webhooks.go to be

logSrv := handlers.LoggingHandler(os.Stdout, srv)
s := &http.Server{Addr: addr, Handler: logSrv}
return s.ListenAndServe()

MergeRequestEventPayload has improper unmarshalling for changes

By default, the changes field of the JSON will look something like this (according to: https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#merge-request-events):

...
"changes":
    "author_id": {
        "previous": null,
        "current": 1
    },
    "merge_params": {
        "previous": {
        },
        "current": {
            "force_remove_source_branch": "0"
        }
    },...
...

The current format looks as it follows:

type Changes struct {
	LabelChanges LabelChanges `json:"labels"`
}

// LabelChanges contains changes in labels associated with a GitLab issue or MR
type LabelChanges struct {
	Previous []Label `json:"previous"`
	Current  []Label `json:"current"`
}

// Label contains all of the GitLab label information
type Label struct {
	ID          int64      `json:"id"`
	Title       string     `json:"title"`
	Color       string     `json:"color"`
	ProjectID   int64      `json:"project_id"`
	CreatedAt   customTime `json:"created_at"`
	UpdatedAt   customTime `json:"updated_at"`
	Template    bool       `json:"template"`
	Description string     `json:"description"`
	Type        string     `json:"type"`
	GroupID     int64      `json:"group_id"`
}

I propose that this should be marked as (excuse my poor Golang):

type Changes map[string]Change

type Change struct {
    Current  interface{}
    Previous interface{}
}

Unless objections, I'll be providing a PR for this.

Thanks in advance!

Parsing requests without passing expected events

We want to parse all Bitbucket events, Parse functions are looking for header to understand event type. So I don't understand the reason behind passing expected event types to Parse functions. Or am I missing something? Sorry for edits...

[DockerHub] use float64 instead of float32 for time fields

I consider this is not a bug, it's UX thing.

When I run simple golang:

type A struct{
	PushedAt float32 `json:"pushed_at"`
}

func main() {
	unixT := float32(1.5229495e+09)
	a := A{
		PushedAt: unixT,
	}
	b, _ := json.Marshal(a)
	var c A
	json.Unmarshal([]byte(b), &c)
	fmt.Printf("%+v\n", a)
	fmt.Printf("%+v\n", c)
	fmt.Printf("%d\n", int64(a.PushedAt))
}

then I got the output:

{PushedAt:1.5229495e+09}
{PushedAt:1.5229495e+09}
1522949504

You can see there is a diff when converting a.PushedAt into int64.
When I use this number with time.Unix(sec int64, nsec int64), this makes severe difference of the time.
If we use float64 then we can get right answer.

How about using float64 instead??
Any pointers would much be appreciated.

Regards,

Event payload to protobuf code

We have some code that translates the GitLab Golang event payloads to their protobuf equivalent. Would you be interested in including it?

testing

testing something don't mind this

Support returning an error to webhook

Hi,

it looks like there is no way for a webhook handler to return an error to the webhook request.
Usually I want to return a failure to whatever is sending the webhook so they can retry it later on.

Make Logger Interface Compatible with logrus.Logger

Hi! Love the package, great work guys.

I'm wondering if it's possible to change the Logger interface to be the same as Logrus's Logger interface, which takes variadic interfaces as arguments instead of a single string, like your package. This would not break the API and would be really useful for us, and probably a lot of people since Logrus is pretty ubiquitous.
Have you ever thought about this? Let me know if you can let this change in. I'm happy to make a PR.

Proposed change:

// old interface
type Logger interface {
	Info(string)
	Error(string)
	Debug(string)
}

// new interface
type Logger interface {
	Info(...interface{})
	Error(...interface{})
	Debug(...interface{})
}

This allows us to do something like:

func init() {
	log := logrus.StandardLogger()
	log.Out = os.Stdout
	// ... more config ...
	webhooks.DefaultLog = log
}

How to add a custom route to the server?

Right now it is not possible to add a custom route to the server, because the HTTP handler is overridden. Is there a way to achieve that? I want to add a route to return a server health check.

Bitbucket server does not send X-Hook-UUID header

Looks like all other interfaces are the same, but the X-Hook-UUID header never gets set in bitbucket server.
Bitbucket: https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html
Bitbucketserver: https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html#Eventpayload-httpheadersHTTPheaders

It appears the X-Hook-UUID is only used as a comparison if the UUID is actually set.
I'm proposing that we validate that the X-Hook-UUID header exists only if a user sets a value to the secret UUID in the bitbucket config.

Error ignored in github/webhook

In the file https://github.com/go-playground/webhooks/blob/v5/github/github.go in line 158, the error returned is ignored

Installation webhook payload format error

When an Installation webhook is triggered for deleting a github app, there is a slight difference in the format of payload. For a delete, the CreatedAt and UpdatedAt fields return a string from the webhook. But the payload struct is defined as int64. Can we have a fix for this ? It could include both int64 and string

CreatedAt int64 json:"created_at,string"
UpdatedAt int64 json:"updated_at,string" ?

Values coming from the delete action webhook is
"created_at":"2018-11-01T11:22:36.000Z",
"updated_at":"2018-11-01T11:22:36.000Z"

Github X-GitHub-Delivery Header

The Github web interface for webhooks shows each webhook request that was sent, along with the request's delivery ID.

This delivery ID is not part of the JSON payload, but rather part of the HTTP headers.

I propose adding a "Delivery" member to each of the payload structs so that this information is available in the webhooks library.

Would you be receptive to a PR that did this?

[GitHub] Verify content type to provide expressive error message

Today I was struggling hours with that the GitHub WebHook's content type must be application/json. It could be helpful we verify the content type and provide expressive error message to help users fix the setting.

I'm glad to send a PR if it's concluded to be a valid issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.