1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-11-06 09:09:29 +02:00

fix: linkedin announcer

This commit is contained in:
Carlos A Becker
2021-11-06 22:24:15 -03:00
parent f0928105d6
commit 9ee5ac1669
4 changed files with 24 additions and 40 deletions

View File

@@ -11,7 +11,7 @@ import (
"golang.org/x/oauth2"
)
type oAuthClientConfig struct {
type oauthClientConfig struct {
Context *context.Context
AccessToken string
}
@@ -30,7 +30,7 @@ type postShareRequest struct {
Owner string `json:"owner"`
}
func createLinkedInClient(cfg oAuthClientConfig) (client, error) {
func createLinkedInClient(cfg oauthClientConfig) (client, error) {
if cfg.Context == nil {
return client{}, fmt.Errorf("context is nil")
}

View File

@@ -4,22 +4,22 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestCreateLinkedInClient(t *testing.T) {
tests := []struct {
name string
cfg OAuthClientConfig
cfg oauthClientConfig
wantErr error
}{
{
"non-empty context and access token",
OAuthClientConfig{
oauthClientConfig{
Context: context.New(config.Project{}),
AccessToken: "foo",
},
@@ -27,7 +27,7 @@ func TestCreateLinkedInClient(t *testing.T) {
},
{
"empty context",
OAuthClientConfig{
oauthClientConfig{
Context: nil,
AccessToken: "foo",
},
@@ -35,7 +35,7 @@ func TestCreateLinkedInClient(t *testing.T) {
},
{
"empty access token",
OAuthClientConfig{
oauthClientConfig{
Context: context.New(config.Project{}),
AccessToken: "",
},
@@ -44,11 +44,11 @@ func TestCreateLinkedInClient(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := CreateLinkedInClient(tt.cfg)
if !reflect.DeepEqual(err, tt.wantErr) {
t.Errorf("CreateLinkedInClient() error = %v, wantErr %v", err, tt.wantErr)
return
_, err := createLinkedInClient(tt.cfg)
if tt.wantErr != nil {
require.EqualError(t, err, tt.wantErr.Error())
} else {
require.NoError(t, err)
}
})
}
@@ -65,7 +65,7 @@ func TestClient_Share(t *testing.T) {
}))
defer server.Close()
c, err := CreateLinkedInClient(OAuthClientConfig{
c, err := createLinkedInClient(oauthClientConfig{
Context: context.New(config.Project{}),
AccessToken: "foo",
})
@@ -81,8 +81,5 @@ func TestClient_Share(t *testing.T) {
}
wantLink := "https://www.linkedin.com/feed/update/123456789"
if link != wantLink {
t.Fatalf("link got: %s want: %s", link, wantLink)
}
require.Equal(t, wantLink, link)
}

View File

@@ -33,28 +33,28 @@ func (Pipe) Default(ctx *context.Context) error {
func (Pipe) Announce(ctx *context.Context) error {
message, err := tmpl.New(ctx).Apply(ctx.Config.Announce.LinkedIn.MessageTemplate)
if err != nil {
return fmt.Errorf("announce: failed to announce to linkedin: %w", err)
return fmt.Errorf("failed to announce to linkedin: %w", err)
}
var cfg Config
if err := env.Parse(&cfg); err != nil {
return fmt.Errorf("announce: failed to announce to linkedin: %w", err)
return fmt.Errorf("failed to announce to linkedin: %w", err)
}
c, err := createLinkedInClient(oAuthClientConfig{
c, err := createLinkedInClient(oauthClientConfig{
Context: ctx,
AccessToken: cfg.AccessToken,
})
if err != nil {
return fmt.Errorf("announce: failed to announce to linkedin: %w", err)
return fmt.Errorf("failed to announce to linkedin: %w", err)
}
url, err := c.Share(message)
if err != nil {
return fmt.Errorf("announce: failed to announce to linkedin: %w", err)
return fmt.Errorf("failed to announce to linkedin: %w", err)
}
log.Infof("announce: The text post is available at: %s\n", url)
log.Infof("The text post is available at: %s\n", url)
return nil
}

View File

@@ -3,7 +3,6 @@ package linkedin
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"
@@ -22,7 +21,7 @@ func TestDefault(t *testing.T) {
func TestAnnounceDisabled(t *testing.T) {
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Default(ctx))
testlib.AssertSkipped(t, Pipe{}.Announce(ctx))
require.EqualError(t, Pipe{}.Announce(ctx), `failed to announce to linkedin: env: environment variable "LINKEDIN_ACCESS_TOKEN" should not be empty`)
}
func TestAnnounceInvalidTemplate(t *testing.T) {
@@ -34,7 +33,7 @@ func TestAnnounceInvalidTemplate(t *testing.T) {
},
},
})
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to linkedin: template: tmpl:1: unexpected "}" in operand`)
require.EqualError(t, Pipe{}.Announce(ctx), `failed to announce to linkedin: template: tmpl:1: unexpected "}" in operand`)
}
func TestAnnounceMissingEnv(t *testing.T) {
@@ -46,19 +45,7 @@ func TestAnnounceMissingEnv(t *testing.T) {
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to linkedin: env: environment variable "LINKEDIN_ACCESS_TOKEN" should not be empty`)
}
func TestAnnounceSkipAnnounce(t *testing.T) {
ctx := context.New(config.Project{
Announce: config.Announce{
LinkedIn: config.LinkedIn{
Enabled: true,
},
},
})
ctx.SkipAnnounce = true
testlib.AssertSkipped(t, Pipe{}.Announce(ctx))
require.EqualError(t, Pipe{}.Announce(ctx), `failed to announce to linkedin: env: environment variable "LINKEDIN_ACCESS_TOKEN" should not be empty`)
}
func TestSkip(t *testing.T) {
@@ -69,7 +56,7 @@ func TestSkip(t *testing.T) {
t.Run("dont skip", func(t *testing.T) {
ctx := context.New(config.Project{
Announce: config.Announce{
Reddit: config.Reddit{
LinkedIn: config.LinkedIn{
Enabled: true,
},
},