-
Notifications
You must be signed in to change notification settings - Fork 6
/
handler.go
154 lines (130 loc) · 4.97 KB
/
handler.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"net/http"
"maibornwolff/vbump/service"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/log"
)
// Handler for handling http routes
type Handler struct {
versionManager *service.VersionManager
}
// NewHandler constructs a new handler
func NewHandler(versionManager *service.VersionManager) *Handler {
return &Handler{
versionManager: versionManager,
}
}
// LoggerMiddleware logs the last error
func (handler *Handler) LoggerMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
err := c.Errors.Last()
if err != nil {
log.Error().Err(err).Msg("")
}
}
}
// GetRouter configures all routes
func (handler *Handler) GetRouter() *gin.Engine {
r := gin.New()
r.Use(handler.LoggerMiddleware())
gin.SetMode(gin.ReleaseMode)
r.POST("/major/:project", handler.OnMajor)
r.POST("/minor/:project", handler.OnMinor)
r.POST("/patch/:project", handler.OnPatch)
r.POST("/transient/minor/:version", handler.OnTransientMinor)
r.POST("/transient/patch/:version", handler.OnTransientPatch)
r.POST("/version/:project/:version", handler.OnSetVersion)
r.GET("/version/:project", handler.OnGetVersion)
r.GET("/", handler.OnHealth)
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
return r
}
// OnHealth is a handler for a health check
func (handler *Handler) OnHealth(context *gin.Context) {
context.String(http.StatusOK, "hello from vbump!")
}
// OnMajor is a handler for bumping the major part for a given project
func (handler *Handler) OnMajor(context *gin.Context) {
project := context.Param("project")
version, err := handler.versionManager.BumpMajor(project)
if err != nil {
_ = context.AbortWithError(http.StatusInternalServerError, err)
return
}
numberOfBumps.With(prometheus.Labels{"project": project, "element": "major"}).Inc()
log.Info().Str("version", version.String()).Str("project", project).Msg("Bumped major version")
context.String(http.StatusOK, "%s", version.String())
}
// OnMinor is a handler for bumping the minor part for a given project
func (handler *Handler) OnMinor(context *gin.Context) {
project := context.Param("project")
version, err := handler.versionManager.BumpMinor(project)
if err != nil {
_ = context.AbortWithError(http.StatusInternalServerError, err)
return
}
numberOfBumps.With(prometheus.Labels{"project": project, "element": "minor"}).Inc()
log.Info().Str("version", version.String()).Str("project", project).Msg("Bumped minor version")
context.String(http.StatusOK, "%s", version.String())
}
// OnPatch is a handler for bumping the patch part for a given project
func (handler *Handler) OnPatch(context *gin.Context) {
project := context.Param("project")
version, err := handler.versionManager.BumpPatch(project)
if err != nil {
_ = context.AbortWithError(http.StatusInternalServerError, err)
return
}
numberOfBumps.With(prometheus.Labels{"project": project, "element": "patch"}).Inc()
log.Info().Str("version", version.String()).Str("project", project).Msg("Bumped patch version")
context.String(http.StatusOK, "%s", version.String())
}
// OnSetVersion is a handler for setting the version for a given project
func (handler *Handler) OnSetVersion(context *gin.Context) {
project := context.Param("project")
version := context.Param("version")
_, err := handler.versionManager.SetVersion(project, version)
if err != nil {
_ = context.AbortWithError(http.StatusBadRequest, err)
return
}
log.Info().Str("version", version).Str("project", project).Msg("Set version explicitly")
context.String(http.StatusOK, "%s", version)
}
// OnGetVersion is a handler for getting the version for a given project
func (handler *Handler) OnGetVersion(context *gin.Context) {
project := context.Param("project")
version, err := handler.versionManager.GetVersion(project)
if err != nil {
_ = context.AbortWithError(http.StatusNotFound, err)
return
}
log.Info().Str("project", project).Msg("Got version")
context.String(http.StatusOK, "%s", version.String())
}
// OnTransientPatch is a handler for a transient patch bump
func (handler *Handler) OnTransientPatch(context *gin.Context) {
version := context.Param("version")
bumpedVersion, err := handler.versionManager.BumpTransientPatch(version)
if err != nil {
_ = context.AbortWithError(http.StatusInternalServerError, err)
return
}
log.Info().Str("version", bumpedVersion.String()).Msg("Bumped patch version transiently")
context.String(http.StatusOK, "%s", bumpedVersion.String())
}
// OnTransientMinor is a handler for a transient minor bump
func (handler *Handler) OnTransientMinor(context *gin.Context) {
version := context.Param("version")
bumpedVersion, err := handler.versionManager.BumpTransientMinor(version)
if err != nil {
_ = context.AbortWithError(http.StatusInternalServerError, err)
return
}
log.Info().Str("version", bumpedVersion.String()).Msg("Bumped minor version transiently")
context.String(http.StatusOK, "%s", bumpedVersion.String())
}