This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbitbucket_cloud.go
95 lines (83 loc) · 2.92 KB
/
bitbucket_cloud.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
fluxapi "github.com/fluxcd/flux/pkg/api"
fluxapi_v9 "github.com/fluxcd/flux/pkg/api/v9"
)
// Handily (not handily) Bitbucket's cloud and self-hosted products
// have different names for all the events and fields. This is for the
// events sent by the "Cloud" product (bitbucket.org):
// https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html#EventPayloads-Push
//
// (For completeness, the docs for the self-hosted Bitbucket "Server"
// are at
// https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html. The
// self-hosted version includes a signature in the header
// "X-Hub-Signature", but this is not present for "Cloud").
const BitbucketCloud = "BitbucketCloud"
func init() {
Sources[BitbucketCloud] = handleBitbucketCloudPush
}
func handleBitbucketCloudPush(s fluxapi.Server, _ []byte, w http.ResponseWriter, r *http.Request, _ Endpoint) {
if event := r.Header.Get("X-Event-Key"); event != "repo:push" {
http.Error(w, "Unexpected or missing header X-Event-Key", http.StatusBadRequest)
log(BitbucketCloud, "missing or incorrect X-Event-Key header:", event)
return
}
type bitbucketCloudPayload struct {
Repository bitbucketCloudRepository
Push struct {
Changes []struct {
New struct {
Type, Name string
}
}
}
}
var payload bitbucketCloudPayload
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "Unable to decode payload as JSON", http.StatusBadRequest)
log(BitbucketCloud, "unable to decode payload:", err.Error())
return
}
// The bitbucket.org events potentially contain many ref updates;
// presumably, it bundles together e.g., the result of a `git
// push` into one event. We only notify about one things at a time
// though, so:
// - collect all the changes
// - send as many as we can before we reach our deadline.
// That may mean we miss some, but this is best effort.
//
// NB a change can be to a branch or a tag; here we'll send both
// through, since it's in principle possible to sync to a tag.
repo := payload.Repository.RepoURL()
ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
for i := range payload.Push.Changes {
refChange := payload.Push.Changes[i].New
change := fluxapi_v9.Change{
Kind: fluxapi_v9.GitChange,
Source: fluxapi_v9.GitUpdate{
URL: repo,
Branch: refChange.Name,
},
}
if err := s.NotifyChange(ctx, change); err != nil {
http.Error(w, "Unable to process all push events", http.StatusInternalServerError)
log(BitbucketCloud, "error from downstream:", err.Error())
return
}
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
// The fields of repository that we care about
type bitbucketCloudRepository struct {
FullName string `json:"full_name"`
}
func (r bitbucketCloudRepository) RepoURL() string {
return fmt.Sprintf("[email protected]:%s.git", r.FullName)
}