From 86c41c77209e808d7116fda3c6913faa2b2fb2c0 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Fri, 22 Sep 2023 22:12:27 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20bulk=20commit=20all=20changes=20acr?= =?UTF-8?q?oss=20providers=20into=201=20commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes the release-process a lot easier and creates less noise in the commit history. Signed-off-by: Dominik Richter --- providers-sdk/v1/util/version/version.go | 102 ++++++++++++++--------- 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/providers-sdk/v1/util/version/version.go b/providers-sdk/v1/util/version/version.go index e566ed198c..817dd9cbf6 100644 --- a/providers-sdk/v1/util/version/version.go +++ b/providers-sdk/v1/util/version/version.go @@ -28,9 +28,7 @@ var updateCmd = &cobra.Command{ Short: "try to update the version of the provider", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - for i := range args { - updateVersion(args[i]) - } + updateVersions(args) }, } @@ -52,8 +50,7 @@ func checkUpdate(providerPath string) { return } - commitTitle := conf.name + "-" + conf.version - changes := countChangesSince(commitTitle, providerPath, conf.path) + changes := countChangesSince(conf.title(), providerPath, conf.path) logChanges(changes, conf) } @@ -79,12 +76,30 @@ type providerConf struct { name string } -func (p providerConf) commitTitle() string { - return "🎉 " + p.name + "-" + p.version +func (conf providerConf) title() string { + return conf.name + "-" + conf.version +} + +func (conf providerConf) commitTitle() string { + return "🎉 " + conf.title() +} + +type updateConfs []*providerConf + +func (confs updateConfs) titles() []string { + titles := make([]string, len(confs)) + for i := range confs { + titles[i] = confs[i].title() + } + return titles +} + +func (confs updateConfs) commitTitle() string { + return "🎉 " + strings.Join(confs.titles(), ", ") } -func (p providerConf) branchName() string { - return "version/" + p.name + "-" + p.version +func (confs updateConfs) branchName() string { + return "version/" + strings.Join(confs.titles(), "+") } func getConfig(providerPath string) (*providerConf, error) { @@ -114,34 +129,45 @@ func getConfig(providerPath string) (*providerConf, error) { return &conf, nil } -func updateVersion(providerPath string) { - conf, err := getConfig(providerPath) - if err != nil { - log.Error().Err(err).Str("path", providerPath).Msg("failed to process version") - return - } +func updateVersions(providerPaths []string) { + updated := []*providerConf{} - didUpdate, err := tryUpdate(providerPath, conf) - if err != nil { - log.Fatal().Err(err).Str("path", providerPath).Msg("failed to process version") + for _, path := range providerPaths { + conf, err := tryUpdate(path) + if err != nil { + log.Error().Err(err).Str("path", path).Msg("failed to process version") + continue + } + if conf == nil { + log.Info().Str("path", path).Msg("nothing to update") + continue + } + updated = append(updated, conf) } - if !didUpdate { - log.Info().Msg("nothing to do, bye") - return + + if doCommit { + if err := commitChanges(updated); err != nil { + log.Error().Err(err).Msg("failed to commit changes") + } } } -func tryUpdate(repoPath string, conf *providerConf) (bool, error) { - changes := countChangesSince(conf.commitTitle(), repoPath, conf.path) +func tryUpdate(providerPath string) (*providerConf, error) { + conf, err := getConfig(providerPath) + if err != nil { + return nil, err + } + + changes := countChangesSince(conf.commitTitle(), providerPath, conf.path) logChanges(changes, conf) if changes == 0 { - return false, nil + return nil, nil } version, err := bumpVersion(conf.version) if err != nil || version == "" { - return false, err + return nil, err } res := reVersion.ReplaceAllStringFunc(conf.content, func(v string) string { @@ -157,15 +183,11 @@ func tryUpdate(repoPath string, conf *providerConf) (bool, error) { } log.Info().Str("path", conf.path).Msg("updated config") - if doCommit { - if err = commitChanges(conf); err != nil { - log.Error().Err(err).Msg("failed to commit changes") - } - } else { + if !doCommit { log.Info().Msg("git add " + conf.path + " && git commit -m \"" + conf.commitTitle() + "\"") } - return true, nil + return conf, nil } func bumpVersion(version string) (string, error) { @@ -210,7 +232,7 @@ func bumpVersion(version string) (string, error) { return versions[selection], nil } -func commitChanges(conf *providerConf) error { +func commitChanges(confs updateConfs) error { repo, err := git.PlainOpen(".") if err != nil { return errors.New("failed to open git: " + err.Error()) @@ -226,7 +248,7 @@ func commitChanges(conf *providerConf) error { return errors.New("failed to get git tree: " + err.Error()) } - branchName := conf.branchName() + branchName := confs.branchName() branchRef := plumbing.NewBranchReferenceName(branchName) // Note: The branch may be local and thus won't be found in repo.Branch(branchName) @@ -249,12 +271,14 @@ func commitChanges(conf *providerConf) error { return errors.New("failed to git checkout+create " + branchName + ": " + err.Error()) } - _, err = worktree.Add(conf.path) - if err != nil { - return errors.New("failed to git add: " + err.Error()) + for i := range confs { + _, err = worktree.Add(confs[i].path) + if err != nil { + return errors.New("failed to git add: " + err.Error()) + } } - commit, err := worktree.Commit(conf.commitTitle(), &git.CommitOptions{ + commit, err := worktree.Commit(confs.commitTitle(), &git.CommitOptions{ Author: &object.Signature{ Name: "Mondoo", Email: "hello@mondoo.com", @@ -270,7 +294,7 @@ func commitChanges(conf *providerConf) error { return errors.New("commit is not in repo: " + err.Error()) } - log.Info().Msg("comitted changes for " + conf.name + " " + conf.version) + log.Info().Msg("comitted changes for " + strings.Join(confs.titles(), ", ")) log.Info().Msg("run: git push -u origin " + branchName) return nil } @@ -300,7 +324,7 @@ func countChangesSince(commitTitle string, repoPath string, confPath string) int fmt.Print(".") } - if strings.HasPrefix(c.Message, commitTitle) { + if strings.Contains(c.Message, " "+commitTitle) { found = c break }