|
| 1 | +UPDATE plugin_pipeline_script SET script=E'PathToCodeDir=/devtroncd$CheckoutPath |
| 2 | +cd $PathToCodeDir |
| 3 | +if [[ -z "$UsePropertiesFileFromProject" || $UsePropertiesFileFromProject == false ]] |
| 4 | +then |
| 5 | + echo "sonar.projectKey=$SonarqubeProjectKey" > sonar-project.properties |
| 6 | +fi |
| 7 | +if [[ -z "$SonarContainerImage" ]] |
| 8 | +then |
| 9 | + SonarContainerImage="sonarsource/sonar-scanner-cli" |
| 10 | +fi |
| 11 | +docker run \\ |
| 12 | +--rm \\ |
| 13 | +-e SONAR_HOST_URL=$SonarqubeEndpoint \\ |
| 14 | +-e SONAR_LOGIN=$SonarqubeApiKey \\ |
| 15 | +-v "/$PWD:/usr/src" \\ |
| 16 | +$SonarContainerImage |
| 17 | +
|
| 18 | +if [[ $CheckForSonarAnalysisReport == true && ! -z "$CheckForSonarAnalysisReport" ]] |
| 19 | +then |
| 20 | + status=$(curl -u ${SonarqubeApiKey}: -sS ${SonarqubeEndpoint}/api/qualitygates/project_status?projectKey=${SonarqubeProjectKey}&branch=master) |
| 21 | + project_status=$(echo $status | jq -r ".projectStatus.status") |
| 22 | + echo "********* SonarQube Policy Report *********" |
| 23 | + echo $status |
| 24 | + if [[ $AbortPipelineOnPolicyCheckFailed == true && $project_status == "ERROR" ]] |
| 25 | + then |
| 26 | + echo "********* SonarQube Policy Violated *********" |
| 27 | + echo "********* Exiting Build *********" |
| 28 | + exit |
| 29 | + elif [[ $AbortPipelineOnPolicyCheckFailed == true && $project_status == "OK" ]] |
| 30 | + then |
| 31 | + echo "********* SonarQube Policy Passed *********" |
| 32 | + fi |
| 33 | +fi' WHERE id=(select script_id FROM plugin_step WHERE plugin_id=(SELECT id FROM plugin_metadata WHERE name='Sonarqube')); |
| 34 | +INSERT INTO plugin_step_variable (id,plugin_step_id,name,format,description,is_exposed,allow_empty_value,default_value,variable_type,value_type,previous_step_index,variable_step_index,variable_step_index_in_plugin,reference_variable_name,deleted,created_on,created_by,updated_on,updated_by) |
| 35 | +VALUES(nextval('id_seq_plugin_step_variable'),(SELECT id FROM plugin_metadata WHERE name='Sonarqube'),'SonarContainerImage','STRING','Container Image that will be used for sonar scanning purpose.','t','t','sonarsource/sonar-scanner-cli','INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1); |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +UPDATE plugin_pipeline_script SET script=E'#!/bin/sh |
| 46 | + repoName="" |
| 47 | + branchName="" |
| 48 | + # Define the function to extract repoName and branchName |
| 49 | + FetchRepoBranchNameFunction() { |
| 50 | + CiMaterialsRequests=$GIT_MATERIAL_REQUEST |
| 51 | + materials=$(echo $CiMaterialsRequests | tr "|" "\n") |
| 52 | + for material in $materials |
| 53 | + do |
| 54 | + # echo "material : $material" |
| 55 | + data=$(echo $material | tr "," "\n") |
| 56 | + # echo "data: $data" |
| 57 | + repo_name=$(echo "$data" | sed -n ''1p'') |
| 58 | + branch_name=$(echo "$data" | sed -n ''3p'') |
| 59 | + # echo Reponame: $repo_name and branchName: $branch_name |
| 60 | + repoName="${repoName}-$repo_name" |
| 61 | + branchName="${branchName}-$branch_name" |
| 62 | + done |
| 63 | + repoName="${repoName#-}" |
| 64 | + branchName="${branchName#-}" |
| 65 | + } |
| 66 | + GlobalSonarqubeProjectName="" |
| 67 | + GlobalSonarqubeBranchName="" |
| 68 | + # Define sonarqube scan function |
| 69 | + SonarqubeScanFunction() { |
| 70 | + echo -e "\n********** Starting the scanning ************" |
| 71 | + if [[ -z "$SonarContainerImage" ]] |
| 72 | + then |
| 73 | + SonarContainerImage="sonarsource/sonar-scanner-cli" |
| 74 | + fi |
| 75 | +
|
| 76 | + docker run --rm -e SONAR_HOST_URL=$SonarqubeEndpoint -e SONAR_LOGIN=$SonarqubeApiKey -v "/$PWD:/usr/src" $SonarContainerImage |
| 77 | + SonarScanStatusCode=$? |
| 78 | + echo -e "\nStatus code of sonarqube scanning command : $SonarScanStatusCode" |
| 79 | + if [ "$SonarScanStatusCode" -ne 0 ]; then |
| 80 | + echo -e "****** Sonarqube scanning command failed to run *********" |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | + if [[ $CheckForSonarAnalysisReport == true && ! -z "$CheckForSonarAnalysisReport" ]] |
| 84 | + then |
| 85 | + status=$(curl -u ${SonarqubeApiKey}: -sS ${SonarqubeEndpoint}/api/qualitygates/project_status?projectKey=$GlobalSonarqubeProjectName&branch=$SonarqubeBranchName) |
| 86 | + project_status=$(echo $status | jq -r ".projectStatus.status") |
| 87 | + export SonarqubeProjectStatus=$project_status |
| 88 | + echo "********* SonarQube Policy Report *********" |
| 89 | + echo $status |
| 90 | + if [[ $AbortPipelineOnPolicyCheckFailed == true && $project_status == "ERROR" ]] |
| 91 | + then |
| 92 | + echo "********* SonarQube Policy Violated *********" |
| 93 | + echo "********* Exiting Build *********" |
| 94 | + exit |
| 95 | + elif [[ $AbortPipelineOnPolicyCheckFailed == true && $project_status == "OK" ]] |
| 96 | + then |
| 97 | + echo "********* SonarQube Policy Passed *********" |
| 98 | + fi |
| 99 | + else |
| 100 | + echo -e "\nFinding the Vulnerabilities and High hotspots in source code ........\n" |
| 101 | + sleep 5 |
| 102 | + export SonarqubeVulnerabilities=$(curl -u ${SonarqubeApiKey}: --location --request GET "$SonarqubeEndpoint/api/issues/search?componentKeys=$GlobalSonarqubeProjectNamee&types=VULNERABILITY" | jq ".issues | length") |
| 103 | + export SonarqubeHighHotspots=$(curl -u ${SonarqubeApiKey}: --location --request GET "$SonarqubeEndpoint/api/hotspots/search?projectKey=$GlobalSonarqubeProjectName" | jq ''.hotspots|[.[]|select(.vulnerabilityProbability=="HIGH")]|length'') |
| 104 | + echo "Total Sonarqube Vulnerability: $SonarqubeVulnerabilities" |
| 105 | + echo "Total High Hotspots: $SonarqubeHighHotspots" |
| 106 | + export TotalSonarqubeIssues=$((SonarqubeVulnerabilities + SonarqubeHighHotspots)) |
| 107 | + echo "Total number of issues found by sonarqube scanner : $TotalSonarqubeIssues" |
| 108 | + echo -e "For analysis report please visit $SonarqubeEndpoint/dashboard?id=$GlobalSonarqubeProjectName" |
| 109 | + fi |
| 110 | + } |
| 111 | +
|
| 112 | +
|
| 113 | + FetchRepoBranchNameFunction |
| 114 | + if [ -z $SonarqubeProjectPrefixName ] |
| 115 | + then |
| 116 | + SonarqubeProjectPrefixName=$repoName |
| 117 | + fi |
| 118 | + if [ -z $SonarqubeBranchName ] |
| 119 | + then |
| 120 | + SonarqubeBranchName=$branchName |
| 121 | + fi |
| 122 | +
|
| 123 | +
|
| 124 | + PathToCodeDir=/devtroncd$CheckoutPath |
| 125 | + cd $PathToCodeDir |
| 126 | + if [ ! -z $SonarqubeProjectKey ] |
| 127 | + then |
| 128 | + GlobalSonarqubeProjectName=$SonarqubeProjectKey |
| 129 | + GlobalSonarqubeBranchName="master" |
| 130 | + else |
| 131 | + GlobalSonarqubeProjectName=$SonarqubeProjectPrefixName-$SonarqubeBranchName |
| 132 | + GlobalSonarqubeBranchName=$SonarqubeBranchName |
| 133 | + fi |
| 134 | + if [[ -z "$UsePropertiesFileFromProject" || $UsePropertiesFileFromProject == false ]] |
| 135 | + then |
| 136 | + echo "sonar.projectKey=$GlobalSonarqubeProjectName" > sonar-project.properties |
| 137 | + fi |
| 138 | + echo -e "\n********** Sonarqube Project Name : $GlobalSonarqubeProjectName , Sonarqube Branch name : $SonarqubeBranchName ***********" |
| 139 | + if [ -z "$GlobalSonarqubeProjectName" ] || [ -z "$SonarqubeBranchName" ]; then |
| 140 | + echo -e "\n****** Sonarqube Project Name and Sonarqube branch name should not be empty *********" |
| 141 | + exit 1 |
| 142 | + fi |
| 143 | +
|
| 144 | + if [ -z $SonarqubeApiKey ] |
| 145 | + then |
| 146 | + echo "************* Sonarqube analysis api key has not been provided *************" |
| 147 | + exit 1 |
| 148 | + fi |
| 149 | + if [ -z $SonarqubeEndpoint ] |
| 150 | + then |
| 151 | + echo "********** Sonarqube endpoint URL has not been provided ********* " |
| 152 | + exit 1 |
| 153 | + fi |
| 154 | +
|
| 155 | + echo -e "\n*********Creating Sonarqube project **********" |
| 156 | + curl -u ${SonarqubeApiKey}: --location --request POST "$SonarqubeEndpoint/api/projects/create?name=$GlobalSonarqubeProjectName&mainBranch=$SonarqubeBranchName&project=$GlobalSonarqubeProjectName" |
| 157 | + CreateProjectStatusCode=$? |
| 158 | + if [ "$CreateProjectStatusCode" -ne 0 ]; then |
| 159 | + echo -e "****** Sonarqube project create command failed to run *********" |
| 160 | + exit 1 |
| 161 | + else |
| 162 | + SonarqubeScanFunction |
| 163 | + fi' WHERE id=(select script_id FROM plugin_step WHERE plugin_id=(SELECT id FROM plugin_metadata WHERE name='Sonarqube v1.1.0')); |
| 164 | +INSERT INTO plugin_step_variable (id,plugin_step_id,name,format,description,is_exposed,allow_empty_value,default_value,variable_type,value_type,previous_step_index,variable_step_index,variable_step_index_in_plugin,reference_variable_name,deleted,created_on,created_by,updated_on,updated_by) |
| 165 | +VALUES(nextval('id_seq_plugin_step_variable'),(SELECT id FROM plugin_metadata WHERE name='Sonarqube v1.1.0'),'SonarContainerImage','STRING','Container Image that will be used for sonar scanning purpose.','t','t','sonarsource/sonar-scanner-cli','INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1); |
0 commit comments