Skip to content

Commit

Permalink
Fix lint. (#304)
Browse files Browse the repository at this point in the history
* Fix lint.

* Fix semgrep issue (using of 'text/template').

* Fix issue.
  • Loading branch information
nickeskov authored Nov 15, 2024
1 parent 10a404d commit 5948f62
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 33 deletions.
24 changes: 6 additions & 18 deletions cmd/bots/internal/common/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"embed"
"encoding/json"
"fmt"
htmlTemplate "html/template"
"html/template"
"sort"
"strconv"
"strings"
"sync"
textTemplate "text/template"

"nodemon/cmd/bots/internal/common/messaging"
"nodemon/pkg/entities"
Expand Down Expand Up @@ -404,7 +403,7 @@ func nodesToUrls(nodes []entities.Node) []string {

func (tgEnv *TelegramBotEnvironment) NodesListMessage(nodes []entities.Node) (string, error) {
urls := nodesToUrls(nodes)
tmpl, err := htmlTemplate.ParseFS(templateFiles, "templates/nodes_list.html")
tmpl, err := template.ParseFS(templateFiles, "templates/nodes_list.html")
if err != nil {
tgEnv.zap.Error("failed to parse nodes list template", zap.Error(err))
return "", err
Expand Down Expand Up @@ -493,7 +492,7 @@ type subscriptionsList struct {
}

func (tgEnv *TelegramBotEnvironment) SubscriptionsList() (string, error) {
tmpl, err := htmlTemplate.ParseFS(templateFiles, "templates/subscriptions.html")
tmpl, err := template.ParseFS(templateFiles, "templates/subscriptions.html")

if err != nil {
tgEnv.zap.Error("failed to parse subscriptions template", zap.Error(err))
Expand Down Expand Up @@ -613,21 +612,10 @@ func sortNodesStatuses(statuses []NodeStatus) {
})
}

func executeTemplate(template string, data any, extension ExpectedExtension) (string, error) {
func executeTemplate(templateName string, data any, extension ExpectedExtension) (string, error) {
switch extension {
case HTML:
tmpl, err := htmlTemplate.ParseFS(templateFiles, template+string(extension))
if err != nil {
return "", err
}
buffer := &bytes.Buffer{}
err = tmpl.Execute(buffer, data)
if err != nil {
return "", err
}
return buffer.String(), nil
case Markdown:
tmpl, err := textTemplate.ParseFS(templateFiles, template+string(extension))
case HTML, Markdown:
tmpl, err := template.ParseFS(templateFiles, templateName+string(extension))
if err != nil {
return "", err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/analysis/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ func runTestCase(zap *zap.Logger, test testCase) func(t *testing.T) {
for j := range test.expectedAlerts {
select {
case actualAlert := <-alerts:
stateHashAlert := *actualAlert.(*entities.StateHashAlert)
require.Contains(t, test.expectedAlerts, stateHashAlert, "test case #%d", j+1)
stateHashAlert, ok := actualAlert.(*entities.StateHashAlert)
require.True(t, ok, "unexpected alert type: %T", actualAlert)
require.Contains(t, test.expectedAlerts, *stateHashAlert, "test case #%d", j+1)
case <-time.After(5 * time.Second):
require.Fail(t, "timeout exceeded")
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/analysis/criteria/base_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ func TestBaseTargetCriterion_Analyze(t *testing.T) {
for j := range test.expectedAlerts {
select {
case actualAlert := <-alerts:
baseTargetAlert := *actualAlert.(*entities.BaseTargetAlert)
require.Contains(t, test.expectedAlerts, baseTargetAlert, "test case #%d", j+1)
baseTargetAlert, ok := actualAlert.(*entities.BaseTargetAlert)
require.True(t, ok, "unexpected alert type: %T", actualAlert)
require.Contains(t, test.expectedAlerts, *baseTargetAlert, "test case #%d", j+1)
case <-time.After(5 * time.Second):
require.Fail(t, "timeout exceeded")
}
Expand Down Expand Up @@ -125,7 +126,8 @@ func TestNoBaseTargetAlertCriterion_Analyze(t *testing.T) {
select {
case actualAlert, ok := <-alerts:
if ok {
baseTargetAlert := *actualAlert.(*entities.BaseTargetAlert)
baseTargetAlert, btOk := actualAlert.(*entities.BaseTargetAlert)
require.True(t, btOk, "unexpected alert type: %T", actualAlert)
require.Fail(t, "unexpected alert: %v", baseTargetAlert)
}
return
Expand Down
5 changes: 3 additions & 2 deletions pkg/analysis/criteria/challenged_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ func TestChallengedBlockCriterion_Analyze(t *testing.T) {
for j := range test.expectedAlerts {
select {
case actualAlert := <-alerts:
challengedBlockAlert := *actualAlert.(*entities.ChallengedBlockAlert)
require.Contains(t, test.expectedAlerts, challengedBlockAlert, "test case #%d", j+1)
challengedBlockAlert, ok := actualAlert.(*entities.ChallengedBlockAlert)
require.True(t, ok, "unexpected alert type: %T", actualAlert)
require.Contains(t, test.expectedAlerts, *challengedBlockAlert, "test case #%d", j+1)
case <-time.After(5 * time.Second):
require.Fail(t, "timeout exceeded")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/analysis/criteria/height_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ func TestHeightCriterion_Analyze(t *testing.T) {
for j := range test.expectedAlerts {
select {
case actualAlert := <-alerts:
heightAlert := *actualAlert.(*entities.HeightAlert)
require.Contains(t, test.expectedAlerts, heightAlert, "test case #%d", j+1)
heightAlert, ok := actualAlert.(*entities.HeightAlert)
require.True(t, ok, "unexpected alert type: %T", actualAlert)
require.Contains(t, test.expectedAlerts, *heightAlert, "test case #%d", j+1)
case <-time.After(5 * time.Second):
require.Fail(t, "timeout exceeded")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/analysis/criteria/incomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ func TestIncompleteCriterion_Analyze(t *testing.T) {
for j := range test.expectedAlerts {
select {
case actualAlert := <-alerts:
incompleteAlert := *actualAlert.(*entities.IncompleteAlert)
require.Contains(t, test.expectedAlerts, incompleteAlert, "test case #%d", j+1)
incompleteAlert, ok := actualAlert.(*entities.IncompleteAlert)
require.True(t, ok, "unexpected alert type: %T", actualAlert)
require.Contains(t, test.expectedAlerts, *incompleteAlert, "test case #%d", j+1)
case <-time.After(5 * time.Second):
require.Fail(t, "timeout exceeded")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/analysis/criteria/state_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ func TestStateHashCriterion_Analyze(t *testing.T) {
for j := range test.expectedAlerts {
select {
case actualAlert := <-alerts:
stateHashAlert := *actualAlert.(*entities.StateHashAlert)
require.Contains(t, test.expectedAlerts, stateHashAlert, "test case #%d", j+1)
stateHashAlert, ok := actualAlert.(*entities.StateHashAlert)
require.True(t, ok, "unexpected alert type: %T", actualAlert)
require.Contains(t, test.expectedAlerts, *stateHashAlert, "test case #%d", j+1)
case <-time.After(5 * time.Second):
require.Fail(t, "timeout exceeded")
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/analysis/criteria/unreachable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ func TestUnreachableCriterion_Analyze(t *testing.T) {
for j := range test.expectedAlerts {
select {
case actualAlert := <-alerts:
unreachableAlert := *actualAlert.(*entities.UnreachableAlert)
require.Contains(t, test.expectedAlerts, unreachableAlert, "test case #%d", j+1)
unreachableAlert, ok := actualAlert.(*entities.UnreachableAlert)
require.True(t, ok, "unexpected alert type: %T", actualAlert)
require.Contains(t, test.expectedAlerts, *unreachableAlert, "test case #%d", j+1)
case <-time.After(5 * time.Second):
require.Fail(t, "timeout exceeded")
}
Expand Down

0 comments on commit 5948f62

Please sign in to comment.