Skip to content

Commit

Permalink
Add use-existing-release option to upload functionality
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jasperkamerling committed Nov 8, 2023
1 parent fc4d837 commit 736daeb
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 packages to existing 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
Expand Down
1 change: 1 addition & 0 deletions cr/cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "Add packages to existing release instead of creating new release")
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")
Expand Down
1 change: 1 addition & 0 deletions doc/cr_upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 Add packages to existing release instead of creating new release
```

### Options inherited from parent commands
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
30 changes: 24 additions & 6 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions pkg/releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions pkg/releaser/releaser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 736daeb

Please sign in to comment.