From 41c62a46627ed9a3209bb7cfc1632296ded2ac1d Mon Sep 17 00:00:00 2001 From: Travis Groth Date: Fri, 22 May 2020 09:03:39 -0400 Subject: [PATCH] Permit specifying the target commit of a release (#45) --- cr/cmd/upload.go | 1 + pkg/config/config.go | 1 + pkg/github/github.go | 8 +++++--- pkg/releaser/releaser.go | 1 + pkg/releaser/releaser_test.go | 14 +++++++++++++- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cr/cmd/upload.go b/cr/cmd/upload.go index 79c1cfae..52cd311f 100644 --- a/cr/cmd/upload.go +++ b/cr/cmd/upload.go @@ -49,4 +49,5 @@ func init() { uploadCmd.Flags().StringP("token", "t", "", "GitHub Auth Token") uploadCmd.Flags().StringP("git-base-url", "b", "https://api.github.com/", "GitHub Base URL (only needed for private GitHub)") 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") } diff --git a/pkg/config/config.go b/pkg/config/config.go index 8cb5817a..1af219c2 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -46,6 +46,7 @@ type Options struct { Token string `mapstructure:"token"` GitBaseURL string `mapstructure:"git-base-url"` GitUploadURL string `mapstructure:"git-upload-url"` + Commit string `mapstructure:"commit"` } func LoadConfiguration(cfgFile string, cmd *cobra.Command, requiredFlags []string) (*Options, error) { diff --git a/pkg/github/github.go b/pkg/github/github.go index 9afb9b37..ba72bdaf 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -33,6 +33,7 @@ type Release struct { Name string Description string Assets []*Asset + Commit string } type Asset struct { @@ -102,9 +103,10 @@ func (c *Client) GetRelease(ctx context.Context, tag string) (*Release, error) { // CreateRelease creates a new release object in the GitHub API func (c *Client) CreateRelease(ctx context.Context, input *Release) error { req := &github.RepositoryRelease{ - Name: &input.Name, - Body: &input.Description, - TagName: &input.Name, + Name: &input.Name, + Body: &input.Description, + TagName: &input.Name, + TargetCommitish: &input.Commit, } release, _, err := c.Repositories.CreateRelease(context.TODO(), c.owner, c.repo, req) diff --git a/pkg/releaser/releaser.go b/pkg/releaser/releaser.go index a24a5c56..279e836d 100644 --- a/pkg/releaser/releaser.go +++ b/pkg/releaser/releaser.go @@ -210,6 +210,7 @@ func (r *Releaser) CreateReleases() error { Assets: []*github.Asset{ {Path: p}, }, + Commit: r.config.Commit, } provFile := fmt.Sprintf("%s.prov", p) if _, err := os.Stat(provFile); err == nil { diff --git a/pkg/releaser/releaser_test.go b/pkg/releaser/releaser_test.go index 828ff37d..adf77b75 100644 --- a/pkg/releaser/releaser_test.go +++ b/pkg/releaser/releaser_test.go @@ -209,6 +209,7 @@ func TestReleaser_CreateReleases(t *testing.T) { packagePath string chart string version string + commit string error bool }{ { @@ -216,6 +217,7 @@ func TestReleaser_CreateReleases(t *testing.T) { "testdata/does-not-exist", "test-chart", "0.1.0", + "", true, }, { @@ -223,6 +225,15 @@ func TestReleaser_CreateReleases(t *testing.T) { "testdata/release-packages", "test-chart", "0.1.0", + "", + false, + }, + { + "valid-package-path-with-commit", + "testdata/release-packages", + "test-chart", + "0.1.0", + "5e239bd19fbefb9eb0181ecf0c7ef73b8fe2753c", false, }, } @@ -230,7 +241,7 @@ func TestReleaser_CreateReleases(t *testing.T) { t.Run(tt.name, func(t *testing.T) { fakeGitHub := new(FakeGitHub) r := &Releaser{ - config: &config.Options{PackagePath: tt.packagePath}, + config: &config.Options{PackagePath: tt.packagePath, Commit: tt.commit}, github: fakeGitHub, } fakeGitHub.On("CreateRelease", mock.Anything, mock.Anything).Return(nil) @@ -248,6 +259,7 @@ func TestReleaser_CreateReleases(t *testing.T) { assert.Equal(t, releaseDescription, fakeGitHub.release.Description) assert.Len(t, fakeGitHub.release.Assets, 1) assert.Equal(t, assetPath, fakeGitHub.release.Assets[0].Path) + assert.Equal(t, tt.commit, fakeGitHub.release.Commit) fakeGitHub.AssertNumberOfCalls(t, "CreateRelease", 1) } })