From 56dea5675bf046e9972c3053e6aedf6cc3433773 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 | 143 ++++++++++++++++------- 1 file changed, 99 insertions(+), 44 deletions(-) diff --git a/providers-sdk/v1/util/version/version.go b/providers-sdk/v1/util/version/version.go index e566ed198c..fa6105f6ea 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, providerPath) logChanges(changes, conf) } @@ -72,6 +69,10 @@ var ( reName = regexp.MustCompile(`Name:\s*"([^"]+)",`) ) +const ( + titlePrefix = "🎉 " +) + type providerConf struct { path string content string @@ -79,12 +80,38 @@ 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 titlePrefix + 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 getVersion(content string) string { + m := reVersion.FindStringSubmatch(content) + if len(m) == 0 { + return "" + } + return m[1] } func getConfig(providerPath string) (*providerConf, error) { @@ -105,43 +132,52 @@ func getConfig(providerPath string) (*providerConf, error) { } conf.name = m[1] - m = reVersion.FindStringSubmatch(conf.content) - if len(m) == 0 { + conf.version = getVersion(conf.content) + if conf.version == "" { return nil, errors.New("no provider version found in config") } - - conf.version = m[1] 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, providerPath) 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 +193,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 +242,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 +258,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 +281,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,12 +304,20 @@ 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 } -func countChangesSince(commitTitle string, repoPath string, confPath string) int { +func titleOf(msg string) string { + i := strings.Index(msg, "\n") + if i != -1 { + return msg[0:i] + } + return msg +} + +func countChangesSince(conf *providerConf, repoPath string) int { repo, err := git.PlainOpen(".") if err != nil { log.Fatal().Err(err).Msg("failed to open git repo") @@ -300,11 +342,24 @@ func countChangesSince(commitTitle string, repoPath string, confPath string) int fmt.Print(".") } - if strings.HasPrefix(c.Message, commitTitle) { + if strings.HasPrefix(c.Message, titlePrefix) && strings.Contains(titleOf(c.Message), " "+conf.title()) { found = c break } + file, _ := c.File(conf.path) + if file != nil { + content, err := file.Contents() + if err != nil { + log.Error().Msg("failed to read change to " + conf.path + " from commit history") + } + + if getVersion(content) == conf.version { + found = c + break + } + } + count++ if fastMode { return count