gitea-sonarqube-bot/internal/api/main.go
justusbunsi 525fa03065
Centralize API response handling
Currently, all API handler functions take care of response code and
message on their own. This leads to a huge injection chain for HTTP
related objects.

This refactors the API to consistently return response code and message
to the API main entrypoint where the response is created and sent.

Now, SonarQube and Gitea will get a response at the very end of any bot
action for one request. SonarQube has a timeout of 10 seconds, which may
be reached due to network latency. We'll see.

Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
2022-07-11 15:24:43 +02:00

85 lines
1.9 KiB
Go

package api
import (
"net/http"
"github.com/gin-gonic/gin"
)
type validSonarQubeEndpointHeader struct {
SonarQubeProject string `header:"X-SonarQube-Project" binding:"required"`
}
type validGiteaEndpointHeader struct {
GiteaEvent string `header:"X-Gitea-Event" binding:"required"`
}
type ApiServer struct {
Engine *gin.Engine
sonarQubeWebhookHandler SonarQubeWebhookHandlerInferface
giteaWebhookHandler GiteaWebhookHandlerInferface
}
func (s *ApiServer) setup() {
s.Engine.Use(gin.Recovery())
s.Engine.Use(gin.LoggerWithConfig(gin.LoggerConfig{
SkipPaths: []string{"/ping", "/favicon.ico"},
}))
s.Engine.GET("/favicon.ico", func(c *gin.Context) {
c.Status(http.StatusNoContent)
}).GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
}).POST("/hooks/sonarqube", func(c *gin.Context) {
h := validSonarQubeEndpointHeader{}
if err := c.ShouldBindHeader(&h); err != nil {
c.Status(http.StatusNotFound)
return
}
status, response := s.sonarQubeWebhookHandler.Handle(c.Request)
c.JSON(status, gin.H{
"message": response,
})
}).POST("/hooks/gitea", func(c *gin.Context) {
h := validGiteaEndpointHeader{}
if err := c.ShouldBindHeader(&h); err != nil {
c.Status(http.StatusNotFound)
return
}
var status int
var response string
switch h.GiteaEvent {
case "pull_request":
status, response = s.giteaWebhookHandler.HandleSynchronize(c.Request)
case "issue_comment":
status, response = s.giteaWebhookHandler.HandleComment(c.Request)
default:
status = http.StatusOK
response = "ignore unknown event"
}
c.JSON(status, gin.H{
"message": response,
})
})
}
func New(giteaHandler GiteaWebhookHandlerInferface, sonarQubeHandler SonarQubeWebhookHandlerInferface) *ApiServer {
s := &ApiServer{
Engine: gin.New(),
giteaWebhookHandler: giteaHandler,
sonarQubeWebhookHandler: sonarQubeHandler,
}
s.setup()
return s
}