-
Notifications
You must be signed in to change notification settings - Fork 75
/
webhook.go
65 lines (58 loc) · 2.22 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package gitea
import (
"encoding/json"
"fmt"
giteaStructs "code.gitea.io/gitea/modules/structs"
)
// whEventType represents a Gitea webhook event.
type whEventType string
// List of supported events
//
// Find them in Gitea's source at /models/webhook.go as HookEventType.
// To correlate with when each of these trigger, see the Trigger On -> Custom Events options
// when editing a repo's webhook in a Gitea project. Those descriptions are helpful.
const (
EventTypeCreate whEventType = "create"
EventTypeDelete whEventType = "delete"
EventTypeFork whEventType = "fork"
EventTypePush whEventType = "push"
EventTypeIssues whEventType = "issues"
EventTypeIssueComment whEventType = "issue_comment"
EventTypeRepository whEventType = "repository"
EventTypeRelease whEventType = "release"
EventTypePullRequest whEventType = "pull_request"
EventTypePullRequestApproved whEventType = "pull_request_approved"
EventTypePullRequestRejected whEventType = "pull_request_rejected"
EventTypePullRequestComment whEventType = "pull_request_comment"
EventTypePullRequestSync whEventType = "pull_request_sync"
)
func parseWebhook(eventType whEventType, payload []byte) (event interface{}, err error) {
switch eventType {
case EventTypePush:
event = &giteaStructs.PushPayload{}
case EventTypeCreate:
event = &giteaStructs.CreatePayload{}
case EventTypeDelete:
event = &giteaStructs.DeletePayload{}
case EventTypeFork:
event = &giteaStructs.ForkPayload{}
case EventTypeIssues:
event = &giteaStructs.IssuePayload{}
case EventTypeIssueComment:
event = &giteaStructs.IssueCommentPayload{}
case EventTypeRepository:
event = &giteaStructs.RepositoryPayload{}
case EventTypeRelease:
event = &giteaStructs.ReleasePayload{}
case EventTypePullRequestComment:
event = &giteaStructs.IssueCommentPayload{}
case EventTypePullRequest, EventTypePullRequestApproved, EventTypePullRequestSync, EventTypePullRequestRejected:
event = &giteaStructs.PullRequestPayload{}
default:
return nil, fmt.Errorf("unexpected event type: %s", eventType)
}
if err := json.Unmarshal(payload, event); err != nil {
return nil, err
}
return event, nil
}