-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
81 lines (65 loc) · 1.57 KB
/
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
package quayd
import (
"encoding/json"
"github.com/gorilla/mux"
"net/http"
)
var validStatuses = []string{"pending", "success", "error", "failure"}
type Server struct {
http.Handler
}
func NewServer(q *Quayd) *Server {
if q == nil {
q = Default
}
m := mux.NewRouter()
m.Handle("/quay/{status}", &Webhook{q}).Methods("POST")
return &Server{m}
}
type Webhook struct {
*Quayd
}
type WebhookForm struct {
Repository string `json:"repository"`
TriggerKind string `json:"trigger_kind"`
IsManual bool `json:"is_manual"`
DockerTags []string `json:"docker_tags"`
BuildName string `json:"build_name"`
BuildURL string `json:"homepage"`
}
func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
status := vars["status"]
if !validStatus(status) {
http.Error(w, "Invalid status: "+status, 400)
return
}
var form WebhookForm
if err := json.NewDecoder(r.Body).Decode(&form); err != nil {
http.Error(w, err.Error(), 500)
return
}
// We don't want to process manually triggered builds.
if !(!form.IsManual && form.TriggerKind == "github") {
w.WriteHeader(204)
return
}
if status == "success" {
if err := wh.Quayd.LoadImageTags(form.DockerTags[0], form.Repository, form.BuildName); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
if err := wh.Quayd.Handle(form.Repository, form.BuildName, form.BuildURL, status); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
func validStatus(a string) bool {
for _, b := range validStatuses {
if b == a {
return true
}
}
return false
}