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
/
bitbucket_server.go
111 lines (101 loc) · 2.77 KB
/
bitbucket_server.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
fluxapi "github.com/fluxcd/flux/pkg/api"
fluxapi_v9 "github.com/fluxcd/flux/pkg/api/v9"
"github.com/google/go-github/v28/github"
"golang.org/x/sync/errgroup"
)
const BitbucketServer = "BitbucketServer"
func init() {
Sources[BitbucketServer] = handleBitbucketServerPush
}
func handleBitbucketServerPush(s fluxapi.Server, key []byte, w http.ResponseWriter, r *http.Request, _ Endpoint) {
// See incomplete docs: https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html
body, err := github.ValidatePayload(r, key)
if err != nil {
http.Error(w, "The signature header is invalid.", http.StatusUnauthorized)
log(BitbucketServer, "invalid signature:", err.Error())
return
}
if eventKey := r.Header.Get("X-Event-Key"); eventKey != "repo:refs_changed" {
http.Error(w, "Unexpected or missing header X-Event-Key", http.StatusBadRequest)
log(BitbucketServer, "unexpected X-Event-Key header:", eventKey)
return
}
var event bitbucketRefsChangedEvent
if err := json.Unmarshal(body, &event); err != nil {
http.Error(w, "Unable to JSON decode payload", http.StatusBadRequest)
log(BitbucketServer, "unable to decode payload:", err.Error())
return
}
repoURL, ok := event.repoCloneLink("ssh")
if !ok {
http.Error(w, "Missing repository SSH clone link", http.StatusBadRequest)
log(BitbucketServer, "missing repository SSH clone link")
return
}
var grp errgroup.Group
ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
for refID := range event.changeRefIDs("BRANCH") {
branch := strings.TrimPrefix(refID, "refs/heads/")
grp.Go(func() error {
return s.NotifyChange(ctx, fluxapi_v9.Change{
Kind: fluxapi_v9.GitChange,
Source: fluxapi_v9.GitUpdate{
URL: repoURL,
Branch: branch,
},
})
})
}
if err := grp.Wait(); err != nil {
http.Error(w, "Unable to process all push events", http.StatusInternalServerError)
log(BitbucketServer, "error from downstream:", err.Error())
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "OK")
}
type bitbucketRefsChangedEvent struct {
Repository struct {
Links struct {
Clone []struct {
Href string
Name string
}
}
}
Changes []struct {
Ref struct {
ID string
Type string
}
}
}
func (e *bitbucketRefsChangedEvent) repoCloneLink(name string) (string, bool) {
for _, link := range e.Repository.Links.Clone {
if link.Name == name {
return link.Href, true
}
}
return "", false
}
func (e *bitbucketRefsChangedEvent) changeRefIDs(typ string) map[string]bool {
var refIDs map[string]bool
for _, c := range e.Changes {
if c.Ref.Type != typ {
continue
}
if refIDs == nil {
refIDs = make(map[string]bool)
}
refIDs[c.Ref.ID] = true
}
return refIDs
}