Skip to content

Commit

Permalink
feat: add capability to set different commit message for extras updates
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Aug 5, 2020
1 parent c7fd793 commit ca7ef7a
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

BIN_NAME := chart-releaser
BIN_VERSION := 0.1.1
BIN_VERSION := 0.1.2
IMG_NAME := chartreleaser/chart-releaser

GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2> /dev/null || true)
Expand Down
4 changes: 2 additions & 2 deletions example/.chartreleaser.yaml → example/.chartreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ release:
extras:
- path: something.txt
updates:
- search: foo
replace: bar
- search: bar
replace: foo
6 changes: 5 additions & 1 deletion pkg/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ var ConfigHeaderComment = heredoc.Doc(`
`)

// DefaultUpdateCommitMessage is the default template for a commit message used when
// updating a file.
// updating the Chart file.
var DefaultUpdateCommitMessage = `[{{ .Chart.Name }}] bump chart to {{ .Chart.NewVersion }} for new application release ({{ .App.NewVersion }})`

// DefaultExtrasCommitMessage is the default template for a commit message used when
// updating a file specified in the `extras` config.
var DefaultExtrasCommitMessage = `[{{ .Chart.Name }}] update {{ .CurrentFile.Path }} for new application release ({{ .App.NewVersion }})`

// DefaultPullRequestTitle is a template for the default title used when opening a pull
// request for the updates generated by chart-releaser.
//
Expand Down
1 change: 1 addition & 0 deletions pkg/v1/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func (c *CommitConfig) validate() error {
// operations.
type CommitTemplateConfig struct {
Update string `yaml:"update,omitempty"`
Extras string `yaml:"extras,omitempty"`
}

// validate the CommitTemplateConfig is correct.
Expand Down
8 changes: 7 additions & 1 deletion pkg/v1/ctx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ type Repository struct {
type Release struct {
PRTitle string
PRBody string
UpdateCommitMsg string
ChartCommitMsg string
ExtrasCommitMsg string
Matches []*regexp.Regexp
Ignores []*regexp.Regexp
}
Expand All @@ -125,6 +126,11 @@ type Context struct {
Repository Repository
Release Release

// CurrentFile holds a reference to a extras file that is currently being
// worked on when publishing changes. This allows template rendering to
// access information about the file, e.g. .CurrentFile.Path
CurrentFile File

AllowDirty bool
DryRun bool
ShowDiff bool
Expand Down
3 changes: 1 addition & 2 deletions pkg/v1/stages/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"path/filepath"
"strings"

"github.com/edaniszewski/chart-releaser/pkg/client"

"github.com/apex/log"
"github.com/edaniszewski/chart-releaser/pkg/client"
version "github.com/edaniszewski/chart-releaser/pkg/semver"
"github.com/edaniszewski/chart-releaser/pkg/strategies"
context "github.com/edaniszewski/chart-releaser/pkg/v1/ctx"
Expand Down
2 changes: 1 addition & 1 deletion pkg/v1/stages/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ appVersion: 0.2.3
assert.Equal(t, "", context.Git.Base)
assert.Equal(t, "", context.Git.Ref)
assert.Equal(t, "", context.Git.Tag)
assert.Equal(t, "", context.Release.UpdateCommitMsg)
assert.Equal(t, "", context.Release.ChartCommitMsg)
assert.Equal(t, "", context.Release.PRBody)
assert.Equal(t, "", context.Release.PRTitle)
assert.Equal(t, "", context.Repository.Name)
Expand Down
21 changes: 15 additions & 6 deletions pkg/v1/stages/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,22 @@ func loadPublishStrategy(ctx *context.Context) error {

func loadTemplateStrings(ctx *context.Context) error {
if ctx.Config.Commit.Templates == nil {
ctx.Release.UpdateCommitMsg = templates.DefaultUpdateCommitMessage
log.WithField("default", ctx.Release.UpdateCommitMsg).Debug("using default commit message for updating files")
ctx.Release.ChartCommitMsg = templates.DefaultUpdateCommitMessage
log.WithField("default", ctx.Release.ChartCommitMsg).Debug("using default commit message for updating Chart")

ctx.Release.ExtrasCommitMsg = templates.DefaultExtrasCommitMessage
log.WithField("default", ctx.Release.ExtrasCommitMsg).Debug("using default commit message for updating extra files")
} else {
ctx.Release.UpdateCommitMsg = ctx.Config.Commit.Templates.Update
if ctx.Release.UpdateCommitMsg == "" {
ctx.Release.UpdateCommitMsg = templates.DefaultUpdateCommitMessage
log.WithField("default", ctx.Release.UpdateCommitMsg).Debug("using default commit message for updating files")
ctx.Release.ChartCommitMsg = ctx.Config.Commit.Templates.Update
if ctx.Release.ChartCommitMsg == "" {
ctx.Release.ChartCommitMsg = templates.DefaultUpdateCommitMessage
log.WithField("default", ctx.Release.ChartCommitMsg).Debug("using default commit message for updating Chart")
}

ctx.Release.ExtrasCommitMsg = ctx.Config.Commit.Templates.Extras
if ctx.Release.ExtrasCommitMsg == "" {
ctx.Release.ExtrasCommitMsg = templates.DefaultExtrasCommitMessage
log.WithField("default", ctx.Release.ExtrasCommitMsg).Debug("using default commit message for updating extra files")
}
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/v1/stages/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestStage_Run(t *testing.T) {
// Release
assert.Equal(t, "Bump {{ .Chart.Name }} Chart from {{ .Chart.PreviousVersion }} to {{ .Chart.NewVersion }}", context.Release.PRTitle)
assert.Equal(t, "Bumps the {{ .Chart.Name }} Helm Chart from {{ .Chart.PreviousVersion }} to {{ .Chart.NewVersion }}.\n\n{{ if .Files }}The following files have also been updated:\n{{ range .Files }}- {{ .Path }}\n{{ end }}{{ end }}\n---\n*This PR was generated with [chart-releaser](https://github.com/edaniszewski/chart-releaser)*\n", context.Release.PRBody)
assert.Equal(t, "[{{ .Chart.Name }}] bump chart to {{ .Chart.NewVersion }} for new application release ({{ .App.NewVersion }})", context.Release.UpdateCommitMsg)
assert.Equal(t, "[{{ .Chart.Name }}] bump chart to {{ .Chart.NewVersion }} for new application release ({{ .App.NewVersion }})", context.Release.ChartCommitMsg)

// Other
assert.Equal(t, "", context.Token)
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestLoadTemplateStringsDefaultsForCommit(t *testing.T) {
err := loadTemplateStrings(context)
assert.NoError(t, err)

assert.Equal(t, templates.DefaultUpdateCommitMessage, context.Release.UpdateCommitMsg)
assert.Equal(t, templates.DefaultUpdateCommitMessage, context.Release.ChartCommitMsg)
assert.Equal(t, "", context.Release.PRTitle)
assert.Equal(t, "", context.Release.PRBody)
assert.Equal(t, "master", context.Git.Ref)
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestLoadTemplateStringsCustomForCommit(t *testing.T) {
err := loadTemplateStrings(context)
assert.NoError(t, err)

assert.Equal(t, "test update", context.Release.UpdateCommitMsg)
assert.Equal(t, "test update", context.Release.ChartCommitMsg)
assert.Equal(t, "", context.Release.PRTitle)
assert.Equal(t, "", context.Release.PRBody)
assert.Equal(t, "test-branch", context.Git.Ref)
Expand All @@ -301,7 +301,7 @@ func TestLoadTemplateStringsDefaultsForPR(t *testing.T) {
err := loadTemplateStrings(context)
assert.NoError(t, err)

assert.Equal(t, templates.DefaultUpdateCommitMessage, context.Release.UpdateCommitMsg)
assert.Equal(t, templates.DefaultUpdateCommitMessage, context.Release.ChartCommitMsg)
assert.Equal(t, templates.DefaultPullRequestTitle, context.Release.PRTitle)
assert.Equal(t, templates.DefaultPullRequestBody, context.Release.PRBody)
assert.Equal(t, templates.DefaultBranchName, context.Git.Ref)
Expand Down Expand Up @@ -332,7 +332,7 @@ func TestLoadTemplateStringsCustomForPR(t *testing.T) {
err := loadTemplateStrings(context)
assert.NoError(t, err)

assert.Equal(t, "test update", context.Release.UpdateCommitMsg)
assert.Equal(t, "test update", context.Release.ChartCommitMsg)
assert.Equal(t, "title-template", context.Release.PRTitle)
assert.Equal(t, "body-template", context.Release.PRBody)
assert.Equal(t, "branch-template", context.Git.Ref)
Expand Down
3 changes: 1 addition & 2 deletions pkg/v1/stages/extras/extras.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"strings"
"text/template"

"github.com/edaniszewski/chart-releaser/pkg/client"

"github.com/apex/log"
"github.com/edaniszewski/chart-releaser/pkg/client"
context "github.com/edaniszewski/chart-releaser/pkg/v1/ctx"
)

Expand Down
16 changes: 11 additions & 5 deletions pkg/v1/stages/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"errors"
"text/template"

"github.com/edaniszewski/chart-releaser/pkg/client"

"github.com/apex/log"
"github.com/davecgh/go-spew/spew"
"github.com/edaniszewski/chart-releaser/pkg/client"
"github.com/edaniszewski/chart-releaser/pkg/strategies"
"github.com/edaniszewski/chart-releaser/pkg/templates"
context "github.com/edaniszewski/chart-releaser/pkg/v1/ctx"
Expand Down Expand Up @@ -48,7 +47,7 @@ func (Stage) Run(ctx *context.Context) error {
return err
}

ctx.Release.UpdateCommitMsg, err = utils.RenderTemplate(ctx, "update-commit", ctx.Release.UpdateCommitMsg)
ctx.Release.ChartCommitMsg, err = utils.RenderTemplate(ctx, "update-commit", ctx.Release.ChartCommitMsg)
if err != nil {
return err
}
Expand Down Expand Up @@ -129,17 +128,24 @@ func publishCommit(ctx *context.Context) error {

// Update the Chart
if ctx.Chart.File.HasChanges() {
if err := ctx.Client.UpdateFile(ctx.Context, opts, ctx.Chart.File.Path, ctx.Release.UpdateCommitMsg, ctx.Chart.File.NewContents); err != nil {
if err := ctx.Client.UpdateFile(ctx.Context, opts, ctx.Chart.File.Path, ctx.Release.ChartCommitMsg, ctx.Chart.File.NewContents); err != nil {
return err
}
} else {
log.Error("chart has no changes - will not update")
return ErrNoChartChanges
}

// Update each of the extra files which have changes.
for _, f := range ctx.Files {
if f.HasChanges() {
if err := ctx.Client.UpdateFile(ctx.Context, opts, f.Path, ctx.Release.UpdateCommitMsg, f.NewContents); err != nil {
ctx.CurrentFile = f

extrasCommitMsg, err := utils.RenderTemplate(ctx, f.Path, ctx.Release.ExtrasCommitMsg)
if err != nil {
return err
}
if err := ctx.Client.UpdateFile(ctx.Context, opts, f.Path, extrasCommitMsg, f.NewContents); err != nil {
return err
}
} else {
Expand Down
Loading

0 comments on commit ca7ef7a

Please sign in to comment.