Skip to content

Commit 612664d

Browse files
Mykola SerdiukSergK
authored andcommitted
chore: Update sonar project creation logic (#356)
1 parent ee79043 commit 612664d

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

charts/pipelines-library/templates/tasks/sonar/sonarqube-dotnet.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,23 @@ spec:
8282
script: |
8383
set -e
8484
85-
PROJECT_EXISTS=$(curl -s -u ${SONAR_TOKEN}: "${SONAR_HOST_URL}/api/components/show?component=${SONAR_PROJECT_KEY}" | jq -r ".component.key")
85+
SONAR_RESPONSE=$(curl -s -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/components/show?component=${SONAR_PROJECT_KEY}")
8686
87-
if [ "$PROJECT_EXISTS" == "null" ]; then
87+
if [[ -z "$SONAR_RESPONSE" ]]; then
88+
echo "Token isn't valid or not defined"
89+
exit 1
90+
fi
91+
92+
if echo "$SONAR_RESPONSE" | jq -e '.errors[0].msg' &>/dev/null; then
8893
default_branch=$(kubectl get codebase $SONAR_PROJECT_NAME -o jsonpath='{.spec.defaultBranch}')
8994
echo "Create project ${SONAR_PROJECT_KEY}"
9095
curl -X POST -u ${SONAR_TOKEN}: "${SONAR_HOST_URL}/api/projects/create?name=${SONAR_PROJECT_KEY}&project=${SONAR_PROJECT_KEY}&mainBranch=${default_branch}"
9196
else
92-
echo "Project ${SONAR_PROJECT_KEY} already exists"
97+
if echo "$SONAR_RESPONSE" | jq -e '.component.key' &>/dev/null; then
98+
echo "Project \"$SONAR_PROJECT_KEY\" already exists"
99+
else
100+
echo "Unknown Response format"
101+
fi
93102
fi
94103
- name: sonar-scanner
95104
image: $(params.BASE_IMAGE)

charts/pipelines-library/templates/tasks/sonar/sonarqube-general.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,23 @@ spec:
7272
script: |
7373
set -e
7474
75-
PROJECT_EXISTS=$(curl -s -u ${SONAR_TOKEN}: "${SONAR_HOST_URL}/api/components/show?component=${SONAR_PROJECT_KEY}" | jq -r ".component.key")
75+
SONAR_RESPONSE=$(curl -s -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/components/show?component=${SONAR_PROJECT_KEY}")
7676
77-
if [ "$PROJECT_EXISTS" == "null" ]; then
77+
if [[ -z "$SONAR_RESPONSE" ]]; then
78+
echo "Token isn't valid or not defined"
79+
exit 1
80+
fi
81+
82+
if echo "$SONAR_RESPONSE" | jq -e '.errors[0].msg' &>/dev/null; then
7883
default_branch=$(kubectl get codebase $SONAR_PROJECT_NAME -o jsonpath='{.spec.defaultBranch}')
7984
echo "Create project ${SONAR_PROJECT_KEY}"
8085
curl -X POST -u ${SONAR_TOKEN}: "${SONAR_HOST_URL}/api/projects/create?name=${SONAR_PROJECT_KEY}&project=${SONAR_PROJECT_KEY}&mainBranch=${default_branch}"
8186
else
82-
echo "Project ${SONAR_PROJECT_KEY} already exists"
87+
if echo "$SONAR_RESPONSE" | jq -e '.component.key' &>/dev/null; then
88+
echo "Project \"$SONAR_PROJECT_KEY\" already exists"
89+
else
90+
echo "Unknown Response format"
91+
fi
8392
fi
8493
- image: registry.access.redhat.com/ubi8/ubi-minimal:8.8
8594
name: prepare-sonar-project-properties

charts/pipelines-library/templates/tasks/sonar/sonarqube-gradle.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,23 @@ spec:
9090
script: |
9191
set -e
9292
93-
PROJECT_EXISTS=$(curl -s -u ${SONAR_TOKEN}: "${SONAR_HOST_URL}/api/components/show?component=${SONAR_PROJECT_KEY}" | jq -r ".component.key")
93+
SONAR_RESPONSE=$(curl -s -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/components/show?component=${SONAR_PROJECT_KEY}")
9494
95-
if [ "$PROJECT_EXISTS" == "null" ]; then
95+
if [[ -z "$SONAR_RESPONSE" ]]; then
96+
echo "Token isn't valid or not defined"
97+
exit 1
98+
fi
99+
100+
if echo "$SONAR_RESPONSE" | jq -e '.errors[0].msg' &>/dev/null; then
96101
default_branch=$(kubectl get codebase $SONAR_PROJECT_NAME -o jsonpath='{.spec.defaultBranch}')
97102
echo "Create project ${SONAR_PROJECT_KEY}"
98103
curl -X POST -u ${SONAR_TOKEN}: "${SONAR_HOST_URL}/api/projects/create?name=${SONAR_PROJECT_KEY}&project=${SONAR_PROJECT_KEY}&mainBranch=${default_branch}"
99104
else
100-
echo "Project ${SONAR_PROJECT_KEY} already exists"
105+
if echo "$SONAR_RESPONSE" | jq -e '.component.key' &>/dev/null; then
106+
echo "Project \"$SONAR_PROJECT_KEY\" already exists"
107+
else
108+
echo "Unknown Response format"
109+
fi
101110
fi
102111
- name: gradle-tasks
103112
image: $(params.BASE_IMAGE)

charts/pipelines-library/templates/tasks/sonar/sonarqube-maven.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,23 @@ spec:
9595
script: |
9696
set -e
9797
98-
PROJECT_EXISTS=$(curl -s -u ${SONAR_TOKEN}: "${SONAR_HOST_URL}/api/components/show?component=${SONAR_PROJECT_KEY}" | jq -r ".component.key")
98+
SONAR_RESPONSE=$(curl -s -u "${SONAR_TOKEN}:" "${SONAR_HOST_URL}/api/components/show?component=${SONAR_PROJECT_KEY}")
9999
100-
if [ "$PROJECT_EXISTS" == "null" ]; then
100+
if [[ -z "$SONAR_RESPONSE" ]]; then
101+
echo "Token isn't valid or not defined"
102+
exit 1
103+
fi
104+
105+
if echo "$SONAR_RESPONSE" | jq -e '.errors[0].msg' &>/dev/null; then
101106
default_branch=$(kubectl get codebase $SONAR_PROJECT_NAME -o jsonpath='{.spec.defaultBranch}')
102107
echo "Create project ${SONAR_PROJECT_KEY}"
103108
curl -X POST -u ${SONAR_TOKEN}: "${SONAR_HOST_URL}/api/projects/create?name=${SONAR_PROJECT_KEY}&project=${SONAR_PROJECT_KEY}&mainBranch=${default_branch}"
104109
else
105-
echo "Project ${SONAR_PROJECT_KEY} already exists"
110+
if echo "$SONAR_RESPONSE" | jq -e '.component.key' &>/dev/null; then
111+
echo "Project \"$SONAR_PROJECT_KEY\" already exists"
112+
else
113+
echo "Unknown Response format"
114+
fi
106115
fi
107116
- name: mvn-goals
108117
image: $(params.MAVEN_IMAGE)

0 commit comments

Comments
 (0)