|
| 1 | +package gitea |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +// parse errors |
| 13 | +var ( |
| 14 | + ErrEventNotSpecifiedToParse = errors.New("no Event specified to parse") |
| 15 | + ErrInvalidHTTPMethod = errors.New("invalid HTTP Method") |
| 16 | + ErrMissingGiteaEventHeader = errors.New("missing X-Gitea-Event Header") |
| 17 | + ErrParsingPayload = errors.New("error parsing payload") |
| 18 | +) |
| 19 | + |
| 20 | +// Gitea hook types |
| 21 | +// https://github.com/go-gitea/gitea/blob/bf7b083cfe47cc922090ce7922b89f7a5030a44d/models/webhook/hooktask.go#L31 |
| 22 | +const ( |
| 23 | + CreateEvent Event = "create" |
| 24 | + DeleteEvent Event = "delete" |
| 25 | + ForkEvent Event = "fork" |
| 26 | + IssuesEvent Event = "issues" |
| 27 | + IssueAssignEvent Event = "issue_assign" |
| 28 | + IssueLabelEvent Event = "issue_label" |
| 29 | + IssueMilestoneEvent Event = "issue_milestone" |
| 30 | + IssueCommentEvent Event = "issue_comment" |
| 31 | + PushEvent Event = "push" |
| 32 | + PullRequestEvent Event = "pull_request" |
| 33 | + PullRequestAssignEvent Event = "pull_request_assign" |
| 34 | + PullRequestLabelEvent Event = "pull_request_label" |
| 35 | + PullRequestMilestoneEvent Event = "pull_request_milestone" |
| 36 | + PullRequestCommentEvent Event = "pull_request_comment" |
| 37 | + PullRequestReviewEvent Event = "pull_request_review" |
| 38 | + PullRequestSyncEvent Event = "pull_request_sync" |
| 39 | + RepositoryEvent Event = "repository" |
| 40 | + ReleaseEvent Event = "release" |
| 41 | +) |
| 42 | + |
| 43 | +// Option is a configuration option for the webhook |
| 44 | +type Option func(*Webhook) error |
| 45 | + |
| 46 | +// Options is a namespace var for configuration options |
| 47 | +var Options = WebhookOptions{} |
| 48 | + |
| 49 | +// WebhookOptions is a namespace for configuration option methods |
| 50 | +type WebhookOptions struct{} |
| 51 | + |
| 52 | +// Secret registers the GitLab secret |
| 53 | +func (WebhookOptions) Secret(secret string) Option { |
| 54 | + return func(hook *Webhook) error { |
| 55 | + hook.secret = secret |
| 56 | + return nil |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Webhook instance contains all methods needed to process events |
| 61 | +type Webhook struct { |
| 62 | + secret string |
| 63 | +} |
| 64 | + |
| 65 | +// Event defines a GitLab hook event type by the X-Gitlab-Event Header |
| 66 | +type Event string |
| 67 | + |
| 68 | +// New creates and returns a WebHook instance denoted by the Provider type |
| 69 | +func New(options ...Option) (*Webhook, error) { |
| 70 | + hook := new(Webhook) |
| 71 | + for _, opt := range options { |
| 72 | + if err := opt(hook); err != nil { |
| 73 | + return nil, errors.New("Error applying Option") |
| 74 | + } |
| 75 | + } |
| 76 | + return hook, nil |
| 77 | +} |
| 78 | + |
| 79 | +// Parse verifies and parses the events specified and returns the payload object or an error |
| 80 | +func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error) { |
| 81 | + defer func() { |
| 82 | + _, _ = io.Copy(ioutil.Discard, r.Body) |
| 83 | + _ = r.Body.Close() |
| 84 | + }() |
| 85 | + |
| 86 | + if len(events) == 0 { |
| 87 | + return nil, ErrEventNotSpecifiedToParse |
| 88 | + } |
| 89 | + if r.Method != http.MethodPost { |
| 90 | + return nil, ErrInvalidHTTPMethod |
| 91 | + } |
| 92 | + |
| 93 | + event := r.Header.Get("X-Gitea-Event") |
| 94 | + if len(event) == 0 { |
| 95 | + return nil, ErrMissingGiteaEventHeader |
| 96 | + } |
| 97 | + |
| 98 | + giteaEvent := Event(event) |
| 99 | + |
| 100 | + payload, err := ioutil.ReadAll(r.Body) |
| 101 | + if err != nil || len(payload) == 0 { |
| 102 | + return nil, ErrParsingPayload |
| 103 | + } |
| 104 | + |
| 105 | + // https://github.com/go-gitea/gitea/blob/33fca2b537d36cf998dd27425b2bb8ed5b0965f3/services/webhook/payloader.go#L27 |
| 106 | + switch giteaEvent { |
| 107 | + case CreateEvent: |
| 108 | + var pl CreatePayload |
| 109 | + err = json.Unmarshal([]byte(payload), &pl) |
| 110 | + return pl, err |
| 111 | + case DeleteEvent: |
| 112 | + var pl DeletePayload |
| 113 | + err = json.Unmarshal([]byte(payload), &pl) |
| 114 | + return pl, err |
| 115 | + case ForkEvent: |
| 116 | + var pl ForkPayload |
| 117 | + err = json.Unmarshal([]byte(payload), &pl) |
| 118 | + return pl, err |
| 119 | + case PushEvent: |
| 120 | + var pl PushPayload |
| 121 | + err = json.Unmarshal([]byte(payload), &pl) |
| 122 | + return pl, err |
| 123 | + case IssuesEvent, IssueAssignEvent, IssueLabelEvent, IssueMilestoneEvent: |
| 124 | + var pl IssuePayload |
| 125 | + err = json.Unmarshal([]byte(payload), &pl) |
| 126 | + return pl, err |
| 127 | + case IssueCommentEvent, PullRequestCommentEvent: |
| 128 | + var pl IssueCommentPayload |
| 129 | + err = json.Unmarshal([]byte(payload), &pl) |
| 130 | + return pl, err |
| 131 | + case PullRequestEvent, PullRequestAssignEvent, PullRequestLabelEvent, PullRequestMilestoneEvent, PullRequestReviewEvent, PullRequestSyncEvent: |
| 132 | + var pl PullRequestPayload |
| 133 | + err = json.Unmarshal([]byte(payload), &pl) |
| 134 | + return pl, err |
| 135 | + case RepositoryEvent: |
| 136 | + var pl RepositoryPayload |
| 137 | + err = json.Unmarshal([]byte(payload), &pl) |
| 138 | + return pl, err |
| 139 | + case ReleaseEvent: |
| 140 | + var pl ReleasePayload |
| 141 | + err = json.Unmarshal([]byte(payload), &pl) |
| 142 | + return pl, err |
| 143 | + default: |
| 144 | + return nil, fmt.Errorf("unknown event %s", giteaEvent) |
| 145 | + } |
| 146 | +} |
0 commit comments