From e9fba55db6a604f98fade13a63c07a6994ab4af9 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Tue, 19 Sep 2023 09:44:36 +0200 Subject: [PATCH] modulegen: generate sonar configuration (#1644) Signed-off-by: Matthieu MOREL --- .../_template/sonar-project.properties.tmpl | 21 +++++++++ modulegen/internal/context/context.go | 4 ++ modulegen/internal/main.go | 2 + modulegen/internal/sonar/main.go | 43 +++++++++++++++++++ modulegen/internal/sonar/types.go | 38 ++++++++++++++++ sonar-project.properties | 3 +- 6 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 modulegen/_template/sonar-project.properties.tmpl create mode 100644 modulegen/internal/sonar/main.go create mode 100644 modulegen/internal/sonar/types.go diff --git a/modulegen/_template/sonar-project.properties.tmpl b/modulegen/_template/sonar-project.properties.tmpl new file mode 100644 index 0000000000..cde281ed29 --- /dev/null +++ b/modulegen/_template/sonar-project.properties.tmpl @@ -0,0 +1,21 @@ +# This file is autogenerated by the 'modulegen' tool. +# Github organization linked to sonarcloud +sonar.organization=testcontainers + +# Project key from sonarcloud dashboard for Github Action, otherwise pick a project key you like +sonar.projectKey=testcontainers_testcontainers-go + +sonar.projectName=testcontainers-go + +sonar.projectVersion={{ .ProjectVersion }} + +sonar.sources=. + +sonar.exclusions=**/*_test.go,**/vendor/**,**/testdata/** + +sonar.tests=. +sonar.test.inclusions=**/*_test.go +sonar.test.exclusions=**/vendor/** + +sonar.go.coverage.reportPaths=**/coverage.out +sonar.go.tests.reportPaths={{ .Go.Tests.ReportPaths }} diff --git a/modulegen/internal/context/context.go b/modulegen/internal/context/context.go index c4d697b93b..d01de43ecb 100644 --- a/modulegen/internal/context/context.go +++ b/modulegen/internal/context/context.go @@ -88,6 +88,10 @@ func (ctx Context) MkdocsConfigFile() string { return filepath.Join(ctx.RootDir, "mkdocs.yml") } +func (ctx Context) SonarProjectFile() string { + return filepath.Join(ctx.RootDir, "sonar-project.properties") +} + func (ctx Context) VSCodeWorkspaceFile() string { return filepath.Join(ctx.RootDir, ".vscode", ".testcontainers-go.code-workspace") } diff --git a/modulegen/internal/main.go b/modulegen/internal/main.go index a86969c27f..df2916a86d 100644 --- a/modulegen/internal/main.go +++ b/modulegen/internal/main.go @@ -9,6 +9,7 @@ import ( "github.com/testcontainers/testcontainers-go/modulegen/internal/make" "github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs" "github.com/testcontainers/testcontainers-go/modulegen/internal/module" + "github.com/testcontainers/testcontainers-go/modulegen/internal/sonar" "github.com/testcontainers/testcontainers-go/modulegen/internal/tools" "github.com/testcontainers/testcontainers-go/modulegen/internal/vscode" "github.com/testcontainers/testcontainers-go/modulegen/internal/workflow" @@ -80,6 +81,7 @@ func GenerateFiles(ctx context.Context, tcModule context.TestcontainersModule) e projectGenerators := []ProjectGenerator{ workflow.Generator{}, // update github ci workflow vscode.Generator{}, // update vscode workspace + sonar.Generator{}, // update sonar-project.properties } for _, generator := range projectGenerators { diff --git a/modulegen/internal/sonar/main.go b/modulegen/internal/sonar/main.go new file mode 100644 index 0000000000..64dbd149b5 --- /dev/null +++ b/modulegen/internal/sonar/main.go @@ -0,0 +1,43 @@ +package sonar + +import ( + "fmt" + "path/filepath" + "text/template" + + "github.com/testcontainers/testcontainers-go/modulegen/internal/context" + "github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs" + internal_template "github.com/testcontainers/testcontainers-go/modulegen/internal/template" +) + +type Generator struct{} + +// Generate updates sonar-project.properties +func (g Generator) Generate(ctx context.Context) error { + rootCtx, err := context.GetRootContext() + if err != nil { + return err + } + examples, err := rootCtx.GetExamples() + if err != nil { + return err + } + modules, err := rootCtx.GetModules() + if err != nil { + return err + } + mkdocsConfig, err := mkdocs.ReadConfig(rootCtx.MkdocsConfigFile()) + if err != nil { + fmt.Printf(">> could not read MkDocs config: %v\n", err) + return err + } + tcVersion := mkdocsConfig.Extra.LatestVersion + config := newConfig(tcVersion, examples, modules) + name := "sonar-project.properties.tmpl" + t, err := template.New(name).ParseFiles(filepath.Join("_template", name)) + if err != nil { + return err + } + + return internal_template.GenerateFile(t, ctx.SonarProjectFile(), name, config) +} diff --git a/modulegen/internal/sonar/types.go b/modulegen/internal/sonar/types.go new file mode 100644 index 0000000000..3a7a541444 --- /dev/null +++ b/modulegen/internal/sonar/types.go @@ -0,0 +1,38 @@ +package sonar + +import ( + "sort" + "strings" +) + +type Config struct { + Go Go + ProjectVersion string +} + +type Go struct { + Tests Tests +} + +type Tests struct { + ReportPaths string +} + +func newConfig(tcVersion string, examples []string, modules []string) *Config { + reportPaths := []string{"TEST-unit.xml", "modulegen/TEST-unit.xml"} + for _, example := range examples { + reportPaths = append(reportPaths, "examples/"+example+"/TEST-unit.xml") + } + for _, module := range modules { + reportPaths = append(reportPaths, "modules/"+module+"/TEST-unit.xml") + } + sort.Strings(reportPaths) + return &Config{ + Go: Go{ + Tests: Tests{ + ReportPaths: strings.Join(reportPaths, ","), + }, + }, + ProjectVersion: tcVersion, + } +} diff --git a/sonar-project.properties b/sonar-project.properties index d625b7fa53..66b7baa422 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,3 +1,4 @@ +# This file is autogenerated by the 'modulegen' tool. # Github organization linked to sonarcloud sonar.organization=testcontainers @@ -17,4 +18,4 @@ sonar.test.inclusions=**/*_test.go sonar.test.exclusions=**/vendor/** sonar.go.coverage.reportPaths=**/coverage.out -sonar.go.tests.reportPaths=**/TEST-unit.xml +sonar.go.tests.reportPaths=TEST-unit.xml,examples/bigtable/TEST-unit.xml,examples/cockroachdb/TEST-unit.xml,examples/consul/TEST-unit.xml,examples/datastore/TEST-unit.xml,examples/firestore/TEST-unit.xml,examples/nginx/TEST-unit.xml,examples/pubsub/TEST-unit.xml,examples/spanner/TEST-unit.xml,examples/toxiproxy/TEST-unit.xml,modulegen/TEST-unit.xml,modules/artemis/TEST-unit.xml,modules/clickhouse/TEST-unit.xml,modules/compose/TEST-unit.xml,modules/couchbase/TEST-unit.xml,modules/elasticsearch/TEST-unit.xml,modules/k3s/TEST-unit.xml,modules/kafka/TEST-unit.xml,modules/localstack/TEST-unit.xml,modules/mariadb/TEST-unit.xml,modules/mongodb/TEST-unit.xml,modules/mysql/TEST-unit.xml,modules/nats/TEST-unit.xml,modules/neo4j/TEST-unit.xml,modules/postgres/TEST-unit.xml,modules/pulsar/TEST-unit.xml,modules/redis/TEST-unit.xml,modules/redpanda/TEST-unit.xml,modules/vault/TEST-unit.xml