gitea-sonarqube-bot/internal/webhooks/gitea/webhook.go
justusbunsi de575605f9
Move sonarqube sdk files
Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
2021-10-10 16:43:37 +02:00

92 lines
2.2 KiB
Go

package gitea
import (
"encoding/json"
"fmt"
"log"
"strings"
"gitea-sonarqube-pr-bot/internal/actions"
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea"
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
"gitea-sonarqube-pr-bot/internal/settings"
)
type issue struct {
Number int64 `json:"number"`
Repository settings.GiteaRepository `json:"repository"`
}
type comment struct {
Body string `json:"body"`
}
type Webhook struct {
Action string `json:"action"`
IsPR bool `json:"is_pull"`
Issue issue `json:"issue"`
Comment comment `json:"comment"`
ConfiguredProject settings.Project
}
func (w *Webhook) inProjectsMapping(p []settings.Project) (bool, int) {
owner := w.Issue.Repository.Owner
name := w.Issue.Repository.Name
for idx, proj := range p {
if proj.Gitea.Owner == owner && proj.Gitea.Name == name {
return true, idx
}
}
return false, 0
}
func (w *Webhook) Validate() error {
if !w.IsPR {
return fmt.Errorf("ignore non-PR hook")
}
found, pIdx := w.inProjectsMapping(settings.Projects)
if !found {
return fmt.Errorf("ignore hook for non-configured project '%s/%s'", w.Issue.Repository.Owner, w.Issue.Repository.Name)
}
if w.Action != "created" {
return fmt.Errorf("ignore hook for action others than created")
}
if !strings.HasPrefix(w.Comment.Body, actions.ActionPrefix) {
return fmt.Errorf("ignore hook for non-bot action comment")
}
w.ConfiguredProject = settings.Projects[pIdx]
return nil
}
func (w *Webhook) ProcessData(gSDK giteaSdk.GiteaSdkInterface, sqSDK sqSdk.SonarQubeSdkInterface) {
headRef, err := gSDK.DetermineHEAD(w.ConfiguredProject.Gitea, w.Issue.Number)
if err != nil {
log.Printf("Error retrieving HEAD ref: %s", err.Error())
return
}
log.Printf("Fetching SonarQube data...")
_ = gSDK.UpdateStatus(w.ConfiguredProject.Gitea, headRef, giteaSdk.StatusDetails{
Url: "",
Message: "OK",
State: giteaSdk.StatusOK,
})
}
func New(raw []byte) (*Webhook, bool) {
w := &Webhook{}
err := json.Unmarshal(raw, &w)
if err != nil {
log.Printf("Error parsing Gitea webhook: %s", err.Error())
return w, false
}
return w, true
}