diff --git a/cmd/release/cmd/push.go b/cmd/release/cmd/push.go index 42d72c2d..b0c8dbff 100644 --- a/cmd/release/cmd/push.go +++ b/cmd/release/cmd/push.go @@ -6,12 +6,10 @@ import ( "fmt" "os" - "github.com/google/go-github/v39/github" - "github.com/spf13/cobra" - "github.com/rancher/ecm-distro-tools/release/charts" "github.com/rancher/ecm-distro-tools/release/k3s" "github.com/rancher/ecm-distro-tools/repository" + "github.com/spf13/cobra" ) var pushCmd = &cobra.Command{ @@ -68,44 +66,31 @@ var pushChartsCmd = &cobra.Command{ return errors.New("expected 1 argument: [branch-line]") } - var ( - releaseBranch string - found bool - ) - - found = charts.IsBranchAvailable(args[0], rootConfig.Charts.BranchLines) - if !found { - return errors.New("release branch not available: " + releaseBranch) + if found := charts.IsBranchAvailable(args[0], rootConfig.Charts.BranchLines); !found { + return errors.New("release branch not available: " + args[0]) } return nil }, RunE: func(cmd *cobra.Command, args []string) error { - var ( - releaseBranch string // given release branch - - ctx context.Context // background context - t string // token - ghc *github.Client // github client - ) - // arguments - releaseBranch = charts.MountReleaseBranch(args[0]) + releaseBranch := charts.MountReleaseBranch(args[0]) debug, err := cmd.Flags().GetBool("debug") if err != nil { return err } - ctx = context.Background() - t = rootConfig.Auth.GithubToken - ghc = repository.NewGithub(ctx, t) + token := rootConfig.Auth.GithubToken + + ctx := context.Background() + ghc := repository.NewGithub(ctx, token) - prURL, err := charts.Push(ctx, rootConfig.Charts, rootConfig.User, ghc, releaseBranch, t, debug) + prURL, err := charts.Push(ctx, rootConfig.Charts, rootConfig.User, ghc, releaseBranch, token, debug) if err != nil { return err } - fmt.Printf("Pull request created: %s\n", prURL) + fmt.Println("Pull request created: " + prURL) return nil }, } diff --git a/exec/exec.go b/exec/exec.go index 00175b0b..fd3b4a42 100644 --- a/exec/exec.go +++ b/exec/exec.go @@ -55,15 +55,12 @@ func RunTemplatedScript(dir, fileName, scriptTemplate string, funcMap template.F // UserInput will ask for user input with a given title func UserInput(title string) bool { - var input string - var err error - fmt.Println(title) reader := bufio.NewReader(os.Stdin) fmt.Print("([Y]es/[N]o/[A]bort) Enter: ") - input, err = reader.ReadString('\n') + input, err := reader.ReadString('\n') if err != nil { fmt.Println(err) os.Exit(1) diff --git a/release/charts/chart_management.go b/release/charts/chart_management.go index 90ab398b..268db30e 100644 --- a/release/charts/chart_management.go +++ b/release/charts/chart_management.go @@ -21,16 +21,11 @@ type asset struct { // ChartArgs will return the list of available charts in the current branch func ChartArgs(ctx context.Context, c *config.ChartsRelease) ([]string, error) { - var ( - assets string - files []os.DirEntry - dirs []string - err error - ) + var dirs []string - assets = filepath.Join(c.Workspace, "assets") + assets := filepath.Join(c.Workspace, "assets") - files, err = os.ReadDir(assets) + files, err := os.ReadDir(assets) if err != nil { return nil, err } @@ -48,12 +43,7 @@ func ChartArgs(ctx context.Context, c *config.ChartsRelease) ([]string, error) { // VersionArgs will return the list of available versions for the target chart func VersionArgs(ctx context.Context, c *config.ChartsRelease, ch string) ([]string, error) { - var ( - status *status - err error - ) - - status, err = loadState(filepath.Join(c.Workspace, "state.json")) + status, err := loadState(filepath.Join(c.Workspace, "state.json")) if err != nil { return nil, err } @@ -109,14 +99,14 @@ func IsChartAvailable(ctx context.Context, conf *config.ChartsRelease, ch string } // IsVersionAvailable exists to be released or forward ported -func IsVersionAvailable(ctx context.Context, conf *config.ChartsRelease, ch, v string) (bool, error) { +func IsVersionAvailable(ctx context.Context, conf *config.ChartsRelease, ch, version string) (bool, error) { availableVersions, err := VersionArgs(ctx, conf, ch) if err != nil { return false, err } for _, c := range availableVersions { - if c == v { + if c == version { return true, nil } } diff --git a/release/charts/charts.go b/release/charts/charts.go index cee0223f..76955a4f 100644 --- a/release/charts/charts.go +++ b/release/charts/charts.go @@ -8,20 +8,17 @@ import ( "os/exec" "strings" - "github.com/google/go-github/v39/github" - "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/storer" - + "github.com/google/go-github/v39/github" "github.com/rancher/ecm-distro-tools/cmd/release/config" ecmExec "github.com/rancher/ecm-distro-tools/exec" "github.com/rancher/ecm-distro-tools/repository" ) -// body is the default PR body for the charts release PR -const body string = ` +// chartsReleasePRBody is the default PR body for the charts release PR +const chartsReleasePRBody = ` ## Charts Checklist (built for v0.9.X charts-build-scripts) ### Checkpoint 0: Validate **release.yaml** @@ -103,55 +100,42 @@ func Update(ctx context.Context, c *config.ChartsRelease, br, ch, vr string) (st } // Push will push the charts updates to the remote upstream charts repository and create a PR. -// - ctx: background context. -// - c: charts release configuration. -// - u: user configuration with username and email. -// - ghc: github client, to be used to interact with github API. -// - b: release branch name. -// - t: token from github. -// - d: debug mode. -func Push(ctx context.Context, c *config.ChartsRelease, u *config.User, ghc *github.Client, b, t string, d bool) (string, error) { +func Push(ctx context.Context, conf *config.ChartsRelease, user *config.User, ghc *github.Client, branch, token string, debug bool) (string, error) { const repoOwner = "rancher" const repoName = "charts" - var ( - r *git.Repository // local opened repository - h *plumbing.Reference // local head reference - remote string // remote repository name - ) - - r, err := git.PlainOpen(c.Workspace) + r, err := git.PlainOpen(conf.Workspace) if err != nil { return "", err } - remote, err = repository.GetUpstreamRemote(r, c.ChartsRepoURL) + remote, err := repository.UpstreamRemote(r, conf.ChartsRepoURL) if err != nil { return "", err } - h, err = r.Head() + h, err := r.Head() if err != nil { return "", err } // create a new PR pr := &github.NewPullRequest{ - Title: github.String(fmt.Sprintf("[%s] batch release", b)), - Base: github.String(b), + Title: github.String("[" + branch + "] batch release"), + Base: github.String(branch), Head: github.String(h.Name().Short()), - Body: github.String(body), + Body: github.String(chartsReleasePRBody), MaintainerCanModify: github.Bool(true), } // debug mode - if d { - if err := debugPullRequest(r, remote, b); err != nil { + if debug { + if err := debugPullRequest(r, remote, branch); err != nil { return "", err } } - if err := repository.PushRemoteBranch(r, remote, u.GithubUsername, t, d); err != nil { + if err := repository.PushRemoteBranch(r, remote, user.GithubUsername, token, debug); err != nil { return "", err } @@ -192,12 +176,8 @@ func runChartsBuild(chartsRepoPath string, args ...string) ([]byte, error) { } // 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 - ) - - if execute = ecmExec.UserInput("Check files that will be pushed?"); execute { +func debugPullRequest(r *git.Repository, remote, branch string) error { + if execute := ecmExec.UserInput("Check files that will be pushed?"); execute { // commit history iter, err := r.Log(&git.LogOptions{}) if err != nil { @@ -220,15 +200,19 @@ func debugPullRequest(r *git.Repository, remote, b string) error { return nil }) + + if err != nil { + return err + } } - if execute = ecmExec.UserInput("Check commits that will be pushed?"); execute { - if err := repository.DiffLocalToRemote(r, remote, b); err != nil { + if execute := ecmExec.UserInput("Check commits that will be pushed?"); execute { + if err := repository.DiffLocalToRemote(r, remote, branch); err != nil { return err } } - if execute = ecmExec.UserInput("Push and Create PR to the remote repository?"); !execute { + if execute := ecmExec.UserInput("Push and Create PR to the remote repository?"); !execute { return errors.New("user aborted the push") } diff --git a/repository/repository.go b/repository/repository.go index b952e658..364f5fb9 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -14,12 +14,9 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/transport/http" - "github.com/google/go-github/v39/github" - "github.com/rancher/ecm-distro-tools/exec" "github.com/rancher/ecm-distro-tools/types" - "github.com/sirupsen/logrus" "golang.org/x/oauth2" "golang.org/x/text/cases" @@ -477,15 +474,15 @@ To find more information on specific steps, please see documentation [here](http - [ ] PJM: Close the milestone in GitHub. ` -// GetUpstreamRemote will return the remote name for a given configured remote URL -func GetUpstreamRemote(r *git.Repository, rURL string) (string, error) { +// UpstreamRemote will return the remote name for a given configured remote URL +func UpstreamRemote(r *git.Repository, remoteURL string) (string, error) { remotes, err := r.Remotes() if err != nil { return "", err } for _, remote := range remotes { - if remote.Config().URLs[0] == rURL { + if remote.Config().URLs[0] == remoteURL { return remote.Config().Name, nil } } @@ -495,7 +492,7 @@ func GetUpstreamRemote(r *git.Repository, rURL string) (string, error) { // DiffLocalToRemote will get the commits from the local branch and from the target remote branch, // will return the commits that are present in the local branch but not in the remote branch. -func DiffLocalToRemote(r *git.Repository, remote, rb string) error { +func DiffLocalToRemote(r *git.Repository, remote, releaseBranch string) error { var ( refSpec string localRef *plumbing.Reference @@ -503,7 +500,7 @@ func DiffLocalToRemote(r *git.Repository, remote, rb string) error { err error ) - refSpec = "refs/heads/" + rb + ":refs/remotes/" + remote + "/" + rb + refSpec = "refs/heads/" + releaseBranch + ":refs/remotes/" + remote + "/" + releaseBranch fetchOpts := &git.FetchOptions{ RemoteName: remote, @@ -522,7 +519,7 @@ func DiffLocalToRemote(r *git.Repository, remote, rb string) error { return err } - remoteRef, err = r.Reference(plumbing.NewRemoteReferenceName(remote, rb), true) + remoteRef, err = r.Reference(plumbing.NewRemoteReferenceName(remote, releaseBranch), true) if err != nil { return err } @@ -542,22 +539,17 @@ func DiffLocalToRemote(r *git.Repository, remote, rb string) error { } // PushRemoteBranch will push the local branch to the remote repository -func PushRemoteBranch(r *git.Repository, remote, u, t string, debug bool) error { - var ( - branchRef string - h *plumbing.Reference - ) - +func PushRemoteBranch(r *git.Repository, remote, user, token string, debug bool) error { h, err := r.Head() if err != nil { return err } - branchRef = h.Name().String() + branchRef := h.Name().String() auth := &http.BasicAuth{ - Username: u, - Password: t, + Username: user, + Password: token, } opts := &git.PushOptions{ @@ -584,12 +576,12 @@ func findUniqueCommits(r *git.Repository, localRef, remoteRef *plumbing.Referenc ) // Get the local and remote commits - localCommits, err = getCommits(r, localRef.Hash()) + localCommits, err = commits(r, localRef.Hash()) if err != nil { return nil, err } - remoteCommits, err = getCommits(r, remoteRef.Hash()) + remoteCommits, err = commits(r, remoteRef.Hash()) if err != nil { return nil, err } @@ -609,8 +601,8 @@ func findUniqueCommits(r *git.Repository, localRef, remoteRef *plumbing.Referenc return uniqueCommits, nil } -// getCommits returns the commits beginning at the given hash -func getCommits(r *git.Repository, h plumbing.Hash) ([]*object.Commit, error) { +// commits returns the commits beginning at the given hash +func commits(r *git.Repository, h plumbing.Hash) ([]*object.Commit, error) { var ( firstCommit *object.Commit commits []*object.Commit @@ -636,9 +628,11 @@ func getCommits(r *git.Repository, h plumbing.Hash) ([]*object.Commit, error) { return nil }) - if err == io.EOF { - err = nil // Reset error if it was forced by the limit + if err != nil { + if err != io.EOF { + return commits, err + } } - - return commits, err + // Handle error as nil if it was forced by the limit (i.e: err == io.EOF) + return commits, nil }