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

CLOUDP-266431: Add the output transformation logic to foascli (skeleton) #144

Merged
merged 4 commits into from
Aug 8, 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
1 change: 1 addition & 0 deletions tools/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/wI2L/jsondiff v0.6.0 // indirect
github.com/yargevad/filepathx v1.0.0 // indirect
github.com/yuin/goldmark v1.7.4 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/text v0.15.0 // indirect
)
2 changes: 2 additions & 0 deletions tools/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ github.com/wI2L/jsondiff v0.6.0 h1:zrsH3FbfVa3JO9llxrcDy/XLkYPLgoMX6Mz3T2PP2AI=
github.com/wI2L/jsondiff v0.6.0/go.mod h1:D6aQ5gKgPF9g17j+E9N7aasmU1O+XvfmWm1y8UMmNpw=
github.com/yargevad/filepathx v1.0.0 h1:SYcT+N3tYGi+NvazubCNlvgIPbzAk7i7y2dwg3I5FYc=
github.com/yargevad/filepathx v1.0.0/go.mod h1:BprfX/gpYNJHJfc35GjRRpVcwWXS89gGulUIU5tK3tA=
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=
Expand Down
86 changes: 86 additions & 0 deletions tools/cli/internal/changelog/changelog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2024 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package changelog

import (
"github.com/mongodb/openapi/tools/cli/internal/changelog/outputfilter"
"github.com/mongodb/openapi/tools/cli/internal/openapi"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
)

const (
deprecationDaysStable = 365 // min days required between deprecating a stable resource and removing it
deprecationDaysBeta = 365 // min days required between deprecating a beta resource and removing it
)

var breakingChangesAdditionalCheckers = []string{
"response-non-success-status-removed",
"api-operation-id-removed",
"api-tag-removed",
"response-property-enum-value-removed",
"response-mediatype-enum-value-removed",
"request-body-enum-value-removed",
"api-schema-removed",
}

type Metadata struct {
Base *load.SpecInfo
Revision *load.SpecInfo // the new spec to compare against the base
Config *checker.Config
OasDiff *openapi.OasDiff
ExceptionFilePath string
}

func NewMetadata(base, revision, exceptionFilePath string) (*Metadata, error) {
baseSpec, err := openapi.CreateNormalizedOpenAPISpecFromPath(base)
if err != nil {
return nil, err
}

revisionSpec, err := openapi.CreateNormalizedOpenAPISpecFromPath(revision)
if err != nil {
return nil, err
}

changelogConfig := checker.NewConfig(
checker.GetAllChecks()).WithOptionalChecks(breakingChangesAdditionalCheckers).WithDeprecation(deprecationDaysBeta, deprecationDaysStable)

return &Metadata{
Base: baseSpec,
Revision: revisionSpec,
ExceptionFilePath: exceptionFilePath,
Config: changelogConfig,
OasDiff: openapi.NewOasDiffWithSpecInfo(baseSpec, revisionSpec, &diff.Config{
IncludePathParams: true,
}),
}, nil
}

func (s *Metadata) Check() ([]*outputfilter.Entry, error) {
diffResult, err := s.OasDiff.NewDiffResult()
if err != nil {
return nil, err
}

changes := checker.CheckBackwardCompatibilityUntilLevel(
s.Config,
diffResult.Report,
diffResult.SourceMap,
checker.INFO)

return outputfilter.NewChangelogEntries(changes, diffResult.SpecInfoPair)
}
79 changes: 79 additions & 0 deletions tools/cli/internal/changelog/outputfilter/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package outputfilter

import (
"regexp"
)

func transformMessage(entry *Entry) *Entry {
text := entry.Text
text = removeResponseStatusCodes(text)
text = setValueSet(text)
text = setValueRemoved(text)
text = removeInlineSchemaIndex(text)
text = removeRedundantOneOfAllOf(text)
entry.Text = text

return entry
}

func removeRedundantOneOfAllOf(text string) string {
blva marked this conversation as resolved.
Show resolved Hide resolved
// /oneOf/components/schemas/<ViewName>/
re := regexp.MustCompile(`/oneOf/components/schemas/[^/]+/`)
text = re.ReplaceAllString(text, "")

re = regexp.MustCompile(`/allOf/components/schemas/[^/]+/`)
text = re.ReplaceAllString(text, "")

re = regexp.MustCompile(`oneOf\[#/components/schemas/[^/]+\]/`)
text = re.ReplaceAllString(text, "")

re = regexp.MustCompile(`allOf\[#/components/schemas/[^/]+\]/`)
text = re.ReplaceAllString(text, "")

return text
}

// removeResponseStatusCodes removes the status codes from the response messages
func removeResponseStatusCodes(text string) string {
// to the response with the '200' status
re := regexp.MustCompile(` property for the response status '\d{3}'`)
text = re.ReplaceAllString(text, " property")

re = regexp.MustCompile(` to the response with the '\d{3}' status`)
text = re.ReplaceAllString(text, " to the response")

re = regexp.MustCompile(` from the response with the '\d{3}' status`)
text = re.ReplaceAllString(text, " from the response")

re = regexp.MustCompile(` list for the response status \d{3}`)
text = re.ReplaceAllString(text, " list for the response")

re = regexp.MustCompile(` for the status '\d{3}'`)
text = re.ReplaceAllString(text, "")

return text
}

