Fetch raw measures from SonarQube
Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
This commit is contained in:
parent
a0f2684029
commit
1dab92385f
|
@ -2,7 +2,6 @@ package gitea_sdk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"gitea-sonarqube-pr-bot/internal/settings"
|
"gitea-sonarqube-pr-bot/internal/settings"
|
||||||
"code.gitea.io/sdk/gitea"
|
"code.gitea.io/sdk/gitea"
|
||||||
)
|
)
|
||||||
|
@ -20,7 +19,6 @@ func (sdk *GiteaSdk) PostComment(repo settings.GiteaRepository, idx int, msg str
|
||||||
Body: msg,
|
Body: msg,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Owner: %s | Repo: %s | Index: %d | Options: %s", repo.Owner, repo.Name, idx, opt)
|
|
||||||
_, _, err := sdk.client.CreateIssueComment(repo.Owner, repo.Name, int64(idx), opt)
|
_, _, err := sdk.client.CreateIssueComment(repo.Owner, repo.Name, int64(idx), opt)
|
||||||
|
|
||||||
return err
|
return err
|
50
internal/clients/sonarqube_sdk/sonarqube_sdk.go
Normal file
50
internal/clients/sonarqube_sdk/sonarqube_sdk.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package sonarqube_sdk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"encoding/base64"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"gitea-sonarqube-pr-bot/internal/settings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SonarQubeSdkInterface interface {
|
||||||
|
GetMeasures(string, string) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type SonarQubeSdk struct {
|
||||||
|
client *http.Client
|
||||||
|
baseUrl string
|
||||||
|
token string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sdk *SonarQubeSdk) GetMeasures(project string, branch string) (string, error) {
|
||||||
|
url := fmt.Sprintf("%s/api/measures/component?additionalFields=metrics&metricKeys=bugs,vulnerabilities,new_security_hotspots,violations&component=%s&pullRequest=%s", sdk.baseUrl, project, branch)
|
||||||
|
log.Println(url)
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("Cannot initialize Request: %w", err))
|
||||||
|
}
|
||||||
|
req.Header.Add("Authorization", sdk.basicAuth())
|
||||||
|
resp, _ := sdk.client.Do(req)
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
return string(body), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sdk *SonarQubeSdk) basicAuth() string {
|
||||||
|
auth := []byte(fmt.Sprintf("%s:", sdk.token))
|
||||||
|
return fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(auth))
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *SonarQubeSdk {
|
||||||
|
return &SonarQubeSdk{
|
||||||
|
client: &http.Client{},
|
||||||
|
baseUrl: settings.SonarQube.Url,
|
||||||
|
token: settings.SonarQube.Token.Value,
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,7 +10,8 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
sdk "gitea-sonarqube-pr-bot/internal/gitea_sdk"
|
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea_sdk"
|
||||||
|
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube_sdk"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
@ -23,7 +24,7 @@ func Serve(c *cli.Context) error {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/hooks/sonarqube", NewSonarQubeWebhookHandler(sdk.New()).Handle).Methods("POST").Headers("X-SonarQube-Project", "")
|
r.HandleFunc("/hooks/sonarqube", NewSonarQubeWebhookHandler(giteaSdk.New(), sqSdk.New()).Handle).Methods("POST").Headers("X-SonarQube-Project", "")
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: "0.0.0.0:8080",
|
Addr: "0.0.0.0:8080",
|
||||||
|
|
|
@ -9,17 +9,42 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea-sonarqube-pr-bot/internal/settings"
|
"gitea-sonarqube-pr-bot/internal/settings"
|
||||||
sdk "gitea-sonarqube-pr-bot/internal/gitea_sdk"
|
giteaSdk "gitea-sonarqube-pr-bot/internal/clients/gitea_sdk"
|
||||||
|
sqSdk "gitea-sonarqube-pr-bot/internal/clients/sonarqube_sdk"
|
||||||
webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube"
|
webhook "gitea-sonarqube-pr-bot/internal/webhooks/sonarqube"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SonarQubeWebhookHandler struct {
|
type SonarQubeWebhookHandler struct {
|
||||||
fetchDetails func(w *webhook.Webhook)
|
fetchDetails func(w *webhook.Webhook)
|
||||||
giteaSdk sdk.GiteaSdkInterface
|
giteaSdk giteaSdk.GiteaSdkInterface
|
||||||
|
sqSdk sqSdk.SonarQubeSdkInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SonarQubeWebhookHandler) composeGiteaComment(w *webhook.Webhook) string {
|
func (h *SonarQubeWebhookHandler) composeGiteaComment(w *webhook.Webhook) string {
|
||||||
return fmt.Sprintf("Hello from pr-bot. SonarQube data for '%s' has been processed.", w.Project.Key)
|
a, _ := h.sqSdk.GetMeasures(w.Project.Key, w.Branch.Name)
|
||||||
|
|
||||||
|
log.Println(a)
|
||||||
|
|
||||||
|
status := ":white_check_mark:"
|
||||||
|
if w.QualityGate.Status != "OK" {
|
||||||
|
status = ":x:"
|
||||||
|
}
|
||||||
|
|
||||||
|
measures := `| Metric | Current |
|
||||||
|
| -------- | -------- |
|
||||||
|
| Bugs | 123 |
|
||||||
|
| Code Smells | 1 |
|
||||||
|
| Vulnerabilities | 1 |
|
||||||
|
`
|
||||||
|
|
||||||
|
msg := `**Quality Gate**: %s
|
||||||
|
|
||||||
|
**Measures**
|
||||||
|
|
||||||
|
%s
|
||||||
|
|
||||||
|
See [SonarQube](https://example.com/sonarqube/dashboard?id=pr-bot&pullRequest=PR-1) for details.`
|
||||||
|
return fmt.Sprintf(msg, status, measures)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ *SonarQubeWebhookHandler) inProjectsMapping(p []settings.Project, n string) (bool, int) {
|
func (_ *SonarQubeWebhookHandler) inProjectsMapping(p []settings.Project, n string) (bool, int) {
|
||||||
|
@ -86,6 +111,10 @@ func fetchDetails(w *webhook.Webhook) {
|
||||||
log.Printf("This method will load additional data from SonarQube based on PR %d", w.PRIndex)
|
log.Printf("This method will load additional data from SonarQube based on PR %d", w.PRIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSonarQubeWebhookHandler(giteaSdk sdk.GiteaSdkInterface) *SonarQubeWebhookHandler {
|
func NewSonarQubeWebhookHandler(g giteaSdk.GiteaSdkInterface, sq sqSdk.SonarQubeSdkInterface) *SonarQubeWebhookHandler {
|
||||||
return &SonarQubeWebhookHandler{fetchDetails, giteaSdk}
|
return &SonarQubeWebhookHandler{
|
||||||
|
fetchDetails: fetchDetails,
|
||||||
|
giteaSdk: g,
|
||||||
|
sqSdk: sq,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue