gitea-sonarqube-bot/internal/api/gitea.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

97 lines
2.4 KiB
Go

package api
import (
"io/ioutil"
"log"
"net/http"
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea"
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
"gitea-sonarqube-pr-bot/internal/settings"
webhook "gitea-sonarqube-pr-bot/internal/webhooks/gitea"
)
type GiteaWebhookHandlerInferface interface {
HandleSynchronize(r *http.Request) (int, string)
HandleComment(r *http.Request) (int, string)
}
type GiteaWebhookHandler struct {
giteaSdk giteaSdk.GiteaSdkInterface
sqSdk sqSdk.SonarQubeSdkInterface
}
func (h *GiteaWebhookHandler) parseBody(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())
return nil, err
}
return raw, nil
}
func (h *GiteaWebhookHandler) HandleSynchronize(r *http.Request) (int, string) {
raw, err := h.parseBody(r)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
ok, err := isValidWebhook(raw, settings.Gitea.Webhook.Secret, r.Header.Get("X-Gitea-Signature"), "Gitea")
if !ok {
log.Print(err.Error())
return http.StatusPreconditionFailed, "Webhook validation failed. Request rejected."
}
w, ok := webhook.NewPullWebhook(raw)
if !ok {
return http.StatusUnprocessableEntity, "Error parsing POST body."
}
if err := w.Validate(); err != nil {
return http.StatusOK, err.Error()
}
w.ProcessData(h.giteaSdk, h.sqSdk)
return http.StatusOK, "Processing data. See bot logs for details."
}
func (h *GiteaWebhookHandler) HandleComment(r *http.Request) (int, string) {
raw, err := h.parseBody(r)
if err != nil {
return http.StatusInternalServerError, err.Error()
}
ok, err := isValidWebhook(raw, settings.Gitea.Webhook.Secret, r.Header.Get("X-Gitea-Signature"), "Gitea")
if !ok {
log.Print(err.Error())
return http.StatusPreconditionFailed, "Webhook validation failed. Request rejected."
}
w, ok := webhook.NewCommentWebhook(raw)
if !ok {
return http.StatusUnprocessableEntity, "Error parsing POST body."
}
if err := w.Validate(); err != nil {
return http.StatusOK, err.Error()
}
w.ProcessData(h.giteaSdk, h.sqSdk)
return http.StatusOK, "Processing data. See bot logs for details."
}
func NewGiteaWebhookHandler(g giteaSdk.GiteaSdkInterface, sq sqSdk.SonarQubeSdkInterface) GiteaWebhookHandlerInferface {
return &GiteaWebhookHandler{
giteaSdk: g,
sqSdk: sq,
}
}