javamachr / sonar-gitlab-plugin

Add to each commit GitLab in a global commentary on the new anomalies added by this commit and add comment lines of modified files
GNU Lesser General Public License v3.0
125 stars 24 forks source link

No variable when customizing template with sonar-scanner #37

Open 69pmb opened 1 year ago

69pmb commented 1 year ago

Hello,

Use versions:

I would like to override the default template when using _sonarscanner. 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 ?