github.com/triarius/goreleaser@v1.12.5/internal/pipe/teams/teams.go (about)

     1  package teams
     2  
     3  import (
     4  	"fmt"
     5  
     6  	goteamsnotify "github.com/atc0005/go-teams-notify/v2"
     7  	"github.com/caarlos0/env/v6"
     8  	"github.com/caarlos0/log"
     9  	"github.com/triarius/goreleaser/internal/tmpl"
    10  	"github.com/triarius/goreleaser/pkg/context"
    11  )
    12  
    13  const (
    14  	defaultColor           = "#2D313E"
    15  	defaultIcon            = "https://goreleaser.com/static/avatar.png"
    16  	defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
    17  	defaultMessageTitle    = `{{ .ProjectName }} {{ .Tag }} is out!`
    18  )
    19  
    20  type Pipe struct{}
    21  
    22  func (Pipe) String() string                 { return "teams" }
    23  func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Teams.Enabled }
    24  
    25  type Config struct {
    26  	Webhook string `env:"TEAMS_WEBHOOK,notEmpty"`
    27  }
    28  
    29  func (p Pipe) Default(ctx *context.Context) error {
    30  	if ctx.Config.Announce.Teams.MessageTemplate == "" {
    31  		ctx.Config.Announce.Teams.MessageTemplate = defaultMessageTemplate
    32  	}
    33  	if ctx.Config.Announce.Teams.TitleTemplate == "" {
    34  		ctx.Config.Announce.Teams.TitleTemplate = defaultMessageTitle
    35  	}
    36  	if ctx.Config.Announce.Teams.IconURL == "" {
    37  		ctx.Config.Announce.Teams.IconURL = defaultIcon
    38  	}
    39  	if ctx.Config.Announce.Teams.Color == "" {
    40  		ctx.Config.Announce.Teams.Color = defaultColor
    41  	}
    42  	return nil
    43  }
    44  
    45  func (p Pipe) Announce(ctx *context.Context) error {
    46  	title, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Teams.TitleTemplate)
    47  	if err != nil {
    48  		return fmt.Errorf("announce: failed to announce to teams: %w", err)
    49  	}
    50  
    51  	msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Teams.MessageTemplate)
    52  	if err != nil {
    53  		return fmt.Errorf("announce: failed to announce to teams: %w", err)
    54  	}
    55  
    56  	var cfg Config
    57  	if err := env.Parse(&cfg); err != nil {
    58  		return fmt.Errorf("announce: failed to announce to teams: %w", err)
    59  	}
    60  
    61  	log.Infof("posting: '%s'", msg)
    62  
    63  	client := goteamsnotify.NewClient()
    64  	msgCard := goteamsnotify.NewMessageCard()
    65  	msgCard.Summary = title
    66  	msgCard.ThemeColor = ctx.Config.Announce.Teams.Color
    67  
    68  	messageCardSection := goteamsnotify.NewMessageCardSection()
    69  	messageCardSection.ActivityTitle = title
    70  	messageCardSection.ActivityText = msg
    71  	messageCardSection.Markdown = true
    72  	messageCardSection.ActivityImage = ctx.Config.Announce.Teams.IconURL
    73  	err = msgCard.AddSection(messageCardSection)
    74  	if err != nil {
    75  		return fmt.Errorf("announce: failed to announce to teams: %w", err)
    76  	}
    77  	err = client.Send(cfg.Webhook, msgCard)
    78  	if err != nil {
    79  		return fmt.Errorf("announce: failed to announce to teams: %w", err)
    80  	}
    81  	return nil
    82  }