Skip to content

Commit

Permalink
fix: move mastodon server to yaml (#3568)
Browse files Browse the repository at this point in the history
refs #3567

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Nov 17, 2022
1 parent fe24ee6 commit ab08d0b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
10 changes: 6 additions & 4 deletions internal/pipe/mastodon/mastodon.go
Expand Up @@ -14,11 +14,13 @@ const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it o

type Pipe struct{}

func (Pipe) String() string { return "mastodon" }
func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Mastodon.Enabled }
func (Pipe) String() string { return "mastodon" }

func (Pipe) Skip(ctx *context.Context) bool {
return !ctx.Config.Announce.Mastodon.Enabled || ctx.Config.Announce.Mastodon.Server == ""
}

type Config struct {
Server string `env:"MASTODON_SERVER,notEmpty"`
ClientID string `env:"MASTODON_CLIENT_ID,notEmpty"`
ClientSecret string `env:"MASTODON_CLIENT_SECRET,notEmpty"`
AccessToken string `env:"MASTODON_ACCESS_TOKEN,notEmpty"`
Expand All @@ -43,7 +45,7 @@ func (Pipe) Announce(ctx *context.Context) error {
}

client := mastodon.NewClient(&mastodon.Config{
Server: cfg.Server,
Server: ctx.Config.Announce.Mastodon.Server,
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
AccessToken: cfg.AccessToken,
Expand Down
19 changes: 15 additions & 4 deletions internal/pipe/mastodon/mastodon_test.go
Expand Up @@ -36,22 +36,33 @@ func TestAnnounceMissingEnv(t *testing.T) {
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to mastodon: env: environment variable "MASTODON_SERVER" should not be empty; environment variable "MASTODON_CLIENT_ID" should not be empty; environment variable "MASTODON_CLIENT_SECRET" should not be empty; environment variable "MASTODON_ACCESS_TOKEN" should not be empty`)
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to mastodon: env: environment variable "MASTODON_CLIENT_ID" should not be empty; environment variable "MASTODON_CLIENT_SECRET" should not be empty; environment variable "MASTODON_ACCESS_TOKEN" should not be empty`)
}

func TestSkip(t *testing.T) {
t.Run("skip", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{})))
})

t.Run("skip empty server", func(t *testing.T) {
require.True(t, Pipe{}.Skip(context.New(config.Project{
Announce: config.Announce{
Mastodon: config.Mastodon{
Enabled: true,
Server: "", // empty
},
},
})))
})

t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
require.False(t, Pipe{}.Skip(context.New(config.Project{
Announce: config.Announce{
Mastodon: config.Mastodon{
Enabled: true,
Server: "https://mastodon.social",
},
},
})
require.False(t, Pipe{}.Skip(ctx))
})))
})
}
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -986,6 +986,7 @@ type Twitter struct {
type Mastodon struct {
Enabled bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
MessageTemplate string `yaml:"message_template,omitempty" json:"message_template,omitempty"`
Server string `yaml:"server" json:"server"`
}

type Reddit struct {
Expand Down
7 changes: 6 additions & 1 deletion www/docs/customization/announce/mastodon.md
@@ -1,9 +1,10 @@
# Mastodon

> Since: v1.13.0
For it to work, you'll need to create a new Mastodon app `https://social.yourdomain.tld/settings/applications/new`, and set
some environment variables on your pipeline:

- `MASTODON_SERVER`
- `MASTODON_CLIENT_ID`
- `MASTODON_CLIENT_SECRET`
- `MASTODON_ACCESS_TOKEN`
Expand All @@ -21,6 +22,10 @@ announce:
# Message template to use while publishing.
# Defaults to `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
message_template: 'Awesome project {{.Tag}} is out!'

# Mastodon server URL.
# Defaults to empty.
server: https://mastodon.social
```

!!! tip
Expand Down

0 comments on commit ab08d0b

Please sign in to comment.