forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitbucket.go
198 lines (169 loc) · 5.75 KB
/
bitbucket.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package bitbucket
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"github.com/golang/glog"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
buildv1 "github.com/openshift/api/build/v1"
"github.com/openshift/origin/pkg/build/buildapihelpers"
"github.com/openshift/origin/pkg/build/webhook"
)
// WebHookPlugin used for processing gitlab webhook requests.
type WebHookPlugin struct{}
// New returns gitlab webhook plugin.
func New() *WebHookPlugin {
return &WebHookPlugin{}
}
// A push event for Bitbucket webhooks. Only some json parameters are used. The
// Bitbucket payload is less flat than GitLab or GitHub
// More information on Bitbucket push events here:
// https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html#EventPayloads-Push
type pushEvent struct {
Push push `json:"push"`
}
type push struct {
Changes []change `json:"changes"`
}
type change struct {
Commits []commit `json:"commits"`
Old info `json:"old"`
}
type commit struct {
Hash string `json:"hash"`
Message string `json:"message"`
Author user `json:"author"`
}
type user struct {
Username string `json:"username"`
DisplayName string `json:"display_name"`
}
type info struct {
Type string `json:"type"`
Name string `json:"name"`
}
type pushEvent54 struct {
Changes []change54 `json:"changes"`
}
type change54 struct {
Ref ref `json:"ref"`
ToHash string `json:"toHash"`
}
type ref struct {
DisplayID string `json:"displayId"`
}
// Extract services webhooks from bitbucket.com
func (p *WebHookPlugin) Extract(buildCfg *buildv1.BuildConfig, trigger *buildv1.WebHookTrigger, req *http.Request) (revision *buildv1.SourceRevision, envvars []corev1.EnvVar, dockerStrategyOptions *buildv1.DockerStrategyOptions, proceed bool, err error) {
glog.V(4).Infof("Verifying build request for BuildConfig %s/%s", buildCfg.Namespace, buildCfg.Name)
if err = verifyRequest(req); err != nil {
return revision, envvars, dockerStrategyOptions, false, err
}
method := getEvent(req.Header)
branch := ""
switch method {
// https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html
case "repo:push":
branch, revision, err = getInfoFromEvent(req.Body)
if err != nil {
return revision, envvars, dockerStrategyOptions, false, errors.NewBadRequest(err.Error())
}
// https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html
case "repo:refs_changed":
branch, revision, err = getInfoFromEvent54(req.Body)
if err != nil {
return revision, envvars, dockerStrategyOptions, false, errors.NewBadRequest(err.Error())
}
default:
return revision, envvars, dockerStrategyOptions, false, errors.NewBadRequest(fmt.Sprintf("Unknown Bitbucket X-Event-Key %s", method))
}
if !webhook.GitRefMatches(branch, webhook.DefaultConfigRef, &buildCfg.Spec.Source) {
glog.V(2).Infof("Skipping build for BuildConfig %s/%s. Branch reference '%s' does not match configuration", buildCfg.Namespace, buildCfg.Name, branch)
return revision, envvars, dockerStrategyOptions, false, err
}
return revision, envvars, dockerStrategyOptions, true, err
}
// GetTriggers retrieves the WebHookTriggers for this webhook type (if any)
func (p *WebHookPlugin) GetTriggers(buildConfig *buildv1.BuildConfig) ([]*buildv1.WebHookTrigger, error) {
triggers := buildapihelpers.FindTriggerPolicy(buildv1.BitbucketWebHookBuildTriggerType, buildConfig)
webhookTriggers := []*buildv1.WebHookTrigger{}
for _, trigger := range triggers {
if trigger.BitbucketWebHook != nil {
webhookTriggers = append(webhookTriggers, trigger.BitbucketWebHook)
}
}
if len(webhookTriggers) == 0 {
return nil, webhook.ErrHookNotEnabled
}
return webhookTriggers, nil
}
func verifyRequest(req *http.Request) error {
if method := req.Method; method != "POST" {
return webhook.MethodNotSupported
}
contentType := req.Header.Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return errors.NewBadRequest(fmt.Sprintf("non-parseable Content-Type %s (%s)", contentType, err))
}
if mediaType != "application/json" {
return errors.NewBadRequest(fmt.Sprintf("unsupported Content-Type %s", contentType))
}
if len(getEvent(req.Header)) == 0 {
return errors.NewBadRequest("missing X-Event-Key")
}
return nil
}
func getEvent(header http.Header) string {
return header.Get("X-Event-Key")
}
func getInfoFromEvent(body io.ReadCloser) (string, *buildv1.SourceRevision, error) {
data, err := ioutil.ReadAll(body)
if err != nil {
return "", nil, err
}
var event pushEvent
if err = json.Unmarshal(data, &event); err != nil {
return "", nil, err
}
if len(event.Push.Changes) == 0 || len(event.Push.Changes[0].Commits) == 0 {
return "", nil, fmt.Errorf("Unable to extract valid event from payload: %s", string(data))
}
lastCommit := event.Push.Changes[0].Commits[0]
author := buildv1.SourceControlUser{
Name: lastCommit.Author.Username,
}
revision := &buildv1.SourceRevision{
Git: &buildv1.GitSourceRevision{
Commit: lastCommit.Hash,
Author: author,
Committer: author,
Message: lastCommit.Message,
},
}
// We use old here specifically. If the branch is deleted in a push, the New
// object will be nil.
return event.Push.Changes[0].Old.Name, revision, nil
}
func getInfoFromEvent54(body io.ReadCloser) (string, *buildv1.SourceRevision, error) {
data, err := ioutil.ReadAll(body)
if err != nil {
return "", nil, err
}
var event pushEvent54
if err = json.Unmarshal(data, &event); err != nil {
return "", nil, err
}
if len(event.Changes) == 0 {
return "", nil, fmt.Errorf("Unable to extract valid event from payload: %s", string(data))
}
revision := &buildv1.SourceRevision{
Git: &buildv1.GitSourceRevision{
Commit: event.Changes[0].ToHash,
},
}
return event.Changes[0].Ref.DisplayID, revision, nil
}