Skip to content

Commit

Permalink
add version info to changelog (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven Harrison authored Nov 16, 2023
1 parent 56e7d64 commit 792c95c
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 66 deletions.
4 changes: 0 additions & 4 deletions checker/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package checker

type Changes []Change

func (changes Changes) Group() GroupedChanges {
return groupChanges(changes)
}

func (changes Changes) HasLevelOrHigher(level Level) bool {
for _, change := range changes {
if change.GetLevel() >= level {
Expand Down
21 changes: 6 additions & 15 deletions checker/changes_by_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,21 @@ type Endpoint struct {

type ChangesByEndpoint map[Endpoint]*Changes

type GroupedChanges struct {
APIChanges ChangesByEndpoint
}

func newGroupedChanges() GroupedChanges {
return GroupedChanges{
APIChanges: ChangesByEndpoint{},
}
}

func groupChanges(changes Changes) GroupedChanges {
func GroupChanges(changes Changes) ChangesByEndpoint {

result := newGroupedChanges()
apiChanges := ChangesByEndpoint{}

for _, change := range changes {
switch change.(type) {
case ApiChange:
ep := Endpoint{Path: change.GetPath(), Operation: change.GetOperation()}
if c, ok := result.APIChanges[ep]; ok {
if c, ok := apiChanges[ep]; ok {
*c = append(*c, change)
} else {
result.APIChanges[ep] = &Changes{change}
apiChanges[ep] = &Changes{change}
}
}
}
return result

return apiChanges
}
2 changes: 1 addition & 1 deletion checker/changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ func TestChanges_Count(t *testing.T) {
}

func TestChanges_Group(t *testing.T) {
require.Contains(t, changes.Group().APIChanges, checker.Endpoint{Path: "/test", Operation: "GET"})
require.Contains(t, checker.GroupChanges(changes), checker.Endpoint{Path: "/test", Operation: "GET"})
}
2 changes: 1 addition & 1 deletion formatters/format_githubactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestGitHubActionsFormatterr_NotImplemented(t *testing.T) {
_, err = formatter.RenderSummary(nil, formatters.RenderOpts{})
assert.Error(t, err)

_, err = formatter.RenderChangelog(nil, formatters.RenderOpts{})
_, err = formatter.RenderChangelog(nil, formatters.RenderOpts{}, nil)
assert.Error(t, err)

_, err = formatter.RenderChecks(nil, formatters.RenderOpts{})
Expand Down
11 changes: 9 additions & 2 deletions formatters/format_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
"github.com/tufin/oasdiff/report"
)

Expand All @@ -28,11 +29,17 @@ func (f HTMLFormatter) RenderDiff(diff *diff.Diff, opts RenderOpts) ([]byte, err
//go:embed templates/changelog.html
var changelog string

func (f HTMLFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts) ([]byte, error) {
type TemplateData struct {
APIChanges checker.ChangesByEndpoint
BaseVersion string
RevisionVersion string
}

func (f HTMLFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts, specInfoPair *load.SpecInfoPair) ([]byte, error) {
tmpl := template.Must(template.New("changelog").Parse(changelog))

var out bytes.Buffer
if err := tmpl.Execute(&out, changes.Group()); err != nil {
if err := tmpl.Execute(&out, TemplateData{checker.GroupChanges(changes), specInfoPair.GetBaseVersion(), specInfoPair.GetRevisionVersion()}); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion formatters/format_html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestHtmlFormatter_RenderChangelog(t *testing.T) {
},
}

out, err := formatter.RenderChangelog(testChanges, formatters.RenderOpts{})
out, err := formatter.RenderChangelog(testChanges, formatters.RenderOpts{}, nil)
require.NoError(t, err)
require.NotEmpty(t, string(out))
}
Expand Down
3 changes: 2 additions & 1 deletion formatters/format_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
)

type JSONFormatter struct {
Expand All @@ -25,7 +26,7 @@ func (f JSONFormatter) RenderBreakingChanges(changes checker.Changes, opts Rende
return printJSON(changes)
}

func (f JSONFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts) ([]byte, error) {
func (f JSONFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts, specInfoPair *load.SpecInfoPair) ([]byte, error) {
return printJSON(changes)
}

Expand Down
2 changes: 1 addition & 1 deletion formatters/format_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestJsonFormatter_RenderChangelog(t *testing.T) {
},
}

out, err := formatter.RenderChangelog(testChanges, formatters.RenderOpts{})
out, err := formatter.RenderChangelog(testChanges, formatters.RenderOpts{}, nil)
require.NoError(t, err)
require.Equal(t, string(out), "[{\"id\":\"change_id\",\"text\":\"This is a breaking change.\",\"level\":3}]")
}
Expand Down
2 changes: 1 addition & 1 deletion formatters/format_junit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestJUnitFormatter_NotImplemented(t *testing.T) {
_, err = formatter.RenderSummary(nil, formatters.RenderOpts{})
assert.Error(t, err)

_, err = formatter.RenderChangelog(nil, formatters.RenderOpts{})
_, err = formatter.RenderChangelog(nil, formatters.RenderOpts{}, nil)
assert.Error(t, err)

_, err = formatter.RenderChecks(nil, formatters.RenderOpts{})
Expand Down
3 changes: 2 additions & 1 deletion formatters/format_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
"github.com/tufin/oasdiff/report"
)

Expand Down Expand Up @@ -43,7 +44,7 @@ func (f TEXTFormatter) RenderBreakingChanges(changes checker.Changes, opts Rende
return result.Bytes(), nil
}

func (f TEXTFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts) ([]byte, error) {
func (f TEXTFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts, specInfoPair *load.SpecInfoPair) ([]byte, error) {
result := bytes.NewBuffer(nil)

if len(changes) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion formatters/format_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestTextFormatter_RenderChangelog(t *testing.T) {
},
}

out, err := formatter.RenderChangelog(testChanges, formatters.RenderOpts{})
out, err := formatter.RenderChangelog(testChanges, formatters.RenderOpts{}, nil)
require.NoError(t, err)
require.Equal(t, string(out), "1 changes: 1 error, 0 warning, 0 info\nerror, in components This is a breaking change. [change_id]. \n\n")
}
Expand Down
3 changes: 2 additions & 1 deletion formatters/format_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
"gopkg.in/yaml.v3"
)

Expand All @@ -25,7 +26,7 @@ func (f YAMLFormatter) RenderBreakingChanges(changes checker.Changes, opts Rende
return printYAML(changes)
}

func (f YAMLFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts) ([]byte, error) {
func (f YAMLFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts, specInfoPair *load.SpecInfoPair) ([]byte, error) {
return printYAML(changes)
}

Expand Down
2 changes: 1 addition & 1 deletion formatters/format_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestYamlFormatter_RenderChangelog(t *testing.T) {
},
}

out, err := formatter.RenderChangelog(testChanges, formatters.RenderOpts{})
out, err := formatter.RenderChangelog(testChanges, formatters.RenderOpts{}, nil)
require.NoError(t, err)
require.Equal(t, string(out), "- id: change_id\n text: This is a breaking change.\n level: 3\n")
}
Expand Down
3 changes: 2 additions & 1 deletion formatters/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/checker/localizations"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
"golang.org/x/exp/slices"
)

Expand All @@ -15,7 +16,7 @@ type Formatter interface {
RenderDiff(diff *diff.Diff, opts RenderOpts) ([]byte, error)
RenderSummary(diff *diff.Diff, opts RenderOpts) ([]byte, error)
RenderBreakingChanges(changes checker.Changes, opts RenderOpts) ([]byte, error)
RenderChangelog(changes checker.Changes, opts RenderOpts) ([]byte, error)
RenderChangelog(changes checker.Changes, opts RenderOpts, specInfoPair *load.SpecInfoPair) ([]byte, error)
RenderChecks(checks Checks, opts RenderOpts) ([]byte, error)
RenderFlatten(spec *openapi3.T, opts RenderOpts) ([]byte, error)
SupportedOutputs() []Output
Expand Down
3 changes: 2 additions & 1 deletion formatters/not_implemented.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
)

type notImplementedFormatter struct{}
Expand All @@ -22,7 +23,7 @@ func (f notImplementedFormatter) RenderBreakingChanges(checker.Changes, RenderOp
return notImplemented()
}

func (f notImplementedFormatter) RenderChangelog(checker.Changes, RenderOpts) ([]byte, error) {
func (f notImplementedFormatter) RenderChangelog(checker.Changes, RenderOpts, *load.SpecInfoPair) ([]byte, error) {
return notImplemented()
}

Expand Down
3 changes: 1 addition & 2 deletions formatters/templates/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
margin: 1em 0 0.5em 0;
font-family: 'Ultra', sans-serif;
font-size: 36px;
text-transform: uppercase;
}

.path {
Expand Down Expand Up @@ -100,7 +99,7 @@
</head>

<body>
<div class="title">Changelog</div>
<div class="title">API Changelog {{ .BaseVersion }} vs. {{ .RevisionVersion }} </div>
{{ range $endpoint, $changes := .APIChanges }}
<div class="endpoint">
<div class="endpoint-header">
Expand Down
2 changes: 1 addition & 1 deletion internal/breaking_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func runBreakingChanges(flags Flags, stdout io.Writer) (bool, *ReturnError) {
return getChangelog(flags, stdout, checker.WARN)
}

func outputBreakingChanges(config checker.Config, format string, lang string, stdout io.Writer, errs checker.Changes, level checker.Level) *ReturnError {
func outputBreakingChanges(format string, lang string, stdout io.Writer, errs checker.Changes) *ReturnError {
// formatter lookup
formatter, err := formatters.Lookup(format, formatters.FormatterOpts{
Language: lang,
Expand Down
13 changes: 7 additions & 6 deletions internal/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/tufin/oasdiff/checker/localizations"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/formatters"
"github.com/tufin/oasdiff/load"
)

func getChangelogCmd() *cobra.Command {
Expand Down Expand Up @@ -58,7 +59,7 @@ func getChangelog(flags Flags, stdout io.Writer, level checker.Level) (bool, *Re

openapi3.CircularReferenceCounter = flags.getCircularReferenceCounter()

diffReport, operationsSources, err := calcDiff(flags)
diffResult, err := calcDiff(flags)
if err != nil {
return false, err
}
Expand All @@ -67,7 +68,7 @@ func getChangelog(flags Flags, stdout io.Writer, level checker.Level) (bool, *Re
bcConfig.Localize = checker.NewLocalizer(flags.getLang())

errs, returnErr := filterIgnored(
checker.CheckBackwardCompatibilityUntilLevel(bcConfig, diffReport, operationsSources, level),
checker.CheckBackwardCompatibilityUntilLevel(bcConfig, diffResult.diffReport, diffResult.operationsSources, level),
flags.getWarnIgnoreFile(), flags.getErrIgnoreFile())

if returnErr != nil {
Expand All @@ -76,12 +77,12 @@ func getChangelog(flags Flags, stdout io.Writer, level checker.Level) (bool, *Re

if level == checker.WARN {
// breaking changes
if returnErr := outputBreakingChanges(bcConfig, flags.getFormat(), flags.getLang(), stdout, errs, level); returnErr != nil {
if returnErr := outputBreakingChanges(flags.getFormat(), flags.getLang(), stdout, errs); returnErr != nil {
return false, returnErr
}
} else {
// changelog
if returnErr := outputChangelog(bcConfig, flags.getFormat(), flags.getLang(), stdout, errs, level); returnErr != nil {
if returnErr := outputChangelog(flags.getFormat(), flags.getLang(), stdout, errs, diffResult.specInfoPair); returnErr != nil {
return false, returnErr
}
}
Expand Down Expand Up @@ -118,7 +119,7 @@ func filterIgnored(errs checker.Changes, warnIgnoreFile string, errIgnoreFile st
return errs, nil
}

func outputChangelog(config checker.Config, format string, lang string, stdout io.Writer, errs checker.Changes, level checker.Level) *ReturnError {
func outputChangelog(format string, lang string, stdout io.Writer, errs checker.Changes, specInfoPair *load.SpecInfoPair) *ReturnError {
// formatter lookup
formatter, err := formatters.Lookup(format, formatters.FormatterOpts{
Language: lang,
Expand All @@ -128,7 +129,7 @@ func outputChangelog(config checker.Config, format string, lang string, stdout i
}

// render
bytes, err := formatter.RenderChangelog(errs, formatters.RenderOpts{})
bytes, err := formatter.RenderChangelog(errs, formatters.RenderOpts{}, specInfoPair)
if err != nil {
return getErrFailedPrint("changelog "+format, err)
}
Expand Down
Loading

0 comments on commit 792c95c

Please sign in to comment.