Repository: goreleaser/goreleaser
Pull request #2515 (2021-09-26 23:27:21)

fix: brew/scoop panic
fixes  #2514

not really sure why it happens on some projects and not in others... but anyway, this should fix it...

the problem is that it might not have the `token type` set when running, which then was returning a nil client... this will now error and also always default to github token type if none was provided.

also some refactory to reuse some code on the client instantiation. 

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/goreleaser/goreleaser/CyBxTkJgVJm4fpPasDRgv82pVR9x](https://vercel.com/goreleaser/goreleaser/CyBxTkJgVJm4fpPasDRgv82pVR9x)  
✅ Preview: [https://goreleaser-git-fix-brew-goreleaser.vercel.app](https://goreleaser-git-fix-brew-goreleaser.vercel.app)


codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2515?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#2515](https://codecov.io/gh/goreleaser/goreleaser/pull/2515?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (87ae73c) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/3d62f95fd53bfa037e3e39894eefe637a8758333?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (3d62f95) will **increase** coverage by `0.08%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2515/graphs/tree.svg?width=650&height=150&src=pr&token=3bJvv6b7hN&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)](https://codecov.io/gh/goreleaser/goreleaser/pull/2515?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@            Coverage Diff             @@
##           master    #2515      +/-   ##
==========================================
+ Coverage   83.36%   83.44%   +0.08%     
==========================================
  Files          93       93              
  Lines        6450     6439      -11     
==========================================
- Hits         5377     5373       -4     
+ Misses        916      910       -6     
+ Partials      157      156       -1     
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/2515?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [internal/pipe/brew/brew.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2515/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9icmV3L2JyZXcuZ28=) | `82.41% <ø> (+1.19%)` | :arrow_up: |
| [internal/client/client.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2515/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvY2xpZW50L2NsaWVudC5nbw==) | `81.08% <100.00%> (+2.82%)` | :arrow_up: |
| [internal/pipe/env/env.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2515/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9lbnYvZW52Lmdv) | `97.00% <100.00%> (+0.06%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2515?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2515?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [3d62f95...87ae73c](https://codecov.io/gh/goreleaser/goreleaser/pull/2515?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser).

caarlos0: Something to think about for sure 🤔

Patch:
diff --git a/internal/client/client.go b/internal/client/client.go
index 0998bcaa..8f378daf 100644
--- a/internal/client/client.go
+++ b/internal/client/client.go
@@ -43,20 +43,11 @@ type Client interface {
 
 // New creates a new client depending on the token type.
 func New(ctx *context.Context) (Client, error) {
-	log.WithField("type", ctx.TokenType).Debug("token type")
-	if ctx.TokenType == context.TokenTypeGitHub {
-		return NewGitHub(ctx, ctx.Token)
-	}
-	if ctx.TokenType == context.TokenTypeGitLab {
-		return NewGitLab(ctx, ctx.Token)
-	}
-	if ctx.TokenType == context.TokenTypeGitea {
-		return NewGitea(ctx, ctx.Token)
-	}
-	return nil, nil
+	return newWithToken(ctx, ctx.Token)
 }
 
 func newWithToken(ctx *context.Context, token string) (Client, error) {
+	log.WithField("type", ctx.TokenType).Debug("token type")
 	if ctx.TokenType == context.TokenTypeGitHub {
 		return NewGitHub(ctx, token)
 	}
@@ -66,7 +57,7 @@ func newWithToken(ctx *context.Context, token string) (Client, error) {
 	if ctx.TokenType == context.TokenTypeGitea {
 		return NewGitea(ctx, token)
 	}
-	return nil, nil
+	return nil, fmt.Errorf("invalid client token type: %q", ctx.TokenType)
 }
 
 func NewIfToken(ctx *context.Context, cli Client, token string) (Client, error) {
diff --git a/internal/pipe/brew/brew.go b/internal/pipe/brew/brew.go
index 2e0c1212..a0759403 100644
--- a/internal/pipe/brew/brew.go
+++ b/internal/pipe/brew/brew.go
@@ -83,11 +83,6 @@ func (Pipe) Run(ctx *context.Context) error {
 
 // Publish brew formula.
 func (Pipe) Publish(ctx *context.Context) error {
-	// we keep GitHub as default for now, in line with releases
-	if string(ctx.TokenType) == "" {
-		ctx.TokenType = context.TokenTypeGitHub
-	}
-
 	cli, err := client.New(ctx)
 	if err != nil {
 		return err
diff --git a/internal/pipe/env/env.go b/internal/pipe/env/env.go
index d6b15cd6..28e2957a 100644
--- a/internal/pipe/env/env.go
+++ b/internal/pipe/env/env.go
@@ -83,12 +83,6 @@ func (Pipe) Run(ctx *context.Context) error {
 		return err
 	}
 
-	if githubToken != "" {
-		log.Debug("token type: github")
-		ctx.TokenType = context.TokenTypeGitHub
-		ctx.Token = githubToken
-	}
-
 	if gitlabToken != "" {
 		log.Debug("token type: gitlab")
 		ctx.TokenType = context.TokenTypeGitLab
@@ -101,6 +95,15 @@ func (Pipe) Run(ctx *context.Context) error {
 		ctx.Token = giteaToken
 	}
 
+	if githubToken != "" {
+		log.Debug("token type: github")
+		ctx.Token = githubToken
+	}
+
+	if ctx.TokenType == "" {
+		ctx.TokenType = context.TokenTypeGitHub
+	}
+
 	return nil
 }
 


Test patch:
diff --git a/internal/client/client_test.go b/internal/client/client_test.go
index 33448f90..6c67aa5c 100644
--- a/internal/client/client_test.go
+++ b/internal/client/client_test.go
@@ -12,7 +12,7 @@ func TestClientEmpty(t *testing.T) {
 	ctx := &context.Context{}
 	client, err := New(ctx)
 	require.Nil(t, client)
-	require.NoError(t, err)
+	require.EqualError(t, err, `invalid client token type: ""`)
 }
 
 func TestClientNewGitea(t *testing.T) {
@@ -153,7 +153,7 @@ func TestNewWithToken(t *testing.T) {
 		}
 
 		cli, err := newWithToken(ctx, "{{ .Env.TK }}")
-		require.NoError(t, err)
+		require.EqualError(t, err, `invalid client token type: "nope"`)
 		require.Nil(t, cli)
 	})
 }
diff --git a/internal/pipe/env/env_test.go b/internal/pipe/env/env_test.go
index 365afdc6..d7dfd87b 100644
--- a/internal/pipe/env/env_test.go
+++ b/internal/pipe/env/env_test.go
@@ -59,6 +59,12 @@ func TestSetDefaultTokenFiles(t *testing.T) {
 		})
 		require.EqualError(t, Pipe{}.Run(ctx), `template: tmpl:1: unexpected "}" in operand`)
 	})
+
+	t.Run("no token", func(t *testing.T) {
+		ctx := context.New(config.Project{})
+		require.NoError(t, Pipe{}.Run(ctx))
+		require.Equal(t, ctx.TokenType, context.TokenTypeGitHub)
+	})
 }
 
 func TestValidGithubEnv(t *testing.T) {