Skip to content

Add document.GatherReleaseNotesDocument and notes.GatherReleaseNotes API #1272

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

Merged
merged 1 commit into from
May 8, 2020
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
20 changes: 4 additions & 16 deletions cmd/krel/cmd/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"bufio"
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -327,23 +326,12 @@ func generateReleaseNotes(opts *changelogOptions, branch, startRev, endRev strin
return "", err
}

gatherer, err := notes.NewGatherer(context.Background(), notesOptions)
if err != nil {
return "", errors.Wrapf(err, "retrieving notes gatherer")
}

releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return "", errors.Wrapf(err, "listing release notes")
}

// Create the markdown
doc, err := document.CreateDocument(releaseNotes, history)
doc, err := document.GatherReleaseNotesDocument(
notesOptions, startRev, opts.tag,
)
if err != nil {
return "", errors.Wrapf(err, "creating release note document")
return "", err
}
doc.PreviousRevision = startRev
doc.CurrentRevision = opts.tag

markdown, err := doc.RenderMarkdownTemplate(
opts.bucket, opts.tars,
Expand Down
25 changes: 10 additions & 15 deletions cmd/krel/cmd/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -592,16 +591,14 @@ func releaseNotesJSON(tag string) (string, error) {
logrus.Infof("Using end tag %v", tag)

// Fetch the notes
gatherer, err := notes.NewGatherer(context.Background(), notesOptions)
releaseNotes, history, err := notes.GatherReleaseNotes(notesOptions)
if err != nil {
return "", errors.Wrapf(err, "retrieving notes gatherer")
}
releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return "", errors.Wrapf(err, "listing release notes")
return "", errors.Wrapf(err, "gathering release notes")
}

doc, err := document.CreateDocument(releaseNotes, history)
doc, err := document.New(
releaseNotes, history, notesOptions.StartRev, notesOptions.EndRev,
)
if err != nil {
return "", errors.Wrapf(err, "creating release note document")
}
Expand Down Expand Up @@ -636,16 +633,14 @@ func releaseNotesFrom(startTag string) (*releaseNotesResult, error) {
logrus.Infof("Using end tag %v", releaseNotesOpts.tag)

// Fetch the notes
gatherer, err := notes.NewGatherer(context.Background(), notesOptions)
releaseNotes, history, err := notes.GatherReleaseNotes(notesOptions)
if err != nil {
return nil, errors.Wrapf(err, "retrieving notes gatherer")
}
releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return nil, errors.Wrapf(err, "listing release notes")
return nil, errors.Wrapf(err, "gathering release notes")
}

doc, err := document.CreateDocument(releaseNotes, history)
doc, err := document.New(
releaseNotes, history, notesOptions.StartRev, notesOptions.EndRev,
)
if err != nil {
return nil, errors.Wrapf(err, "creating release note document")
}
Expand Down
27 changes: 3 additions & 24 deletions cmd/release-notes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package main

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -223,21 +222,6 @@ func init() {
)
}

func GetReleaseNotes() (notes.ReleaseNotes, notes.ReleaseNotesHistory, error) {
logrus.Info("fetching all commits. This might take a while...")

gatherer, err := notes.NewGatherer(context.Background(), opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "retrieving notes gatherer")
}
releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return nil, nil, errors.Wrapf(err, "listing release notes")
}

return releaseNotes, history, nil
}

