Skip to content

Commit

Permalink
feat: Sonarqube polling script update (#2846)
Browse files Browse the repository at this point in the history
* added migration for sonarqube plugin with polling feature

* updated plugin_pipeline script for legacy support

* update migration script for polling script

* updated if condition in sonar plugin script
  • Loading branch information
pawan-59 authored Jan 11, 2023
1 parent d7b95a9 commit d37c1c1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
15 changes: 15 additions & 0 deletions scripts/sql/106_sonarqube_plugin_polling.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DELETE FROM plugin_step_variable WHERE name = 'UsePropertiesFileFromProject';

DELETE FROM plugin_step_variable WHERE name = 'CheckForSonarAnalysisReport';

DELETE FROM plugin_step_variable WHERE name = 'AbortPipelineOnPolicyCheckFailed';

UPDATE plugin_pipeline_script SET script=E'PathToCodeDir=/devtroncd$CheckoutPath
cd $PathToCodeDir
echo "sonar.projectKey=$SonarqubeProjectKey" > sonar-project.properties
docker run
--rm
-e SONAR_HOST_URL=$SonarqubeEndpoint
-e SONAR_LOGIN=$SonarqubeApiKey
-v "/$PWD:/usr/src"
sonarsource/sonar-scanner-cli' WHERE id = 2;
54 changes: 54 additions & 0 deletions scripts/sql/106_sonarqube_plugin_polling.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
do $$
BEGIN
IF NOT EXISTS (SELECT * FROM plugin_step_variable WHERE name = 'UsePropertiesFileFromProject' AND plugin_step_id = 2) THEN
INSERT INTO plugin_step_variable (id,plugin_step_id,name,format,description,is_exposed,allow_empty_value,default_value,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)
VALUES(nextval('id_seq_plugin_step_variable'),2,'UsePropertiesFileFromProject','BOOL','Boolean value - true or false. Whether poll Generated Report or not','t','f',false,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);
END IF;
END;
$$;

INSERT INTO plugin_step_variable (id,plugin_step_id,name,format,description,is_exposed,allow_empty_value,default_value,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)
VALUES(nextval('id_seq_plugin_step_variable'),2,'CheckForSonarAnalysisReport','BOOL','Boolean value - true or false. Whether poll Generated Report or not','t','f',false,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);

INSERT INTO plugin_step_variable (id,plugin_step_id,name,format,description,is_exposed,allow_empty_value,default_value,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)
VALUES(nextval('id_seq_plugin_step_variable'),2,'AbortPipelineOnPolicyCheckFailed','BOOL','Boolean value - true or false. Whether to proceed on policy check failed or not','t','f',false,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);


UPDATE plugin_pipeline_script SET script=E'PathToCodeDir=/devtroncd$CheckoutPath
cd $PathToCodeDir
if [[ -z "$UsePropertiesFileFromProject" ]]
then
echo "sonar.projectKey=$SonarqubeProjectKey" > sonar-project.properties
docker run \\
--rm \\
-e SONAR_HOST_URL=$SonarqubeEndpoint \\
-e SONAR_LOGIN=$SonarqubeApiKey \\
-v "/$PWD:/usr/src" \\
sonarsource/sonar-scanner-cli
elif [[ $UsePropertiesFileFromProject == false ]]
then
echo "sonar.projectKey=$SonarqubeProjectKey" > sonar-project.properties
docker run \\
--rm \\
-e SONAR_HOST_URL=$SonarqubeEndpoint \\
-e SONAR_LOGIN=$SonarqubeApiKey \\
-v "/$PWD:/usr/src" \\
sonarsource/sonar-scanner-cli
if [[ $CheckForSonarAnalysisReport == true && ! -z "$CheckForSonarAnalysisReport" ]]
then
status=$(curl -u ${SonarqubeApiKey}: -sS ${SonarqubeEndpoint}/api/qualitygates/project_status?projectKey=${SonarqubeProjectKey}&branch=master)
project_status=$(echo $status | jq -r ".projectStatus.status")
echo "********* SonarQube Policy Report *********"
echo $status
if [[ $AbortPipelineOnPolicyCheckFailed == true && $project_status == "ERROR" ]]
then
echo "********* SonarQube Policy Violated *********"
echo "********* Exiting Build *********"
exit
elif [[ $AbortPipelineOnPolicyCheckFailed == true && $project_status == "OK" ]]
then
echo "********* SonarQube Policy Passed *********"
fi
fi
fi' WHERE id=2;

0 comments on commit d37c1c1

Please sign in to comment.