func setValueSet(text string) string {
re := regexp.MustCompile(`default value( was)? changed from 'null' to ('.+')`)
text = re.ReplaceAllString(text, "default value was set to $2")

return text
}

func setValueRemoved(text string) string {
re := regexp.MustCompile(`default value( was)? changed from '.+' to 'null'`)
text = re.ReplaceAllString(text, "default value was removed")

return text
}

func removeInlineSchemaIndex(text string) string {
re := regexp.MustCompile(`BaseSchema\[\d+]:`)
text = re.ReplaceAllString(text, "")

re = regexp.MustCompile(`RevisionSchema\[\d+]:`)
text = re.ReplaceAllString(text, "")

return text
}
54 changes: 54 additions & 0 deletions tools/cli/internal/changelog/outputfilter/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package outputfilter

import (
"reflect"
"testing"
)

func TestTransformMessagesInTextField(t *testing.T) {
tests := []struct {
name string
entry *Entry
expected *Entry
}{
{
name: "Remove response status codes",
entry: &Entry{Text: " property for the response status '200'"},
expected: &Entry{Text: " property"},
},
{
name: "Set value removed",
entry: &Entry{Text: "default value changed from 'some_value' to 'null'"},
expected: &Entry{Text: "default value was removed"},
},
{
name: "Set value set",
entry: &Entry{Text: "default value changed from 'null' to 'some_value'"},
expected: &Entry{Text: "default value was set to 'some_value'"},
},
{
name: "Remove inline schema index",
entry: &Entry{Text: "BaseSchema[123]: some text"},
expected: &Entry{Text: " some text"},
},
{
name: "Remove redundant oneOf/allOf",
entry: &Entry{Text: "/oneOf/components/schemas/ViewName/"},
expected: &Entry{Text: ""},
},
{
name: "Mixed transformations",
entry: &Entry{Text: "default value changed from 'null' to 'some_value' BaseSchema[123]: /oneOf/components/schemas/ViewName/"},
expected: &Entry{Text: "default value was set to 'some_value' "},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := transformMessage(tt.entry)
if !reflect.DeepEqual(actual, tt.expected) {
t.Errorf("transformMessagesInTextField() = %v, want %v", actual, tt.expected)
}
})
}
}
52 changes: 52 additions & 0 deletions tools/cli/internal/changelog/outputfilter/outputfilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package outputfilter

import (
"encoding/json"

"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/formatters"
"github.com/tufin/oasdiff/load"
)

const lan = "en" // language for localized output

type Entry struct {
ID string `json:"id"`
Text string `json:"text"`
Level int `json:"level"`
Operation string `json:"operation,omitempty"`
OperationID string `json:"operationId,omitempty"`
Path string `json:"path,omitempty"`
Source string `json:"source,omitempty"`
Section string `json:"section"`
}

func NewChangelogEntries(checkers checker.Changes, specInfoPair *load.SpecInfoPair) ([]*Entry, error) {
formatter, err := formatters.Lookup("json", formatters.FormatterOpts{
Language: lan,
})
if err != nil {
return nil, err
}

bytes, err := formatter.RenderChangelog(checkers, formatters.RenderOpts{ColorMode: checker.ColorAuto}, specInfoPair)
if err != nil {
return nil, err
}

var entries []*Entry
err = json.Unmarshal(bytes, &entries)
if err != nil {
return nil, err
}

return transformEntries(entries), nil
}

func transformEntries(entries []*Entry) []*Entry {
for _, entry := range entries {
transformMessage(entry)
}

return entries
}
18 changes: 10 additions & 8 deletions tools/cli/internal/cli/changelog/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"encoding/json"
"fmt"

"github.com/mongodb/openapi/tools/cli/internal/changelog"
"github.com/mongodb/openapi/tools/cli/internal/cli/flag"
"github.com/mongodb/openapi/tools/cli/internal/cli/usage"
"github.com/mongodb/openapi/tools/cli/internal/openapi"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
Expand All @@ -34,7 +34,7 @@ type Opts struct {
}

func (o *Opts) Run() error {
changelog, err := openapi.NewChangelog(
metadata, err := changelog.NewMetadata(
fmt.Sprintf("%s/%s", o.basePath, "v2.json"),
fmt.Sprintf("%s/%s", o.revisionPath, "v2.json"),
o.exceptionsPaths)
Expand All @@ -43,18 +43,20 @@ func (o *Opts) Run() error {
return err
}

checks, err := changelog.Check()
checks, err := metadata.Check()
if err != nil {
return err
}

fmt.Print("Printing the checks\n")
base, err := json.MarshalIndent(*checks, "", " ")
if err != nil {
return err
}
for _, check := range checks {
base, err := json.MarshalIndent(*check, "", " ")
if err != nil {
return err
}

fmt.Println(string(base))
fmt.Println(string(base))
}

return nil
}
Expand Down
59 changes: 0 additions & 59 deletions tools/cli/internal/openapi/changelog.go

This file was deleted.

Loading
Loading