Skip to content

Commit

Permalink
refactor(internal/pkg/cmd/bump.go): Update pre-bump and post-bump scr…
Browse files Browse the repository at this point in the history
…ipts execution flow (#66)

- Replace config.RunHook("pre-bump") with config.RunPreBump() for pre-bump script execution
- Replace config.RunHook("post-bump") with config.RunPostBump() for post-bump script execution
- Update error handling in case of script failures to provide more informative messages
- Modify the increment version function call sequence and update related error handling
- Add logging for change information during bump operation
- Update the version updating process and handle errors appropriately
  • Loading branch information
sergiotejon authored Nov 21, 2024
1 parent 312dcda commit 8c66425
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
55 changes: 45 additions & 10 deletions internal/app/gommitizen/config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ import (
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"strings"
)

type ConfigVersion struct {
dirPath string

Version string `json:"version" yaml:"version" plain:"version"`
Commit string `json:"commit" yaml:"commit" plain:"commit"`
VersionFiles []string `json:"version_files" yaml:"version_files" plain:"version_files"`
Alias string `json:"alias" yaml:"alias" plain:"alias"`
Hooks map[string]string `json:"hooks,omitempty" yaml:"hooks,omitempty" plain:"hooks,omitempty"`
UpdateChangelogOnBump bool `json:"update_changelog_on_bump,omitempty" yaml:"update_changelog_on_bump,omitempty" plain:"update_changelog_on_bump,omitempty"`
Version string `json:"version" yaml:"version" plain:"version"`
Commit string `json:"commit" yaml:"commit" plain:"commit"`
VersionFiles []string `json:"version_files" yaml:"version_files" plain:"version_files"`
Alias string `json:"alias" yaml:"alias" plain:"alias"`
Hooks HookTypes `json:"hooks,omitempty" yaml:"hooks,omitempty" plain:"hooks,omitempty"`
UpdateChangelogOnBump bool `json:"update_changelog_on_bump,omitempty" yaml:"update_changelog_on_bump,omitempty" plain:"update_changelog_on_bump,omitempty"`
}

type HookTypes struct {
PreBump string `json:"pre_bump,omitempty" yaml:"pre_bump,omitempty" plain:"pre_bump,omitempty"`
PostBump string `json:"post_bump,omitempty" yaml:"post_bump,omitempty" plain:"post_bump,omitempty"`
PreChangelog string `json:"pre_changelog,omitempty" yaml:"pre_changelog,omitempty" plain:"pre_changelog,omitempty"`
PostChangelog string `json:"post_changelog,omitempty" yaml:"post_changelog,omitempty" plain:"post_changelog,omitempty"`
}

func NewConfigVersion(dirPath string, version string, commit string, alias string) *ConfigVersion {
Expand All @@ -45,6 +53,8 @@ func ReadConfigVersion(configVersionPath string) (*ConfigVersion, error) {
return nil, fmt.Errorf("read file %s: %v", configVersionPath, err)
}

slog.Debug(fmt.Sprintf("reading config version in %s with data:\n%s", configVersionPath, string(data)))

var version ConfigVersion
err = json.Unmarshal(data, &version)
if err != nil {
Expand All @@ -55,12 +65,14 @@ func ReadConfigVersion(configVersionPath string) (*ConfigVersion, error) {
return &version, nil
}

func (v ConfigVersion) Save() error {
func (v *ConfigVersion) Save() error {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Errorf("parse struct to json: %v", err)
}

slog.Debug(fmt.Sprintf("saving config version in %s with data:\n%s", v.GetFilePath(), string(data)))

err = os.WriteFile(v.GetFilePath(), data, 0644)
if err != nil {
return fmt.Errorf("write file %s: %v", v.GetFilePath(), err)
Expand All @@ -84,12 +96,35 @@ func (v ConfigVersion) GetGitTag() string {
return v.Version
}

func (v *ConfigVersion) RunHook(hookName string) error {
hook, ok := v.Hooks[hookName]
if !ok {
func (v *ConfigVersion) RunPreBump() error {
return v.runHook("PreBump")
}

func (v *ConfigVersion) RunPostBump() error {
return v.runHook("PostBump")
}

func (v *ConfigVersion) RunPreChangelog() error {
return v.runHook("PreChangelog")
}

func (v *ConfigVersion) RunPostChangelog() error {
return v.runHook("PostChangelog")
}

func (v *ConfigVersion) runHook(hookName string) error {
hookValue := reflect.ValueOf(v.Hooks).FieldByName(hookName)
if !hookValue.IsValid() {
slog.Debug(fmt.Sprintf("hook %s not found", hookName))
return nil
}
hook := hookValue.String()
if len(hook) == 0 {
slog.Debug(fmt.Sprintf("hook %s is empty", hookName))
return nil
}

slog.Debug(fmt.Sprintf("running hook %s: %s", hookName, hook))

output, err := exec.Command("bash", "-c", hook).CombinedOutput()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/cmd/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func bumpByConfig(configVersionPath string, createChangelog bool, incrementType
// If the file has been modified, update the version
if incrementType != "none" {
// Running pre-bump scripts
err = config.RunHook("pre-bump")
err = config.RunPreBump()
if err != nil {
return []string{}, "", fmt.Errorf("pre bump scripts: %s", err)
}
Expand All @@ -144,14 +144,14 @@ func bumpByConfig(configVersionPath string, createChangelog bool, incrementType
}

// Running post-bump scripts
err = config.RunHook("post-bump")
err = config.RunPostBump()
if err != nil {
return []string{}, "", fmt.Errorf("post bump scripts: %s", err)
}

if createChangelog {
// Running pre-changelog scripts
err = config.RunHook("pre-changelog")
err = config.RunPreChangelog()
if err != nil {
return []string{}, "", fmt.Errorf("pre changelog scripts: %s", err)
}
Expand All @@ -164,7 +164,7 @@ func bumpByConfig(configVersionPath string, createChangelog bool, incrementType
modifiedFiles = append(modifiedFiles, changelogFilePath)

// Running post-changelog scripts
err = config.RunHook("post-changelog")
err = config.RunPostChangelog()
if err != nil {
return []string{}, "", fmt.Errorf("post changelog scripts: %s", err)
}
Expand Down

0 comments on commit 8c66425

Please sign in to comment.