1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-09-16 09:26:52 +02:00

feat: announce: teams (#2482)

Signed-off-by: Engin Diri <engin.diri@mail.schwarz>
This commit is contained in:
Engin Diri
2021-09-13 00:57:58 +02:00
committed by GitHub
parent 35bb673049
commit 1c44c2149d
9 changed files with 204 additions and 1 deletions

1
go.mod
View File

@@ -117,6 +117,7 @@ require (
github.com/DisgoOrg/disgohook v1.4.3 // indirect
github.com/DisgoOrg/log v1.1.0 // indirect
github.com/DisgoOrg/restclient v1.2.7 // indirect
github.com/atc0005/go-teams-notify/v2 v2.6.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.9.0 // indirect
github.com/aws/aws-sdk-go-v2/config v1.7.0 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.4.0 // indirect

2
go.sum
View File

@@ -162,6 +162,8 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmV
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/atc0005/go-teams-notify/v2 v2.6.0 h1:YegKDWbjlatR0fP2yHsQYXzTcUGNJXhm1/OiCgbyysc=
github.com/atc0005/go-teams-notify/v2 v2.6.0/go.mod h1:xo6GejLDHn3tWBA181F8LrllIL0xC1uRsRxq7YNXaaY=
github.com/aws/aws-sdk-go v1.15.27/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0=
github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.37.0/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=

View File

@@ -4,6 +4,8 @@ package announce
import (
"fmt"
"github.com/goreleaser/goreleaser/internal/pipe/teams"
"github.com/goreleaser/goreleaser/internal/middleware"
"github.com/goreleaser/goreleaser/internal/pipe/discord"
"github.com/goreleaser/goreleaser/internal/pipe/reddit"
@@ -33,6 +35,7 @@ var announcers = []Announcer{
reddit.Pipe{},
slack.Pipe{},
twitter.Pipe{},
teams.Pipe{},
}
// Run the pipe.

View File

@@ -0,0 +1,89 @@
package teams
import (
"fmt"
"github.com/apex/log"
goteamsnotify "github.com/atc0005/go-teams-notify/v2"
"github.com/caarlos0/env/v6"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/context"
)
const (
defaultColor = "#2D313E"
defaultIcon = "https://goreleaser.com/static/avatar.png"
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .GitURL }}/releases/tag/{{ .Tag }}`
defaultMessageTitle = `{{ .ProjectName }} {{ .Tag }} is out!`
)
type Pipe struct{}
func (Pipe) String() string { return "teams" }
type Config struct {
Webhook string `env:"TEAMS_WEBHOOK,notEmpty"`
}
func (p Pipe) Default(ctx *context.Context) error {
if ctx.Config.Announce.Teams.MessageTemplate == "" {
ctx.Config.Announce.Teams.MessageTemplate = defaultMessageTemplate
}
if ctx.Config.Announce.Teams.TitleTemplate == "" {
ctx.Config.Announce.Teams.TitleTemplate = defaultMessageTitle
}
if ctx.Config.Announce.Teams.IconURL == "" {
ctx.Config.Announce.Teams.IconURL = defaultIcon
}
if ctx.Config.Announce.Teams.Color == "" {
ctx.Config.Announce.Teams.Color = defaultColor
}
return nil
}
func (p Pipe) Announce(ctx *context.Context) error {
if ctx.SkipAnnounce {
return pipe.ErrSkipAnnounceEnabled
}
if !ctx.Config.Announce.Teams.Enabled {
return pipe.ErrSkipDisabledPipe
}
title, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Teams.TitleTemplate)
if err != nil {
return fmt.Errorf("announce: failed to announce to teams: %w", err)
}
msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Teams.MessageTemplate)
if err != nil {
return fmt.Errorf("announce: failed to announce to teams: %w", err)
}
var cfg Config
if err := env.Parse(&cfg); err != nil {
return fmt.Errorf("announce: failed to announce to teams: %w", err)
}
log.Infof("posting: '%s'", msg)
client := goteamsnotify.NewClient()
msgCard := goteamsnotify.NewMessageCard()
msgCard.Summary = title
msgCard.ThemeColor = ctx.Config.Announce.Teams.Color
messageCardSection := goteamsnotify.NewMessageCardSection()
messageCardSection.ActivityTitle = title
messageCardSection.ActivityText = msg
messageCardSection.Markdown = true
messageCardSection.ActivityImage = ctx.Config.Announce.Teams.IconURL
err = msgCard.AddSection(messageCardSection)
if err != nil {
return fmt.Errorf("announce: failed to announce to teams: %w", err)
}
err = client.Send(cfg.Webhook, msgCard)
if err != nil {
return fmt.Errorf("announce: failed to announce to teams: %w", err)
}
return nil
}

View File

@@ -0,0 +1,62 @@
package teams
import (
"testing"
"github.com/goreleaser/goreleaser/internal/testlib"
"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(), "teams")
}
func TestDefault(t *testing.T) {
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, ctx.Config.Announce.Teams.MessageTemplate, defaultMessageTemplate)
}
func TestAnnounceDisabled(t *testing.T) {
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Default(ctx))
testlib.AssertSkipped(t, Pipe{}.Announce(ctx))
}
func TestAnnounceInvalidTemplate(t *testing.T) {
ctx := context.New(config.Project{
Announce: config.Announce{
Teams: config.Teams{
Enabled: true,
MessageTemplate: "{{ .Foo }",
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to teams: template: tmpl:1: unexpected "}" in operand`)
}
func TestAnnounceMissingEnv(t *testing.T) {
ctx := context.New(config.Project{
Announce: config.Announce{
Teams: config.Teams{
Enabled: true,
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to teams: env: environment variable "TEAMS_WEBHOOK" should not be empty`)
}
func TestAnnounceSkipAnnounce(t *testing.T) {
ctx := context.New(config.Project{
Announce: config.Announce{
Teams: config.Teams{
Enabled: true,
},
},
})
ctx.SkipAnnounce = true
testlib.AssertSkipped(t, Pipe{}.Announce(ctx))
}

View File

@@ -695,6 +695,7 @@ type Announce struct {
Reddit Reddit `yaml:"reddit,omitempty"`
Slack Slack `yaml:"slack,omitempty"`
Discord Discord `yaml:"discord,omitempty"`
Teams Teams `yaml:"teams,omitempty"`
}
type Twitter struct {
@@ -728,6 +729,14 @@ type Discord struct {
IconURL string `yaml:"icon_url,omitempty"`
}
type Teams struct {
Enabled bool `yaml:"enabled,omitempty"`
TitleTemplate string `yaml:"title_template,omitempty"`
MessageTemplate string `yaml:"message_template,omitempty"`
Color string `yaml:"color,omitempty"`
IconURL string `yaml:"icon_url,omitempty"`
}
// Load config file.
func Load(file string) (config Project, err error) {
f, err := os.Open(file) // #nosec

View File

@@ -5,6 +5,8 @@ package defaults
import (
"fmt"
"github.com/goreleaser/goreleaser/internal/pipe/teams"
"github.com/goreleaser/goreleaser/internal/pipe/archive"
"github.com/goreleaser/goreleaser/internal/pipe/artifactory"
"github.com/goreleaser/goreleaser/internal/pipe/blob"
@@ -60,6 +62,7 @@ var Defaulters = []Defaulter{
brew.Pipe{},
scoop.Pipe{},
discord.Pipe{},
teams.Pipe{},
reddit.Pipe{},
slack.Pipe{},
twitter.Pipe{},

View File

@@ -2,7 +2,7 @@
title: Announce
---
GoReleaser can also announce new releases, currently, to Twitter, Reddit, Slack and Discourse only.
GoReleaser can also announce new releases, currently, to Twitter, Reddit, Slack, Discourse and Teams only.
It runs at the very end of the pipeline.
@@ -30,6 +30,39 @@ announce:
message_template: 'Awesome project {{.Tag}} is out!'
```
## Teams
To use [Teams](https://www.microsoft.com/de-de/microsoft-teams/group-chat-software), you need to [create a Webhook](https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook), and set following environment variable on your pipeline:
- `TEAMS_WEBHOOK`
After this, you can add following section to your `.goreleaser.yml` config:
```yaml
# .goreleaser.yml
announce:
teams:
# Wether its enabled or not.
# Defaults to false.
enabled: true
# Title template to use while publishing.
# Defaults to `{{ .ProjectName }} {{ .Tag }} is out!`
title_template: 'GoReleaser {{ .Tag }} was just released!'
# Message template to use while publishing.
# Defaults to `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .GitURL }}/releases/tag/{{ .Tag }}`
message_template: 'Awesome project {{.Tag}} is out!'
# Color code of the message. You have to use hexadecimal.
# Defaults to `#2D313E` - the grey-ish from goreleaser
color: ''
# URL to an image to use as the icon for the message.
# Defaults to `https://goreleaser.com/static/avatar.png`
icon_url: ''
```
## Discord
To use [Discord](https://discord.com/), you need to [create a Webhook](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks), and set following environment variables on your pipeline:

View File

@@ -5,6 +5,7 @@ IgnoreURLs:
- https://gitlab.com/profile/personal_access_tokens
- https://twitter.com/goreleaser
- support.discord.com
- microsoft.com
IgnoreDirs:
- overrides
IgnoreDirectoryMissingTrailingSlash: true