Skip to content

Commit

Permalink
refactor: rename "internal/release" to "internal/notes" package
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Nov 17, 2022
1 parent 3890607 commit 8ed0858
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 100 deletions.
24 changes: 12 additions & 12 deletions internal/commands/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
"text/template"
"time"

"github.com/pivotal-cf/kiln/internal/gh"

"github.com/go-git/go-git/v5"
"github.com/google/go-github/v40/github"
"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/kiln/internal/release"

"github.com/pivotal-cf/kiln/internal/gh"
"github.com/pivotal-cf/kiln/internal/notes"
)

const releaseDateFormat = "2006-01-02"
Expand All @@ -32,7 +32,7 @@ type ReleaseNotes struct {
GithubToken string `long:"github-token" short:"g" description:"auth token for fetching issues merged between releases" env:"GITHUB_TOKEN"`
Kilnfile string `long:"kilnfile" short:"k" description:"path to Kilnfile"`
DocsFile string `long:"update-docs" short:"u" description:"path to docs file to update"`
release.IssuesQuery
notes.IssuesQuery
}

repository *git.Repository
Expand All @@ -45,11 +45,11 @@ type ReleaseNotes struct {
repoOwner, repoName string
}

type FetchNotesData func(ctx context.Context, repo *git.Repository, client *github.Client, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string, issuesQuery release.IssuesQuery) (release.NotesData, error)
type FetchNotesData func(ctx context.Context, repo *git.Repository, client *github.Client, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string, issuesQuery notes.IssuesQuery) (notes.Data, error)

