2021-05-25 00:19:06 -03:00
|
|
|
package twitter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/caarlos0/env/v6"
|
2022-06-21 21:11:15 -03:00
|
|
|
"github.com/caarlos0/log"
|
2021-05-25 00:19:06 -03:00
|
|
|
"github.com/dghubble/go-twitter/twitter"
|
|
|
|
"github.com/dghubble/oauth1"
|
|
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
|
|
|
|
2021-11-06 21:39:43 +03:00
|
|
|
const defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
|
2021-05-25 00:19:06 -03:00
|
|
|
|
|
|
|
type Pipe struct{}
|
|
|
|
|
2021-09-18 10:21:29 -03:00
|
|
|
func (Pipe) String() string { return "twitter" }
|
|
|
|
func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Twitter.Enabled }
|
2021-05-25 00:19:06 -03:00
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
ConsumerKey string `env:"TWITTER_CONSUMER_KEY,notEmpty"`
|
|
|
|
ConsumerSecret string `env:"TWITTER_CONSUMER_SECRET,notEmpty"`
|
|
|
|
AccessToken string `env:"TWITTER_ACCESS_TOKEN,notEmpty"`
|
|
|
|
AccessSecret string `env:"TWITTER_ACCESS_TOKEN_SECRET,notEmpty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
|
|
|
if ctx.Config.Announce.Twitter.MessageTemplate == "" {
|
|
|
|
ctx.Config.Announce.Twitter.MessageTemplate = defaultMessageTemplate
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Pipe) Announce(ctx *context.Context) error {
|
|
|
|
msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Twitter.MessageTemplate)
|
|
|
|
if err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("twitter: %w", err)
|
2021-05-25 00:19:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
if err := env.Parse(&cfg); err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("twitter: %w", err)
|
2021-05-25 00:19:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("posting: '%s'", msg)
|
|
|
|
config := oauth1.NewConfig(cfg.ConsumerKey, cfg.ConsumerSecret)
|
|
|
|
token := oauth1.NewToken(cfg.AccessToken, cfg.AccessSecret)
|
|
|
|
client := twitter.NewClient(config.Client(oauth1.NoContext, token))
|
|
|
|
if _, _, err := client.Statuses.Update(msg, nil); err != nil {
|
2022-12-28 12:24:21 -03:00
|
|
|
return fmt.Errorf("twitter: %w", err)
|
2021-05-25 00:19:06 -03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|