Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to template brew message #2447

Merged
merged 2 commits into from Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/pipe/brew/brew.go
Expand Up @@ -92,6 +92,9 @@ func (Pipe) Default(ctx *context.Context) error {
if brew.CommitAuthor.Email == "" {
brew.CommitAuthor.Email = "goreleaser@carlosbecker.com"
}
if brew.CommitMessageTemplate == "" {
brew.CommitMessageTemplate = "Brew formula update for {{ .ProjectName }} version {{ .Tag }}"
}
if brew.Name == "" {
brew.Name = ctx.Config.ProjectName
}
Expand Down Expand Up @@ -182,7 +185,10 @@ func doRun(ctx *context.Context, brew config.Homebrew, cl client.Client) error {
WithField("repo", repo.String()).
Info("pushing")

msg := fmt.Sprintf("Brew formula update for %s version %s", ctx.Config.ProjectName, ctx.Git.CurrentTag)
msg, err := tmpl.New(ctx).Apply(brew.CommitMessageTemplate)
if err != nil {
return err
}
return cl.CreateFile(ctx, brew.CommitAuthor, repo, []byte(content), gpath, msg)
}

Expand Down
104 changes: 66 additions & 38 deletions internal/pipe/brew/brew_test.go
Expand Up @@ -127,43 +127,65 @@ func TestSplit(t *testing.T) {
}

func TestRunPipe(t *testing.T) {
for name, fn := range map[string]func(ctx *context.Context){
"default": func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
},
"custom_download_strategy": func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"

ctx.Config.Brews[0].DownloadStrategy = "GitHubPrivateRepositoryReleaseDownloadStrategy"
},
"custom_require": func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"

ctx.Config.Brews[0].DownloadStrategy = "CustomDownloadStrategy"
ctx.Config.Brews[0].CustomRequire = "custom_download_strategy"
},
"custom_block": func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"

