2021-06-24 14:38:04 +02:00
|
|
|
package sonarqube
|
|
|
|
|
|
|
|
import (
|
2021-10-11 14:21:17 +02:00
|
|
|
"encoding/json"
|
2021-06-24 14:38:04 +02:00
|
|
|
"log"
|
2021-10-10 19:01:48 +02:00
|
|
|
|
2022-07-12 16:57:12 +02:00
|
|
|
sqSdk "codeberg.org/justusbunsi/gitea-sonarqube-bot/internal/clients/sonarqube"
|
2021-06-24 14:38:04 +02:00
|
|
|
)
|
|
|
|
|
2022-07-11 15:53:28 +02:00
|
|
|
type properties struct {
|
|
|
|
OriginalCommit string `json:"sonar.analysis.sqbot,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-06-24 14:38:04 +02:00
|
|
|
type Webhook struct {
|
2021-10-11 14:21:17 +02:00
|
|
|
ServerUrl string `json:"serverUrl"`
|
|
|
|
Revision string `json:"revision"`
|
2021-10-03 17:57:22 +02:00
|
|
|
Project struct {
|
2021-10-11 14:21:17 +02:00
|
|
|
Key string `json:"key"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
} `json:"project"`
|
2021-06-29 11:59:49 +02:00
|
|
|
Branch struct {
|
2021-10-11 14:21:17 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
} `json:"branch"`
|
2021-06-29 11:59:49 +02:00
|
|
|
QualityGate struct {
|
2021-10-11 14:21:17 +02:00
|
|
|
Status string `json:"status"`
|
2021-06-29 11:59:49 +02:00
|
|
|
Conditions []struct {
|
|
|
|
Metric string
|
|
|
|
Status string
|
2021-10-11 14:21:17 +02:00
|
|
|
} `json:"conditions"`
|
|
|
|
} `json:"qualityGate"`
|
2022-07-11 15:53:28 +02:00
|
|
|
Properties *properties `json:"properties,omitempty"`
|
|
|
|
PRIndex int
|
2021-07-11 13:54:32 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 14:21:17 +02:00
|
|
|
func (w *Webhook) GetRevision() string {
|
|
|
|
if w.Properties != nil && w.Properties.OriginalCommit != "" {
|
|
|
|
return w.Properties.OriginalCommit
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.Revision
|
|
|
|
}
|
2021-06-24 14:38:04 +02:00
|
|
|
|
2021-10-11 14:21:17 +02:00
|
|
|
func New(raw []byte) (*Webhook, bool) {
|
2021-06-29 11:59:49 +02:00
|
|
|
w := &Webhook{}
|
2021-06-24 14:38:04 +02:00
|
|
|
|
2021-10-11 14:21:17 +02:00
|
|
|
err := json.Unmarshal(raw, w)
|
2021-06-24 14:38:04 +02:00
|
|
|
if err != nil {
|
2021-10-03 17:57:22 +02:00
|
|
|
log.Printf("Error parsing SonarQube webhook: %s", err.Error())
|
2021-06-29 11:59:49 +02:00
|
|
|
return w, false
|
2021-06-24 14:38:04 +02:00
|
|
|
}
|
|
|
|
|
2021-10-10 19:01:48 +02:00
|
|
|
idx, err1 := sqSdk.ParsePRIndex(w.Branch.Name)
|
2021-07-12 12:36:22 +02:00
|
|
|
if err1 != nil {
|
|
|
|
log.Printf("Error parsing PR index: %s", err1.Error())
|
|
|
|
return w, false
|
|
|
|
}
|
|
|
|
|
|
|
|
w.PRIndex = idx
|
|
|
|
|
2021-06-29 11:59:49 +02:00
|
|
|
return w, true
|
2021-06-24 14:38:04 +02:00
|
|
|
}
|