Allow override the provided revision

Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
This commit is contained in:
justusbunsi 2021-10-11 14:21:17 +02:00
parent bb156f95bf
commit 0bd65d8a1d
No known key found for this signature in database
GPG key ID: 990B348ECAC9C7DB
3 changed files with 34 additions and 22 deletions

View file

@ -43,6 +43,13 @@ Luckily, both endpoints have a proper REST API to communicate with each others.
- Create a token for this user that will be used by the bot. - Create a token for this user that will be used by the bot.
- Create a project/organization/system webhook pointing to `https://<bot-url>/gitea`. Consider securing it with a secret. - Create a project/organization/system webhook pointing to `https://<bot-url>/gitea`. Consider securing it with a secret.
**CI system**
- Some CI systems may emulate a merge and therefore produce another, not yet existing commit hash that is promoted to SonarQube.
This would cause the bot to fail to set the commit status in Gitea because the webhook sent by SonarQube contains that commit hash.
To mitigate that situation, the bot will look inside the `properties` object for the key `sonar.analysis.sqbot`. If available, this
key can contain the actual commit hash to use for updating the status in Gitea.
See [SonarQube docs](https://docs.sonarqube.org/latest/project-administration/webhooks) for details.
## Bot configuration ## Bot configuration
See [config.example.yaml](config/config.example.yaml) for a full configuration specification and description. See [config.example.yaml](config/config.example.yaml) for a full configuration specification and description.

View file

@ -42,7 +42,7 @@ func (h *SonarQubeWebhookHandler) processData(w *webhook.Webhook, repo settings.
if w.QualityGate.Status != "OK" { if w.QualityGate.Status != "OK" {
status = giteaSdk.StatusFailure status = giteaSdk.StatusFailure
} }
_ = h.giteaSdk.UpdateStatus(repo, w.Revision, giteaSdk.StatusDetails{ _ = h.giteaSdk.UpdateStatus(repo, w.GetRevision(), giteaSdk.StatusDetails{
Url: w.Branch.Url, Url: w.Branch.Url,
Message: w.QualityGate.Status, Message: w.QualityGate.Status,
State: status, State: status,

View file

@ -1,45 +1,50 @@
package sonarqube package sonarqube
import ( import (
"bytes" "encoding/json"
"log" "log"
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube" sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
"github.com/spf13/viper"
) )
type Webhook struct { type Webhook struct {
ServerUrl string `mapstructure:"serverUrl"` ServerUrl string `json:"serverUrl"`
Revision string Revision string `json:"revision"`
Project struct { Project struct {
Key string Key string `json:"key"`
Name string Name string `json:"name"`
Url string Url string `json:"url"`
} } `json:"project"`
Branch struct { Branch struct {
Name string Name string `json:"name"`
Type string Type string `json:"type"`
Url string Url string `json:"url"`
} } `json:"branch"`
QualityGate struct { QualityGate struct {
Status string Status string `json:"status"`
Conditions []struct { Conditions []struct {
Metric string Metric string
Status string Status string
} } `json:"conditions"`
} `mapstructure:"qualityGate"` } `json:"qualityGate"`
Properties *struct {
OriginalCommit string `json:"sonar.analysis.sqbot,omitempty"`
} `json:"properties,omitempty"`
PRIndex int PRIndex int
} }
func New(raw []byte) (*Webhook, bool) { func (w *Webhook) GetRevision() string {
v := viper.New() if w.Properties != nil && w.Properties.OriginalCommit != "" {
v.SetConfigType("json") return w.Properties.OriginalCommit
v.ReadConfig(bytes.NewBuffer(raw)) }
return w.Revision
}
func New(raw []byte) (*Webhook, bool) {
w := &Webhook{} w := &Webhook{}
err := v.Unmarshal(&w) err := json.Unmarshal(raw, w)
if err != nil { if err != nil {
log.Printf("Error parsing SonarQube webhook: %s", err.Error()) log.Printf("Error parsing SonarQube webhook: %s", err.Error())
return w, false return w, false