forked from fluxcd/flux-recv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab.go
63 lines (53 loc) · 1.59 KB
/
gitlab.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
package main
import (
"context"
"encoding/json"
"net/http"
"strings"
fluxapi "github.com/fluxcd/flux/pkg/api"
fluxapi_v9 "github.com/fluxcd/flux/pkg/api/v9"
)
const GitLab = "GitLab"
func init() {
Sources[GitLab] = handleGitlabPush
}
func handleGitlabPush(s fluxapi.Server, key []byte, w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Gitlab-Token") != string(key) {
http.Error(w, "The Gitlab token does not match", http.StatusUnauthorized)
log(GitLab, "missing or incorrect X-Gitlab-Token header (!= shared secret)")
return
}
if event := r.Header.Get("X-Gitlab-Event"); event != "Push Hook" {
http.Error(w, "Unexpected or missing X-Gitlab-Event", http.StatusBadRequest)
log(GitLab, "unknown gitlab event header:", event)
return
}
type gitlabPayload struct {
Ref string
Project struct {
SSHURL string `json:"git_ssh_url"`
}
}
var payload gitlabPayload
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "Unable to parse hook payload", http.StatusBadRequest)
log(GitLab, "unable to parse payload:", err.Error())
return
}
change := fluxapi_v9.Change{
Kind: fluxapi_v9.GitChange,
Source: fluxapi_v9.GitUpdate{
URL: payload.Project.SSHURL,
Branch: strings.TrimPrefix(payload.Ref, "refs/heads/"),
},
}
ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
if err := s.NotifyChange(ctx, change); err != nil {
http.Error(w, "Error forwarding hook", http.StatusInternalServerError)
log(GitLab, "error from downstream:", err.Error())
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}