af38a31bbe
The current code base regarding API entrypoint is not testable as it directly connects to Gitea when creating the API endpoints. This prevented my from writing tests in the past for that part. As the SonarQube quality gate broke due to changes in the API entrypoint logic, tests are now required to satisfy the quality gate. Therefore, the instantiation of the API handlers is now decoupled from building the bot API endpoints and follows the same interface wrapper strategy as used for the Gitea API client. This makes it testable. Now, tests are written for the most parts of the API entrypoint. I've also noticed that there was much overhead within the tests for a non-implemented function `fetchDetails`. So I dropped that function for now. Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
|
|
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea"
|
|
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
|
|
webhook "gitea-sonarqube-pr-bot/internal/webhooks/gitea"
|
|
)
|
|
|
|
type GiteaWebhookHandlerInferface interface {
|
|
HandleSynchronize(rw http.ResponseWriter, r *http.Request)
|
|
HandleComment(rw http.ResponseWriter, r *http.Request)
|
|
}
|
|
|
|
type GiteaWebhookHandler struct {
|
|
giteaSdk giteaSdk.GiteaSdkInterface
|
|
sqSdk sqSdk.SonarQubeSdkInterface
|
|
}
|
|
|
|
func (h *GiteaWebhookHandler) parseBody(rw http.ResponseWriter, r *http.Request) ([]byte, error) {
|
|
if r.Body != nil {
|
|
defer r.Body.Close()
|
|
}
|
|
|
|
raw, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
log.Printf("Error reading request body %s", err.Error())
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
io.WriteString(rw, fmt.Sprintf(`{"message": "%s"}`, err.Error()))
|
|
return nil, err
|
|
}
|
|
|
|
return raw, nil
|
|
}
|
|
|
|
func (h *GiteaWebhookHandler) HandleSynchronize(rw http.ResponseWriter, r *http.Request) {
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
|
|
raw, err := h.parseBody(rw, r)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
w, ok := webhook.NewPullWebhook(raw)
|
|
if !ok {
|
|
rw.WriteHeader(http.StatusUnprocessableEntity)
|
|
io.WriteString(rw, `{"message": "Error parsing POST body."}`)
|
|
return
|
|
}
|
|
|
|
if err := w.Validate(); err != nil {
|
|
rw.WriteHeader(http.StatusOK)
|
|
io.WriteString(rw, fmt.Sprintf(`{"message": "%s"}`, err.Error()))
|
|
return
|
|
}
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
io.WriteString(rw, `{"message": "Processing data. See bot logs for details."}`)
|
|
|
|
w.ProcessData(h.giteaSdk, h.sqSdk)
|
|
}
|
|
|
|
func (h *GiteaWebhookHandler) HandleComment(rw http.ResponseWriter, r *http.Request) {
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
|
|
raw, err := h.parseBody(rw, r)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
w, ok := webhook.NewCommentWebhook(raw)
|
|
if !ok {
|
|
rw.WriteHeader(http.StatusUnprocessableEntity)
|
|
io.WriteString(rw, `{"message": "Error parsing POST body."}`)
|
|
return
|
|
}
|
|
|
|
if err := w.Validate(); err != nil {
|
|
rw.WriteHeader(http.StatusOK)
|
|
io.WriteString(rw, fmt.Sprintf(`{"message": "%s"}`, err.Error()))
|
|
return
|
|
}
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
io.WriteString(rw, `{"message": "Processing data. See bot logs for details."}`)
|
|
|
|
w.ProcessData(h.giteaSdk, h.sqSdk)
|
|
}
|
|
|
|
func NewGiteaWebhookHandler(g giteaSdk.GiteaSdkInterface, sq sqSdk.SonarQubeSdkInterface) GiteaWebhookHandlerInferface {
|
|
return &GiteaWebhookHandler{
|
|
giteaSdk: g,
|
|
sqSdk: sq,
|
|
}
|
|
}
|