func WriteReleaseNotes(releaseNotes notes.ReleaseNotes, history notes.ReleaseNotesHistory) (err error) {
logrus.Info("got the commits, performing rendering")

Expand Down Expand Up @@ -293,16 +277,11 @@ func WriteReleaseNotes(releaseNotes notes.ReleaseNotes, history notes.ReleaseNot
return errors.Wrapf(err, "encoding JSON output")
}
case strings.HasPrefix(format, options.GoTemplatePrefix):
doc, err := document.CreateDocument(releaseNotes, history)
doc, err := document.New(releaseNotes, history, opts.StartRev, opts.EndRev)
if err != nil {
return errors.Wrapf(err, "creating release note document")
}

// TODO: Not sure these options are guaranteed to be set but we need
// them in rendering. Perhaps these should be set in CreateDocument()?
doc.PreviousRevision = opts.StartRev
doc.CurrentRevision = opts.EndRev

markdown, err := doc.RenderMarkdownTemplate(opts.ReleaseBucket, opts.ReleaseTars, opts.Format)
if err != nil {
return errors.Wrapf(err, "rendering release note document with template")
Expand Down Expand Up @@ -332,9 +311,9 @@ func WriteReleaseNotes(releaseNotes notes.ReleaseNotes, history notes.ReleaseNot
}

func run(*cobra.Command, []string) error {
releaseNotes, history, err := GetReleaseNotes()
releaseNotes, history, err := notes.GatherReleaseNotes(opts)
if err != nil {
return errors.Wrapf(err, "retrieving release notes")
return errors.Wrapf(err, "gathering release notes")
}

return WriteReleaseNotes(releaseNotes, history)
Expand Down
29 changes: 26 additions & 3 deletions pkg/notes/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,35 @@ var kindMap = map[Kind]Kind{
KindFlake: KindOther,
}

// CreateDocument assembles an organized document from an unorganized set of
// release notes
func CreateDocument(releaseNotes notes.ReleaseNotes, history notes.ReleaseNotesHistory) (*Document, error) {
// GatherReleaseNotesDocument creates a new gatherer and collects the release
// notes into a fresh document
func GatherReleaseNotesDocument(
opts *options.Options, previousRev, currentRev string,
) (*Document, error) {
releaseNotes, history, err := notes.GatherReleaseNotes(opts)
if err != nil {
return nil, errors.Wrapf(err, "gathering release notes")
}

doc, err := New(releaseNotes, history, previousRev, currentRev)
if err != nil {
return nil, errors.Wrapf(err, "creating release note document")
}

return doc, nil
}

// New assembles an organized document from an unorganized set of release notes
func New(
releaseNotes notes.ReleaseNotes,
history notes.ReleaseNotesHistory,
previousRev, currentRev string,
) (*Document, error) {
doc := &Document{
NotesWithActionRequired: Notes{},
Notes: NoteCollection{},
CurrentRevision: currentRev,
PreviousRevision: previousRev,
}

stripRE := regexp.MustCompile(`^([-\*]+\s+)`)
Expand Down
8 changes: 3 additions & 5 deletions pkg/notes/document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func setupTestDir(t *testing.T, dir string) {
}
}

func TestCreateDocument(t *testing.T) {
func TestNew(t *testing.T) {
type args struct {
releaseNotes notes.ReleaseNotes
history notes.ReleaseNotesHistory
Expand Down Expand Up @@ -426,7 +426,7 @@ func TestCreateDocument(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateDocument(tt.args.releaseNotes, tt.args.history)
got, err := New(tt.args.releaseNotes, tt.args.history, "", "")
require.NoError(t, err)
require.Equal(t, got, tt.want, "Unexpected return.")
})
Expand Down Expand Up @@ -488,10 +488,8 @@ func TestDocument_RenderMarkdownTemplate(t *testing.T) {
testNotes[11] = duplicate
testNotes[12] = actionNeeded

doc, err := CreateDocument(testNotes, makeReleaseNoteHistory(testNotes))
doc, err := New(testNotes, makeReleaseNoteHistory(testNotes), "v1.16.0", "v1.16.1")
require.NoError(t, err, "Creating test document.")
doc.PreviousRevision = "v1.16.0"
doc.CurrentRevision = "v1.16.1"

templateSpec := tt.templateSpec
var dir string
Expand Down
18 changes: 18 additions & 0 deletions pkg/notes/notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ func NewGathererWithClient(ctx context.Context, c github.Client) *Gatherer {
}
}

// GatherReleaseNotes creates a new gatherer and collects the release notes
// afterwards
func GatherReleaseNotes(opts *options.Options) (ReleaseNotes, ReleaseNotesHistory, error) {
logrus.Info("Gathering release notes")
gatherer, err := NewGatherer(context.Background(), opts)

if err != nil {
return nil, nil, errors.Wrapf(err, "retrieving notes gatherer")
}

releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return nil, nil, errors.Wrapf(err, "listing release notes")
}

return releaseNotes, history, nil
}

// ListReleaseNotes produces a list of fully contextualized release notes
// starting from a given commit SHA and ending at starting a given commit SHA.
func (g *Gatherer) ListReleaseNotes() (ReleaseNotes, ReleaseNotesHistory, error) {
Expand Down