-
Notifications
You must be signed in to change notification settings - Fork 23
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
99 additions
and
44 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, providerPath) | ||
logChanges(changes, conf) | ||
} | ||
|
||
|
@@ -72,19 +69,49 @@ var ( | |
reName = regexp.MustCompile(`Name:\s*"([^"]+)",`) | ||
) | ||
|
||
const ( | ||
titlePrefix = "🎉 " | ||
) | ||
|
||
type providerConf struct { | ||
path string | ||
content string | ||
version string | ||
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: "[email protected]", | ||
|
@@ -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 | ||
|