Skip to content

Commit

Permalink
Merge pull request #188 from 5ouma/feat-commit-prefix
Browse files Browse the repository at this point in the history
Change the commit message prefix
  • Loading branch information
Songmu authored Oct 27, 2024
2 parents e7bca42 + 710f694 commit d9c28da
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ Label of major update targets. Default is [major]
### tagpr.minorLabels (Optional)
Label of minor update targets. Default is [minor]

### tagpr.commitPrefix (Optional)
Prefix of commit message. Default is "[tagpr]"

## GitHub Enterprise
If you are using GitHub Enterprise, use `GH_ENTERPRISE_TOKEN` instead of `GITHUB_TOKEN`.

Expand Down
22 changes: 22 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ const (
# tagpr.minorLabels (Optional)
# Label of minor update targets. Default is [minor]
#
# tagpr.commitPrefix (Optional)
# Prefix of commit message. Default is "[tagpr]"
#
[tagpr]
`
defaultMajorLabels = "major"
defaultMinorLabels = "minor"
defaultCommitPrefix = "[tagpr]"
envConfigFile = "TAGPR_CONFIG_FILE"
envReleaseBranch = "TAGPR_RELEASE_BRANCH"
envVersionFile = "TAGPR_VERSION_FILE"
Expand All @@ -67,6 +71,7 @@ const (
envRelease = "TAGPR_RELEASE"
envMajorLabels = "TAGPR_MAJOR_LABELS"
envMinorLabels = "TAGPR_MAINOR_LABELS"
envCommitPrefix = "TAGPR_COMMIT_PREFIX"
configReleaseBranch = "tagpr.releaseBranch"
configVersionFile = "tagpr.versionFile"
configVPrefix = "tagpr.vPrefix"
Expand All @@ -77,6 +82,7 @@ const (
configRelease = "tagpr.release"
configMajorLabels = "tagpr.majorLabels"
configMinorLabels = "tagpr.minorLabels"
configCommitPrefix = "tagpr.commitPrefix"
)

type config struct {
Expand All @@ -90,6 +96,7 @@ type config struct {
changelog *bool
majorLabels *string
minorLabels *string
commitPrefix *string

conf string
gitconfig *gitconfig.Config
Expand Down Expand Up @@ -211,6 +218,17 @@ func (cfg *config) Reload() error {
}
}

if prefix := os.Getenv(envCommitPrefix); prefix != "" {
cfg.commitPrefix = github.String(prefix)
} else {
prefix, err := cfg.gitconfig.Get(configCommitPrefix)
if err == nil {
cfg.commitPrefix = github.String(prefix)
} else {
cfg.commitPrefix = github.String(defaultCommitPrefix)
}
}

return nil
}

Expand Down Expand Up @@ -333,3 +351,7 @@ func (cfg *config) MinorLabels() []string {

return labels
}

func (cfg *config) CommitPrefix() string {
return stringify(cfg.commitPrefix)
}
3 changes: 3 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func TestConfig(t *testing.T) {
if e, g := []string{"minor"}, cfg.MinorLabels(); !reflect.DeepEqual(e, g) {
t.Errorf("got: %s, expext: %s", g, e)
}
if e, g := "[tagpr]", cfg.CommitPrefix(); !reflect.DeepEqual(e, g) {
t.Errorf("got: %s, expext: %s", g, e)
}

b, err := os.ReadFile(confPath)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions tagpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const (
gitUser = "github-actions[bot]"
gitEmail = "github-actions[bot]@users.noreply.github.com"
defaultReleaseBranch = "main"
autoCommitMessage = "[tagpr] prepare for the next release"
autoChangelogMessage = "[tagpr] update CHANGELOG.md"
autoCommitMessage = "prepare for the next release"
autoChangelogMessage = "update CHANGELOG.md"
autoLabelName = "tagpr"
branchPrefix = "tagpr-from-"
)
Expand Down Expand Up @@ -119,6 +119,9 @@ func isTagPR(pr *github.PullRequest) bool {
}

func (tp *tagpr) Run(ctx context.Context) error {
commitMessage := tp.cfg.CommitPrefix() + " " + autoCommitMessage
changelogMessage := tp.cfg.CommitPrefix() + " " + autoChangelogMessage

latestSemverTag := tp.latestSemverTag()
currVerStr := latestSemverTag
fromCommitish := "refs/tags/" + currVerStr
Expand Down Expand Up @@ -345,7 +348,7 @@ OUT:
tp.c.Git("add", "-f", releaseYml)
}

if _, _, err := tp.c.Git("commit", "--allow-empty", "-am", autoCommitMessage); err != nil {
if _, _, err := tp.c.Git("commit", "--allow-empty", "-am", commitMessage); err != nil {
return err
}

Expand All @@ -366,7 +369,7 @@ OUT:
}
commitish := m[0]
subject := strings.TrimSpace(m[1])
if subject != autoCommitMessage && subject != autoChangelogMessage {
if subject != commitMessage && subject != changelogMessage {
cherryPicks = append(cherryPicks, commitish)
}
}
Expand Down Expand Up @@ -430,7 +433,7 @@ OUT:
}

tp.c.Git("add", changelogMd)
tp.c.Git("commit", "-m", autoChangelogMessage)
tp.c.Git("commit", "-m", changelogMessage)
}

if _, _, err := tp.c.Git("push", "--force", tp.remoteName, rcBranch); err != nil {
Expand Down

0 comments on commit d9c28da

Please sign in to comment.