func NewReleaseNotesCommand() (ReleaseNotes, error) {
return ReleaseNotes{
fetchNotesData: release.FetchNotesData,
fetchNotesData: notes.FetchData,
readFile: os.ReadFile,
Writer: os.Stdout,
stat: os.Stat,
Expand Down Expand Up @@ -86,7 +86,7 @@ func (r ReleaseNotes) Execute(args []string) error {
client = gh.Client(ctx, r.Options.GithubToken)
}

_ = release.FetchNotesData // fetchNotesData is FetchNotesData
_ = notes.FetchData // fetchNotesData is github.com/pivotal/kiln/internal/notes.FetchData
data, err := r.fetchNotesData(ctx,
r.repository, client, r.repoOwner, r.repoName,
r.Options.Kilnfile,
Expand All @@ -111,13 +111,13 @@ func (r ReleaseNotes) Execute(args []string) error {
return r.updateDocsFile(data)
}

func (r *ReleaseNotes) updateDocsFile(data release.NotesData) error {
func (r *ReleaseNotes) updateDocsFile(data notes.Data) error {
// TODO: add helpful logging
docsFileContent, err := r.readFile(r.Options.DocsFile)
if err != nil {
return err
}
page, err := release.ParseNotesPage(string(docsFileContent))
page, err := notes.ParsePage(string(docsFileContent))
if err != nil {
return err
}
Expand Down Expand Up @@ -183,8 +183,8 @@ func (r *ReleaseNotes) initRepo() error {
return nil
}

func (r ReleaseNotes) writeNotes(w io.Writer, info release.NotesData) error {
releaseNotesTemplate := release.DefaultNotesTemplate()
func (r ReleaseNotes) writeNotes(w io.Writer, info notes.Data) error {
releaseNotesTemplate := notes.DefaultTemplate()
if r.Options.TemplateName != "" {
templateBuf, err := r.readFile(r.Options.TemplateName)
if err != nil {
Expand All @@ -193,7 +193,7 @@ func (r ReleaseNotes) writeNotes(w io.Writer, info release.NotesData) error {
releaseNotesTemplate = string(templateBuf)
}

t, err := release.DefaultTemplateFunctions(template.New(r.Options.TemplateName)).Parse(releaseNotesTemplate)
t, err := notes.DefaultTemplateFunctions(template.New(r.Options.TemplateName)).Parse(releaseNotesTemplate)
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}
Expand Down
11 changes: 5 additions & 6 deletions internal/commands/release_notes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
_ "embed"
"github.com/pivotal-cf/kiln/internal/notes"
"testing"
"time"

Expand All @@ -18,8 +19,6 @@ import (
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/go-github/v40/github"
"github.com/pivotal-cf/jhanda"

"github.com/pivotal-cf/kiln/internal/release"
)

var _ jhanda.Command = ReleaseNotes{}
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestReleaseNotes_Execute(t *testing.T) {
var (
tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string

issuesQuery release.IssuesQuery
issuesQuery notes.IssuesQuery
repository *git.Repository
client *github.Client
ctx context.Context
Expand All @@ -73,11 +72,11 @@ func TestReleaseNotes_Execute(t *testing.T) {
repoOwner: "bunch",
repoName: "banana",
readFile: readFileFunc,
fetchNotesData: func(c context.Context, repo *git.Repository, ghc *github.Client, tro, trn, kfp, ir, fr string, iq release.IssuesQuery) (release.NotesData, error) {
fetchNotesData: func(c context.Context, repo *git.Repository, ghc *github.Client, tro, trn, kfp, ir, fr string, iq notes.IssuesQuery) (notes.Data, error) {
ctx, repository, client = c, repo, ghc
tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision = tro, trn, kfp, ir, fr
issuesQuery = iq
return release.NotesData{
return notes.Data{
ReleaseDate: mustParseTime(time.Parse(releaseDateFormat, "2021-11-04")),
Version: semver.MustParse("0.1.0-build.50000"),
Issues: []*github.Issue{
Expand All @@ -87,7 +86,7 @@ func TestReleaseNotes_Execute(t *testing.T) {
Stemcell: cargo.Stemcell{
OS: "fruit-tree", Version: "40000.2",
},
Components: []release.ComponentData{
Components: []notes.BOSHReleaseData{
{ComponentLock: cargo.ComponentLock{Name: "banana", Version: "1.2.0"}, Releases: []*github.RepositoryRelease{
{TagName: strPtr("1.2.0"), Body: strPtr("peal\nis\nyellow")},
{TagName: strPtr("1.1.1"), Body: strPtr("remove from bunch")},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
40 changes: 20 additions & 20 deletions internal/release/notes_data.go → internal/notes/notes_data.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package release
package notes

import (
"bytes"
Expand Down Expand Up @@ -27,12 +27,12 @@ import (
"github.com/pivotal-cf/kiln/pkg/history"
)

type ComponentData struct {
type BOSHReleaseData struct {
cargo.ComponentLock
Releases []*github.RepositoryRelease
}

func (cd ComponentData) HasReleaseNotes() bool {
func (cd BOSHReleaseData) HasReleaseNotes() bool {
for _, r := range cd.Releases {
if len(strings.TrimSpace(r.GetBody())) > 0 {
return true
Expand All @@ -41,39 +41,39 @@ func (cd ComponentData) HasReleaseNotes() bool {
return false
}

type NotesData struct {
type Data struct {
Version *semver.Version
ReleaseDate time.Time

Issues []*github.Issue
Components []ComponentData
Components []BOSHReleaseData
Bumps cargo.BumpList

Stemcell cargo.Stemcell
}

//func (notes NotesData) String() string {
//func (notes Data) String() string {
// note, _ := notes.WriteVersionNotes()
// return note.Notes
//}

func (notes NotesData) WriteVersionNotes() (VersionNote, error) {
noteTemplate, err := DefaultTemplateFunctions(template.New("")).Parse(DefaultNotesTemplate())
func (notes Data) WriteVersionNotes() (TileRelease, error) {
noteTemplate, err := DefaultTemplateFunctions(template.New("")).Parse(DefaultTemplate())
if err != nil {
return VersionNote{}, err
return TileRelease{}, err
}
var buf bytes.Buffer
err = noteTemplate.Execute(&buf, notes)
if err != nil {
return VersionNote{}, err
return TileRelease{}, err
}
return VersionNote{
return TileRelease{
Version: notes.Version.String(),
Notes: buf.String(),
}, nil
}

func (notes NotesData) HasComponentReleases() bool {
func (notes Data) HasComponentReleases() bool {
for _, r := range notes.Components {
if len(r.Releases) > 0 {
return true
Expand Down Expand Up @@ -101,10 +101,10 @@ func (q IssuesQuery) Exp() (*regexp.Regexp, error) {
return regexp.Compile(str)
}

func FetchNotesData(ctx context.Context, repo *git.Repository, client *github.Client, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string, issuesQuery IssuesQuery) (NotesData, error) {
func FetchData(ctx context.Context, repo *git.Repository, client *github.Client, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision string, issuesQuery IssuesQuery) (Data, error) {
f, err := newFetchNotesData(repo, tileRepoOwner, tileRepoName, kilnfilePath, initialRevision, finalRevision, client, issuesQuery)
if err != nil {
return NotesData{}, err
return Data{}, err
}
return f.fetch(ctx)
}
Expand Down Expand Up @@ -154,30 +154,30 @@ type fetchNotesData struct {
issuesQuery IssuesQuery
}

func (r fetchNotesData) fetch(ctx context.Context) (NotesData, error) {
func (r fetchNotesData) fetch(ctx context.Context) (Data, error) {
initialKilnfileLock, finalKilnfileLock, finalKilnfile, finalVersion, err := r.fetchHistoricFiles(r.kilnfilePath, r.initialRevision, r.finalRevision)
if err != nil {
return NotesData{}, err
return Data{}, err
}

data := NotesData{
data := Data{
Version: finalVersion,
Bumps: cargo.CalculateBumps(finalKilnfileLock.Releases, initialKilnfileLock.Releases),
Stemcell: finalKilnfileLock.Stemcell,
}

wtKilnfile, err := r.kilnfileFromWorktree(r.kilnfilePath)
if err != nil {
return NotesData{}, err
return Data{}, err
}

data.Issues, data.Bumps, err = r.fetchIssuesAndReleaseNotes(ctx, finalKilnfile, wtKilnfile, data.Bumps, r.issuesQuery)
if err != nil {
return NotesData{}, err
return Data{}, err
}

for _, c := range finalKilnfileLock.Releases {
data.Components = append(data.Components, ComponentData{
data.Components = append(data.Components, BOSHReleaseData{
ComponentLock: c,
Releases: data.Bumps.ForLock(c).Releases,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package release
package notes

import (
"context"
Expand All @@ -16,7 +16,7 @@ import (
"github.com/go-git/go-git/v5/storage/memory"
"github.com/google/go-github/v40/github"

"github.com/pivotal-cf/kiln/internal/release/fakes"
"github.com/pivotal-cf/kiln/internal/notes/fakes"
"github.com/pivotal-cf/kiln/pkg/cargo"
)

Expand Down
38 changes: 19 additions & 19 deletions internal/release/notes_page.go → internal/notes/notes_page.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package release
package notes

import (
"bytes"
Expand All @@ -14,39 +14,39 @@ const DefaultReleasesSentinel = "\n## <a id='releases'></a> Releases\n\n"

var releaseNoteExp = regexp.MustCompile(`(?m)(?P<notes>### <a id='(?P<version>\d+\.\d+\.\d+(-.+)?)'></a> (\d+\.\d+\.\d+(-.+)?)\w*(?P<header_suffix>.*)\n*((\*.*\n)|\n|(</?.*)|( +.*)|(####+.*)|(\t.*)|(\w.*))*)`)

type NotesPage struct {
type Page struct {
Exp *regexp.Regexp

Prefix, Suffix string
Releases []VersionNote
Releases []TileRelease
}

func ParseNotesPage(input string) (NotesPage, error) {
return ParseNotesPageWithExpressionAndReleasesSentinel(input, releaseNoteExp.String(), DefaultReleasesSentinel)
func ParsePage(input string) (Page, error) {
return ParsePageWithExpressionAndReleasesSentinel(input, releaseNoteExp.String(), DefaultReleasesSentinel)
}

func ParseNotesPageWithExpressionAndReleasesSentinel(input, releaseRegularExpression, releasesSentinel string) (NotesPage, error) {
func ParsePageWithExpressionAndReleasesSentinel(input, releaseRegularExpression, releasesSentinel string) (Page, error) {
const (
versionCaptureGroup = "version"
notesCaptureGroup = "notes"
)

if !strings.Contains(input, releasesSentinel) {
return NotesPage{}, fmt.Errorf("releases sentinal not found in input: expected input to contain %q", releasesSentinel)
return Page{}, fmt.Errorf("releases sentinal not found in input: expected input to contain %q", releasesSentinel)
}

exp, err := regexp.Compile(releaseRegularExpression)
if err != nil {
return NotesPage{}, fmt.Errorf(`release regular expression parse failure: %w`, err)
return Page{}, fmt.Errorf(`release regular expression parse failure: %w`, err)
}
if !stringsSliceContains(exp.SubexpNames(), versionCaptureGroup) {
return NotesPage{}, fmt.Errorf(`release regular expression must contain named capture group %q`, versionCaptureGroup)
return Page{}, fmt.Errorf(`release regular expression must contain named capture group %q`, versionCaptureGroup)
}
if !stringsSliceContains(exp.SubexpNames(), notesCaptureGroup) {
return NotesPage{}, fmt.Errorf(`release regular expression must contain named capture group %q`, notesCaptureGroup)
return Page{}, fmt.Errorf(`release regular expression must contain named capture group %q`, notesCaptureGroup)
}

page := NotesPage{
page := Page{
Exp: exp,
}

Expand All @@ -63,7 +63,7 @@ func ParseNotesPageWithExpressionAndReleasesSentinel(input, releaseRegularExpres
matchStrings := page.Exp.FindAllStringSubmatch(input, -1)

for _, match := range matchStrings {
page.Releases = append(page.Releases, VersionNote{
page.Releases = append(page.Releases, TileRelease{
Version: match[versionSubExpIndex],
Notes: match[notesSubExpIndex],
})
Expand All @@ -76,7 +76,7 @@ func ParseNotesPageWithExpressionAndReleasesSentinel(input, releaseRegularExpres
return page, nil
}

func (page *NotesPage) validateRelease(tile VersionNote) error {
func (page *Page) validateRelease(tile TileRelease) error {
_, err := tile.version()
if err != nil {
return fmt.Errorf("invalid version: %w", err)
Expand All @@ -87,14 +87,14 @@ func (page *NotesPage) validateRelease(tile VersionNote) error {
return nil
}

func (page *NotesPage) Add(versionNote VersionNote) error {
func (page *Page) Add(versionNote TileRelease) error {
err := page.validateRelease(versionNote)
if err != nil {
return err
}

if len(page.Releases) == 0 {
page.Releases = []VersionNote{versionNote}
page.Releases = []TileRelease{versionNote}
return nil
}

Expand All @@ -111,7 +111,7 @@ func (page *NotesPage) Add(versionNote VersionNote) error {
if !nv.GreaterThan(tv) {
continue
}
page.Releases = append(page.Releases[:i], append([]VersionNote{versionNote}, page.Releases[i:]...)...)
page.Releases = append(page.Releases[:i], append([]TileRelease{versionNote}, page.Releases[i:]...)...)
return nil
}

Expand All @@ -120,7 +120,7 @@ func (page *NotesPage) Add(versionNote VersionNote) error {
return nil
}

func (page *NotesPage) WriteTo(w io.Writer) (int64, error) {
func (page *Page) WriteTo(w io.Writer) (int64, error) {
buf := new(bytes.Buffer)
n, err := buf.WriteString(page.Prefix)
if err != nil {
Expand All @@ -139,12 +139,12 @@ func (page *NotesPage) WriteTo(w io.Writer) (int64, error) {
return buf.WriteTo(w)
}

type VersionNote struct {
type TileRelease struct {
Version string
Notes string
}

func (notes VersionNote) version() (*semver.Version, error) {
func (notes TileRelease) version() (*semver.Version, error) {
return semver.NewVersion(notes.Version)
}

Expand Down
Loading

0 comments on commit 8ed0858

Please sign in to comment.