Compare commits
2 Commits
master
...
gitea-support
| Author | SHA1 | Date | |
|---|---|---|---|
| a2bdbf1756 | |||
| 1c899982c1 |
+210
@@ -0,0 +1,210 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
structs "code.gitea.io/gitea/modules/structs"
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// parse errors
|
||||
var (
|
||||
ErrEventNotSpecifiedToParse = errors.New("no Event specified to parse")
|
||||
ErrInvalidHTTPMethod = errors.New("invalid HTTP Method")
|
||||
ErrMissingGiteaEventHeader = errors.New("missing X-Gitea-Event Header")
|
||||
ErrMissingHubSignatureHeader = errors.New("missing X-Hub-Signature Header")
|
||||
ErrEventNotFound = errors.New("event not defined to be parsed")
|
||||
ErrParsingPayload = errors.New("error parsing payload")
|
||||
ErrHMACVerificationFailed = errors.New("HMAC verification failed")
|
||||
)
|
||||
|
||||
// Event defines a Gitea hook event type
|
||||
type Event string
|
||||
|
||||
// Gitea hook types
|
||||
const (
|
||||
CreateEvent Event = "create"
|
||||
DeleteEvent Event = "delete"
|
||||
ForkEvent Event = "fork"
|
||||
IssuesEvent Event = "issues"
|
||||
IssueAssignEvent Event = "issue_assign"
|
||||
IssueLabelEvent Event = "issue_label"
|
||||
IssueMilestoneEvent Event = "issue_milestone"
|
||||
IssueCommentEvent Event = "issue_comment"
|
||||
PushEvent Event = "push"
|
||||
PullRequestEvent Event = "pull_request"
|
||||
PullRequestAssignEvent Event = "pull_request_assign"
|
||||
PullRequestLabelEvent Event = "pull_request_label"
|
||||
PullRequestMilestoneEvent Event = "pull_request_milestone"
|
||||
PullRequestCommentEvent Event = "pull_request_comment"
|
||||
PullRequestReviewEvent Event = "pull_request_review"
|
||||
PullRequestSyncEvent Event = "pull_request_sync"
|
||||
RepositoryEvent Event = "repository"
|
||||
ReleaseEvent Event = "release"
|
||||
PackageEvent Event = "package"
|
||||
)
|
||||
|
||||
type CreatePayload structs.CreatePayload
|
||||
type DeletePayload structs.DeletePayload
|
||||
type ForkPayload structs.ForkPayload
|
||||
type IssuePayload structs.IssuePayload
|
||||
type IssueCommentPayload structs.IssueCommentPayload
|
||||
type PushPayload structs.PushPayload
|
||||
type PullRequestPayload structs.PullRequestPayload
|
||||
type RepositoryPayload structs.RepositoryPayload
|
||||
type ReleasePayload structs.ReleasePayload
|
||||
type PackagePayload structs.PackagePayload
|
||||
|
||||
// EventSubtype defines a GitHub Hook Event subtype
|
||||
type EventSubtype string
|
||||
|
||||
// Gitea hook event subtypes
|
||||
const (
|
||||
NoSubtype EventSubtype = ""
|
||||
BranchSubtype EventSubtype = "branch"
|
||||
TagSubtype EventSubtype = "tag"
|
||||
PullSubtype EventSubtype = "pull"
|
||||
IssueSubtype EventSubtype = "issues"
|
||||
)
|
||||
|
||||
// Option is a configuration option for the webhook
|
||||
type Option func(*Webhook) error
|
||||
|
||||
// Options is a namespace var for configuration options
|
||||
var Options = WebhookOptions{}
|
||||
|
||||
// WebhookOptions is a namespace for configuration option methods
|
||||
type WebhookOptions struct{}
|
||||
|
||||
// Secret registers the Gitea secret
|
||||
func (WebhookOptions) Secret(secret string) Option {
|
||||
return func(hook *Webhook) error {
|
||||
hook.secret = secret
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Webhook instance contains all methods needed to process events
|
||||
type Webhook struct {
|
||||
secret string
|
||||
}
|
||||
|
||||
// New creates and returns a WebHook instance denoted by the Provider type
|
||||
func New(options ...Option) (*Webhook, error) {
|
||||
hook := new(Webhook)
|
||||
for _, opt := range options {
|
||||
if err := opt(hook); err != nil {
|
||||
return nil, errors.New("Error applying Option")
|
||||
}
|
||||
}
|
||||
return hook, nil
|
||||
}
|
||||
|
||||
// Parse verifies and parses the events specified and returns the payload object or an error
|
||||
func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error) {
|
||||
defer func() {
|
||||
_, _ = io.Copy(ioutil.Discard, r.Body)
|
||||
_ = r.Body.Close()
|
||||
}()
|
||||
|
||||
if len(events) == 0 {
|
||||
return nil, ErrEventNotSpecifiedToParse
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
return nil, ErrInvalidHTTPMethod
|
||||
}
|
||||
|
||||
event := r.Header.Get("X-Gitea-Event")
|
||||
if event == "" {
|
||||
return nil, ErrMissingGiteaEventHeader
|
||||
}
|
||||
giteaEvent := Event(event)
|
||||
|
||||
var found bool
|
||||
for _, evt := range events {
|
||||
if evt == giteaEvent {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
// event not defined to be parsed
|
||||
if !found {
|
||||
return nil, ErrEventNotFound
|
||||
}
|
||||
|
||||
payload, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil || len(payload) == 0 {
|
||||
return nil, ErrParsingPayload
|
||||
}
|
||||
|
||||
// If we have a Secret set, we should check the MAC
|
||||
if len(hook.secret) > 0 {
|
||||
signature := r.Header.Get("X-Hub-Signature")
|
||||
if len(signature) == 0 {
|
||||
return nil, ErrMissingHubSignatureHeader
|
||||
}
|
||||
mac := hmac.New(sha1.New, []byte(hook.secret))
|
||||
_, _ = mac.Write(payload)
|
||||
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
||||
|
||||
if !hmac.Equal([]byte(signature[5:]), []byte(expectedMAC)) {
|
||||
return nil, ErrHMACVerificationFailed
|
||||
}
|
||||
}
|
||||
|
||||
switch giteaEvent {
|
||||
case CreateEvent:
|
||||
var pl CreatePayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case DeleteEvent:
|
||||
var pl DeletePayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case ForkEvent:
|
||||
var pl ForkPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case IssuesEvent, IssueAssignEvent, IssueLabelEvent, IssueMilestoneEvent:
|
||||
var pl IssuePayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case IssueCommentEvent, PullRequestCommentEvent:
|
||||
var pl IssueCommentPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PushEvent:
|
||||
var pl PushPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PullRequestEvent, PullRequestAssignEvent, PullRequestLabelEvent,
|
||||
PullRequestMilestoneEvent, PullRequestSyncEvent:
|
||||
var pl PullRequestPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PullRequestReviewEvent:
|
||||
var pl PullRequestPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case RepositoryEvent:
|
||||
var pl RepositoryPayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case ReleaseEvent:
|
||||
var pl ReleasePayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
case PackageEvent:
|
||||
var pl PackagePayload
|
||||
err = json.Unmarshal([]byte(payload), &pl)
|
||||
return pl, err
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown event %s", giteaEvent)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ module github.com/go-playground/webhooks/v6
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
code.gitea.io/gitea v1.17.4
|
||||
github.com/gogits/go-gogs-client v0.0.0-20200905025246-8bb8a50cb355
|
||||
github.com/stretchr/testify v1.6.1
|
||||
github.com/stretchr/testify v1.8.0
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user