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 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
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" {
status = giteaSdk.StatusFailure
}
_ = h.giteaSdk.UpdateStatus(repo, w.Revision, giteaSdk.StatusDetails{
_ = h.giteaSdk.UpdateStatus(repo, w.GetRevision(), giteaSdk.StatusDetails{
Url: w.Branch.Url,
Message: w.QualityGate.Status,
State: status,

View file

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