From 27bc41956a3d9da84e3466fe7c4b3c0fc0235eb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Apayd=C4=B1n?= Date: Wed, 25 Aug 2021 23:50:23 +0300 Subject: [PATCH 1/4] feat: announce: reddit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Batuhan Apaydın --- .goreleaser.yml | 5 ++ go.mod | 1 + go.sum | 2 + internal/pipe/announce/announce.go | 3 ++ internal/pipe/reddit/reddit.go | 83 +++++++++++++++++++++++++++++ internal/pipe/reddit/reddit_test.go | 74 +++++++++++++++++++++++++ pkg/config/config.go | 10 ++++ pkg/defaults/defaults.go | 3 ++ www/docs/customization/announce.md | 32 +++++++++++ 9 files changed, 213 insertions(+) create mode 100644 internal/pipe/reddit/reddit.go create mode 100644 internal/pipe/reddit/reddit_test.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 1944a23255a..aa417e7cb41 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -202,3 +202,8 @@ announce: twitter: enabled: true message_template: 'GoReleaser {{ .Tag }} was just released! Get it while its fresh: https://github.com/goreleaser/goreleaser/releases/tag/{{ .Tag }}' + reddit: + enabled: true + title_template: 'GoReleaser {{ .Tag }} was just released!' + url_template: 'https://github.com/goreleaser/goreleaser/releases/tag/{{ .Tag }}' + sub: test diff --git a/go.mod b/go.mod index 15e8ef823f4..2da00b7f560 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/spf13/cobra v1.2.1 github.com/stretchr/testify v1.7.0 github.com/ulikunitz/xz v0.5.10 + github.com/vartanbeno/go-reddit/v2 v2.0.1 github.com/xanzy/go-gitlab v0.50.3 gocloud.dev v0.23.0 golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 diff --git a/go.sum b/go.sum index 1033f62d78b..29a8f0652f6 100644 --- a/go.sum +++ b/go.sum @@ -581,6 +581,8 @@ github.com/ulikunitz/xz v0.5.7/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oW github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8= github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= +github.com/vartanbeno/go-reddit/v2 v2.0.1 h1:P6ITpf5YHjdy7DHZIbUIDn/iNAoGcEoDQnMa+L4vutw= +github.com/vartanbeno/go-reddit/v2 v2.0.1/go.mod h1:758/S10hwZSLm43NPtwoNQdZFSg3sjB5745Mwjb0ANI= github.com/xanzy/go-gitlab v0.50.3 h1:M7ncgNhCN4jaFNyXxarJhCLa9Qi6fdmCxFFhMTQPZiY= github.com/xanzy/go-gitlab v0.50.3/go.mod h1:Q+hQhV508bDPoBijv7YjK/Lvlb4PhVhJdKqXVQrUoAE= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= diff --git a/internal/pipe/announce/announce.go b/internal/pipe/announce/announce.go index a12b8eafbb1..6724b77fb4e 100644 --- a/internal/pipe/announce/announce.go +++ b/internal/pipe/announce/announce.go @@ -4,6 +4,8 @@ package announce import ( "fmt" + "github.com/goreleaser/goreleaser/internal/pipe/reddit" + "github.com/goreleaser/goreleaser/internal/middleware" "github.com/goreleaser/goreleaser/internal/pipe/twitter" "github.com/goreleaser/goreleaser/pkg/context" @@ -26,6 +28,7 @@ type Announcer interface { // nolint: gochecknoglobals var announcers = []Announcer{ twitter.Pipe{}, // announce to twitter + reddit.Pipe{}, // announce to twitter } // Run the pipe. diff --git a/internal/pipe/reddit/reddit.go b/internal/pipe/reddit/reddit.go new file mode 100644 index 00000000000..96c996df3e8 --- /dev/null +++ b/internal/pipe/reddit/reddit.go @@ -0,0 +1,83 @@ +package reddit + +import ( + "fmt" + + "github.com/apex/log" + "github.com/caarlos0/env/v6" + "github.com/goreleaser/goreleaser/internal/pipe" + "github.com/goreleaser/goreleaser/internal/tmpl" + "github.com/goreleaser/goreleaser/pkg/context" + "github.com/vartanbeno/go-reddit/v2/reddit" +) + +const ( + defaultTitleTemplate = `{{ .ProjectName }} {{ .Tag }} is out!` + defaultURLTemplate = `{{ .GitURL }}/releases/tag/{{ .Tag }}` +) + +type Pipe struct{} + +func (Pipe) String() string { return "reddit" } + +type Config struct { + Secret string `env:"REDDIT_SECRET,notEmpty"` + Password string `env:"REDDIT_PASSWORD,notEmpty"` +} + +func (Pipe) Default(ctx *context.Context) error { + if ctx.Config.Announce.Reddit.TitleTemplate == "" { + ctx.Config.Announce.Reddit.TitleTemplate = defaultTitleTemplate + } + + if ctx.Config.Announce.Reddit.URLTemplate == "" { + ctx.Config.Announce.Reddit.URLTemplate = defaultURLTemplate + } + + return nil +} + +func (Pipe) Announce(ctx *context.Context) error { + if ctx.SkipAnnounce { + return pipe.ErrSkipAnnounceEnabled + } + if !ctx.Config.Announce.Reddit.Enabled { + return pipe.ErrSkipDisabledPipe + } + + title, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Reddit.TitleTemplate) + if err != nil { + return fmt.Errorf("announce: failed to announce to reddit: %w", err) + } + + url, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Reddit.URLTemplate) + if err != nil { + return fmt.Errorf("announce: failed to announce to reddit: %w", err) + } + + linkRequest := reddit.SubmitLinkRequest{ + Subreddit: ctx.Config.Announce.Reddit.Sub, + Title: title, + URL: url, + } + + var cfg Config + if err := env.Parse(&cfg); err != nil { + return fmt.Errorf("announce: failed to announce to reddit: %w", err) + } + + credentials := reddit.Credentials{ID: ctx.Config.Announce.Reddit.ApplicationID, Secret: cfg.Secret, Username: ctx.Config.Announce.Reddit.Username, Password: cfg.Password} + client, err := reddit.NewClient(credentials) + if err != nil { + return fmt.Errorf("announce: failed to announce to reddit: %w", err) + } + + post, _, err := client.Post.SubmitLink(ctx, linkRequest) + if err != nil { + return fmt.Errorf("announce: failed to announce to reddit: %w", err) + } + + log.Infof("announce: The text post is available at: %s\n", post.URL) + + return nil +} diff --git a/internal/pipe/reddit/reddit_test.go b/internal/pipe/reddit/reddit_test.go new file mode 100644 index 00000000000..508fcab090c --- /dev/null +++ b/internal/pipe/reddit/reddit_test.go @@ -0,0 +1,74 @@ +package reddit + +import ( + "testing" + + "github.com/goreleaser/goreleaser/internal/testlib" + "github.com/goreleaser/goreleaser/pkg/config" + "github.com/goreleaser/goreleaser/pkg/context" + "github.com/stretchr/testify/require" +) + +func TestStringer(t *testing.T) { + require.Equal(t, Pipe{}.String(), "reddit") +} + +func TestDefault(t *testing.T) { + ctx := context.New(config.Project{}) + require.NoError(t, Pipe{}.Default(ctx)) + require.Equal(t, ctx.Config.Announce.Reddit.TitleTemplate, defaultTitleTemplate) +} + +func TestAnnounceDisabled(t *testing.T) { + ctx := context.New(config.Project{}) + require.NoError(t, Pipe{}.Default(ctx)) + testlib.AssertSkipped(t, Pipe{}.Announce(ctx)) +} + +func TestAnnounceInvalidURLTemplate(t *testing.T) { + ctx := context.New(config.Project{ + Announce: config.Announce{ + Reddit: config.Reddit{ + Enabled: true, + URLTemplate: "{{ .Foo }", + }, + }, + }) + require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to reddit: template: tmpl:1: unexpected "}" in operand`) +} + +func TestAnnounceInvalidTitleTemplate(t *testing.T) { + ctx := context.New(config.Project{ + Announce: config.Announce{ + Reddit: config.Reddit{ + Enabled: true, + TitleTemplate: "{{ .Foo }", + }, + }, + }) + require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to reddit: template: tmpl:1: unexpected "}" in operand`) +} + +func TestAnnounceMissingEnv(t *testing.T) { + ctx := context.New(config.Project{ + Announce: config.Announce{ + Reddit: config.Reddit{ + Enabled: true, + }, + }, + }) + require.NoError(t, Pipe{}.Default(ctx)) + require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to reddit: env: environment variable "REDDIT_SECRET" should not be empty`) +} + +func TestAnnounceSkipAnnounce(t *testing.T) { + ctx := context.New(config.Project{ + Announce: config.Announce{ + Reddit: config.Reddit{ + Enabled: true, + }, + }, + }) + ctx.SkipAnnounce = true + testlib.AssertSkipped(t, Pipe{}.Announce(ctx)) +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 3b289383a96..b8fd0238a0f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -690,6 +690,7 @@ type GoMod struct { type Announce struct { Twitter Twitter `yaml:"twitter,omitempty"` + Reddit Reddit `yaml:"reddit,omitempty"` } type Twitter struct { @@ -697,6 +698,15 @@ type Twitter struct { MessageTemplate string `yaml:"message_template,omitempty"` } +type Reddit struct { + Enabled bool `yaml:"enabled,omitempty"` + ApplicationID string `yaml:"application_id,omitempty"` + Username string `yaml:"username,omitempty"` + TitleTemplate string `yaml:"title_template,omitempty"` + URLTemplate string `yaml:"url_template,omitempty"` + Sub string `yaml:"sub,omitempty"` +} + // Load config file. func Load(file string) (config Project, err error) { f, err := os.Open(file) // #nosec diff --git a/pkg/defaults/defaults.go b/pkg/defaults/defaults.go index 24da35b4675..1c7226fa13a 100644 --- a/pkg/defaults/defaults.go +++ b/pkg/defaults/defaults.go @@ -5,6 +5,8 @@ package defaults import ( "fmt" + "github.com/goreleaser/goreleaser/internal/pipe/reddit" + "github.com/goreleaser/goreleaser/internal/pipe/archive" "github.com/goreleaser/goreleaser/internal/pipe/artifactory" "github.com/goreleaser/goreleaser/internal/pipe/blob" @@ -57,5 +59,6 @@ var Defaulters = []Defaulter{ brew.Pipe{}, scoop.Pipe{}, twitter.Pipe{}, + reddit.Pipe{}, milestone.Pipe{}, } diff --git a/www/docs/customization/announce.md b/www/docs/customization/announce.md index cd3b3681e09..f2281830a1a 100644 --- a/www/docs/customization/announce.md +++ b/www/docs/customization/announce.md @@ -32,3 +32,35 @@ announce: !!! tip Learn more about the [name template engine](/customization/templates/). + +## Reddit + +For it to work, you'll need to [create a new Reddit app](https://www.reddit.com/prefs/apps), and set some environment variables on your pipeline: + +- `REDDIT_SECRET` +- `REDDIT_PASSWORD` + +Then, you can add something like the following to your `.goreleaser.yml` config: + +```yaml +# .goreleaser.yml +announce: + reddit: + # Wether its enabled or not. + # Defaults to false. + enabled: true + + # Application ID for Reddit Application + application_id: "" + + # Username for your Reddit account + username: "" + + # URL template to use while publishing. + # Defaults to `{{ .GitURL }}/releases/tag/{{ .Tag }}` + url_template: 'https://github.com/goreleaser/goreleaser/releases/tag/{{ .Tag }}' + + # Title template to use while publishing. + # Defaults to `{{ .ProjectName }} {{ .Tag }} is out!` + title_template: ''GoReleaser {{ .Tag }} was just released!'' +``` From f61c767cdedddb13f4bb5548aaf561370a5e552a Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 31 Aug 2021 10:47:43 -0300 Subject: [PATCH 2/4] Update .goreleaser.yml --- .goreleaser.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index aa417e7cb41..1944a23255a 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -202,8 +202,3 @@ announce: twitter: enabled: true message_template: 'GoReleaser {{ .Tag }} was just released! Get it while its fresh: https://github.com/goreleaser/goreleaser/releases/tag/{{ .Tag }}' - reddit: - enabled: true - title_template: 'GoReleaser {{ .Tag }} was just released!' - url_template: 'https://github.com/goreleaser/goreleaser/releases/tag/{{ .Tag }}' - sub: test From d5098aa932a827c967c7757aace220cc843cd9f4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 31 Aug 2021 10:48:07 -0300 Subject: [PATCH 3/4] Update internal/pipe/announce/announce.go --- internal/pipe/announce/announce.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/pipe/announce/announce.go b/internal/pipe/announce/announce.go index 6724b77fb4e..ce129d6f495 100644 --- a/internal/pipe/announce/announce.go +++ b/internal/pipe/announce/announce.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/goreleaser/goreleaser/internal/pipe/reddit" - "github.com/goreleaser/goreleaser/internal/middleware" "github.com/goreleaser/goreleaser/internal/pipe/twitter" "github.com/goreleaser/goreleaser/pkg/context" From fa303f1c4dcc784d90f1f43ee50a0071a54e03ea Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 31 Aug 2021 10:48:29 -0300 Subject: [PATCH 4/4] Update pkg/defaults/defaults.go --- pkg/defaults/defaults.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/defaults/defaults.go b/pkg/defaults/defaults.go index 1c7226fa13a..2df974f9ad0 100644 --- a/pkg/defaults/defaults.go +++ b/pkg/defaults/defaults.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/goreleaser/goreleaser/internal/pipe/reddit" - "github.com/goreleaser/goreleaser/internal/pipe/archive" "github.com/goreleaser/goreleaser/internal/pipe/artifactory" "github.com/goreleaser/goreleaser/internal/pipe/blob"