Skip to content

Commit

Permalink
Shorten deps flag for rendering and updating
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarocabanas committed Sep 7, 2022
1 parent 31c2ea0 commit 045fb5f
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 13 deletions.
16 changes: 12 additions & 4 deletions src/app/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
)

const (
mdPathFlag = "md"
versionFlag = "version"
dateFlag = "date"
mdPathFlag = "md"
versionFlag = "version"
dateFlag = "date"
shortenDepsFlag = "shorten_deps"
)

// Cmd is the cli.Command object for the is-held command.
Expand Down Expand Up @@ -47,6 +48,12 @@ var Cmd = &cli.Command{
Value: cli.NewTimestamp(time.Now()),
Layout: "2006-01-02",
},
&cli.BoolFlag{
Name: shortenDepsFlag,
EnvVars: common.EnvFor(shortenDepsFlag),
Usage: "If set, dependencies with full-route names will be shortened to the last segment.",
Value: false,
},
},
Action: Render,
}
Expand All @@ -72,7 +79,8 @@ func Render(cCtx *cli.Context) error {
return fmt.Errorf("creating destination file at %q: %w", mdPath, err)
}

rnd := renderer.New(ch)
shortenDeps := cCtx.Bool(shortenDepsFlag)
rnd := renderer.New(ch, renderer.ShortenDeps(shortenDeps))

