From 3e65e78dee20cd5a61359476097975e22ac91a9a Mon Sep 17 00:00:00 2001 From: Jasper Kamerling Date: Wed, 8 Nov 2023 15:56:33 +0100 Subject: [PATCH] Add use-existing-release option to upload functionality This makes it possible to add the release packages to an existing release instead of creating a new release. This is useful when a release is used to trigger the helm package and upload. Signed-off-by: Jasper Kamerling --- README.md | 1 + cr/cmd/upload.go | 1 + doc/cr_upload.md | 1 + pkg/config/config.go | 1 + pkg/github/github.go | 30 ++++++++++++++++++++++++------ pkg/releaser/releaser.go | 12 ++++++++++-- pkg/releaser/releaser_test.go | 5 +++++ 7 files changed, 43 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a76de519..7fa1e18f 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ Flags: --release-name-template string Go template for computing release names, using chart metadata (default "{{ .Name }}-{{ .Version }}") --release-notes-file string Markdown file with chart release notes. If it is set to empty string, or the file is not found, the chart description will be used instead. The file is read from the chart package --skip-existing Skip upload if release exists + --use-existing-release Add pacakges to exisiting release instead of creating new release -t, --token string GitHub Auth Token --make-release-latest bool Mark the created GitHub release as 'latest' (default "true") --packages-with-index Host the package files in the GitHub Pages branch diff --git a/cr/cmd/upload.go b/cr/cmd/upload.go index 18cd6f83..e68a4015 100644 --- a/cr/cmd/upload.go +++ b/cr/cmd/upload.go @@ -52,6 +52,7 @@ func init() { uploadCmd.Flags().StringP("git-upload-url", "u", "https://uploads.github.com/", "GitHub Upload URL (only needed for private GitHub)") uploadCmd.Flags().StringP("commit", "c", "", "Target commit for release") uploadCmd.Flags().Bool("skip-existing", false, "Skip upload if release exists") + uploadCmd.Flags().Bool("use-existing-release", false, "Skip upload if release exists") uploadCmd.Flags().String("release-name-template", "{{ .Name }}-{{ .Version }}", "Go template for computing release names, using chart metadata") uploadCmd.Flags().String("release-notes-file", "", "Markdown file with chart release notes. "+ "If it is set to empty string, or the file is not found, the chart description will be used instead. The file is read from the chart package") diff --git a/doc/cr_upload.md b/doc/cr_upload.md index a6bf1ec6..f6642414 100644 --- a/doc/cr_upload.md +++ b/doc/cr_upload.md @@ -31,6 +31,7 @@ cr upload [flags] --remote string The Git remote used when creating a local worktree for the GitHub Pages branch (default "origin") --skip-existing Skip upload if release exists -t, --token string GitHub Auth Token + --use-existing-release Skip upload if release exists ``` ### Options inherited from parent commands diff --git a/pkg/config/config.go b/pkg/config/config.go index a0d309ca..ce744d73 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -58,6 +58,7 @@ type Options struct { Remote string `mapstructure:"remote"` ReleaseNameTemplate string `mapstructure:"release-name-template"` SkipExisting bool `mapstructure:"skip-existing"` + UseExistingRelease bool `mapstructure:"use-existing-release"` ReleaseNotesFile string `mapstructure:"release-notes-file"` GenerateReleaseNotes bool `mapstructure:"generate-release-notes"` MakeReleaseLatest bool `mapstructure:"make-release-latest"` diff --git a/pkg/github/github.go b/pkg/github/github.go index 4143c50d..71be5bcc 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -118,12 +118,20 @@ func (c *Client) CreateRelease(_ context.Context, input *Release) error { return err } - for _, asset := range input.Assets { - if err := c.uploadReleaseAsset(context.TODO(), *release.ID, asset.Path); err != nil { - return err - } + return c.uploadReleaseAssets(context.TODO(), *release.ID, input.Assets) +} + +// AddAssetsToRelease Adds assets to an existing release +func (c *Client) AddAssetsToRelease(_ context.Context, releaseName string, assets []*Asset) error { + release, _, err := c.Repositories.GetReleaseByTag(context.TODO(), c.owner, c.repo, releaseName) + if err != nil { + return err } - return nil + if release == nil { + return errors.Wrapf(err, "could not find GitHub release to add assets %s", releaseName) + } + + return c.uploadReleaseAssets(context.TODO(), *release.ID, assets) } // CreatePullRequest creates a pull request in the repository specified by repoURL. @@ -149,7 +157,17 @@ func (c *Client) CreatePullRequest(owner string, repo string, message string, he return *pullRequest.HTMLURL, nil } -// UploadAsset uploads specified assets to a given release object +// uploadReleaseAssets uploads specified assets to a given release object +func (c *Client) uploadReleaseAssets(_ context.Context, releaseID int64, assets []*Asset) error { + for _, asset := range assets { + if err := c.uploadReleaseAsset(context.TODO(), releaseID, asset.Path); err != nil { + return err + } + } + return nil +} + +// uploadReleaseAsset uploads specified an asset to a given release object func (c *Client) uploadReleaseAsset(_ context.Context, releaseID int64, filename string) error { filename, err := filepath.Abs(filename) if err != nil { diff --git a/pkg/releaser/releaser.go b/pkg/releaser/releaser.go index 7974647e..a69f1d4c 100644 --- a/pkg/releaser/releaser.go +++ b/pkg/releaser/releaser.go @@ -49,6 +49,7 @@ import ( // objects type GitHub interface { CreateRelease(ctx context.Context, input *github.Release) error + AddAssetsToRelease(ctx context.Context, tag string, assets []*github.Asset) error GetRelease(ctx context.Context, tag string) (*github.Release, error) CreatePullRequest(owner string, repo string, message string, head string, base string) (string, error) } @@ -341,8 +342,15 @@ func (r *Releaser) CreateReleases() error { continue } } - if err := r.github.CreateRelease(context.TODO(), release); err != nil { - return errors.Wrapf(err, "error creating GitHub release %s", releaseName) + + if r.config.UseExistingRelease { + if err := r.github.AddAssetsToRelease(context.TODO(), releaseName, release.Assets); err != nil { + return errors.Wrapf(err, "error adding assets to GitHub release %s", releaseName) + } + } else { + if err := r.github.CreateRelease(context.TODO(), release); err != nil { + return errors.Wrapf(err, "error creating GitHub release %s", releaseName) + } } if r.config.PackagesWithIndex { diff --git a/pkg/releaser/releaser_test.go b/pkg/releaser/releaser_test.go index 55c7841f..d2c54f57 100644 --- a/pkg/releaser/releaser_test.go +++ b/pkg/releaser/releaser_test.go @@ -92,6 +92,11 @@ func (f *FakeGitHub) CreateRelease(ctx context.Context, input *github.Release) e return nil } +func (f *FakeGitHub) AddAssetsToRelease(ctx context.Context, releaseName string, assets []*github.Asset) error { + f.Called(ctx, releaseName, assets) + return nil +} + func (f *FakeGitHub) GetRelease(ctx context.Context, tag string) (*github.Release, error) { //nolint: revive release := &github.Release{ Name: "testdata/release-packages/test-chart-0.1.0",