Skip to content

Commit a28f5ce

Browse files
badal773Badal Kumar Prusty
andauthored
chore: Enhanced SonarQube Script by exposing Docker Image (#4600)
* exposed docker image that used in the sonar-qube script * update the script according to script ID * update the script to handel existing scripts --------- Co-authored-by: Badal Kumar Prusty <[email protected]>
1 parent c9d8027 commit a28f5ce

File tree

2 files changed

+318
-0
lines changed

2 files changed

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

Comments
 (0)