Skip to content

Commit 8390d6b

Browse files
authored
Fix-ruff-convert-to-sonar (#1987)
* Exclude ruff rule D401 * Fix ruff report conversion to external issues format * Fix conversion ruff to external issues * Improvement * Exclude silly ruff rules * Fix close for loop * Create fresh TESTSYNC before each tests * Format shellcheck native JSON report * Quality pass * Exclude useless ruff rules * Quality pass * Fix ruff rule exclusions * Quality pass * Exclude shell from coverage calculation * Quality pass * Shell quality pass * Quality pass * Quality pass * Quality pass * Quality pass * Quality pass * Quality pass * Quality pass * Quality pass * Quality pass * Fix typo * Quality pass * Quality pass * Update lock * Quality pass
1 parent 1e2402a commit 8390d6b

File tree

17 files changed

+365
-437
lines changed

17 files changed

+365
-437
lines changed

cli/audit.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818
# along with this program; if not, write to the Free Software Foundation,
1919
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2020
#
21-
"""
21+
"""Audits a SonarQube platform"""
2222

23-
Audits a SonarQube platform
23+
from __future__ import annotations
2424

25-
"""
26-
27-
import sys
2825
import json
2926
import csv
30-
from typing import TextIO
27+
from typing import TextIO, Optional
3128
from threading import Thread
3229
from queue import Queue
3330
from requests import RequestException
@@ -58,7 +55,7 @@ def _audit_sif(sysinfo: str, audit_settings: types.ConfigSettings) -> tuple[str,
5855
"""Audits a SIF and return found problems"""
5956
log.info("Auditing SIF file '%s'", sysinfo)
6057
try:
61-
with open(sysinfo, "r", encoding="utf-8") as f:
58+
with open(sysinfo, encoding="utf-8") as f:
6259
sysinfo = json.loads(f.read())
6360
except json.decoder.JSONDecodeError:
6461
log.critical("File %s does not seem to be a legit JSON file", sysinfo)
@@ -74,7 +71,7 @@ def _audit_sif(sysinfo: str, audit_settings: types.ConfigSettings) -> tuple[str,
7471

7572

7673
def write_csv(queue: Queue[list[problem.Problem]], fd: TextIO, settings: types.ConfigSettings) -> None:
77-
"""Writes the CSV file of audit problems"""
74+
"""Thread callback to write audit problems in a CSV file"""
7875
server_id = settings.get("SERVER_ID", None)
7976
with_url = settings.get("WITH_URL", False)
8077
csvwriter = csv.writer(fd, delimiter=settings.get("CSV_DELIMITER", ","))
@@ -93,9 +90,7 @@ def write_csv(queue: Queue[list[problem.Problem]], fd: TextIO, settings: types.C
9390

9491

9592
def write_json(queue: Queue[list[problem.Problem]], fd: TextIO, settings: types.ConfigSettings) -> None:
96-
"""
97-
Thread to write problems in a JSON file
98-
"""
93+
"""Thread callback to write problems in a JSON file"""
9994
server_id = settings.get("SERVER_ID", None)
10095
with_url = settings.get("WITH_URL", False)
10196
comma = ""
@@ -114,7 +109,7 @@ def write_json(queue: Queue[list[problem.Problem]], fd: TextIO, settings: types.
114109

115110

116111
def _audit_sq(
117-
sq: platform.Platform, settings: types.ConfigSettings, what_to_audit: list[str] = None, key_list: types.KeyList = None
112+
sq: platform.Platform, settings: types.ConfigSettings, what_to_audit: Optional[list[str]] = None, key_list: types.KeyList = None
118113
) -> list[problem.Problem]:
119114
"""Audits a SonarQube/Cloud platform"""
120115
everything = what_to_audit is None
@@ -179,9 +174,8 @@ def __parser_args(desc: str) -> object:
179174

180175
def __check_keys_exist(key_regexp: list[str], sq: platform.Platform, what: list[str]) -> None:
181176
"""Checks if project keys exist"""
182-
if key_regexp and "projects" in what:
183-
if len(component_helper.get_components(sq, "projects", key_regexp)) == 0:
184-
raise options.ArgumentsError(f"No projects found with key matching regexp '{key_regexp}'")
177+
if key_regexp and "projects" in what and len(component_helper.get_components(sq, "projects", key_regexp)) == 0:
178+
raise options.ArgumentsError(f"No projects found with key matching regexp '{key_regexp}'")
185179

186180

187181
def main() -> None:

conf/build.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
2020
CONFDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
21-
SONAR_TOOLS_RELEASE="$ROOTDIR/sonar/version.py"
21+
SONAR_TOOLS_RELEASE="${ROOTDIR}/sonar/version.py"
2222

2323
build_docs=0
2424
build_docker=0
2525

26-
while [ $# -ne 0 ]; do
27-
case $1 in
26+
while [[ $# -ne 0 ]]; do
27+
case "${1}" in
2828
docs|doc)
2929
build_docs=1
3030
;;
@@ -40,18 +40,18 @@ done
4040
echo "======= FORMATTING CODE ========="
4141
ruff format
4242
echo "======= BUILDING PACKAGE ========="
43-
rm -rf "$ROOTDIR/build/lib/sonar" "$ROOTDIR/build/lib/cli" "$ROOTDIR"/build/scripts*/sonar-tools "$ROOTDIR"/dist/sonar_tools*
43+
rm -rf "${ROOTDIR}/build/lib/sonar" "${ROOTDIR}/build/lib/cli" "${ROOTDIR}"/build/scripts*/sonar-tools "${ROOTDIR}"/dist/sonar_tools*
4444
# python -m build
4545
poetry build
4646

47-
if [ "$build_docs" == "1" ]; then
47+
if [[ "${build_docs}" = "1" ]]; then
4848
echo "======= BUILDING DOCS ========="
4949
rm -rf doc/api/build
5050
sphinx-build -b html doc/api/source doc/api/build
5151
fi
5252

53-
if [ "$build_docker" == "1" ]; then
53+
if [[ "${build_docker}" = "1" ]]; then
5454
echo "======= BUILDING DOCKER IMAGE WITH SNAPSHOT ========="
55-
version=$(grep PACKAGE_VERSION "$SONAR_TOOLS_RELEASE" | cut -d "=" -f 2 | cut -d '"' -f 2)
56-
docker build -t "olivierkorach/sonar-tools:$version-snapshot" -t olivierkorach/sonar-tools:latest -f "$CONFDIR/snapshot.Dockerfile" "$ROOTDIR" --load
55+
version=$(grep PACKAGE_VERSION "${SONAR_TOOLS_RELEASE}" | cut -d "=" -f 2 | cut -d '"' -f 2)
56+
docker build -t "olivierkorach/sonar-tools:${version}-snapshot" -t olivierkorach/sonar-tools:latest -f "${CONFDIR}/snapshot.Dockerfile" "${ROOTDIR}" --load
5757
fi

conf/build_tests.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
2323

24-
cd "$ROOTDIR/test/unit" || exit 1
24+
cd "${ROOTDIR}/test/unit" || exit 1
2525

2626
export GEN_LOC=test/gen
2727

@@ -30,23 +30,23 @@ echo "Generating edition / version specific tests"
3030

3131
for target in lts latest cb 9 9-ce common
3232
do
33-
echo "Generating tests for $target"
34-
rm -rf "${ROOTDIR:?}/${GEN_LOC:?}/$target"
35-
mkdir -p "${ROOTDIR:?}/${GEN_LOC:?}/$target" 2>/dev/null
36-
if [ "$target" == "common" ]; then
37-
b=$(basename "$f" .py)
38-
cp conftest.py utilities.py credentials.py "$ROOTDIR/$GEN_LOC/$target"
39-
cp test_common*.py "$ROOTDIR/$GEN_LOC/$target"
33+
echo "Generating tests for ${target}"
34+
rm -rf "${ROOTDIR:?}/${GEN_LOC:?}/${target}"
35+
mkdir -p "${ROOTDIR:?}/${GEN_LOC:?}/${target}" 2>/dev/null
36+
if [[ "${target}" = "common" ]]; then
37+
b=$(basename "${f}" .py)
38+
cp conftest.py utilities.py credentials.py "${ROOTDIR}/${GEN_LOC}/${target}"
39+
cp test_common*.py "${ROOTDIR}/${GEN_LOC}/${target}"
4040
else
4141
for f in *.py
4242
do
43-
b=$(basename "$f" .py)
44-
cp "$f" "$ROOTDIR/$GEN_LOC/$target/${b}_${target}.py"
43+
b=$(basename "${f}" .py)
44+
cp "${f}" "${ROOTDIR}/${GEN_LOC}/${target}/${b}_${target}.py"
4545
done
46-
cp "credentials-$target.py" "$ROOTDIR/$GEN_LOC/$target/credentials.py"
47-
mv "$ROOTDIR/$GEN_LOC/$target/conftest_${target}.py" "$ROOTDIR/$GEN_LOC/$target/conftest.py"
48-
mv "$ROOTDIR/$GEN_LOC/$target/utilities_${target}.py" "$ROOTDIR/$GEN_LOC/$target/utilities.py"
49-
rm "$ROOTDIR/$GEN_LOC/$target/"test_common*.py
46+
cp "credentials-${target}.py" "${ROOTDIR}/${GEN_LOC}/${target}/credentials.py"
47+
mv "${ROOTDIR}/${GEN_LOC}/${target}/conftest_${target}.py" "${ROOTDIR}/${GEN_LOC}/${target}/conftest.py"
48+
mv "${ROOTDIR}/${GEN_LOC}/${target}/utilities_${target}.py" "${ROOTDIR}/${GEN_LOC}/${target}/utilities.py"
49+
rm "${ROOTDIR}/${GEN_LOC}/${target}/"test_common*.py
5050
fi
5151
done
5252

conf/deploy.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
2323
CONFDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2424

2525
deps=0
26-
"$CONFDIR"/build.sh "$@"
26+
"${CONFDIR}"/build.sh "$@"
2727

28-
while [ $# -ne 0 ]; do
29-
case $1 in
28+
while [[ $# -ne 0 ]]; do
29+
case "${1}" in
3030
deps)
3131
deps=1
3232
;;
@@ -37,9 +37,9 @@ while [ $# -ne 0 ]; do
3737
done
3838

3939
# Deploy locally for tests
40-
if [ "$deps" == "1" ]; then
40+
if [[ "${deps}" = "1" ]]; then
4141
pipopts="--upgrade"
4242
else
4343
pipopts="--no-deps"
4444
fi
45-
pip install "$pipopts" --force-reinstall "$ROOTDIR"/dist/sonar_tools-*-py3-*.whl
45+
pip install "${pipopts}" --force-reinstall "${ROOTDIR}"/dist/sonar_tools-*-py3-*.whl

conf/prep_all_tests.sh

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,40 @@
2121

2222
SYNC_PROJECT_KEY="TESTSYNC"
2323

24-
# Deletes and recreates a fresh $SYNC_PROJECT_KEY project in SonarQube
25-
curl -X POST -u "$SONAR_TOKEN_LATEST_ADMIN_USER:" "http://localhost:20010/api/projects/delete?project=$SYNC_PROJECT_KEY"
26-
conf/scan.sh -Dsonar.host.url=http://localhost:20010 -Dsonar.projectKey=$SYNC_PROJECT_KEY -Dsonar.projectName=$SYNC_PROJECT_KEY -Dsonar.token="$SONAR_TOKEN_LATEST_ADMIN_ANALYSIS"
27-
28-
curl -X POST -u "$SONAR_TOKEN_LATEST_ADMIN_USER:" "http://localhost:10000/api/projects/delete?project=$SYNC_PROJECT_KEY"
29-
sonar-scanner -Dsonar.host.url=http://localhost:10000 -Dsonar.projectKey=$SYNC_PROJECT_KEY -Dsonar.projectName=$SYNC_PROJECT_KEY -Dsonar.login="$SONAR_TOKEN_LATEST_ADMIN_ANALYSIS" -Dsonar.token="$SONAR_TOKEN_LATEST_ADMIN_ANALYSIS"
30-
curl -X POST -u "$SONAR_TOKEN_LATEST_ADMIN_USER:" "http://localhost:20010/api/projects/delete?project=$SYNC_PROJECT_KEY"
31-
sonar-scanner -Dsonar.host.url=http://localhost:20010 -Dsonar.projectKey=$SYNC_PROJECT_KEY -Dsonar.projectName=$SYNC_PROJECT_KEY -Dsonar.login="$SONAR_TOKEN_LATEST_ADMIN_ANALYSIS" -Dsonar.token="$SONAR_TOKEN_LATEST_ADMIN_ANALYSIS"
32-
curl -X POST -u "$SONAR_TOKEN_LATEST_ADMIN_USER:" "http://localhost:7000/api/projects/delete?project=$SYNC_PROJECT_KEY"
33-
sonar-scanner -Dsonar.host.url=http://localhost:7000 -Dsonar.projectKey=$SYNC_PROJECT_KEY -Dsonar.projectName=$SYNC_PROJECT_KEY -Dsonar.login="$SONAR_TOKEN_LATEST_ADMIN_ANALYSIS" -Dsonar.token="$SONAR_TOKEN_LATEST_ADMIN_ANALYSIS"
34-
curl -X POST -u "$SONAR_TOKEN_9_ADMIN_USER:" "http://localhost:9000/api/projects/delete?project=$SYNC_PROJECT_KEY"
35-
sonar-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.projectKey=$SYNC_PROJECT_KEY -Dsonar.projectName=$SYNC_PROJECT_KEY -Dsonar.login="$SONAR_TOKEN_9_ADMIN_ANALYSIS" -Dsonar.token="$SONAR_TOKEN_9_ADMIN_ANALYSIS"
36-
37-
curl -X POST -u "$SONAR_TOKEN_SONARCLOUD:" "https://sonarcloud.io/api/projects/delete?project=$SYNC_PROJECT_KEY"
38-
sonar-scanner -Dsonar.host.url=https://sonarcloud.io -Dsonar.projectKey=$SYNC_PROJECT_KEY -Dsonar.projectName=$SYNC_PROJECT_KEY -Dsonar.organization=okorach -Dsonar.login="$SONAR_TOKEN_SONARCLOUD" -Dsonar.token="$SONAR_TOKEN_SONARCLOUD"
39-
40-
sonar-scanner -Dsonar.host.url=http://localhost:10000 -Dsonar.pullrequest.key=5 -Dsonar.pullrequest.branch=feature/5
41-
sonar-scanner -Dsonar.host.url=http://localhost:10000 -Dsonar.pullrequest.key=7 -Dsonar.pullrequest.branch=feature/7
42-
43-
sonar-scanner -Dsonar.host.url=http://localhost:8000 -Dsonar.pullrequest.key=5 -Dsonar.pullrequest.branch=feature/5 -Dsonar.login="$SONAR_TOKEN_LTS_ADMIN_ANALYSIS"
44-
sonar-scanner -Dsonar.host.url=http://localhost:8000 -Dsonar.pullrequest.key=7 -Dsonar.pullrequest.branch=feature/7 -Dsonar.login="$SONAR_TOKEN_LTS_ADMIN_ANALYSIS"
45-
46-
sonar-scanner -Dsonar.host.url=http://localhost:7000 -Dsonar.login="$SONAR_TOKEN_CB_ADMIN_ANALYSIS"
47-
24+
function create_fresh_project {
25+
key="${1}"
26+
url="${2}"
27+
usertoken="${3}"
28+
token="${4}"
29+
shift 3
30+
opts=("$@")
31+
opt_org=""
32+
if [[ "${url}" = "https://sonarcloud.io" ]]; then
33+
opt_org="-Dsonar.organization=okorach"
34+
fi
35+
opt_token="-Dsonar.token=${token}"
36+
if [[ "${url}" = "${SONAR_HOST_URL_9}" ]]; then
37+
opt_token="-Dsonar.login=${token}"
38+
fi
39+
curl -X POST -u "${usertoken}:" "${url}/api/projects/delete?project=${key}"
40+
conf/scan.sh "${opts[@]}" -Dsonar.projectKey="${key}" -Dsonar.projectName="${key}" -Dsonar.host.url="${url}" "${opt_token}" "${opt_org}"
41+
return 0
42+
}
43+
44+
create_fresh_project "${SYNC_PROJECT_KEY}" "${SONAR_HOST_URL_TEST:?}" "${SONAR_TOKEN_TEST_ADMIN_USER}" "${SONAR_TOKEN_TEST_ADMIN_ANALYSIS}" -nolint
45+
create_fresh_project "${SYNC_PROJECT_KEY}" "${SONAR_HOST_URL_LATEST:?}" "${SONAR_TOKEN_LATEST_ADMIN_USER}" "${SONAR_TOKEN_LATEST_ADMIN_ANALYSIS}"
46+
create_fresh_project "${SYNC_PROJECT_KEY}" "${SONAR_HOST_URL_CB:?}" "${SONAR_TOKEN_CB_ADMIN_USER}" "${SONAR_TOKEN_CB_ADMIN_ANALYSIS}"
47+
create_fresh_project "${SYNC_PROJECT_KEY}" "${SONAR_HOST_URL_9:?}" "${SONAR_TOKEN_9_ADMIN_USER}" "${SONAR_TOKEN_9_ADMIN_ANALYSIS}"
48+
create_fresh_project "${SYNC_PROJECT_KEY}" "https://sonarcloud.io" "${SONAR_TOKEN_SONARCLOUD" "${SONAR_TOKEN_SONARCLOUD"
49+
create_fresh_project "${SYNC_PROJECT_KEY}" "${SONAR_HOST_URL_CB:?}" "${SONAR_TOKEN_CB_ADMIN_USER}" "${SONAR_TOKEN_CB_ADMIN_ANALYSIS}"
50+
51+
for pr in 5 7; do
52+
sonar-scanner -Dsonar.host.url="${SONAR_HOST_URL_LATEST:?}" -Dsonar.pullrequest.key="${pr}" -Dsonar.pullrequest.branch="feature/${pr}" -Dsonar.token="${SONAR_TOKEN_LATEST_ADMIN_ANALYSIS}"
53+
sonar-scanner -Dsonar.host.url="${SONAR_HOST_URL_LTS:?}" -Dsonar.pullrequest.key="${pr}" -Dsonar.pullrequest.branch="feature/${pr}" -Dsonar.token="${SONAR_TOKEN_LTS_ADMIN_ANALYSIS}"
54+
done
4855
# Format for 10.x and 9.x is different, file was generated for 10.x, so removing for 9.9
49-
rm build/external-issues*
50-
sonar-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.pullrequest.key=5 -Dsonar.pullrequest.branch=feature/5 -Dsonar.login="$SONAR_TOKEN_9_ADMIN_ANALYSIS"
51-
sonar-scanner -Dsonar.host.url=http://localhost:9000 -Dsonar.pullrequest.key=7 -Dsonar.pullrequest.branch=feature/7 -Dsonar.login="$SONAR_TOKEN_9_ADMIN_ANALYSIS"
5256
57+
rm build/external-issues*
58+
for pr in 5 7; do
59+
sonar-scanner -Dsonar.host.url="${SONAR_HOST_URL_9:?}" -Dsonar.pullrequest.key="${pr}" -Dsonar.pullrequest.branch="feature/${pr}" -Dsonar.login="${SONAR_TOKEN_9_ADMIN_ANALYSIS}"
60+
done

conf/release.sh

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,37 @@
1919
ROOTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
2020
CONFDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2121

22-
"$CONFDIR"/build.sh
22+
"${CONFDIR}"/build.sh
2323

24-
SONAR_TOOLS_RELEASE="$ROOTDIR/sonar/version.py"
25-
DOCKERFILE_RELEASE="$CONFDIR/release.Dockerfile"
24+
SONAR_TOOLS_RELEASE="${ROOTDIR}/sonar/version.py"
25+
DOCKERFILE_RELEASE="${CONFDIR}/release.Dockerfile"
2626

27-
version=$(grep PACKAGE_VERSION "$SONAR_TOOLS_RELEASE" | cut -d "=" -f 2 | cut -d '"' -f 2)
27+
version=$(grep PACKAGE_VERSION "${SONAR_TOOLS_RELEASE}" | cut -d "=" -f 2 | cut -d '"' -f 2)
2828

29-
docker_version=$(grep 'pip install sonar-tools==' "$DOCKERFILE_RELEASE" | sed -E 's/.*sonar-tools==([0-9\.]+).*/\1/')
29+
docker_version=$(grep 'pip install sonar-tools==' "${DOCKERFILE_RELEASE}" | sed -E 's/.*sonar-tools==([0-9\.]+).*/\1/')
3030

31-
if [ "$version" != "$docker_version" ]; then
32-
echo "Docker version and pypi version are different ($docker_version vs $version), release aborted"
31+
if [[ "${version}" != "${docker_version}" ]]; then
32+
echo "Docker version and pypi version are different (${docker_version} vs ${version}), release aborted"
3333
exit 1
3434
fi
3535

3636
echo "Confirm release [y/n] ?"
3737
read -r confirm
38-
if [ "$confirm" = "y" ]; then
39-
version=$(grep PACKAGE_VERSION "$ROOTDIR"/sonar/version.py | cut -d "=" -f 2 | sed -e "s/[\'\" ]//g" -e "s/^ +//" -e "s/ +$//")
38+
if [[ "${confirm}" = "y" ]]; then
39+
version=$(grep PACKAGE_VERSION "${ROOTDIR}"/sonar/version.py | cut -d "=" -f 2 | sed -e "s/[\'\" ]//g" -e "s/^ +//" -e "s/ +$//")
4040

4141
echo "Releasing on pypi.org"
42-
python3 -m twine upload "$ROOTDIR/dist/sonar_tools-$version-py3-none-any.whl"
42+
python3 -m twine upload "${ROOTDIR}/dist/sonar_tools-${version}-py3-none-any.whl"
4343
echo -n "Waiting pypi release to be effective"
44-
while [ "$(get_pypi_latest_version sonar-tools)" != "$version" ]; do
44+
while [[ "$(get_pypi_latest_version sonar-tools)" != "${version}" ]]; do
4545
sleep 10
4646
echo -n "."
4747
done
4848
echo " done"
4949
echo "Releasing on dockerhub"
50-
docker buildx build --push --platform linux/amd64,linux/arm64 -t "olivierkorach/sonar-tools:$version" -t olivierkorach/sonar-tools:latest -f "$CONFDIR/release.Dockerfile" "$ROOTDIR"
51-
cd "$ROOTDIR" && docker pushrm olivierkorach/sonar-tools
50+
docker buildx build --push --platform linux/amd64,linux/arm64 -t "olivierkorach/sonar-tools:${version}" -t olivierkorach/sonar-tools:latest -f "${CONFDIR}/release.Dockerfile" "${ROOTDIR}"
51+
cd "${ROOTDIR}" && docker pushrm olivierkorach/sonar-tools
5252

5353
echo "Running scan"
54-
"$CONFDIR/scan.sh" -test
54+
"${CONFDIR}/scan.sh" -test
5555
fi

0 commit comments

Comments
 (0)