Skip to content

Commit

Permalink
Fix changelog text (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
blva authored Jul 6, 2023
1 parent 57acd2f commit 2b96ed4
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 19 deletions.
26 changes: 20 additions & 6 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func (errs BackwardCompatibilityErrors) HasLevelOrHigher(level Level) bool {
return false
}

func (errs BackwardCompatibilityErrors) GetLevelCount() map[Level]int {
counts := map[Level]int{}
for _, err := range errs {
counts[err.Level] = counts[err.Level] + 1
}
return counts
}

func (bcErrors BackwardCompatibilityErrors) Len() int {
return len(bcErrors)
}
Expand Down Expand Up @@ -106,13 +114,9 @@ func IsPipedOutput() bool {
return *pipedOutput
}

func (r *BackwardCompatibilityError) PrettyErrorText(l localizations.Localizer) string {
if IsPipedOutput() {
return r.LocalizedError(l)
}

func PrettyLevelText(level Level) string {
var levelName string
switch r.Level {
switch level {
case ERR:
levelName = color.InRed("error")
case WARN:
Expand All @@ -122,6 +126,16 @@ func (r *BackwardCompatibilityError) PrettyErrorText(l localizations.Localizer)
default:
levelName = color.InGray("issue")
}

return levelName
}

func (r *BackwardCompatibilityError) PrettyErrorText(l localizations.Localizer) string {
if IsPipedOutput() {
return r.LocalizedError(l)
}

levelName := PrettyLevelText(r.Level)
comment := ""
if r.Comment != "" {
comment = fmt.Sprintf("\n\t\t%s", r.Comment)
Expand Down
8 changes: 5 additions & 3 deletions checker/localizations/localizations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion checker/localizations_src/en/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ at: at
in: in
added-required-request-body: added required request body
request-parameter-removed: deleted the %s request parameter %s
total-errors: "Backward compatibility errors (%d):\\n"
total-errors: "%d breaking changes: %d %s, %d %s\\n"
total-changes: "%d changes: %d %s, %d %s, %d %s\\n"
request-parameter-pattern-added: "added the pattern '%s' for the %s request parameter %s"
request-parameter-pattern-changed: "changed the pattern for the %s request parameter %s from '%s' to '%s'"
request-property-pattern-added: "added the pattern '%s' for the request property %s"
Expand Down
3 changes: 2 additions & 1 deletion checker/localizations_src/ru/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ at: в
in: в
added-required-request-body: добавлено обязательное тело запроса
request-parameter-removed: удалён %s параметр запроса %s
total-errors: "Ошибки обратной совместимости (всего: %d):\\n"
total-errors: "%d критические изменения: %d %s, %d %s\\n"
total-changes: "%d изменений: %d %s, %d %s, %d %s\\n"
request-parameter-pattern-added: добавлен pattern '%s' у %s параметра запроса %s
request-parameter-pattern-changed: изменён pattern у %s параметра запроса %s со значения '%s' на значение '%s'
request-property-pattern-added: добавлен pattern '%s' у поля запроса %s
Expand Down
5 changes: 3 additions & 2 deletions diff/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ func ExampleGetPathsDiff() {

// pretty print breaking changes errors
if len(errs) > 0 {
fmt.Printf(c.Localizer.Get("messages.total-errors"), len(errs))
count := errs.GetLevelCount()
fmt.Printf(c.Localizer.Get("messages.total-errors"), len(errs), count[checker.ERR], "error", count[checker.WARN], "warning")
for _, bcerr := range errs {
fmt.Printf("%s\n\n", strings.TrimRight(bcerr.PrettyErrorText(c.Localizer), " "))
}
}

// Output:
// Backward compatibility errors (4):
// 4 breaking changes: 1 error, 3 warning
// error at ../data/openapi-test3.yaml, in API GET /api/{domain}/{project}/badges/security-score removed the success response with the status '201' [response-success-status-removed].
//
// warning at ../data/openapi-test3.yaml, in API GET /api/{domain}/{project}/badges/security-score deleted the 'cookie' request parameter 'test' [request-parameter-removed].
Expand Down
16 changes: 15 additions & 1 deletion internal/breaking_changes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"fmt"
"io"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -64,5 +65,18 @@ In 'composed' mode, base and revision can be a glob and oasdiff will compare mat
}

func runBreakingChanges(flags *ChangelogFlags, stdout io.Writer) (bool, *ReturnError) {
return getChangelog(flags, stdout, checker.WARN)
return getChangelog(flags, stdout, checker.WARN, getBreakingChangesTitle)
}

func getBreakingChangesTitle(config checker.BackwardCompatibilityCheckConfig, errs checker.BackwardCompatibilityErrors) string {
count := errs.GetLevelCount()

return fmt.Sprintf(
config.Localizer.Get("messages.total-errors"),
len(errs),
count[checker.ERR],
checker.PrettyLevelText(checker.ERR),
count[checker.WARN],
checker.PrettyLevelText(checker.WARN),
)
}
27 changes: 22 additions & 5 deletions internal/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ In 'composed' mode, base and revision can be a glob and oasdiff will compare mat
}

func runChangelog(flags *ChangelogFlags, stdout io.Writer) (bool, *ReturnError) {
return getChangelog(flags, stdout, checker.INFO)
return getChangelog(flags, stdout, checker.INFO, getChangelogTitle)
}

func getChangelog(flags *ChangelogFlags, stdout io.Writer, level checker.Level) (bool, *ReturnError) {
func getChangelog(flags *ChangelogFlags, stdout io.Writer, level checker.Level, getOutputTitle GetOutputTitle) (bool, *ReturnError) {

openapi3.CircularReferenceCounter = flags.circularReferenceCounter

Expand All @@ -89,7 +89,7 @@ func getChangelog(flags *ChangelogFlags, stdout io.Writer, level checker.Level)
return false, returnErr
}

if returnErr := outputChangelog(bcConfig, flags.format, stdout, errs); returnErr != nil {
if returnErr := outputChangelog(bcConfig, flags.format, stdout, errs, getOutputTitle); returnErr != nil {
return false, returnErr
}

Expand Down Expand Up @@ -125,7 +125,24 @@ func filterIgnored(errs checker.BackwardCompatibilityErrors, warnIgnoreFile stri
return errs, nil
}

func outputChangelog(config checker.BackwardCompatibilityCheckConfig, format string, stdout io.Writer, errs checker.BackwardCompatibilityErrors) *ReturnError {
func getChangelogTitle(config checker.BackwardCompatibilityCheckConfig, errs checker.BackwardCompatibilityErrors) string {
count := errs.GetLevelCount()

return fmt.Sprintf(
config.Localizer.Get("messages.total-changes"),
len(errs),
count[checker.ERR],
checker.PrettyLevelText(checker.ERR),
count[checker.WARN],
checker.PrettyLevelText(checker.WARN),
count[checker.INFO],
checker.PrettyLevelText(checker.INFO),
)
}

type GetOutputTitle func(config checker.BackwardCompatibilityCheckConfig, errs checker.BackwardCompatibilityErrors) string

func outputChangelog(config checker.BackwardCompatibilityCheckConfig, format string, stdout io.Writer, errs checker.BackwardCompatibilityErrors, getOutputTitle GetOutputTitle) *ReturnError {
switch format {
case FormatYAML:
if err := printYAML(stdout, errs); err != nil {
Expand All @@ -137,7 +154,7 @@ func outputChangelog(config checker.BackwardCompatibilityCheckConfig, format str
}
case FormatText:
if len(errs) > 0 {
fmt.Fprintf(stdout, config.Localizer.Get("messages.total-errors"), len(errs))
fmt.Fprint(stdout, getOutputTitle(config, errs))
}

for _, bcerr := range errs {
Expand Down
11 changes: 11 additions & 0 deletions internal/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,14 @@ func Test_BreakingChangesChangelogOptionalCheckersAreInfoLevel(t *testing.T) {
require.Equal(t, c.Level, checker.INFO)
}
}

func Test_BreakingChangesChangelogOptionalCheckersAreErrorLevelWhenSpecified(t *testing.T) {
var stdout bytes.Buffer
require.Zero(t, internal.Run(cmdToArgs("oasdiff changelog ../data/run_test/changelog_include_checks_base.yaml ../data/run_test/changelog_include_checks_revision.yaml --format json --include-checks api-tag-removed,response-non-success-status-removed"), &stdout, io.Discard))
cl := checker.BackwardCompatibilityErrors{}
require.NoError(t, json.Unmarshal(stdout.Bytes(), &cl))
require.Len(t, cl, 2)
for _, c := range cl {
require.Equal(t, c.Level, checker.ERR)
}
}

0 comments on commit 2b96ed4

Please sign in to comment.