Post analysis details on bot action comment
Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
This commit is contained in:
parent
369edfcfae
commit
e28e524456
|
@ -8,7 +8,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea-sonarqube-pr-bot/internal/actions"
|
|
||||||
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea"
|
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea"
|
||||||
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
|
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
|
||||||
"gitea-sonarqube-pr-bot/internal/settings"
|
"gitea-sonarqube-pr-bot/internal/settings"
|
||||||
|
@ -21,22 +20,6 @@ type SonarQubeWebhookHandler struct {
|
||||||
sqSdk sqSdk.SonarQubeSdkInterface
|
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) {
|
func (*SonarQubeWebhookHandler) inProjectsMapping(p []settings.Project, n string) (bool, int) {
|
||||||
for idx, proj := range p {
|
for idx, proj := range p {
|
||||||
if proj.SonarQube.Key == n {
|
if proj.SonarQube.Key == n {
|
||||||
|
@ -65,9 +48,13 @@ func (h *SonarQubeWebhookHandler) processData(w *webhook.Webhook, repo settings.
|
||||||
State: status,
|
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 {
|
if err != nil {
|
||||||
log.Printf("Error composing Gitea comment: %s", err.Error())
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.giteaSdk.PostComment(repo, w.PRIndex, comment)
|
h.giteaSdk.PostComment(repo, w.PRIndex, comment)
|
||||||
|
|
|
@ -55,6 +55,10 @@ func (h *SQSdkMock) GetPullRequest(project string, index int64) (*sqSdk.PullRequ
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *SQSdkMock) ComposeGiteaComment(data *sqSdk.CommentComposeData) (string, error) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
func defaultMockPreparation(h *HandlerPartialMock) {
|
func defaultMockPreparation(h *HandlerPartialMock) {
|
||||||
h.On("fetchDetails", mock.Anything).Return(nil)
|
h.On("fetchDetails", mock.Anything).Return(nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,9 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea-sonarqube-pr-bot/internal/actions"
|
||||||
"gitea-sonarqube-pr-bot/internal/settings"
|
"gitea-sonarqube-pr-bot/internal/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,10 +29,27 @@ func PRNameFromIndex(index int64) string {
|
||||||
return fmt.Sprintf("PR-%d", index)
|
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 {
|
type SonarQubeSdkInterface interface {
|
||||||
GetMeasures(string, string) (*MeasuresResponse, error)
|
GetMeasures(string, string) (*MeasuresResponse, error)
|
||||||
GetPullRequestUrl(string, int64) string
|
GetPullRequestUrl(string, int64) string
|
||||||
GetPullRequest(string, int64) (*PullRequest, error)
|
GetPullRequest(string, int64) (*PullRequest, error)
|
||||||
|
ComposeGiteaComment(*CommentComposeData) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommentComposeData struct {
|
||||||
|
Key string
|
||||||
|
PRName string
|
||||||
|
Url string
|
||||||
|
QualityGate string
|
||||||
}
|
}
|
||||||
|
|
||||||
type SonarQubeSdk struct {
|
type SonarQubeSdk struct {
|
||||||
|
@ -104,6 +123,23 @@ func (sdk *SonarQubeSdk) GetMeasures(project string, branch string) (*MeasuresRe
|
||||||
return response, nil
|
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 {
|
func (sdk *SonarQubeSdk) basicAuth() string {
|
||||||
auth := []byte(fmt.Sprintf("%s:", sdk.token))
|
auth := []byte(fmt.Sprintf("%s:", sdk.token))
|
||||||
return fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(auth))
|
return fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(auth))
|
||||||
|
|
|
@ -83,11 +83,24 @@ func (w *CommentWebhook) ProcessData(gSDK giteaSdk.GiteaSdkInterface, sqSDK sqSd
|
||||||
status = giteaSdk.StatusFailure
|
status = giteaSdk.StatusFailure
|
||||||
}
|
}
|
||||||
|
|
||||||
|
url := sqSDK.GetPullRequestUrl(w.ConfiguredProject.SonarQube.Key, w.Issue.Number)
|
||||||
|
|
||||||
_ = gSDK.UpdateStatus(w.ConfiguredProject.Gitea, headRef, giteaSdk.StatusDetails{
|
_ = gSDK.UpdateStatus(w.ConfiguredProject.Gitea, headRef, giteaSdk.StatusDetails{
|
||||||
Url: sqSDK.GetPullRequestUrl(w.ConfiguredProject.SonarQube.Key, w.Issue.Number),
|
Url: url,
|
||||||
Message: pr.Status.QualityGateStatus,
|
Message: pr.Status.QualityGateStatus,
|
||||||
State: status,
|
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) {
|
func NewCommentWebhook(raw []byte) (*CommentWebhook, bool) {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package sonarqube
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
|
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube"
|
||||||
|
@ -33,15 +32,6 @@ type Webhook struct {
|
||||||
PRIndex int
|
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) {
|
func New(raw []byte) (*Webhook, bool) {
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
v.SetConfigType("json")
|
v.SetConfigType("json")
|
||||||
|
|
Loading…
Reference in a new issue