Skip to content
Snippets Groups Projects
Commit 7052db8b authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

chore: improve auto detect MR

parent c44a43d5
Branches
Tags
No related merge requests found
......@@ -45,10 +45,14 @@ Those is a great SonarQube features but it assumes one of the following conditio
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, if `SONAR_GITLAB_TOKEN` is provided, the template will try to autodetect (using GitLab APIs) an opened merge request matching the current branch:
* If one is found, a SonarQube [Pull Request Analysis](https://docs.sonarqube.org/latest/analysis/pull-request/) will be made.
* Otherwise, a simple [Branch Analysis](https://docs.sonarqube.org/latest/branches/overview/) is performed on the current branch.
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.
### About Sonar GitLab plugin
......
......@@ -159,7 +159,10 @@ stages:
log_info "Merge Request pipeline detected: let SonarScanner handle..."
return
fi
if [[ -n "$SONAR_GITLAB_TOKEN" ]]
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)" != "[]" ]]
......@@ -174,12 +177,16 @@ stages:
export sonar_mr_args="-Dsonar.branch.name=${CI_COMMIT_REF_NAME}"
fi
else
log_info "\$SONAR_GITLAB_TOKEN unset: trigger branch analysis..."
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment