Skip to content
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 pkg/anago/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ func (d *DefaultStage) GenerateChangelog() error {
Branch: branch,
Bucket: d.options.Bucket(),
HTMLFile: filepath.Join(workspaceDir, "src/release-notes.html"),
JSONFile: filepath.Join(workspaceDir, "src/release-notes.json"),
Dependencies: true,
Tars: filepath.Join(
gitRoot,
Expand Down
62 changes: 49 additions & 13 deletions pkg/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package changelog
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand All @@ -41,6 +42,7 @@ type Options struct {
Bucket string
Tars string
HTMLFile string
JSONFile string
RecordDir string
ReplayDir string
Dependencies bool
Expand Down Expand Up @@ -97,7 +99,7 @@ func (c *Changelog) Run() error {
}
logrus.Infof("Found latest %s commit %s", remoteBranch, head)

var markdown, startRev, endRev string
var markdown, jsonStr, startRev, endRev string
if tag.Patch == 0 {
if len(tag.Pre) == 0 {
// Still create the downloads table
Expand Down Expand Up @@ -132,7 +134,7 @@ func (c *Changelog) Run() error {
// the current HEAD as end revision.
endRev = head

markdown, err = c.generateReleaseNotes(branch, startRev, endRev)
markdown, jsonStr, err = c.generateReleaseNotes(branch, startRev, endRev)
} else {
// New minor alpha, beta and rc releases get generated notes
latestTags, tErr := c.impl.LatestGitHubTagsPerBranch()
Expand All @@ -148,7 +150,7 @@ func (c *Changelog) Run() error {
startRev = startTag
endRev = head

markdown, err = c.generateReleaseNotes(branch, startRev, endRev)
markdown, jsonStr, err = c.generateReleaseNotes(branch, startRev, endRev)
} else {
return errors.Errorf(
"no latest tag available for branch %s", branch,
Expand All @@ -164,7 +166,7 @@ func (c *Changelog) Run() error {
startRev = startTag
endRev = head

markdown, err = c.generateReleaseNotes(branch, startTag, endRev)
markdown, jsonStr, err = c.generateReleaseNotes(branch, startTag, endRev)
}
if err != nil {
return errors.Wrap(err, "generate release notes")
Expand Down Expand Up @@ -213,14 +215,21 @@ func (c *Changelog) Run() error {
return errors.Wrap(err, "write HTML")
}

logrus.Info("Writing JSON")
if err := c.writeJSON(tag, jsonStr); err != nil {
return errors.Wrap(err, "write JSON")
}

logrus.Info("Committing changes")
return errors.Wrap(
c.commitChanges(repo, branch, tag),
"commit changes",
)
}

func (c *Changelog) generateReleaseNotes(branch, startRev, endRev string) (string, error) {
func (c *Changelog) generateReleaseNotes(
branch, startRev, endRev string,
) (markdown, jsonStr string, err error) {
logrus.Info("Generating release notes")

notesOptions := options.New()
Expand All @@ -236,25 +245,33 @@ func (c *Changelog) generateReleaseNotes(branch, startRev, endRev string) (strin
notesOptions.Pull = false

if err := c.impl.ValidateAndFinish(notesOptions); err != nil {
return "", errors.Wrap(err, "validating notes options")
return "", "", errors.Wrap(err, "validating notes options")
}

doc, err := c.impl.GatherReleaseNotesDocument(
notesOptions, startRev, c.options.Tag,
)
releaseNotes, err := c.impl.GatherReleaseNotes(notesOptions)
if err != nil {
return "", "", errors.Wrapf(err, "gather release notes")
}

doc, err := c.impl.NewDocument(releaseNotes, startRev, c.options.Tag)
if err != nil {
return "", errors.Wrap(err, "gather release notes")
return "", "", errors.Wrapf(err, "create release note document")
}

markdown, err := c.impl.RenderMarkdownTemplate(
releaseNotesJSON, err := json.MarshalIndent(releaseNotes.ByPR(), "", " ")
if err != nil {
return "", "", errors.Wrapf(err, "build release notes JSON")
}

markdown, err = c.impl.RenderMarkdownTemplate(
doc, c.options.Bucket, c.options.Tars,
options.GoTemplateInline+releaseNotesTemplate,
)
if err != nil {
return "", errors.Wrapf(err, "render release notes to markdown")
return "", "", errors.Wrapf(err, "render release notes to markdown")
}

return markdown, nil
return markdown, string(releaseNotesJSON), nil
}

func (c *Changelog) writeMarkdown(
Expand Down Expand Up @@ -316,6 +333,13 @@ func (c *Changelog) htmlChangelogFilename(tag semver.Version) string {
return changelogFilename(tag, "html")
}

func (c *Changelog) jsonChangelogFilename(tag semver.Version) string {
if c.options.JSONFile != "" {
return c.options.JSONFile
}
return changelogFilename(tag, "json")
}

func markdownChangelogReadme() string {
return filepath.Join(RepoChangelogDir, "README.md")
}
Expand Down Expand Up @@ -361,6 +385,18 @@ func (c *Changelog) writeHTML(tag semver.Version, markdown string) error {
)
}

func (c *Changelog) writeJSON(tag semver.Version, jsonStr string) error {
absOutputPath, err := c.impl.Abs(c.jsonChangelogFilename(tag))
if err != nil {
return errors.Wrap(err, "get absolute file path")
}
logrus.Infof("Writing JSON file to %s", absOutputPath)
return errors.Wrap(
c.impl.WriteFile(absOutputPath, []byte(jsonStr), os.FileMode(0o644)),
"write JSON",
)
}

func (c *Changelog) lookupRemoteReleaseNotes(branch string) (string, error) {
logrus.Info("Assuming new minor release")

Expand Down
31 changes: 31 additions & 0 deletions pkg/changelog/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/release/pkg/changelog"
"k8s.io/release/pkg/changelog/changelogfakes"
"k8s.io/release/pkg/github"
"k8s.io/release/pkg/notes"
)

func TestRun(t *testing.T) {
Expand All @@ -49,6 +50,7 @@ func TestRun(t *testing.T) {
},
}, nil)
mock.ReadFileReturns([]byte(changelog.TocEnd), nil)
mock.GatherReleaseNotesReturns(&notes.ReleaseNotes{}, nil)
},
shouldErr: false,
},
Expand All @@ -60,6 +62,7 @@ func TestRun(t *testing.T) {
Patch: 3,
}, nil)
mock.ReadFileReturns([]byte(changelog.TocEnd), nil)
mock.GatherReleaseNotesReturns(&notes.ReleaseNotes{}, nil)
},
shouldErr: false,
},
Expand All @@ -77,6 +80,7 @@ func TestRun(t *testing.T) {
"release-1.19": "v1.19.0-beta.0",
}, nil)
mock.ReadFileReturns([]byte(changelog.TocEnd), nil)
mock.GatherReleaseNotesReturns(&notes.ReleaseNotes{}, nil)
},
shouldErr: false,
},
Expand Down Expand Up @@ -234,6 +238,33 @@ func TestRun(t *testing.T) {
},
shouldErr: true,
},
{ // GatherReleaseNotes failed
prepare: func(mock *changelogfakes.FakeImpl, _ *changelog.Options) {
mock.TagStringToSemverReturns(semver.Version{
Pre: []semver.PRVersion{
{VersionStr: "alpha"},
{VersionNum: 1},
},
}, nil)
mock.ReadFileReturns([]byte(changelog.TocEnd), nil)
mock.GatherReleaseNotesReturns(nil, err)
},
shouldErr: true,
},
{ // NewDocumentReturns failed
prepare: func(mock *changelogfakes.FakeImpl, _ *changelog.Options) {
mock.TagStringToSemverReturns(semver.Version{
Pre: []semver.PRVersion{
{VersionStr: "alpha"},
{VersionNum: 1},
},
}, nil)
mock.ReadFileReturns([]byte(changelog.TocEnd), nil)
mock.GatherReleaseNotesReturns(&notes.ReleaseNotes{}, nil)
mock.NewDocumentReturns(nil, err)
},
shouldErr: true,
},
} {
options := &changelog.Options{}
sut := changelog.New(options)
Expand Down
Loading