From 0ce362f839b68dd9f683ef525984408b0a0bf231 Mon Sep 17 00:00:00 2001 From: Kenneth Bingham Date: Tue, 22 Oct 2024 16:33:28 -0400 Subject: [PATCH] handle case where no prior releases --- pkg/github/github.go | 9 +++++++-- pkg/releaser/releaser.go | 20 +++----------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/pkg/github/github.go b/pkg/github/github.go index 2e70e3e..becc3e3 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -31,6 +31,9 @@ import ( "golang.org/x/oauth2" ) +// Define a custom error for no releases found +var ErrNoReleasesFound = errors.New("no releases found") + type Release struct { Name string Description string @@ -118,8 +121,6 @@ func (c *Client) GetLatestChartRelease(_ context.Context, prefix string) (*Relea rels, resp, err := c.Repositories.ListReleases(context.TODO(), c.owner, c.repo, opt) if err != nil { return nil, err - } else if len(rels) == 0 { - return nil, errors.New("no releases found") } for _, rel := range rels { if strings.HasPrefix(*rel.TagName, prefix) { @@ -133,6 +134,10 @@ func (c *Client) GetLatestChartRelease(_ context.Context, prefix string) (*Relea opt.Page = resp.NextPage } + if len(versions) == 0 { + return nil, ErrNoReleasesFound + } + // Sort versions ascending semver.Sort(versions) diff --git a/pkg/releaser/releaser.go b/pkg/releaser/releaser.go index f0cc531..3b7de08 100644 --- a/pkg/releaser/releaser.go +++ b/pkg/releaser/releaser.go @@ -253,24 +253,10 @@ func (r *Releaser) getReleaseNotes(chart *chart.Chart) (string, error) { if r.config.GenerateReleaseNotes { latestRelease, err := r.github.GetLatestChartRelease(context.TODO(), chart.Metadata.Name) if err != nil { - return "", errors.Wrapf(err, "failed to get latest release for chart %s", chart.Metadata.Name) - } - nextVersion := semver.MustParse(chart.Metadata.Version) - versions := []semver.Version{nextVersion, latestRelease.SemVer} - semver.Sort(versions) - highest := versions[len(versions)-1] - // skip generating notes if there's already a higher version in GitHub - if nextVersion.String() == highest.String() { - notes, err := r.github.GenerateReleaseNotes(context.TODO(), latestRelease, chart) - if err != nil { - return "", errors.Wrapf(err, "failed to generate release notes for chart %s", chart.Metadata.Name) + if errors.Is(err, github.ErrNoReleasesFound) { + // Handle the case where there are no releases found + return chart.Metadata.Description, nil } - return notes, nil - } - } - if r.config.GenerateReleaseNotes { - latestRelease, err := r.github.GetLatestChartRelease(context.TODO(), chart.Metadata.Name) - if err != nil { return "", errors.Wrapf(err, "failed to get latest release for chart %s", chart.Metadata.Name) } nextVersion := semver.MustParse(chart.Metadata.Version)