diff --git a/examples/sonar-scan-example/README.md b/examples/sonar-scan-example/README.md index 929b4e5..cb00b89 100644 --- a/examples/sonar-scan-example/README.md +++ b/examples/sonar-scan-example/README.md @@ -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=` - The path to the sonar report task file. - `--FailOnAnalysisFailure` - Fail with exit code 1 if the sonar analysis failed in sonar quality gate. - - +`--WaitTime=` - between sonar analysis results checks> +`--MaxRetries=` - 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 diff --git a/examples/sonar-scan-example/main.go b/examples/sonar-scan-example/main.go index 10cfc74..9275ffe 100644 --- a/examples/sonar-scan-example/main.go +++ b/examples/sonar-scan-example/main.go @@ -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 { @@ -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) @@ -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 == "" { @@ -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) diff --git a/examples/sonar-scan-example/sonar-helper.go b/examples/sonar-scan-example/sonar-helper.go index ca4a2f1..5801442 100644 --- a/examples/sonar-scan-example/sonar-helper.go +++ b/examples/sonar-scan-example/sonar-helper.go @@ -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) @@ -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