Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions examples/sonar-scan-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ should be checked using a policy.
## Environment variables
- `SONAR_TOKEN` - The sonar server token.
- `SONAR_TYPE` - Should be Either SAAS or SELFHOSTED, defaulting to SAAS.
- `SONAR_HOST` - The sonar server host name, for example sonar.myconpany.org. required for SELFHOSTED type, if not provided for SAAS type sonarcloud.io is used as default.
- `SONAR_HOST_URL` - The sonar server host name, for example sonar.myconpany.org. required for SELFHOSTED type, if not provided for SAAS type sonarcloud.io is used as default.
- `SONAR_PROXY_URL` - The proxy server URL, in the format of http://your-proxy-server:port. or https://username:password@your-proxy-server:port

## Arguments
`--reportTaskFile=<path>` - The path to the sonar report task file.

`--FailOnAnalysisFailure` - Fail with exit code 1 if the sonar analysis failed in sonar quality gate.


`--WaitTime=<seconds>` - between sonar analysis results checks>
`--MaxRetries=<number>` - The maximum number of retries to check the sonar analysis results.
`--UseProxy` - Use a proxy server URL, requires PROXY_URL environment variable to be set.

## The example is based on the following steps:
1. set sonar token as an environment variable
2. call sonar scan
Expand Down
14 changes: 11 additions & 3 deletions examples/sonar-scan-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func main() {
failOnAnalysisFailure := false
maxRetries := 1
waitTime := 5
proxyURL := ""
if len(os.Args) > 0 {
// loop over all args
for i, arg := range os.Args {
Expand All @@ -123,6 +124,13 @@ func main() {
logger.Println("Invalid wait time argument:",waitTimeStr , "error:" ,err)
os.Exit(1)
}
} else if strings.HasPrefix(arg, "--UseProxy") {
proxyURL = os.Getenv("SONAR_PROXY_URL")

if proxyURL == "" {
logger.Println("SONAR_PROXY_URL not found, set SONAR_PROXY_URL variable when using --UseProxy argument")
os.Exit(1)
}
}
}
logger.Println("reportTaskFile:", reportTaskFile)
Expand All @@ -132,7 +140,7 @@ func main() {
}
response := SonarResponse{}
defaultSonarHost := "sonarcloud.io"
sonarHost := os.Getenv("SONAR_HOST")
sonarHost := os.Getenv("SONAR_HOST_URL")

if sonar_type == "SAAS" {
if sonarHost == "" {
Expand All @@ -142,12 +150,12 @@ func main() {

}else if sonar_type == "SELFHOSTED" {
if sonarHost == "" {
logger.Println("Sonar host not found, set SONAR_HOST variable")
logger.Println("Sonar host not found, set SONAR_HOST_URL variable")
os.Exit(1)
}
logger.Println("Running sonar analysis extraction for " , sonar_type, " server", sonarHost)
}
response, err = runReport(ctx, logger, sonarHost, sonar_token, reportTaskFile, failOnAnalysisFailure, maxRetries, waitTime)
response, err = runReport(ctx, logger, sonarHost, proxyURL, sonar_token, reportTaskFile, failOnAnalysisFailure, maxRetries, waitTime)

if err != nil {
logger.Println("Error in generating report predicate:", err)
Expand Down
35 changes: 28 additions & 7 deletions examples/sonar-scan-example/sonar-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
"time"
"bufio"
"strings"
"net/url"
)

const (
SELFHOSTED_ANALYSIS_URL = "https://$sonarhost/api/qualitygates/project_status?analysisId=$analysisId"

)
func runReport(ctx context.Context, logger *log.Logger, sonarHost string , sonar_token string, reportTaskFile string, failOnAnalysisFailure bool, maxRetries int, waitTime int) (SonarResponse, error) {
func runReport(ctx context.Context, logger *log.Logger, sonarHost string, proxyURL string, sonar_token string, reportTaskFile string, failOnAnalysisFailure bool, maxRetries int, waitTime int) (SonarResponse, error) {
logger.Println("Running sonar analysis extraction")

// fmt.Println("reportTaskFile: ", reportTaskFile)
Expand Down Expand Up @@ -56,15 +57,35 @@ func runReport(ctx context.Context, logger *log.Logger, sonarHost string , sona
fmt.Printf("ceTaskUrl Key not found")
return SonarResponse{}, fmt.Errorf("ceTaskUrl Key not found")
}
// Add a reusable HTTP client
var client = &http.Client{
Timeout: DEFAULT_HTTP_TIMEOUT,
Transport: &http.Transport{

// Add a reusable HTTP client
var transport *http.Transport

if proxyURL != "" {
logger.Println("Using proxy url")
proxy, err := url.Parse(proxyURL)
if err != nil {
log.Fatal(err)
}
transport = &http.Transport{
Proxy: http.ProxyURL(proxy),
MaxIdleConns: 100,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
}
}else {
transport = &http.Transport{
MaxIdleConns: 100,
IdleConnTimeout: 10 * time.Second,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
},
}
}

var client = &http.Client{
Timeout: DEFAULT_HTTP_TIMEOUT,
Transport: transport,
}

logger.Println("ceTaskUrl", ceTaskUrl)
// get the report task
retries := 0
Expand Down