forked from seal-io/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
142 lines (124 loc) · 3.67 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package scm
import (
"errors"
"net/http"
)
var (
// ErrSignatureInvalid is returned when the webhook
// signature is invalid or cannot be calculated.
ErrSignatureInvalid = errors.New("Invalid webhook signature")
// ErrUnknownEvent is returned when the webhook event
// is not recognized by the system.
ErrUnknownEvent = errors.New("Unknown webhook event")
)
type (
// Webhook defines a webhook for repository events.
Webhook interface {
Repository() Repository
}
// PushHook represents a push hook, eg push events.
PushHook struct {
Ref string
BaseRef string
Repo Repository
Before string
After string
Commit Commit
Sender User
Commits []Commit
}
// BranchHook represents a branch or tag event,
// eg create and delete github event types.
BranchHook struct {
Ref Reference
Repo Repository
Action Action
Sender User
}
// TagHook represents a tag event, eg create and delete
// github event types.
TagHook struct {
Ref Reference
Repo Repository
Action Action
Sender User
}
// IssueHook represents an issue event, eg issues.
IssueHook struct {
Action Action
Repo Repository
Issue Issue
Sender User
}
// IssueCommentHook represents an issue comment event,
// eg issue_comment.
IssueCommentHook struct {
Action Action
Repo Repository
Issue Issue
Comment Comment
Sender User
}
// PullRequestHook represents an pull request event,
// eg pull_request.
PullRequestHook struct {
Action Action
Repo Repository
PullRequest PullRequest
Sender User
}
// PullRequestCommentHook represents an pull request
// comment event, eg pull_request_comment.
PullRequestCommentHook struct {
Action Action
Repo Repository
PullRequest PullRequest
Comment Comment
Sender User
}
// ReviewCommentHook represents a pull request review
// comment, eg pull_request_review_comment.
ReviewCommentHook struct {
Action Action
Repo Repository
PullRequest PullRequest
Review Review
}
// DeployHook represents a deployment event. This is
// currently a GitHub-specific event type.
DeployHook struct {
Data interface{}
Desc string
Number int64
Ref Reference
Repo Repository
Sender User
Target string
TargetURL string
Task string
}
// SecretFunc provides the Webhook parser with the
// secret key used to validate webhook authenticity.
SecretFunc func(webhook Webhook) (string, error)
// WebhookService provides abstract functions for
// parsing and validating webhooks requests.
WebhookService interface {
// Parse returns the parsed the repository webhook payload.
Parse(req *http.Request, fn SecretFunc) (Webhook, error)
}
)
// Repository() defines the repository webhook and provides
// a convenient way to get the associated repository without
// having to cast the type.
func (h *PushHook) Repository() Repository { return h.Repo }
func (h *BranchHook) Repository() Repository { return h.Repo }
func (h *DeployHook) Repository() Repository { return h.Repo }
func (h *TagHook) Repository() Repository { return h.Repo }
func (h *IssueHook) Repository() Repository { return h.Repo }
func (h *IssueCommentHook) Repository() Repository { return h.Repo }
func (h *PullRequestHook) Repository() Repository { return h.Repo }
func (h *PullRequestCommentHook) Repository() Repository { return h.Repo }
func (h *ReviewCommentHook) Repository() Repository { return h.Repo }