From c979773c8521cfb9d46a2ea9a780990ff69c2721 Mon Sep 17 00:00:00 2001 From: jose Date: Fri, 10 May 2024 23:49:18 +0100 Subject: [PATCH] feat: enable adding the chart to existing releases --- pkg/github/github.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/github/github.go b/pkg/github/github.go index 4143c50d..cc3e1912 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -16,6 +16,7 @@ package github import ( "context" + "fmt" "net/url" "os" "path/filepath" @@ -115,7 +116,14 @@ func (c *Client) CreateRelease(_ context.Context, input *Release) error { release, _, err := c.Repositories.CreateRelease(context.TODO(), c.owner, c.repo, req) if err != nil { - return err + if !isErrTagAlreadyExist(err) { + return err + } + + release, _, err = c.Repositories.GetReleaseByTag(context.TODO(), c.owner, c.repo, input.Name) + if err != nil { + return fmt.Errorf("getting the existing relase: %w", err) + } } for _, asset := range input.Assets { @@ -126,6 +134,18 @@ func (c *Client) CreateRelease(_ context.Context, input *Release) error { return nil } +func isErrTagAlreadyExist(err error) bool { + var ghErrs *github.ErrorResponse + if errors.As(err, &ghErrs) { + for _, ghErr := range ghErrs.Errors { + if ghErr.Code == "already_exists" && ghErr.Field == "tag_name" { + return true + } + } + } + return false +} + // CreatePullRequest creates a pull request in the repository specified by repoURL. // The return value is the pull request URL. func (c *Client) CreatePullRequest(owner string, repo string, message string, head string, base string) (string, error) {