1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

Add proxy config to sonar scan step (#4333)

* Add proxy config for sonar scan step

Update sonar.go

Import fmt

Update sonar.go

Use serverUrl from config

Update sonarExecuteScan.go

Add proxy param

Add proxy check

Update sonarExecuteScan.go

Update sonarExecuteScan.go

Update http.go

Update sonarExecuteScan.go

Update sonarExecuteScan.go

Add env variable

Fix typo

Fix string

Split host port

Typo

Remove echoes

* Code review change

* Refactor

* Update cmd/sonarExecuteScan.go

Co-authored-by: dimitrij-afonitschkin <131276293+dimitrij-afonitschkin@users.noreply.github.com>

* Add proxy config for sonar scan step

Update sonar.go

Import fmt

Update sonar.go

Use serverUrl from config

Update sonarExecuteScan.go

Add proxy param

Add proxy check

Update sonarExecuteScan.go

Update sonarExecuteScan.go

Update http.go

Update sonarExecuteScan.go

Update sonarExecuteScan.go

Add env variable

Fix typo

Fix string

Split host port

Typo

Remove echoes

* Code review change

* Refactor

* Update cmd/sonarExecuteScan.go

Co-authored-by: dimitrij-afonitschkin <131276293+dimitrij-afonitschkin@users.noreply.github.com>

* Add compatability to other usecases

---------

Co-authored-by: dimitrij-afonitschkin <131276293+dimitrij-afonitschkin@users.noreply.github.com>
This commit is contained in:
Srinikitha Kondreddy 2023-05-16 09:31:33 +02:00 committed by GitHub
parent 659cf9f988
commit 1d78ef35d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 5 deletions

View File

@ -1,7 +1,10 @@
package cmd
import (
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"os/exec"
"path"
@ -82,8 +85,27 @@ func sonarExecuteScan(config sonarExecuteScanOptions, _ *telemetry.CustomData, i
downloadClient.SetOptions(piperhttp.ClientOptions{TransportTimeout: 20 * time.Second})
// client for talking to the SonarQube API
apiClient := &piperhttp.Client{}
//TODO: implement certificate handling
apiClient.SetOptions(piperhttp.ClientOptions{TransportSkipVerification: true})
proxy := config.Proxy
if proxy != "" {
transportProxy, err := url.Parse(proxy)
if err != nil {
log.Entry().WithError(err).Fatalf("Failed to parse proxy string %v into a URL structure", proxy)
}
host, port, err := net.SplitHostPort(transportProxy.Host)
if err != nil {
log.Entry().WithError(err).Fatalf("Failed to retrieve host and port from the proxy URL")
}
// provide proxy setting for Java based Sonar scanner
javaToolOptions := fmt.Sprintf("-Dhttp.proxyHost=%v -Dhttp.proxyPort=%v", host, port)
os.Setenv("JAVA_TOOL_OPTIONS", javaToolOptions)
apiClient.SetOptions(piperhttp.ClientOptions{TransportProxy: transportProxy, TransportSkipVerification: true})
log.Entry().Infof("HTTP client instructed to use %v proxy", proxy)
} else {
//TODO: implement certificate handling
apiClient.SetOptions(piperhttp.ClientOptions{TransportSkipVerification: true})
}
sonar = sonarSettings{
workingDir: "./",
@ -195,6 +217,14 @@ func runSonar(config sonarExecuteScanOptions, client piperhttp.Downloader, runne
log.Entry().WithError(err).Warning("no scan report found")
return nil
}
var serverUrl string
if len(config.Proxy) > 0 {
serverUrl = config.ServerURL
} else {
serverUrl = taskReport.ServerURL
}
// write reports JSON
reports := []piperutils.Path{
{
@ -215,14 +245,14 @@ func runSonar(config sonarExecuteScanOptions, client piperhttp.Downloader, runne
log.Entry().Warn("no measurements are fetched due to missing credentials")
return nil
}
taskService := SonarUtils.NewTaskService(taskReport.ServerURL, config.Token, taskReport.TaskID, apiClient)
taskService := SonarUtils.NewTaskService(serverUrl, config.Token, taskReport.TaskID, apiClient)
// wait for analysis task to complete
err = taskService.WaitForTask()
if err != nil {
return err
}
// fetch number of issues by severity
issueService := SonarUtils.NewIssuesService(taskReport.ServerURL, config.Token, taskReport.ProjectKey, config.Organization, config.BranchName, config.ChangeID, apiClient)
issueService := SonarUtils.NewIssuesService(serverUrl, config.Token, taskReport.ProjectKey, config.Organization, config.BranchName, config.ChangeID, apiClient)
influx.sonarqube_data.fields.blocker_issues, err = issueService.GetNumberOfBlockerIssues()
if err != nil {
return err
@ -259,7 +289,7 @@ func runSonar(config sonarExecuteScanOptions, client piperhttp.Downloader, runne
Info: influx.sonarqube_data.fields.info_issues,
}}
componentService := SonarUtils.NewMeasuresComponentService(taskReport.ServerURL, config.Token, taskReport.ProjectKey, config.Organization, config.BranchName, config.ChangeID, apiClient)
componentService := SonarUtils.NewMeasuresComponentService(serverUrl, config.Token, taskReport.ProjectKey, config.Organization, config.BranchName, config.ChangeID, apiClient)
cov, err := componentService.GetCoverage()
if err != nil {
log.Entry().Warnf("failed to retrieve sonar coverage data: %v", err)

View File

@ -23,6 +23,7 @@ import (
type sonarExecuteScanOptions struct {
Instance string `json:"instance,omitempty"`
Proxy string `json:"proxy,omitempty"`
ServerURL string `json:"serverUrl,omitempty"`
Token string `json:"token,omitempty"`
Organization string `json:"organization,omitempty"`
@ -238,6 +239,7 @@ func SonarExecuteScanCommand() *cobra.Command {
func addSonarExecuteScanFlags(cmd *cobra.Command, stepConfig *sonarExecuteScanOptions) {
cmd.Flags().StringVar(&stepConfig.Instance, "instance", os.Getenv("PIPER_instance"), "Jenkins only: The name of the SonarQube instance defined in the Jenkins settings. DEPRECATED: use serverUrl parameter instead")
cmd.Flags().StringVar(&stepConfig.Proxy, "proxy", os.Getenv("PIPER_proxy"), "Proxy URL to be used for communication with the SonarQube instance.")
cmd.Flags().StringVar(&stepConfig.ServerURL, "serverUrl", os.Getenv("PIPER_serverUrl"), "The URL to the Sonar backend.")
cmd.Flags().StringVar(&stepConfig.Token, "token", os.Getenv("PIPER_token"), "Token used to authenticate with the Sonar Server.")
cmd.Flags().StringVar(&stepConfig.Organization, "organization", os.Getenv("PIPER_organization"), "SonarCloud.io only: Organization that the project will be assigned to in SonarCloud.io.")
@ -292,6 +294,15 @@ func sonarExecuteScanMetadata() config.StepData {
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_instance"),
},
{
Name: "proxy",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STEPS", "STAGES"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
Default: os.Getenv("PIPER_proxy"),
},
{
Name: "serverUrl",
ResourceRef: []config.ResourceReference{},

View File

@ -23,6 +23,13 @@ spec:
- PARAMETERS
- STAGES
- STEPS
- name: proxy
type: string
description: Proxy URL to be used for communication with the SonarQube instance.
scope:
- PARAMETERS
- STEPS
- STAGES
- name: serverUrl
aliases:
- name: host