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

feat: warn if readme plugin contains a comment. #253

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions pkg/analysis/passes/readme/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package readme
import (
"os"
"path/filepath"
"regexp"
"strings"

"github.com/grafana/plugin-validator/pkg/analysis"
Expand All @@ -11,6 +12,7 @@ import (

var (
missingReadme = &analysis.Rule{Name: "missing-readme", Severity: analysis.Error}
readmeComment = &analysis.Rule{Name: "readme-comment", Severity: analysis.Warning}
)

var Analyzer = &analysis.Analyzer{
Expand All @@ -33,15 +35,30 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, nil
}
return nil, err
} else {
if len(strings.TrimSpace(string(b))) == 0 {
pass.ReportResult(pass.AnalyzerName, missingReadme, "README.md is empty", "A README.md file is required for plugins. The contents of the file will be displayed in the Plugin catalog.")
return nil, nil
}
if missingReadme.ReportAll {
missingReadme.Severity = analysis.OK
pass.ReportResult(pass.AnalyzerName, missingReadme, "README.md: exists", "")
}
}
if len(strings.TrimSpace(string(b))) == 0 {
pass.ReportResult(pass.AnalyzerName, missingReadme, "README.md is empty", "A README.md file is required for plugins. The contents of the file will be displayed in the Plugin catalog.")
return nil, nil
}
if missingReadme.ReportAll {
missingReadme.Severity = analysis.OK
pass.ReportResult(pass.AnalyzerName, missingReadme, "README.md: exists", "")
}

readmeContent := string(b)

commentRegex := `<!--(.*?)-->`
re := regexp.MustCompile(commentRegex)

// No need find all
comment := re.FindString(readmeContent)

if len(comment) > 0 {
pass.ReportResult(pass.AnalyzerName, readmeComment, "README.md contains comment(s).", "")
}
if readmeComment.ReportAll {
readmeComment.Severity = analysis.OK
pass.ReportResult(pass.AnalyzerName, readmeComment, "README.md does not contain any comments.", "")
}

return b, nil
Expand Down
17 changes: 17 additions & 0 deletions pkg/analysis/passes/readme/readme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,20 @@ func TestReadmeValid(t *testing.T) {

require.Len(t, interceptor.Diagnostics, 0)
}

func TestWithComment(t *testing.T) {
var interceptor testpassinterceptor.TestPassInterceptor
pass := &analysis.Pass{
RootDir: filepath.Join("./"),
ResultOf: map[*analysis.Analyzer]interface{}{
archive.Analyzer: filepath.Join("testdata", "comment"),
},
Report: interceptor.ReportInterceptor(),
}
_, err := Analyzer.Run(pass)
require.NoError(t, err)

require.Len(t, interceptor.Diagnostics, 1)
require.Equal(t, interceptor.Diagnostics[0].Severity, analysis.Warning)
require.Equal(t, interceptor.Diagnostics[0].Title, "README.md contains comment(s).")
}
5 changes: 5 additions & 0 deletions pkg/analysis/passes/readme/testdata/comment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Comment

<!-- Some comment -->

Normal text.