diff --git a/internal/api/sonarqube.go b/internal/api/sonarqube.go index 53e300e..209640b 100644 --- a/internal/api/sonarqube.go +++ b/internal/api/sonarqube.go @@ -8,7 +8,6 @@ import ( "net/http" "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" @@ -21,22 +20,6 @@ type SonarQubeWebhookHandler struct { sqSdk sqSdk.SonarQubeSdkInterface } -func (h *SonarQubeWebhookHandler) composeGiteaComment(w *webhook.Webhook) (string, error) { - m, err := h.sqSdk.GetMeasures(w.Project.Key, w.Branch.Name) - if err != nil { - return "", err - } - - message := make([]string, 5) - message[0] = w.GetRenderedQualityGate() - message[1] = m.GetRenderedMarkdownTable() - message[2] = fmt.Sprintf("See [SonarQube](%s) for details.", w.Branch.Url) - message[3] = "---" - message[4] = fmt.Sprintf("- If you want the bot to check again, post `%s`", actions.ActionReview) - - return strings.Join(message, "\n\n"), nil -} - func (*SonarQubeWebhookHandler) inProjectsMapping(p []settings.Project, n string) (bool, int) { for idx, proj := range p { if proj.SonarQube.Key == n { @@ -65,9 +48,13 @@ func (h *SonarQubeWebhookHandler) processData(w *webhook.Webhook, repo settings. State: status, }) - comment, err := h.composeGiteaComment(w) + comment, err := h.sqSdk.ComposeGiteaComment(&sqSdk.CommentComposeData{ + Key: w.Project.Key, + PRName: w.Branch.Name, + Url: w.Branch.Url, + QualityGate: w.QualityGate.Status, + }) if err != nil { - log.Printf("Error composing Gitea comment: %s", err.Error()) return } h.giteaSdk.PostComment(repo, w.PRIndex, comment) diff --git a/internal/api/sonarqube_test.go b/internal/api/sonarqube_test.go index 30f0a93..5d4a3fc 100644 --- a/internal/api/sonarqube_test.go +++ b/internal/api/sonarqube_test.go @@ -55,6 +55,10 @@ func (h *SQSdkMock) GetPullRequest(project string, index int64) (*sqSdk.PullRequ return nil, nil } +func (h *SQSdkMock) ComposeGiteaComment(data *sqSdk.CommentComposeData) (string, error) { + return "", nil +} + func defaultMockPreparation(h *HandlerPartialMock) { h.On("fetchDetails", mock.Anything).Return(nil) } diff --git a/internal/clients/sonarqube/sonarqube.go b/internal/clients/sonarqube/sonarqube.go index 628be0a..5565948 100644 --- a/internal/clients/sonarqube/sonarqube.go +++ b/internal/clients/sonarqube/sonarqube.go @@ -9,7 +9,9 @@ import ( "net/http" "regexp" "strconv" + "strings" + "gitea-sonarqube-pr-bot/internal/actions" "gitea-sonarqube-pr-bot/internal/settings" ) @@ -27,10 +29,27 @@ func PRNameFromIndex(index int64) string { return fmt.Sprintf("PR-%d", index) } +func GetRenderedQualityGate(qg string) string { + status := ":white_check_mark:" + if qg != "OK" { + status = ":x:" + } + + return fmt.Sprintf("**Quality Gate**: %s", status) +} + type SonarQubeSdkInterface interface { GetMeasures(string, string) (*MeasuresResponse, error) GetPullRequestUrl(string, int64) string GetPullRequest(string, int64) (*PullRequest, error) + ComposeGiteaComment(*CommentComposeData) (string, error) +} + +type CommentComposeData struct { + Key string + PRName string + Url string + QualityGate string } type SonarQubeSdk struct { @@ -104,6 +123,23 @@ func (sdk *SonarQubeSdk) GetMeasures(project string, branch string) (*MeasuresRe return response, nil } +func (sdk *SonarQubeSdk) ComposeGiteaComment(data *CommentComposeData) (string, error) { + m, err := sdk.GetMeasures(data.Key, data.PRName) + if err != nil { + log.Printf("Error composing Gitea comment: %s", err.Error()) + return "", err + } + + message := make([]string, 5) + message[0] = GetRenderedQualityGate(data.QualityGate) + message[1] = m.GetRenderedMarkdownTable() + message[2] = fmt.Sprintf("See [SonarQube](%s) for details.", data.Url) + message[3] = "---" + message[4] = fmt.Sprintf("- If you want the bot to check again, post `%s`", actions.ActionReview) + + return strings.Join(message, "\n\n"), nil +} + func (sdk *SonarQubeSdk) basicAuth() string { auth := []byte(fmt.Sprintf("%s:", sdk.token)) return fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(auth)) diff --git a/internal/webhooks/gitea/comment.go b/internal/webhooks/gitea/comment.go index 4e4eff6..51bf054 100644 --- a/internal/webhooks/gitea/comment.go +++ b/internal/webhooks/gitea/comment.go @@ -83,11 +83,24 @@ func (w *CommentWebhook) ProcessData(gSDK giteaSdk.GiteaSdkInterface, sqSDK sqSd status = giteaSdk.StatusFailure } + url := sqSDK.GetPullRequestUrl(w.ConfiguredProject.SonarQube.Key, w.Issue.Number) + _ = gSDK.UpdateStatus(w.ConfiguredProject.Gitea, headRef, giteaSdk.StatusDetails{ - Url: sqSDK.GetPullRequestUrl(w.ConfiguredProject.SonarQube.Key, w.Issue.Number), + Url: url, Message: pr.Status.QualityGateStatus, State: status, }) + + comment, err := sqSDK.ComposeGiteaComment(&sqSdk.CommentComposeData{ + Key: w.ConfiguredProject.SonarQube.Key, + PRName: sqSdk.PRNameFromIndex(w.Issue.Number), + Url: url, + QualityGate: pr.Status.QualityGateStatus, + }) + if err != nil { + return + } + gSDK.PostComment(w.ConfiguredProject.Gitea, int(w.Issue.Number), comment) } func NewCommentWebhook(raw []byte) (*CommentWebhook, bool) { diff --git a/internal/webhooks/sonarqube/webhook.go b/internal/webhooks/sonarqube/webhook.go index 5bcdd17..2836043 100644 --- a/internal/webhooks/sonarqube/webhook.go +++ b/internal/webhooks/sonarqube/webhook.go @@ -2,7 +2,6 @@ package sonarqube import ( "bytes" - "fmt" "log" sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube" @@ -33,15 +32,6 @@ type Webhook struct { PRIndex int } -func (w *Webhook) GetRenderedQualityGate() string { - status := ":white_check_mark:" - if w.QualityGate.Status != "OK" { - status = ":x:" - } - - return fmt.Sprintf("**Quality Gate**: %s", status) -} - func New(raw []byte) (*Webhook, bool) { v := viper.New() v.SetConfigType("json")