if t := cCtx.Timestamp(dateFlag); t != nil {
tv := *t
Expand Down
51 changes: 51 additions & 0 deletions src/app/render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,57 @@ This is a release note
- Support has been removed
`),
},
{
name: "Changelog_With_Long_Dependency_Without_Shorten_deps",
yaml: strings.TrimSpace(`
notes: |-
### Important announcement (note)
This is a release note
dependencies:
- name: foobar
from: 0.0.1
to: 0.1.0
- name: github.com/golangci/golangci-lint
from: 0.2.5
to: 0.3.0
`),
expected: strings.TrimSpace(`
### Important announcement (note)
This is a release note
### ⛓️ Dependencies
- Upgraded foobar from 0.0.1 to 0.1.0
- Upgraded github.com/golangci/golangci-lint from 0.2.5 to 0.3.0
`),
},
{
name: "Changelog_With_Long_Dependency_Shorten_deps",
args: "-shorten_deps=true",
yaml: strings.TrimSpace(`
notes: |-
### Important announcement (note)
This is a release note
dependencies:
- name: foobar
from: 0.0.1
to: 0.1.0
- name: github.com/golangci/golangci-lint
from: 0.2.5
to: 0.3.0
`),
expected: strings.TrimSpace(`
### Important announcement (note)
This is a release note
### ⛓️ Dependencies
- Upgraded foobar from 0.0.1 to 0.1.0
- Upgraded golangci-lint from 0.2.5 to 0.3.0
`),
},
} {
tc := tc
//nolint:paralleltest // urfave/cli cannot be tested concurrently.
Expand Down
16 changes: 12 additions & 4 deletions src/app/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
)

const (
mdPathFlag = "md"
versionFlag = "version"
dateFlag = "date"
mdPathFlag = "md"
versionFlag = "version"
dateFlag = "date"
shortenDepsFlag = "shorten_deps"
)

// Cmd is the cli.Command object for the update-changelog command.
Expand Down Expand Up @@ -48,6 +49,12 @@ var Cmd = &cli.Command{
Value: cli.NewTimestamp(time.Now()),
Layout: "2006-01-02",
},
&cli.BoolFlag{
Name: shortenDepsFlag,
EnvVars: common.EnvFor(shortenDepsFlag),
Usage: "If set, dependencies with full-route names will be shortened to the last segment.",
Value: false,
},
},
Action: Update,
}
Expand Down Expand Up @@ -85,7 +92,8 @@ func Update(cCtx *cli.Context) error {
return fmt.Errorf("parsing version: %w", err)
}

mrg := merger.New(ch, version)
shortenDeps := cCtx.Bool(shortenDepsFlag)
mrg := merger.New(ch, version, merger.ShortenDeps(shortenDeps))
if t := cCtx.Timestamp(dateFlag); t != nil {
tv := *t
mrg.ReleasedOn = func() time.Time {
Expand Down
82 changes: 82 additions & 0 deletions src/app/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,88 @@ This is a release note
## v1.2.3 - 20YY-DD-MM
### Enhancements
- This is in the past and should be preserved
`) + "\n",
},
{
name: "Changelog_Now_Long_Dependency_Without_Shorten_Deps",
args: "-version v1.2.4",
yaml: strings.TrimSpace(`
notes: |-
### Important announcement (note)
This is a release note
dependencies:
- name: github.com/golangci/golangci-lint
from: 0.0.1
to: 0.1.0
`),
existing: strings.TrimSpace(`
# Changelog
This is based on blah blah blah
## v1.2.3 - 20YY-DD-MM
### Enhancements
- This is in the past and should be preserved
`) + "\n",
expected: strings.TrimSpace(`
# Changelog
This is based on blah blah blah
## v1.2.4 - {NOW}
### Important announcement (note)
This is a release note
### ⛓️ Dependencies
- Upgraded github.com/golangci/golangci-lint from 0.0.1 to 0.1.0
## v1.2.3 - 20YY-DD-MM
### Enhancements
- This is in the past and should be preserved
`) + "\n",
},
{
name: "Changelog_Now_Long_Dependency_With_Shorten_Deps",
args: "-version v1.2.4 -shorten_deps=true",
yaml: strings.TrimSpace(`
notes: |-
### Important announcement (note)
This is a release note
dependencies:
- name: github.com/golangci/golangci-lint
from: 0.0.1
to: 0.1.0
`),
existing: strings.TrimSpace(`
# Changelog
This is based on blah blah blah
## v1.2.3 - 20YY-DD-MM
### Enhancements
- This is in the past and should be preserved
`) + "\n",
expected: strings.TrimSpace(`
# Changelog
This is based on blah blah blah
## v1.2.4 - {NOW}
### Important announcement (note)
This is a release note
### ⛓️ Dependencies
- Upgraded golangci-lint from 0.0.1 to 0.1.0
## v1.2.3 - 20YY-DD-MM
### Enhancements
- This is in the past and should be preserved
`) + "\n",
Expand Down
24 changes: 22 additions & 2 deletions src/changelog/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package renderer
import (
"fmt"
"io"
"path"
"strings"
"text/template"
"time"
Expand All @@ -17,6 +18,15 @@ type Stringer interface {
String() string
}

type OptionFunc func(*Renderer)

// ShortenDeps returns an option that will set the shortenDeps attribute.
func ShortenDeps(sDeps bool) OptionFunc {
return func(s *Renderer) {
s.shortenDeps = sDeps
}
}

// Renderer outputs a human-readable, markdown version of a changelog.
type Renderer struct {
// If non-nil, the output will include a level 2 header with the version to which this changelog corresponds.
Expand All @@ -26,12 +36,19 @@ type Renderer struct {
ReleasedOn func() time.Time

changelog *changelog.Changelog
// If true, dependencies with full-route names will be shortened to the last segment.
shortenDeps bool
}

func New(c *changelog.Changelog) Renderer {
return Renderer{
func New(c *changelog.Changelog, opts ...OptionFunc) Renderer {
r := Renderer{
changelog: c,
}
for _, opt := range opts {
opt(&r)
}

return r
}

type parsedChangelog struct {
Expand Down Expand Up @@ -86,6 +103,9 @@ func (r Renderer) parse() parsedChangelog {
}

for _, dep := range r.changelog.Dependencies {
if r.shortenDeps {
dep.Name = path.Base(dep.Name)
}
parsed.Sections[string(changelog.TypeDependency)] = append(parsed.Sections[string(changelog.TypeDependency)], dep)
}

Expand Down
22 changes: 19 additions & 3 deletions src/changelog/sources/markdown/merger/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import (
log "github.com/sirupsen/logrus"
)

type OptionFunc func(*Merger)

// ShortenDeps returns an option that will set the shortenDeps attribute.
func ShortenDeps(sDeps bool) OptionFunc {
return func(s *Merger) {
s.shortenDeps = sDeps
}
}

// Merger is an object that can incorporate a changelog.Changelog (section) into an existing CHANGELOG.md document.
type Merger struct {
// ReleasedOn is a function that returns the date in which the new section was released. It defaults to time.Now.
Expand All @@ -25,19 +34,26 @@ type Merger struct {
version *semver.Version
// ch is the already populated, partial changelog containing the latest changes.
ch *changelog.Changelog
// If true, dependencies with full-route names will be shortened to the last segment.
shortenDeps bool
}

// New creates a Merger that will integrate the supplied changelog.Changelog into a full Markdown document that contains
// changelogs for older versions. The inserted section will be marked as being the changelog for the version specified
// in the supplied semver.Version, and also marked as released on the date returned by Merger.ReleasedOn.
// Merger is an immutable object and does not modify the changelog.Changelog object, nor the original document supplied
// to Merge.
func New(ch *changelog.Changelog, newVersion *semver.Version) Merger {
return Merger{
func New(ch *changelog.Changelog, newVersion *semver.Version, opts ...OptionFunc) Merger {
m := Merger{
ReleasedOn: time.Now,
ch: ch,
version: newVersion,
}
for _, opt := range opts {
opt(&m)
}

return m
}

var (
Expand All @@ -55,7 +71,7 @@ var (
func (m Merger) Merge(srcChangelog io.Reader, dst io.Writer) error {
newSection := &bytes.Buffer{}

rdr := renderer.New(m.ch)
rdr := renderer.New(m.ch, renderer.ShortenDeps(m.shortenDeps))
rdr.Next = m.version
rdr.ReleasedOn = m.ReleasedOn

Expand Down

0 comments on commit 045fb5f

Please sign in to comment.