Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No variable when customizing template with sonar-scanner #37

Open
69pmb opened this issue Nov 22, 2023 · 0 comments
Open

No variable when customizing template with sonar-scanner #37

69pmb opened this issue Nov 22, 2023 · 0 comments

Comments

@69pmb
Copy link

69pmb commented Nov 22, 2023

Hello,

Use versions:

  • Gitlab: 16.3.5
  • Sonarqube: 9.9.2.77730
  • Sonar scanner: 5.0.1.3006-linux
  • Sonar-gitlab-plugin: 5.4.0

I would like to override the default template when using sonar_scanner.
I succeed to do it with maven by using this command for java projects:

 mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install org.jacoco:jacoco-maven-plugin:report sonar:sonar -s settings.xml
      -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA
      -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
      -Dsonar.gitlab.project_id=$CI_PROJECT_ID
      -Dsonar.gitlab.user_token=${TECHNICAL_GITLAB_TOKEN_API}
      -Dsonar.gitlab.global_template="$(cat sonar-comment.ftl)"
      -Dsonar.issuesReport.html.enable=true
      -Dsonar.login=${SONARQUBE_TOKEN}
      -Dsonar.branch.name=$CI_COMMIT_REF_NAME
      -Dsonar.newCode.referenceBranch=develop

For Angular projets I use this command:

sonar-scanner -X
      -Dsonar.login=$SONARQUBE_TOKEN
      -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA
      -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
      -Dsonar.gitlab.project_id=$CI_PROJECT_ID
      -Dsonar.gitlab.global_template="$(cat sonar-comment.ftl)"
      -Dsonar.gitlab.user_token=${TECHNICAL_GITLAB_TOKEN_API}
      -Dsonar.issuesReport.html.enable=true
      -Dsonar.branch.name=$CI_COMMIT_REF_NAME
      -Dsonar.newCode.referenceBranch=develop

The global comment is indeed overridden but variables are not available/evaluated.
My Template:

<#if qualityGate??>
  ## <@g gate=qualityGate/> The SonarQube analysis indicates that quality gate is <@s status=qualityGate.status/> <@g gate=qualityGate/>

    <#list qualityGate.conditions() as condition>
        <@c condition=condition/>

    </#list>
</#if>
<#macro g gate><#if gate.status == OK>✔️<#else>❌</#if></#macro>
<#macro c condition>* [${condition.metricName}](${sonarUrl}component_measures?branch=${refName?url('ISO-8859-1')}&id=YOUR_PROJECT_ID&view=list&metric=${condition.metricKey}) <@s status=condition.status/>: Actual value ${condition.actual}<#if condition.status == WARN> is ${condition.symbol} ${condition.warning}</#if><#if condition.status == ERROR> is ${condition.symbol} ${condition.error}</#if> <@s_emoji status=condition.status/></#macro>
<#macro s status><#if status == OK>passed<#elseif status == WARN>warning<#elseif status == ERROR>failed<#else>unknown</#if></#macro>
<#macro s_emoji status><#if status == OK>✔️<#elseif status == WARN>⚠️<#elseif status == ERROR>❌<#else>❔</#if></#macro>
<#assign newIssueCount = issueCount() notReportedIssueCount = issueCount(false)>
<#assign hasInlineIssues = newIssueCount gt notReportedIssueCount extraIssuesTruncated = notReportedIssueCount gt maxGlobalIssues>

---
### Report [**is available here**](${sonarUrl}dashboard?branch=${refName?url('ISO-8859-1')}&id=YOUR_PROJECT_ID) for more details.

<#if newIssueCount == 0>
SonarQube analysis reported no issues ! 👏
<#else>
**SonarQube analysis reported ${newIssueCount} new issue<#if newIssueCount gt 1>s</#if>:**
    <#assign newIssuesBlocker = issueCount(BLOCKER) newIssuesCritical = issueCount(CRITICAL) newIssuesMajor = issueCount(MAJOR) newIssuesMinor = issueCount(MINOR) newIssuesInfo = issueCount(INFO)>
    <#if newIssuesBlocker gt 0>
* ${emojiSeverity(BLOCKER)} ${newIssuesBlocker} Blocker
    </#if>
    <#if newIssuesCritical gt 0>
* ${emojiSeverity(CRITICAL)} ${newIssuesCritical} Critical
    </#if>
    <#if newIssuesMajor gt 0>
* ${emojiSeverity(MAJOR)} ${newIssuesMajor} Major
    </#if>
    <#if newIssuesMinor gt 0>
* ${emojiSeverity(MINOR)} ${newIssuesMinor} Minor
    </#if>
    <#if newIssuesInfo gt 0>
* ${emojiSeverity(INFO)} ${newIssuesInfo} Info
    </#if>
    <#if !disableIssuesInline && hasInlineIssues>

Watch the comments in the merge request to review them.
    </#if>
    <#if notReportedIssueCount gt 0>
        <#if !disableIssuesInline>
            <#if hasInlineIssues || extraIssuesTruncated>
                <#if notReportedIssueCount <= maxGlobalIssues>

#### ${notReportedIssueCount} extra issue<#if notReportedIssueCount gt 1>s</#if>
                <#else>

#### Top ${maxGlobalIssues} extra issue<#if maxGlobalIssues gt 1>s</#if>
                </#if>
            </#if>

Note: The following issues were found on lines that were not modified in the commit. Because these issues can't be reported as line comments, they are summarized here:
        <#elseif extraIssuesTruncated>

#### Top ${maxGlobalIssues} issue<#if maxGlobalIssues gt 1>s</#if>
        </#if>

        <#assign reportedIssueCount = 0>
        <#list issues(false) as issue>
            <#if reportedIssueCount < maxGlobalIssues>
1. ${print(issue)}
            </#if>
            <#assign reportedIssueCount++>
        </#list>
        <#if notReportedIssueCount gt maxGlobalIssues>
* ... ${notReportedIssueCount-maxGlobalIssues} more
        </#if>
    </#if>
</#if>

The generated message:

  ## ✔️ The SonarQube analysis indicates that quality gate is passed ✔️

* [](component_measures?branch=develop&id=com.xxx.dsp.xxx.apps:cart-web&view=list&metric=) passed: Actual value  ✔️
* [](component_measures?branch=develop&id=com.xxx.dsp.xxx.apps:cart-web&view=list&metric=) passed: Actual value  ✔️
* [](component_measures?branch=develop&id=com.xxx.dsp.xxx.apps:cart-web&view=list&metric=) passed: Actual value  ✔️

---
### Report [**is available here**](dashboard?branch=develop&id=com.xxx.dsp.xxx.apps:cart-web) for more details.

SonarQube analysis reported no issues ! 👏

Have you any idea please ?

@69pmb 69pmb changed the title No variable when customizes template with sonar-scanner No variable when customizing template with sonar-scanner Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant