2021-10-10 16:18:14 +02:00
|
|
|
package api
|
2021-07-11 15:17:03 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2021-10-10 16:28:59 +02:00
|
|
|
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea"
|
2021-07-12 16:58:48 +02:00
|
|
|
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube_sdk"
|
2021-10-09 18:09:54 +02:00
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
"github.com/fvbock/endless"
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-07-11 15:17:03 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
func addPingApi(r *gin.Engine) {
|
|
|
|
r.GET("/ping", func(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"message": "pong",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
func addSonarQubeEndpoint(r *gin.Engine) {
|
|
|
|
webhookHandler := NewSonarQubeWebhookHandler(giteaSdk.New(), sqSdk.New())
|
|
|
|
r.POST("/hooks/sonarqube", func(c *gin.Context) {
|
|
|
|
h := validSonarQubeEndpointHeader{}
|
|
|
|
|
|
|
|
if err := c.ShouldBindHeader(&h); err != nil {
|
|
|
|
c.Status(http.StatusNotFound)
|
|
|
|
return
|
2021-07-11 15:17:03 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
webhookHandler.Handle(c.Writer, c.Request)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-10 15:58:03 +02:00
|
|
|
func addGiteaEndpoint(r *gin.Engine) {
|
|
|
|
webhookHandler := NewGiteaWebhookHandler(giteaSdk.New(), sqSdk.New())
|
|
|
|
r.POST("/hooks/gitea", func(c *gin.Context) {
|
|
|
|
webhookHandler.Handle(c.Writer, c.Request)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
func Serve(c *cli.Context) error {
|
|
|
|
fmt.Println("Hi! I'm the Gitea-SonarQube-PR bot. At your service.")
|
2021-07-11 15:17:03 +02:00
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
r := gin.Default()
|
2021-07-11 15:17:03 +02:00
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
addPingApi(r)
|
|
|
|
addSonarQubeEndpoint(r)
|
2021-10-10 15:58:03 +02:00
|
|
|
addGiteaEndpoint(r)
|
2021-07-11 15:17:03 +02:00
|
|
|
|
2021-10-09 21:48:51 +02:00
|
|
|
return endless.ListenAndServe(":3000", r)
|
2021-07-11 15:17:03 +02:00
|
|
|
}
|