@@ -210,26 +210,43 @@ else
210210 echo_info " Default '${ADMIN_USER_NAME} ' password is not in use."
211211fi
212212
213+ # Check whether pipeline user exists; create it if missing.
213214echo_info " Checking if '${PIPELINE_USER_NAME} ' exists ..."
214215encodedPipelineUser=" $( uriencode " ${PIPELINE_USER_NAME} " ) "
215- encodedPipelinePassword=" $( uriencode " ${ADMIN_USER_PASSWORD} " ) "
216- if curl ${INSECURE} -X POST -sSf --user " ${ADMIN_USER_NAME} :${ADMIN_USER_PASSWORD} " \
217- " ${SONARQUBE_URL} /api/users/search?q=${encodedPipelineUser} " | grep ' "users":\[\]' > /dev/null; then
218- echo_info " No user '${PIPELINE_USER_NAME} ' present yet."
216+
217+ # Query SonarQube for matching users and get count (fallback to 0 on error).
218+ userCount=$( curl ${INSECURE} -sS --user " ${ADMIN_USER_NAME} :${ADMIN_USER_PASSWORD} " \
219+ " ${SONARQUBE_URL} /api/users/search?q=${encodedPipelineUser} " | jq -r ' .users | length' || echo 0)
220+
221+ if [ " ${userCount} " -eq 0 ]; then
222+ echo_info " No user '${PIPELINE_USER_NAME} ' found — creating it now."
223+
219224 if [ -z " ${PIPELINE_USER_PWD} " ]; then
220- echo " Please enter '${PIPELINE_USER_NAME} ' password :"
225+ echo " Enter password for '${PIPELINE_USER_NAME} ':"
221226 read -r -e -s input
222227 PIPELINE_USER_PWD=${input:- " " }
223228 fi
224- echo_info " Trying to login in as '${PIPELINE_USER_NAME} ' ..."
229+
230+ encodedPipelinePassword=" $( uriencode " ${PIPELINE_USER_PWD} " ) "
231+
232+ echo_info " Creating SonarQube user '${PIPELINE_USER_NAME} ' ..."
233+ if ! curl ${INSECURE} -X POST -sSf --user " ${ADMIN_USER_NAME} :${ADMIN_USER_PASSWORD} " \
234+ " ${SONARQUBE_URL} /api/users/create?login=${encodedPipelineUser} &name=${encodedPipelineUser} &password=${encodedPipelinePassword} " ; then
235+ echo_error " Could not create user '${PIPELINE_USER_NAME} '."
236+ exit 1
237+ fi
238+ echo_info " User '${PIPELINE_USER_NAME} ' created."
239+
240+ echo_info " Verifying login for '${PIPELINE_USER_NAME} ' ..."
225241 if ! curl ${INSECURE} -X POST -sSf \
226242 " ${SONARQUBE_URL} /api/authentication/login?login=${encodedPipelineUser} &password=${encodedPipelinePassword} " ; then
227- echo_error " Could not login as '${PIPELINE_USER_NAME} '."
243+ echo_error " Login verification for '${PIPELINE_USER_NAME} ' failed ."
228244 exit 1
229245 fi
230246 echo_info " Login for '${PIPELINE_USER_NAME} ' successful."
247+ else
248+ echo_info " User '${PIPELINE_USER_NAME} ' already exists in SonarQube."
231249fi
232- echo_info " User '${PIPELINE_USER_NAME} ' exists in SonarQube."
233250
234251sampleToken=$( grep SONAR_AUTH_TOKEN_B64 " ${ODS_CORE_DIR} /configuration-sample/ods-core.env.sample" | cut -d " =" -f 2-)
235252configuredToken=$( grep SONAR_AUTH_TOKEN_B64 " ${ODS_CONFIGURATION_DIR} /ods-core.env" | cut -d " =" -f 2- | base64 --decode)
0 commit comments