Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds [mastodon](https://joinmastodon.org/) as an announcement pipeline. 馃コ ![mastodon-announce](https://user-images.githubusercontent.com/42128690/202544345-d90d8f10-0818-4bc2-bc35-3dbcba4254b0.png) Resolves #3566 Signed-off-by: jolheiser <john.olheiser@gmail.com>
- Loading branch information
Showing
9 changed files
with
160 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,59 @@ | ||
package mastodon | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/caarlos0/env/v6" | ||
"github.com/caarlos0/log" | ||
"github.com/goreleaser/goreleaser/internal/tmpl" | ||
"github.com/goreleaser/goreleaser/pkg/context" | ||
"github.com/mattn/go-mastodon" | ||
) | ||
|
||
const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}` | ||
|
||
type Pipe struct{} | ||
|
||
func (Pipe) String() string { return "mastodon" } | ||
func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Mastodon.Enabled } | ||
|
||
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"` | ||
} | ||
|
||
func (Pipe) Default(ctx *context.Context) error { | ||
if ctx.Config.Announce.Mastodon.MessageTemplate == "" { | ||
ctx.Config.Announce.Mastodon.MessageTemplate = defaultMessageTemplate | ||
} | ||
return nil | ||
} | ||
|
||
func (Pipe) Announce(ctx *context.Context) error { | ||
msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Mastodon.MessageTemplate) | ||
if err != nil { | ||
return fmt.Errorf("announce: failed to announce to mastodon: %w", err) | ||
} | ||
|
||
var cfg Config | ||
if err := env.Parse(&cfg); err != nil { | ||
return fmt.Errorf("announce: failed to announce to mastodon: %w", err) | ||
} | ||
|
||
client := mastodon.NewClient(&mastodon.Config{ | ||
Server: cfg.Server, | ||
ClientID: cfg.ClientID, | ||
ClientSecret: cfg.ClientSecret, | ||
AccessToken: cfg.AccessToken, | ||
}) | ||
|
||
log.Infof("posting: '%s'", msg) | ||
if _, err := client.PostStatus(ctx, &mastodon.Toot{ | ||
Status: msg, | ||
}); err != nil { | ||
return fmt.Errorf("announce: failed to announce to mastodon: %w", err) | ||
} | ||
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,57 @@ | ||
package mastodon | ||
|
||
import ( | ||
"testing" | ||
|
||
"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(), "mastodon") | ||
} | ||
|
||
func TestDefault(t *testing.T) { | ||
ctx := context.New(config.Project{}) | ||
require.NoError(t, Pipe{}.Default(ctx)) | ||
require.Equal(t, ctx.Config.Announce.Mastodon.MessageTemplate, defaultMessageTemplate) | ||
} | ||
|
||
func TestAnnounceInvalidTemplate(t *testing.T) { | ||
ctx := context.New(config.Project{ | ||
Announce: config.Announce{ | ||
Mastodon: config.Mastodon{ | ||
MessageTemplate: "{{ .Foo }", | ||
}, | ||
}, | ||
}) | ||
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to mastodon: template: tmpl:1: unexpected "}" in operand`) | ||
} | ||
|
||
func TestAnnounceMissingEnv(t *testing.T) { | ||
ctx := context.New(config.Project{ | ||
Announce: config.Announce{ | ||
Mastodon: config.Mastodon{}, | ||
}, | ||
}) | ||
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`) | ||
} | ||
|
||
func TestSkip(t *testing.T) { | ||
t.Run("skip", func(t *testing.T) { | ||
require.True(t, Pipe{}.Skip(context.New(config.Project{}))) | ||
}) | ||
|
||
t.Run("dont skip", func(t *testing.T) { | ||
ctx := context.New(config.Project{ | ||
Announce: config.Announce{ | ||
Mastodon: config.Mastodon{ | ||
Enabled: true, | ||
}, | ||
}, | ||
}) | ||
require.False(t, Pipe{}.Skip(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Mastodon | ||
|
||
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` | ||
|
||
Then, you can add something like the following to your `.goreleaser.yaml` config: | ||
|
||
```yaml | ||
# .goreleaser.yaml | ||
announce: | ||
mastodon: | ||
# Whether its enabled or not. | ||
# Defaults to false. | ||
enabled: true | ||
|
||
# Message template to use while publishing. | ||
# Defaults to `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}` | ||
message_template: 'Awesome project {{.Tag}} is out!' | ||
``` | ||
|
||
!!! tip | ||
Learn more about the [name template engine](/customization/templates/). |
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