Skip to content
Closed
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
43 changes: 43 additions & 0 deletions .ci/magician/cmd/generate_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"os"
Expand All @@ -34,6 +35,9 @@ import (
"github.com/GoogleCloudPlatform/magic-modules/tools/issue-labeler/labeler"

"github.com/spf13/cobra"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"go.abhg.dev/goldmark/frontmatter"
"golang.org/x/exp/maps"

_ "embed"
Expand Down Expand Up @@ -329,6 +333,11 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
errors[repo.Title] = append(errors[repo.Title], "The missing doc detector failed to run.")
}
data.MissingDocs = missingDocs

errStrs := checkDocumentFrontmatter(repo)
if len(errStrs) > 0 {
errors[repo.Title] = append(errors[repo.Title], errStrs...)
}
}

simpleDiff, err := computeAffectedResources(diffProcessorPath, rnr, repo)
Expand Down Expand Up @@ -636,3 +645,37 @@ func pathChanged(path string, changedFiles []string) bool {
func init() {
rootCmd.AddCommand(generateCommentCmd)
}

// checkDocumentFrontmatter checks changed markdown files' frontmatter
// structure in the repo and returns error strings when applicable.
func checkDocumentFrontmatter(repo source.Repo) []string {
var errs []string
for _, f := range repo.ChangedFiles {
if !strings.HasSuffix(f, ".markdown") {
continue
}
src, err := os.ReadFile(filepath.Join(repo.Path, f))
if err != nil {
errs = append(errs, "Error reading file "+f)
continue
}

md := goldmark.New(
goldmark.WithExtensions(&frontmatter.Extender{}),
)

ctx := parser.NewContext()
var buff bytes.Buffer

err = md.Convert(src, &buff, parser.WithContext(ctx))
if err != nil {
errs = append(errs, "Error parsing file "+f)
continue
}
if d := frontmatter.Get(ctx); d == nil {
errs = append(errs, fmt.Sprintf("No frontmatter found in file %s. This is usually due to an incorrect structure in the frontmatter.", f))
}

}
return errs
}
66 changes: 66 additions & 0 deletions .ci/magician/cmd/generate_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"os"
"path/filepath"
"reflect"
"testing"

Expand Down Expand Up @@ -510,3 +511,68 @@ func TestPathChanged(t *testing.T) {
})
}
}

func TestCheckDocumentFrontmatter(t *testing.T) {
tmpDir := t.TempDir()
files := map[string]string{
"malformed.markdown": `
subcategory: Example Subcategory
---
`,
"sample.markdown": `
---
subcategory: Example Subcategory
---
`,
}
for name, content := range files {
fullPath := filepath.Join(tmpDir, name)
err := os.WriteFile(fullPath, []byte(content), 0644)
if err != nil {
t.Fatalf("Failed to create file %s: %v", name, err)
}
}

tests := []struct {
name string
changedFiles []string
wantErr bool
}{
{
name: "No changed files",
changedFiles: []string{"abc.txt"},
wantErr: false,
},
{
name: "malformed markdown",
changedFiles: []string{"malformed.markdown"},
wantErr: true,
},
{
name: "not exist markdown",
changedFiles: []string{"abc.markdown"},
wantErr: true,
},
{
name: "Changed files with no frontmatter",
changedFiles: []string{"sample.markdown"},
wantErr: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
repo := source.Repo{
Path: tmpDir,
ChangedFiles: tc.changedFiles,
}
got := checkDocumentFrontmatter(repo)
if tc.wantErr && len(got) == 0 {
t.Errorf("checkDocumentFrontmatter() = %v, want error", got)
}
if !tc.wantErr && len(got) > 0 {
t.Errorf("checkDocumentFrontmatter() = %v, want no error", got)
}
})
}
}
3 changes: 3 additions & 0 deletions .ci/magician/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ require (
github.com/google/go-github/v68 v68.0.0
github.com/otiai10/copy v1.12.0
github.com/stretchr/testify v1.10.0
github.com/yuin/goldmark v1.7.8
go.abhg.dev/goldmark/frontmatter v0.2.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -31,6 +33,7 @@ require (
cloud.google.com/go/compute/metadata v0.6.0 // indirect
cloud.google.com/go/iam v1.2.2 // indirect
cloud.google.com/go/monitoring v1.21.2 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48.1 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.48.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions .ci/magician/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ cloud.google.com/go/storage v1.50.0/go.mod h1:l7XeiD//vx5lfqE3RavfmU9yvk5Pp0Zhcv
cloud.google.com/go/trace v1.11.2 h1:4ZmaBdL8Ng/ajrgKqY5jfvzqMXbrDcBsUGXOT9aqTtI=
cloud.google.com/go/trace v1.11.2/go.mod h1:bn7OwXd4pd5rFuAnTrzBuoZ4ax2XQeG3qNgYmfCy0Io=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 h1:3c8yed4lgqTt+oTQ+JNMDo+F4xprBf+O/il4ZC0nRLw=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0/go.mod h1:obipzmGjfSjam60XLwGfqUkJsfiheAl+TUjG+4yzyPM=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.48.1 h1:UQ0AhxogsIRZDkElkblfnwjc3IaltCm2HUMvezQaL7s=
Expand Down Expand Up @@ -132,6 +134,10 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.abhg.dev/goldmark/frontmatter v0.2.0 h1:P8kPG0YkL12+aYk2yU3xHv4tcXzeVnN+gU0tJ5JnxRw=
go.abhg.dev/goldmark/frontmatter v0.2.0/go.mod h1:XqrEkZuM57djk7zrlRUB02x8I5J0px76YjkOzhB4YlU=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib/detectors/gcp v1.29.0 h1:TiaiXB4DpGD3sdzNlYQxruQngn5Apwzi1X0DRhuGvDQ=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

subcategory: "Cloud SQL"
description: |-
Creates a new SQL user in Google Cloud SQL.
Expand Down