diff --git a/README.md b/README.md index aee2cf9953ae70b67db504d14deb2367a8d51365..7e32fceb78f012d6fb187be5f9d0e15ac787a78f 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,9 @@ This is a great SonarQube feature but it assumes one of the following conditions * you are using a [Developer Edition](https://www.sonarqube.org/developer-edition/) version, * or you are using Community Edition with an opensource plugin emulating the Branch Analysis feature: - * etiher [sonar-branch-community](https://github.com/msanez/sonar-branch-community), - * or [sonarqube-community-branch-plugin](https://github.com/mc1arke/sonarqube-community-branch-plugin), - * ... + * etiher [sonar-branch-community](https://github.com/msanez/sonar-branch-community), + * or [sonarqube-community-branch-plugin](https://github.com/mc1arke/sonarqube-community-branch-plugin), + * ... If you're not in those cases, then the SonarQube analysis will fail with default parameters. You'll have to override the default `SONAR_BASE_ARGS` and disable it by removing the `sonar.branch.name` argument. diff --git a/templates/gitlab-ci-sonar.yml b/templates/gitlab-ci-sonar.yml index 02ca3796e4846d3dbb450fb5dadb9fb1b54f63c0..bee8e25260aab12b36b5f76299c991bb158c9730 100644 --- a/templates/gitlab-ci-sonar.yml +++ b/templates/gitlab-ci-sonar.yml @@ -119,6 +119,88 @@ stages: fi } + function unscope_variables() { + _scoped_vars=$(env | awk -F '=' "/^scoped__[a-zA-Z0-9_]+=/ {print \$1}" | sort) + if [[ -z "$_scoped_vars" ]]; then return; fi + log_info "Processing scoped variables..." + for _scoped_var in $_scoped_vars + do + _fields=${_scoped_var//__/:} + _condition=$(echo "$_fields" | cut -d: -f3) + case "$_condition" in + if) _not="";; + ifnot) _not=1;; + *) + log_warn "... unrecognized condition \\e[1;91m$_condition\\e[0m in \\e[33;1m${_scoped_var}\\e[0m" + continue + ;; + esac + _target_var=$(echo "$_fields" | cut -d: -f2) + _cond_var=$(echo "$_fields" | cut -d: -f4) + _cond_val=$(eval echo "\$${_cond_var}") + _test_op=$(echo "$_fields" | cut -d: -f5) + case "$_test_op" in + defined) + if [[ -z "$_not" ]] && [[ -z "$_cond_val" ]]; then continue; + elif [[ "$_not" ]] && [[ "$_cond_val" ]]; then continue; + fi + ;; + equals|startswith|endswith|contains|in|equals_ic|startswith_ic|endswith_ic|contains_ic|in_ic) + # comparison operator + # sluggify actual value + _cond_val=$(echo "$_cond_val" | tr '[:punct:]' '_') + # retrieve comparison value + _cmp_val_prefix="scoped__${_target_var}__${_condition}__${_cond_var}__${_test_op}__" + _cmp_val=${_scoped_var#$_cmp_val_prefix} + # manage 'ignore case' + if [[ "$_test_op" == *_ic ]] + then + # lowercase everything + _cond_val=$(echo "$_cond_val" | tr '[:upper:]' '[:lower:]') + _cmp_val=$(echo "$_cmp_val" | tr '[:upper:]' '[:lower:]') + fi + case "$_test_op" in + equals*) + if [[ -z "$_not" ]] && [[ "$_cond_val" != "$_cmp_val" ]]; then continue; + elif [[ "$_not" ]] && [[ "$_cond_val" == "$_cmp_val" ]]; then continue; + fi + ;; + startswith*) + if [[ -z "$_not" ]] && [[ "$_cond_val" != "$_cmp_val"* ]]; then continue; + elif [[ "$_not" ]] && [[ "$_cond_val" == "$_cmp_val"* ]]; then continue; + fi + ;; + endswith*) + if [[ -z "$_not" ]] && [[ "$_cond_val" != *"$_cmp_val" ]]; then continue; + elif [[ "$_not" ]] && [[ "$_cond_val" == *"$_cmp_val" ]]; then continue; + fi + ;; + contains*) + if [[ -z "$_not" ]] && [[ "$_cond_val" != *"$_cmp_val"* ]]; then continue; + elif [[ "$_not" ]] && [[ "$_cond_val" == *"$_cmp_val"* ]]; then continue; + fi + ;; + in*) + if [[ -z "$_not" ]] && [[ "__${_cmp_val}__" != *"__${_cond_val}__"* ]]; then continue; + elif [[ "$_not" ]] && [[ "__${_cmp_val}__" == *"__${_cond_val}__"* ]]; then continue; + fi + ;; + esac + ;; + *) + log_warn "... unrecognized test operator \\e[1;91m${_test_op}\\e[0m in \\e[33;1m${_scoped_var}\\e[0m" + continue + ;; + esac + # matches + _val=$(eval echo "\$${_target_var}") + log_info "... apply \\e[32m${_target_var}\\e[0m from \\e[32m\$${_scoped_var}\\e[0m${_val:+ (\\e[33;1moverwrite\\e[0m)}" + _val=$(eval echo "\$${_scoped_var}") + export "${_target_var}"="${_val}" + done + log_info "... done" + } + # builds the Java proxy options from Linux env (http_proxy, https_proxy, ftp_proxy and no_proxy) function eval_java_proxy_args() { # transform no_proxy into Java stype nonProxyHosts @@ -165,6 +247,7 @@ stages: } if [[ -z "$TEMPLATE_CHECK_UPDATE_DISABLED" ]]; then check_for_update sonar "1.0.0"; fi + unscope_variables # ENDSCRIPT