Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: announce: reddit Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> * Update .goreleaser.yml * Update internal/pipe/announce/announce.go * Update pkg/defaults/defaults.go Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
- Loading branch information
1 parent
5e906b2
commit d3db692
Showing
8 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d3db692
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: