From 0bd65d8a1d924e4110929c1a4d3fccd66c3299e2 Mon Sep 17 00:00:00 2001 From: justusbunsi <61625851+justusbunsi@users.noreply.github.com> Date: Mon, 11 Oct 2021 14:21:17 +0200 Subject: [PATCH] Allow override the provided revision Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com> --- README.md | 7 ++++ internal/api/sonarqube.go | 2 +- internal/webhooks/sonarqube/webhook.go | 47 ++++++++++++++------------ 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index d95fd7e..2fe28ee 100644 --- a/README.md +++ b/README.md @@ -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:///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. diff --git a/internal/api/sonarqube.go b/internal/api/sonarqube.go index 209640b..77fbf30 100644 --- a/internal/api/sonarqube.go +++ b/internal/api/sonarqube.go @@ -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, diff --git a/internal/webhooks/sonarqube/webhook.go b/internal/webhooks/sonarqube/webhook.go index 2836043..c5bfcce 100644 --- a/internal/webhooks/sonarqube/webhook.go +++ b/internal/webhooks/sonarqube/webhook.go @@ -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