2021-07-12 16:58:48 +02:00
|
|
|
package sonarqube_sdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2021-10-03 17:57:22 +02:00
|
|
|
"fmt"
|
2021-07-12 16:58:48 +02:00
|
|
|
"io"
|
2021-10-03 17:57:22 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-07-12 16:58:48 +02:00
|
|
|
|
|
|
|
"gitea-sonarqube-pr-bot/internal/settings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SonarQubeSdkInterface interface {
|
|
|
|
GetMeasures(string, string) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SonarQubeSdk struct {
|
2021-10-03 17:57:22 +02:00
|
|
|
client *http.Client
|
2021-07-12 16:58:48 +02:00
|
|
|
baseUrl string
|
2021-10-03 17:57:22 +02:00
|
|
|
token string
|
2021-07-12 16:58:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2021-10-03 17:57:22 +02:00
|
|
|
client: &http.Client{},
|
2021-07-12 16:58:48 +02:00
|
|
|
baseUrl: settings.SonarQube.Url,
|
2021-10-03 17:57:22 +02:00
|
|
|
token: settings.SonarQube.Token.Value,
|
2021-07-12 16:58:48 +02:00
|
|
|
}
|
|
|
|
}
|