-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ bulk commit all changes across providers into 1 commit
This makes the release-process a lot easier and creates less noise in the commit history. Signed-off-by: Dominik Richter <[email protected]>
- Loading branch information
Showing
1 changed file
with
63 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: "[email protected]", | ||
|
@@ -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 | ||
} | ||
|