2021-10-10 16:18:14 +02:00
|
|
|
package api
|
2021-07-11 15:17:03 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-07-11 15:17:03 +02:00
|
|
|
)
|
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
type validSonarQubeEndpointHeader struct {
|
2021-10-10 13:07:10 +02:00
|
|
|
SonarQubeProject string `header:"X-SonarQube-Project" binding:"required"`
|
2021-10-09 21:48:51 +02:00
|
|
|
}
|
2021-07-11 15:17:03 +02:00
|
|
|
|
2022-05-21 18:21:05 +02:00
|
|
|
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) {
|
2021-10-09 21:48:51 +02:00
|
|
|
h := validSonarQubeEndpointHeader{}
|
|
|
|
|
|
|
|
if err := c.ShouldBindHeader(&h); err != nil {
|
|
|
|
c.Status(http.StatusNotFound)
|
|
|
|
return
|
2021-07-11 15:17:03 +02:00
|
|
|
}
|
|
|
|
|
2022-07-11 15:24:43 +02:00
|
|
|
status, response := s.sonarQubeWebhookHandler.Handle(c.Request)
|
|
|
|
c.JSON(status, gin.H{
|
|
|
|
"message": response,
|
|
|
|
})
|
2022-05-21 18:21:05 +02:00
|
|
|
}).POST("/hooks/gitea", func(c *gin.Context) {
|
2021-10-10 17:17:29 +02:00
|
|
|
h := validGiteaEndpointHeader{}
|
|
|
|
|
|
|
|
if err := c.ShouldBindHeader(&h); err != nil {
|
|
|
|
c.Status(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-11 15:24:43 +02:00
|
|
|
var status int
|
|
|
|
var response string
|
|
|
|
|
2021-10-10 17:17:29 +02:00
|
|
|
switch h.GiteaEvent {
|
|
|
|
case "pull_request":
|
2022-07-11 15:24:43 +02:00
|
|
|
status, response = s.giteaWebhookHandler.HandleSynchronize(c.Request)
|
2021-10-10 17:17:29 +02:00
|
|
|
case "issue_comment":
|
2022-07-11 15:24:43 +02:00
|
|
|
status, response = s.giteaWebhookHandler.HandleComment(c.Request)
|
2021-10-10 17:17:29 +02:00
|
|
|
default:
|
2022-07-11 15:24:43 +02:00
|
|
|
status = http.StatusOK
|
|
|
|
response = "ignore unknown event"
|
2021-10-10 17:17:29 +02:00
|
|
|
}
|
2022-07-11 15:24:43 +02:00
|
|
|
|
|
|
|
c.JSON(status, gin.H{
|
|
|
|
"message": response,
|
|
|
|
})
|
2021-10-10 15:58:03 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-21 18:21:05 +02:00
|
|
|
func New(giteaHandler GiteaWebhookHandlerInferface, sonarQubeHandler SonarQubeWebhookHandlerInferface) *ApiServer {
|
|
|
|
s := &ApiServer{
|
|
|
|
Engine: gin.New(),
|
|
|
|
giteaWebhookHandler: giteaHandler,
|
|
|
|
sonarQubeWebhookHandler: sonarQubeHandler,
|
|
|
|
}
|
2021-07-11 15:17:03 +02:00
|
|
|
|
2022-05-21 18:21:05 +02:00
|
|
|
s.setup()
|
2021-10-11 08:50:50 +02:00
|
|
|
|
2022-05-21 18:21:05 +02:00
|
|
|
return s
|
2021-07-11 15:17:03 +02:00
|
|
|
}
|