Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Sonarqube polling script update #2846

Merged
merged 4 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;