From 6a6d28b840574307ff3d190ed7465f9f5540c3b8 Mon Sep 17 00:00:00 2001
From: Pierre Smeyers <pierre.smeyers@gmail.com>
Date: Sat, 9 Jul 2022 08:54:32 +0200
Subject: [PATCH] feat: remove explicit MR analysis

---
 README.md                     | 22 ++++--------------
 kicker.json                   | 10 --------
 templates/gitlab-ci-sonar.yml | 43 +----------------------------------
 3 files changed, 6 insertions(+), 69 deletions(-)

diff --git a/README.md b/README.md
index e673625..ebfc7b0 100644
--- a/README.md
+++ b/README.md
@@ -32,28 +32,16 @@ It is bound to the `test` stage, and uses the following variables:
 | :lock: `SONAR_LOGIN`     | SonarQube login (depends on your authentication method)                | _none_ |
 | :lock: `SONAR_PASSWORD`  | SonarQube password (depends on your authentication method)             | _none_ |
 | `SONAR_BASE_ARGS`        | SonarQube [analysis arguments](https://docs.sonarqube.org/latest/analysis/analysis-parameters/) | `-Dsonar.links.homepage=${CI_PROJECT_URL} -Dsonar.links.ci=${CI_PROJECT_URL}/-/pipelines -Dsonar.links.issue=${CI_PROJECT_URL}/-/issues` |
-| :lock: `SONAR_GITLAB_TOKEN` | GitLab [access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) with `api` scope. When set, activates the [Sonar GitLab plugin](https://github.com/gabrie-allaigre/sonar-gitlab-plugin/#plugins-properties) integration. | _none_ |
-| `SONAR_BRANCH_ANALYSIS_DISABLED` | Set to `true` to disable automatic [Pull Request Analysis](https://docs.sonarqube.org/latest/analysis/pull-request/) and [Branch Analysis](https://docs.sonarqube.org/latest/branches/overview/)  | _none_ (enabled) |
 
 ### Automatic Branch Analysis & Merge Request Analysis
 
-By default, this template tries to auto-detect and use [Pull Request Analysis](https://docs.sonarqube.org/latest/analysis/pull-request/) or [Branch Analysis](https://docs.sonarqube.org/latest/branches/overview/) (depending on the context).
+This template relies on SonarScanner's [GitLab integration](https://docs.sonarqube.org/latest/analysis/gitlab-integration), that is able to auto-detect whether to launch Branch Analysis or Merge Request Analysis
+from GitLab's environment variables.
 
-Those is a great SonarQube features but it assumes one of the following conditions:
+:warning: This feature also depends on your SonarQube server version and license.
+If using Community Edition, you'll have to install the [sonarqube-community-branch-plugin](https://github.com/mc1arke/sonarqube-community-branch-plugin) to enable automatic Branch & Merge Request analysis (only works from SonarQube version 8).
 
-* you are using a [Developer Edition](https://www.sonarqube.org/developer-edition/) version,
-* or you are using Community Edition with an opensource plugin emulating those features, such as [sonarqube-community-branch-plugin](https://github.com/mc1arke/sonarqube-community-branch-plugin).
-
-If you're not in one of those cases, then you shall disable this feature by setting `SONAR_BRANCH_ANALYSIS_DISABLED`.
-
-If you leave the feature enabled, the template will try to auto-detect whether the current branch is associated to an open Merge Request or not:
-
-1. if you're running [Merge Request pipelines](https://docs.gitlab.com/ee/ci/yaml/workflow.html#switch-between-branch-pipelines-and-merge-request-pipelines), 
-   then the template will automatically detect the MR,
-2. if you're running [branch pipelines](https://docs.gitlab.com/ee/ci/yaml/workflow.html#switch-between-branch-pipelines-and-merge-request-pipelines)
-   instead, the template will have to call GitLab APIs to determine whether the current branch is associated to an open Merge Request.
-   This will only be possible if you provided `SONAR_GITLAB_TOKEN`. 
-   Otherwise, a simple [Branch Analysis](https://docs.sonarqube.org/latest/branches/overview/) is performed on the current branch.
+:warning: Merge Request Analysis only works if you're running [Merge Request pipeline](https://docs.gitlab.com/ee/ci/yaml/workflow.html#switch-between-branch-pipelines-and-merge-request-pipelines) strategy (default).
 
 ### Configuring SonarQube project key, project name and other parameters
 
diff --git a/kicker.json b/kicker.json
index f3d1ddc..d1214cc 100644
--- a/kicker.json
+++ b/kicker.json
@@ -45,16 +45,6 @@
       "description": "SonarQube [analysis arguments](https://docs.sonarqube.org/latest/analysis/analysis-parameters/)",
       "default": "-Dsonar.links.homepage=${CI_PROJECT_URL} -Dsonar.links.ci=${CI_PROJECT_URL}/-/pipelines -Dsonar.links.issue=${CI_PROJECT_URL}/-/issues",
       "advanced": true
-    },
-    {
-      "name": "SONAR_GITLAB_TOKEN",
-      "description": "GitLab API access token. When set, enables SonarQube [Pull Request Analysis](https://docs.sonarqube.org/latest/analysis/pull-request/)",
-      "secret": true
-    },
-    {
-      "name": "SONAR_BRANCH_ANALYSIS_DISABLED",
-      "description": "Set to disable automatic [Pull Request Analysis](https://docs.sonarqube.org/latest/analysis/pull-request/) and [Branch Analysis](https://docs.sonarqube.org/latest/branches/overview/)",
-      "type": "boolean"
     }
   ]
 }
diff --git a/templates/gitlab-ci-sonar.yml b/templates/gitlab-ci-sonar.yml
index fd5c112..e695587 100644
--- a/templates/gitlab-ci-sonar.yml
+++ b/templates/gitlab-ci-sonar.yml
@@ -134,46 +134,6 @@ stages:
     fi
   }
   
-  function sonar_autodetect_mr() {
-    if [[ "$SONAR_BRANCH_ANALYSIS_DISABLED" == "true" ]]
-    then
-      log_info "Branch Analysis and Merge Request Analysis are disabled"
-      return
-    fi
-    if [[ "$CI_MERGE_REQUEST_ID" ]]
-    then
-      # we are in an MR pipeline: no need to pass arguments as SonarScanner will
-      log_info "Merge Request pipeline detected: let SonarScanner handle..."
-      return
-    fi
-    if [[ "$CI_OPEN_MERGE_REQUESTS" ]]
-    then
-      # we are in a branch pipeline associated with at least one MR: try to use GitLab APIs to get details
-      if [[ "$SONAR_GITLAB_TOKEN" ]]
-      then
-        wget -q "$CI_API_V4_URL/projects/${CI_PROJECT_ID}/merge_requests?state=opened&source_branch=${CI_COMMIT_REF_NAME}&private_token=$SONAR_GITLAB_TOKEN" -O mr.json || log_warn "Failed requesting GitLab API: check \$SONAR_GITLAB_TOKEN"
-        if [[ -f mr.json ]] && [[ "$(cat mr.json)" != "[]" ]]
-        then
-          mr_title=$(sed -E 's/\[\{[^{]*"title":"([^"]*)".*/\1/g' < mr.json)
-          mr_target=$(sed -E 's/\[\{[^{]*"target_branch":"([^"]*)".*/\1/g' < mr.json)
-          mr_id=$(sed -E 's/\[\{[^{]*"iid":([0-9]+).*/\1/g' < mr.json)
-          log_info "Merge Request \\e[33;1m$mr_title\\e[0m detected associated to this branch: trigger MR analysis..."
-          export sonar_mr_args="-Dsonar.pullrequest.key=$mr_id -Dsonar.pullrequest.branch=${CI_COMMIT_REF_NAME} -Dsonar.pullrequest.base=$mr_target"
-        else 
-          log_info "No Merge Request associated to this branch: trigger branch analysis..."
-          export sonar_mr_args="-Dsonar.branch.name=${CI_COMMIT_REF_NAME}"
-        fi
-      else
-        log_warn "Current branch $CI_COMMIT_REF_NAME associated to a MR but can't retrieve details..."
-        log_warn "Consider changing to merge request pipelines or set \$SONAR_GITLAB_TOKEN to allow the template to retrieve MR details (see doc)"
-        export sonar_mr_args="-Dsonar.branch.name=${CI_COMMIT_REF_NAME}"
-      fi
-    else
-      log_info "Current branch $CI_COMMIT_REF_NAME not associated to any MR: trigger branch analysis..."
-      export sonar_mr_args="-Dsonar.branch.name=${CI_COMMIT_REF_NAME}"
-    fi
-  }
-
   function unscope_variables() {
     _scoped_vars=$(env | awk -F '=' "/^scoped__[a-zA-Z0-9_]+=/ {print \$1}" | sort)
     if [[ -z "$_scoped_vars" ]]; then return; fi
@@ -316,7 +276,6 @@ sonar:
     - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
     - eval_java_proxy_args
   script:
-    - sonar_autodetect_mr
     - |
       if [[ -z "$SONAR_PROJECT_KEY" ]] && ! has_sonar_param projectKey
       then
@@ -349,7 +308,7 @@ sonar:
       ${SONAR_PASSWORD+-Dsonar.password=$SONAR_PASSWORD} 
       ${SONAR_PROJECT_KEY+-Dsonar.projectKey=$SONAR_PROJECT_KEY} 
       ${SONAR_PROJECT_NAME+-Dsonar.projectName=$SONAR_PROJECT_NAME} 
-      $SONAR_BASE_ARGS $sonar_mr_args
+      $SONAR_BASE_ARGS
   rules:
     - !reference [.test-policy, rules]
 
-- 
GitLab