ctx.Config.Brews[0].CustomBlock = `head "https://github.com/caarlos0/test.git"`
},
"default_gitlab": func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitLab
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://gitlab.com/goreleaser"
type testcase struct {
prepare func(ctx *context.Context)
expectedError string
}
for name, tt := range map[string]testcase{
"default": {
prepare: func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"
},
},
"custom_download_strategy": {
prepare: func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"

ctx.Config.Brews[0].DownloadStrategy = "GitHubPrivateRepositoryReleaseDownloadStrategy"
},
},
"custom_require": {
prepare: func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"

ctx.Config.Brews[0].DownloadStrategy = "CustomDownloadStrategy"
ctx.Config.Brews[0].CustomRequire = "custom_download_strategy"
},
},
"custom_block": {
prepare: func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitHub
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://github.com/goreleaser"

ctx.Config.Brews[0].CustomBlock = `head "https://github.com/caarlos0/test.git"`
},
},
"default_gitlab": {
prepare: func(ctx *context.Context) {
ctx.TokenType = context.TokenTypeGitLab
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].Homepage = "https://gitlab.com/goreleaser"
},
},
"invalid_commit_template": {
prepare: func(ctx *context.Context) {
ctx.Config.Brews[0].Tap.Owner = "test"
ctx.Config.Brews[0].Tap.Name = "test"
ctx.Config.Brews[0].CommitMessageTemplate = "{{ .Asdsa }"
},
expectedError: `template: tmpl:1: unexpected "}" in operand`,
},
} {
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -197,7 +219,7 @@ func TestRunPipe(t *testing.T) {
},
},
}
fn(ctx)
tt.prepare(ctx)
ctx.Artifacts.Add(&artifact.Artifact{
Name: "bar_bin.tar.gz",
Path: "doesnt matter",
Expand Down Expand Up @@ -228,6 +250,11 @@ func TestRunPipe(t *testing.T) {
client := &DummyClient{}
distFile := filepath.Join(folder, name+".rb")

if tt.expectedError != "" {
require.EqualError(t, doRun(ctx, ctx.Config.Brews[0], client), tt.expectedError)
return
}

require.NoError(t, doRun(ctx, ctx.Config.Brews[0], client))
require.True(t, client.CreatedFile)
golden.RequireEqualRb(t, []byte(client.Content))
Expand Down Expand Up @@ -855,6 +882,7 @@ func TestDefault(t *testing.T) {
require.Equal(t, ctx.Config.ProjectName, ctx.Config.Brews[0].Name)
require.NotEmpty(t, ctx.Config.Brews[0].CommitAuthor.Name)
require.NotEmpty(t, ctx.Config.Brews[0].CommitAuthor.Email)
require.NotEmpty(t, ctx.Config.Brews[0].CommitMessageTemplate)
require.Equal(t, `bin.install "myproject"`, ctx.Config.Brews[0].Install)
}

Expand Down
5 changes: 1 addition & 4 deletions internal/pipe/scoop/scoop.go
Expand Up @@ -52,11 +52,9 @@ func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Scoop.CommitAuthor.Email == "" {
ctx.Config.Scoop.CommitAuthor.Email = "goreleaser@carlosbecker.com"
}

if ctx.Config.Scoop.CommitMessageTemplate == "" {
ctx.Config.Scoop.CommitMessageTemplate = "Scoop update for {{ .ProjectName }} version {{ .Tag }}"
}

return nil
}

Expand Down Expand Up @@ -127,8 +125,7 @@ func doRun(ctx *context.Context, cl client.Client) error {
return pipe.Skip("release is disabled")
}

commitMessage, err := tmpl.New(ctx).
Apply(scoop.CommitMessageTemplate)
commitMessage, err := tmpl.New(ctx).Apply(scoop.CommitMessageTemplate)
if err != nil {
return err
}
Expand Down
43 changes: 22 additions & 21 deletions pkg/config/config.go
Expand Up @@ -90,27 +90,28 @@ func (r Repo) String() string {

// Homebrew contains the brew section.
type Homebrew struct {
Name string `yaml:",omitempty"`
Tap RepoRef `yaml:",omitempty"`
CommitAuthor CommitAuthor `yaml:"commit_author,omitempty"`
Folder string `yaml:",omitempty"`
Caveats string `yaml:",omitempty"`
Plist string `yaml:",omitempty"`
Install string `yaml:",omitempty"`
PostInstall string `yaml:"post_install,omitempty"`
Dependencies []HomebrewDependency `yaml:",omitempty"`
Test string `yaml:",omitempty"`
Conflicts []string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Homepage string `yaml:",omitempty"`
License string `yaml:",omitempty"`
SkipUpload string `yaml:"skip_upload,omitempty"`
DownloadStrategy string `yaml:"download_strategy,omitempty"`
URLTemplate string `yaml:"url_template,omitempty"`
CustomRequire string `yaml:"custom_require,omitempty"`
CustomBlock string `yaml:"custom_block,omitempty"`
IDs []string `yaml:"ids,omitempty"`
Goarm string `yaml:"goarm,omitempty"`
Name string `yaml:",omitempty"`
Tap RepoRef `yaml:",omitempty"`
CommitAuthor CommitAuthor `yaml:"commit_author,omitempty"`
CommitMessageTemplate string `yaml:"commit_msg_template,omitempty"`
Folder string `yaml:",omitempty"`
Caveats string `yaml:",omitempty"`
Plist string `yaml:",omitempty"`
Install string `yaml:",omitempty"`
PostInstall string `yaml:"post_install,omitempty"`
Dependencies []HomebrewDependency `yaml:",omitempty"`
Test string `yaml:",omitempty"`
Conflicts []string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Homepage string `yaml:",omitempty"`
License string `yaml:",omitempty"`
SkipUpload string `yaml:"skip_upload,omitempty"`
DownloadStrategy string `yaml:"download_strategy,omitempty"`
URLTemplate string `yaml:"url_template,omitempty"`
CustomRequire string `yaml:"custom_require,omitempty"`
CustomBlock string `yaml:"custom_block,omitempty"`
IDs []string `yaml:"ids,omitempty"`
Goarm string `yaml:"goarm,omitempty"`
}

// Scoop contains the scoop.sh section.
Expand Down
3 changes: 3 additions & 0 deletions www/docs/customization/homebrew.md
Expand Up @@ -69,6 +69,9 @@ brews:
name: goreleaserbot
email: goreleaser@carlosbecker.com

# The project name and current git tag are used in the format string.
commit_msg_template: "Brew formula update for {{ .ProjectName }} version {{ .Tag }}"

# Folder inside the repository to put the formula.
# Default is the root folder.
folder: Formula
Expand Down