forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
160 lines (150 loc) · 5.54 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package webhook
import (
"net/http"
"net/url"
"regexp"
"strings"
appclientset "github.com/argoproj/argo-cd/pkg/client/clientset/versioned"
"github.com/argoproj/argo-cd/util/argo"
"github.com/argoproj/argo-cd/util/settings"
log "github.com/sirupsen/logrus"
webhooks "gopkg.in/go-playground/webhooks.v3"
"gopkg.in/go-playground/webhooks.v3/bitbucket"
"gopkg.in/go-playground/webhooks.v3/github"
"gopkg.in/go-playground/webhooks.v3/gitlab"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type ArgoCDWebhookHandler struct {
ns string
appClientset appclientset.Interface
github *github.Webhook
githubHandler http.Handler
gitlab *gitlab.Webhook
gitlabHandler http.Handler
bitbucket *bitbucket.Webhook
bitbucketHandler http.Handler
}
func NewHandler(namespace string, appClientset appclientset.Interface, set *settings.ArgoCDSettings) *ArgoCDWebhookHandler {
acdWebhook := ArgoCDWebhookHandler{
ns: namespace,
appClientset: appClientset,
github: github.New(&github.Config{Secret: set.WebhookGitHubSecret}),
gitlab: gitlab.New(&gitlab.Config{Secret: set.WebhookGitLabSecret}),
bitbucket: bitbucket.New(&bitbucket.Config{UUID: set.WebhookBitbucketUUID}),
}
acdWebhook.github.RegisterEvents(acdWebhook.HandleEvent, github.PushEvent)
acdWebhook.gitlab.RegisterEvents(acdWebhook.HandleEvent, gitlab.PushEvents, gitlab.TagEvents)
acdWebhook.bitbucket.RegisterEvents(acdWebhook.HandleEvent, bitbucket.RepoPushEvent)
acdWebhook.githubHandler = webhooks.Handler(acdWebhook.github)
acdWebhook.gitlabHandler = webhooks.Handler(acdWebhook.gitlab)
acdWebhook.bitbucketHandler = webhooks.Handler(acdWebhook.bitbucket)
return &acdWebhook
}
// affectedRevisionInfo examines a payload from a webhook event, and extracts the repo web URL,
// the revision, and whether or not this affected origin/HEAD (the default branch of the repository)
func affectedRevisionInfo(payloadIf interface{}) (string, string, bool) {
var webURL string
var revision string
var touchedHead bool
parseRef := func(ref string) string {
refParts := strings.SplitN(ref, "/", 3)
return refParts[len(refParts)-1]
}
switch payload := payloadIf.(type) {
case github.PushPayload:
// See: https://developer.github.com/v3/activity/events/types/#pushevent
webURL = payload.Repository.HTMLURL
revision = parseRef(payload.Ref)
touchedHead = bool(payload.Repository.DefaultBranch == revision)
case gitlab.PushEventPayload:
// See: https://docs.gitlab.com/ee/user/project/integrations/webhooks.html
// NOTE: this is untested
webURL = payload.Project.WebURL
revision = parseRef(payload.Ref)
touchedHead = bool(payload.Project.DefaultBranch == revision)
case gitlab.TagEventPayload:
// See: https://docs.gitlab.com/ee/user/project/integrations/webhooks.html
// NOTE: this is untested
webURL = payload.Project.WebURL
revision = parseRef(payload.Ref)
touchedHead = bool(payload.Project.DefaultBranch == revision)
case bitbucket.RepoPushPayload:
// See: https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html#EventPayloads-Push
// NOTE: this is untested
webURL = payload.Repository.Links.HTML.Href
// TODO: bitbucket includes multiple changes as part of a single event.
// We only pick the first but need to consider how to handle multiple
for _, change := range payload.Push.Changes {
revision = change.New.Name
break
}
// Not actually sure how to check if the incoming change affected HEAD just by examining the
// payload alone. To be safe, we just return true and let the controller check for himself.
touchedHead = true
}
return webURL, revision, touchedHead
}
// HandleEvent handles webhook events for repo push events
func (a *ArgoCDWebhookHandler) HandleEvent(payload interface{}, header webhooks.Header) {
webURL, revision, touchedHead := affectedRevisionInfo(payload)
// NOTE: the webURL does not include the .git extension
if webURL == "" {
log.Info("Ignoring webhook event")
return
}
log.Infof("Received push event repo: %s, revision: %s, touchedHead: %v", webURL, revision, touchedHead)
appIf := a.appClientset.ArgoprojV1alpha1().Applications(a.ns)
apps, err := appIf.List(metav1.ListOptions{})
if err != nil {
log.Warnf("Failed to list applications: %v", err)
return
}
urlObj, err := url.Parse(webURL)
if err != nil {
log.Warnf("Failed to parse repoURL '%s'", webURL)
return
}
regexpStr := "(?i)(http://|https://|git@)" + urlObj.Host + "[:/]" + urlObj.Path[1:] + "(\\.git)?"
repoRegexp, err := regexp.Compile(regexpStr)
if err != nil {
log.Warn("Failed to compile repoURL regexp")
return
}
for _, app := range apps.Items {
if !repoRegexp.MatchString(app.Spec.Source.RepoURL) {
log.Debugf("%s does not match", app.Spec.Source.RepoURL)
continue
}
targetRev := app.Spec.Source.TargetRevision
if targetRev == "HEAD" || targetRev == "" {
if !touchedHead {
continue
}
} else if targetRev != revision {
continue
}
_, err = argo.RefreshApp(appIf, app.ObjectMeta.Name)
if err != nil {
log.Warnf("Failed to refresh app '%s' for controller reprocessing: %v", app.ObjectMeta.Name, err)
continue
}
}
}
func (a *ArgoCDWebhookHandler) Handler(w http.ResponseWriter, r *http.Request) {
event := r.Header.Get("X-GitHub-Event")
if len(event) > 0 {
a.githubHandler.ServeHTTP(w, r)
return
}
event = r.Header.Get("X-Gitlab-Event")
if len(event) > 0 {
a.gitlabHandler.ServeHTTP(w, r)
return
}
uuid := r.Header.Get("X-Hook-UUID")
if len(uuid) > 0 {
a.bitbucketHandler.ServeHTTP(w, r)
return
}
log.Debug("Ignoring unknown webhook event")
}