From f7e4366accb9e01645874bd635968cf702700df4 Mon Sep 17 00:00:00 2001 From: Ivan Panteleev Date: Tue, 29 Oct 2024 15:29:48 +0300 Subject: [PATCH] Peform single push on upload in case of multiple releases (#314) * Move worktree creation out of loop and add single push on upload action Signed-off-by: Ivan Panteleev Signed-off-by: amalgamm * Fix lint Signed-off-by: amalgamm --------- Signed-off-by: Ivan Panteleev Signed-off-by: amalgamm --- pkg/releaser/releaser.go | 21 +++++++++++---------- pkg/releaser/releaser_test.go | 4 ++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/releaser/releaser.go b/pkg/releaser/releaser.go index 439ae818..d7676599 100644 --- a/pkg/releaser/releaser.go +++ b/pkg/releaser/releaser.go @@ -289,6 +289,12 @@ func (r *Releaser) addToIndexFile(indexFile *repo.IndexFile, url string) error { // CreateReleases finds and uploads Helm chart packages to GitHub func (r *Releaser) CreateReleases() error { + worktree, err := r.git.AddWorktree("", r.config.Remote+"/"+r.config.PagesBranch) + if err != nil { + return err + } + defer r.git.RemoveWorktree("", worktree) // nolint: errcheck + packages, err := r.getListOfPackages(r.config.PackagePath) if err != nil { return err @@ -334,12 +340,6 @@ func (r *Releaser) CreateReleases() error { } if r.config.PackagesWithIndex { - worktree, err := r.git.AddWorktree("", r.config.Remote+"/"+r.config.PagesBranch) - if err != nil { - return err - } - defer r.git.RemoveWorktree("", worktree) //nolint: errcheck - pkgTargetPath := filepath.Join(worktree, filepath.Base(p)) if err := copyFile(p, pkgTargetPath); err != nil { return err @@ -352,10 +352,11 @@ func (r *Releaser) CreateReleases() error { if err := r.git.Commit(worktree, fmt.Sprintf("Publishing chart package for %s", releaseName)); err != nil { return err } - - if err := r.pushToPagesBranch(worktree); err != nil { - return err - } + } + } + if r.config.Push { + if err := r.pushToPagesBranch(worktree); err != nil { + return err } } diff --git a/pkg/releaser/releaser_test.go b/pkg/releaser/releaser_test.go index 55c7841f..bbb7571a 100644 --- a/pkg/releaser/releaser_test.go +++ b/pkg/releaser/releaser_test.go @@ -510,13 +510,17 @@ func TestReleaser_ReleaseNotes(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { fakeGitHub := new(FakeGitHub) + fakeGit := new(FakeGit) r := &Releaser{ config: &config.Options{ PackagePath: "testdata/release-packages", ReleaseNotesFile: tt.releaseNotesFile, }, github: fakeGitHub, + git: fakeGit, } + fakeGit.On("AddWorktree", mock.Anything, mock.Anything).Return("/tmp/chart-releaser-012345678", nil) + fakeGit.On("RemoveWorktree", mock.Anything, mock.Anything).Return(nil) fakeGitHub.On("CreateRelease", mock.Anything, mock.Anything).Return(nil) err := r.CreateReleases() assert.NoError(t, err)