From 5d99437cc744e82d1586fb866faece9ea4683310 Mon Sep 17 00:00:00 2001 From: nicholasSSUSE Date: Sat, 10 Aug 2024 12:24:12 -0300 Subject: [PATCH] fixes --- cmd/release/cmd/push.go | 37 +++++++++--------- release/charts/chart_management.go | 5 +++ release/charts/charts.go | 63 ++++++++++++++---------------- repository/repository.go | 3 +- 4 files changed, 54 insertions(+), 54 deletions(-) diff --git a/cmd/release/cmd/push.go b/cmd/release/cmd/push.go index a4c9499e..31801ec7 100644 --- a/cmd/release/cmd/push.go +++ b/cmd/release/cmd/push.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "log" + "os" "github.com/google/go-github/v39/github" "github.com/spf13/cobra" @@ -43,27 +43,29 @@ var pushK3sTagsCmd = &cobra.Command{ } var pushChartsCmd = &cobra.Command{ - Use: "charts [release_branch] [debug (optional)]", + Use: "charts [branch-line] [debug (optional)]", Short: "Push charts updates to remote upstream charts repository", - Example: "release push charts release-v2.9", + Example: "release push charts 2.9", ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if err := validateChartConfig(); err != nil { - log.Fatal(err) + fmt.Println(err) + os.Exit(1) } if len(args) == 0 { - return charts.ReleaseBranches(), cobra.ShellCompDirectiveNoFileComp + return rootConfig.Charts.BranchLines, cobra.ShellCompDirectiveNoFileComp } return nil, cobra.ShellCompDirectiveNoFileComp }, Args: func(cmd *cobra.Command, args []string) error { if err := validateChartConfig(); err != nil { - log.Fatal(err) + fmt.Println(err) + os.Exit(1) } if len(args) < 1 { - return errors.New("expected 1 argument: [release_branch]") + return errors.New("expected 1 argument: [branch-line]") } var ( @@ -71,9 +73,7 @@ var pushChartsCmd = &cobra.Command{ found bool ) - releaseBranch = args[0] - - found = charts.CheckReleaseBranchArgs(releaseBranch) + found = charts.IsBranchAvailable(args[0], rootConfig.Charts.BranchLines) if !found { return errors.New("release branch not available: " + releaseBranch) } @@ -82,17 +82,16 @@ var pushChartsCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) error { var ( - releaseBranch string // given release branch - debug bool // debug mode - ctx context.Context // background context - t string // token - ghc *github.Client // github client - prURL string // created PR Url to be printed - err error + releaseBranch string // given release branch + debug bool // debug mode + + ctx context.Context // background context + t string // token + ghc *github.Client // github client ) // arguments - releaseBranch = args[0] + releaseBranch = charts.MountReleaseBranch(args[0]) if len(args) > 1 { if args[1] == "debug" || args[1] == "d" { debug = true @@ -103,7 +102,7 @@ var pushChartsCmd = &cobra.Command{ t = rootConfig.Auth.GithubToken ghc = repository.NewGithub(ctx, t) - prURL, err = charts.Push(ctx, rootConfig.Charts, rootConfig.User, ghc, releaseBranch, t, debug) + prURL, err := charts.Push(ctx, rootConfig.Charts, rootConfig.User, ghc, releaseBranch, t, debug) if err != nil { return err } diff --git a/release/charts/chart_management.go b/release/charts/chart_management.go index 2e752218..90ab398b 100644 --- a/release/charts/chart_management.go +++ b/release/charts/chart_management.go @@ -76,6 +76,11 @@ func VersionArgs(ctx context.Context, c *config.ChartsRelease, ch string) ([]str return versions, nil } +// MountReleaseBranch will mount the release branch name from the line provided +func MountReleaseBranch(line string) string { + return "release-v" + line +} + // IsBranchAvailable will check if the branch line exists func IsBranchAvailable(branch string, availableBranches []string) bool { for _, b := range availableBranches { diff --git a/release/charts/charts.go b/release/charts/charts.go index e0446385..2c54b706 100644 --- a/release/charts/charts.go +++ b/release/charts/charts.go @@ -103,34 +103,6 @@ func Update(ctx context.Context, c *config.ChartsRelease, br, ch, vr string) (st return string(output), nil } -func runChartsBuild(chartsRepoPath string, args ...string) ([]byte, error) { - // save current working dir - ecmWorkDir, err := os.Getwd() - if err != nil { - return nil, err - } - - // change working dir to the charts repo - if err := os.Chdir(chartsRepoPath); err != nil { - return nil, err - } - - bin := strings.Join([]string{chartsRepoPath, "bin", "charts-build-scripts"}, string(os.PathSeparator)) - - cmd := exec.Command(bin, args...) - output, err := cmd.CombinedOutput() - if err != nil { - return nil, errors.New(err.Error() + ": " + string(output)) - } - - // Change back working dir for the caller - if err := os.Chdir(ecmWorkDir); err != nil { - return nil, errors.New(err.Error() + ": " + string(output)) - } - - return output, nil -} - // Push will push the charts updates to the remote upstream charts repository func Push(ctx context.Context, c *config.ChartsRelease, u *config.User, ghc *github.Client, b, t string, d bool) (string, error) { const repoOwner = "rancher" @@ -140,10 +112,9 @@ func Push(ctx context.Context, c *config.ChartsRelease, u *config.User, ghc *git r *git.Repository // local opened repository h *plumbing.Reference // local head reference remote string // remote repository name - err error ) - r, err = git.PlainOpen(c.Workspace) + r, err := git.PlainOpen(c.Workspace) if err != nil { return "", err } @@ -186,17 +157,43 @@ func Push(ctx context.Context, c *config.ChartsRelease, u *config.User, ghc *git return prResp.GetHTMLURL(), nil } +func runChartsBuild(chartsRepoPath string, args ...string) ([]byte, error) { + // save current working dir + ecmWorkDir, err := os.Getwd() + if err != nil { + return nil, err + } + + // change working dir to the charts repo + if err := os.Chdir(chartsRepoPath); err != nil { + return nil, err + } + + bin := strings.Join([]string{chartsRepoPath, "bin", "charts-build-scripts"}, string(os.PathSeparator)) + + cmd := exec.Command(bin, args...) + output, err := cmd.CombinedOutput() + if err != nil { + return nil, errors.New(err.Error() + ": " + string(output)) + } + + // Change back working dir for the caller + if err := os.Chdir(ecmWorkDir); err != nil { + return nil, errors.New(err.Error() + ": " + string(output)) + } + + return output, nil +} + // debugPullRequest will prompt the user to check the files and commits that will be pushed to the remote repository func debugPullRequest(r *git.Repository, remote, b string) error { var ( execute bool - iter object.CommitIter - err error ) if execute = ecmExec.UserInput("Check files that will be pushed?"); execute { // commit history - iter, err = r.Log(&git.LogOptions{}) + iter, err := r.Log(&git.LogOptions{}) if err != nil { return err } diff --git a/repository/repository.go b/repository/repository.go index 59257c81..e3974f09 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -545,10 +545,9 @@ func PushRemoteBranch(r *git.Repository, remote, u, t string, debug bool) error var ( branchRef string h *plumbing.Reference - err error ) - h, err = r.Head() + h, err := r.Head() if err != nil { return err }