1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/internal/pipe/bluesky/bluesky.go
Carlos Alexandro Becker ec2db4a727
feat!: rename module to /v2 (#4894)
<!--

Hi, thanks for contributing!

Please make sure you read our CONTRIBUTING guide.

Also, add tests and the respective documentation changes as well.

-->


<!-- If applied, this commit will... -->

...

<!-- Why is this change being made? -->

...

<!-- # Provide links to any relevant tickets, URLs or other resources
-->

...

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2024-05-26 15:02:57 -03:00

112 lines
2.7 KiB
Go

package bluesky
import (
"fmt"
"strings"
"time"
"github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/api/bsky"
"github.com/bluesky-social/indigo/lex/util"
butil "github.com/bluesky-social/indigo/util"
"github.com/bluesky-social/indigo/xrpc"
"github.com/caarlos0/env/v11"
"github.com/goreleaser/goreleaser/v2/internal/tmpl"
"github.com/goreleaser/goreleaser/v2/pkg/context"
)
const (
defaultPDSURL = "https://bsky.social"
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
)
type Pipe struct{}
func (Pipe) String() string { return "bluesky" }
func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Bluesky.Enabled }
type Config struct {
Password string `env:"BLUESKY_APP_PASSWORD,notEmpty"`
}
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.Announce.Bluesky.MessageTemplate == "" {
ctx.Config.Announce.Bluesky.MessageTemplate = defaultMessageTemplate
}
return nil
}
func (p Pipe) Announce(ctx *context.Context) error {
msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Bluesky.MessageTemplate)
if err != nil {
return fmt.Errorf("bluesky: %w", err)
}
var cfg Config
if err = env.Parse(&cfg); err != nil {
return fmt.Errorf("bluesky: %w", err)
}
post := bsky.FeedPost{
CreatedAt: time.Now().Format(time.RFC3339),
Text: msg,
}
// if there is a reference to the release URL, create a link to it
startIdx := strings.Index(msg, ctx.ReleaseURL)
if startIdx >= 0 {
post.Facets = []*bsky.RichtextFacet{
{
Index: &bsky.RichtextFacet_ByteSlice{
ByteStart: int64(startIdx),
ByteEnd: int64(startIdx + len(ctx.ReleaseURL)),
},
Features: []*bsky.RichtextFacet_Features_Elem{
{
RichtextFacet_Link: &bsky.RichtextFacet_Link{
Uri: ctx.ReleaseURL,
},
},
},
},
}
}
httpClient := butil.RobustHTTPClient()
userAgent := "goreleaser/v1"
xrpcClient := &xrpc.Client{
Client: httpClient,
Host: defaultPDSURL,
UserAgent: &userAgent,
}
loginInput := &atproto.ServerCreateSession_Input{
Identifier: ctx.Config.Announce.Bluesky.Username,
Password: cfg.Password,
}
authResult, err := atproto.ServerCreateSession(ctx, xrpcClient, loginInput)
if err != nil {
return fmt.Errorf("could not log in to Bluesky: %w", err)
}
xrpcClient.Auth = &xrpc.AuthInfo{
AccessJwt: authResult.AccessJwt,
RefreshJwt: authResult.RefreshJwt,
Handle: authResult.Handle,
Did: authResult.Did,
}
_, err = atproto.RepoCreateRecord(ctx, xrpcClient, &atproto.RepoCreateRecord_Input{
Collection: "app.bsky.feed.post",
Repo: xrpcClient.Auth.Did,
Record: &util.LexiconTypeDecoder{
Val: &post,
},
})
return err
}