forked from go-playground/webhooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazuredevops.go
125 lines (106 loc) · 3.4 KB
/
azuredevops.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
package azuredevops
// this package receives Azure DevOps Server webhooks
// https://docs.microsoft.com/en-us/azure/devops/service-hooks/services/webhooks?view=azure-devops-2020
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)
// parse errors
var (
ErrInvalidHTTPMethod = errors.New("invalid HTTP Method")
ErrParsingPayload = errors.New("error parsing payload")
ErrBasicAuthVerificationFailed = errors.New("basic auth verification failed")
)
// Event defines an Azure DevOps server hook event type
type Event string
// Azure DevOps Server hook types
const (
BuildCompleteEventType Event = "build.complete"
GitPullRequestCreatedEventType Event = "git.pullrequest.created"
GitPullRequestUpdatedEventType Event = "git.pullrequest.updated"
GitPullRequestMergedEventType Event = "git.pullrequest.merged"
GitPushEventType Event = "git.push"
GitPullRequestCommentEventType Event = "ms.vss-code.git-pullrequest-comment-event"
)
// 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{}
// BasicAuth verifies payload using basic auth
func (WebhookOptions) BasicAuth(username, password string) Option {
return func(hook *Webhook) error {
hook.username = username
hook.password = password
return nil
}
}
// Webhook instance contains all methods needed to process events
type Webhook struct {
username string
password string
}
// New creates and returns a WebHook instance
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(io.Discard, r.Body)
_ = r.Body.Close()
}()
if !hook.verifyBasicAuth(r) {
return nil, ErrBasicAuthVerificationFailed
}
if r.Method != http.MethodPost {
return nil, ErrInvalidHTTPMethod
}
payload, err := io.ReadAll(r.Body)
if err != nil || len(payload) == 0 {
return nil, ErrParsingPayload
}
var pl BasicEvent
err = json.Unmarshal([]byte(payload), &pl)
if err != nil {
return nil, ErrParsingPayload
}
switch pl.EventType {
case GitPushEventType:
var fpl GitPushEvent
err = json.Unmarshal([]byte(payload), &fpl)
return fpl, err
case GitPullRequestCreatedEventType, GitPullRequestMergedEventType, GitPullRequestUpdatedEventType:
var fpl GitPullRequestEvent
err = json.Unmarshal([]byte(payload), &fpl)
return fpl, err
case BuildCompleteEventType:
var fpl BuildCompleteEvent
err = json.Unmarshal([]byte(payload), &fpl)
return fpl, err
case GitPullRequestCommentEventType:
var fpl GitPullRequestCommentEvent
err = json.Unmarshal([]byte(payload), &fpl)
return fpl, err
default:
return nil, fmt.Errorf("unknown event %s", pl.EventType)
}
}
func (hook Webhook) verifyBasicAuth(r *http.Request) bool {
// skip validation if username or password was not provided
if hook.username == "" && hook.password == "" {
return true
}
username, password, ok := r.BasicAuth()
return ok && username == hook.username && password == hook.password
}