Implement skeleton for different webhook handler

Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
This commit is contained in:
justusbunsi 2021-10-10 17:32:25 +02:00
parent 5b72ee7bc0
commit 56f7a1081b
No known key found for this signature in database
GPG key ID: 990B348ECAC9C7DB
2 changed files with 26 additions and 5 deletions

View file

@ -17,9 +17,7 @@ type GiteaWebhookHandler struct {
sqSdk sqSdk.SonarQubeSdkInterface
}
func (h *GiteaWebhookHandler) Handle(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
func (h *GiteaWebhookHandler) parseBody(rw http.ResponseWriter, r *http.Request) ([]byte, error) {
if r.Body != nil {
defer r.Body.Close()
}
@ -30,6 +28,29 @@ func (h *GiteaWebhookHandler) Handle(rw http.ResponseWriter, r *http.Request) {
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")
_, err := h.parseBody(rw, r)
if err != nil {
return
}
rw.WriteHeader(http.StatusOK)
io.WriteString(rw, `{"message": "Processing data. See bot logs for details."}`)
}
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
}

View file

@ -54,9 +54,9 @@ func addGiteaEndpoint(r *gin.Engine) {
switch h.GiteaEvent {
case "pull_request":
fmt.Println("Pull Request activity")
webhookHandler.HandleSynchronize(c.Writer, c.Request)
case "issue_comment":
webhookHandler.Handle(c.Writer, c.Request)
webhookHandler.HandleComment(c.Writer, c.Request)
default:
c.JSON(http.StatusOK, gin.H{
"message": "ignore unknown event",