Skip to content

Commit

Permalink
Create rule S6893: Ensure whitespace in-between braces in template di…
Browse files Browse the repository at this point in the history
…rectives (#3581)
  • Loading branch information
github-actions[bot] authored Feb 1, 2024
1 parent fcf8910 commit 1598ec1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
23 changes: 23 additions & 0 deletions rules/S6893/kubernetes/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"title": "Ensure whitespace in-between braces in template directives",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "1min"
},
"tags": [
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6893",
"sqKey": "S6893",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM"
},
"attribute": "FORMATTED"
}
}
69 changes: 69 additions & 0 deletions rules/S6893/kubernetes/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
This rule raises an issue when template directives do not have a whitespace after the opening brace and before the closing brace. It's based on best practices in Helm and emphasizes the importance of maintaining this whitespace for better readability and understanding of the code.

== Why is this an issue?
The absence of whitespace in template directives in Helm can lead to several issues:

**Readability**: Code is read more often than it is written. When template directives are tightly packed without whitespaces, it can be challenging to distinguish between the braces and the content inside. This can slow down the process of understanding the code, especially for those who are new to the codebase.

**Maintainability**: Hard-to-read code is also hard to maintain. If a developer struggles to understand what a piece of code does due to poor formatting, they may introduce bugs when they try to modify it.

In summary, not having a whitespace after the opening brace and before the closing brace in template directives can make the code harder to read, understand, and maintain.


== How to fix it in Helm
To fix this issue, add a whitespace after the opening brace and before the closing brace in template directives. Also, ensure there is a whitespace after opening chomping braces `{{-` and before closing chomping braces `-}}`.

=== Code examples

==== Noncompliant code example

[source,text,diff-id=1,diff-type=noncompliant]
----
apiVersion: v1
kind: Pod
metadata:
name: {{.Values.podName}} # Noncompliant
spec:
containers:
- name: {{-.Values.containerName-}} # Noncompliant
----

==== Compliant solution

[source,text,diff-id=1,diff-type=compliant]
----
apiVersion: v1
kind: Pod
metadata:
name: {{ .Values.podName }}
spec:
containers:
- name: {{- .Values.containerName -}}
----


== Resources
=== Documentation
* Helm Best Practices - https://helm.sh/docs/chart_best_practices/templates/#formatting-templates[Formatting Templates]
* Go Documentation - https://pkg.go.dev/text/template?utm_source=godoc#hdr-Text_and_spaces[Text and spaces]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

If possible try to display a specific message tailored to the issue.

* Add a whitespace after `{{` in the template directive.
* Add a whitespace before `}}` in the template directive.
* Add a whitespace after `{{-` in the template directive.
* Add a whitespace before `}}-` in the template directive.


=== Highlighting

* Highlight the template directive that does not have a whitespace after opening braces `{{`, `{{-` and/ or before closing braces `}}`, `-}}`
endif::env-github,rspecator-view[]
2 changes: 2 additions & 0 deletions rules/S6893/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 1598ec1

Please sign in to comment.