This repository has been archived by the owner on Oct 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
90 lines (68 loc) · 1.62 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
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/Azure/brigade/pkg/brigade"
"github.com/Azure/brigade/pkg/storage"
"github.com/Azure/brigade/pkg/storage/kube"
"github.com/gin-gonic/gin"
)
var store storage.Store
func main() {
client, err := kube.GetClient("", os.Getenv("KUBECONFIG"))
if err != nil {
panic(err)
}
store = kube.New(client, os.Getenv("NAMESPACE"))
router := gin.Default()
router.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "OK",
})
})
webhook := router.Group("/webhook")
webhook.POST("/:org/:repo/:commit", func(c *gin.Context) {
org := c.Param("org")
repo := c.Param("repo")
commit := c.Param("commit")
projectName := fmt.Sprintf("%s/%s", org, repo)
project, err := store.GetProject(projectName)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"message": fmt.Sprintf("Brigade project not found for %s", projectName),
})
return
}
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Malformed request body",
})
return
}
c.Request.Body.Close()
build := &brigade.Build{
ProjectID: project.ID,
Type: "gcr_image_push",
Provider: "google-container-registry",
Payload: body,
Revision: &brigade.Revision{
Ref: commit,
},
}
if err := store.CreateBuild(build); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Failed to invoke hook",
})
return
}
// TODO: send build id
c.JSON(http.StatusOK, gin.H{
"message": "OK",
"buildID": build.ID,
})
})
router.Run()
}