-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
60 lines (51 loc) · 1.19 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
// Copyright 2017 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package main
import (
"encoding/json"
"log"
"net/http"
)
type gitlabRequest struct {
ObjectKind string `json:"object_kind"`
Ref string `json:"ref"`
Project struct {
Name string `json:"name"`
}
}
func parseGitLabRequest(req *http.Request) (h hook, status int) {
var jr gitlabRequest
jd := json.NewDecoder(req.Body)
err := jd.Decode(&jr)
if err != nil {
log.Println("decode error:", err)
status = http.StatusBadRequest
return
}
if jr.ObjectKind != "push" {
log.Println("wrong kind:", jr.ObjectKind)
status = http.StatusBadRequest
return
}
// check if a handler exists for this project
h, ok := cfg.Hooks[jr.Project.Name]
if !ok {
log.Println("hook not found:", jr.Project.Name)
status = http.StatusTeapot
return
}
// check if the ref matches
if jr.Ref != h.Ref {
log.Println("ref mismatch:", h.Ref)
status = http.StatusTeapot
return
}
// check if the secret matches
if req.Header.Get("X-Gitlab-Token") != h.Secret {
status = http.StatusForbidden
return
}
status = http.StatusOK
return
}