Repository: goreleaser/goreleaser
Pull request #2733 (2021-12-04 21:09:24)

feat: improve multiple tokens error
- improve error message
- adds an error page detailing how to fix it

resolves #2732

codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2733?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#2733](https://codecov.io/gh/goreleaser/goreleaser/pull/2733?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (f1c7182) into [main](https://codecov.io/gh/goreleaser/goreleaser/commit/f9687b482ad00c9176244e9d76f600c765d6ec90?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (f9687b4) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/2733/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/2733?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

```diff
@@           Coverage Diff           @@
##             main    #2733   +/-   ##
=======================================
  Coverage   84.90%   84.90%           
=======================================
  Files         105      105           
  Lines        7901     7903    +2     
=======================================
+ Hits         6708     6710    +2     
  Misses        978      978           
  Partials      215      215           
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/2733?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [internal/pipe/env/env.go](https://codecov.io/gh/goreleaser/goreleaser/pull/2733/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvcGlwZS9lbnYvZW52Lmdv) | `97.14% <100.00%> (+0.05%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/2733?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/2733?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Last update [f9687b4...f1c7182](https://codecov.io/gh/goreleaser/goreleaser/pull/2733?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).


Patch:
diff --git a/internal/pipe/env/env.go b/internal/pipe/env/env.go
index 4fd17b07..e00315ad 100644
--- a/internal/pipe/env/env.go
+++ b/internal/pipe/env/env.go
@@ -7,6 +7,7 @@ import (
 	"errors"
 	"fmt"
 	"os"
+	"strings"
 
 	"github.com/apex/log"
 	"github.com/goreleaser/goreleaser/internal/tmpl"
@@ -19,7 +20,13 @@ var ErrMissingToken = errors.New("missing GITHUB_TOKEN, GITLAB_TOKEN and GITEA_T
 
 // ErrMultipleTokens indicates that multiple tokens are defined. ATM only one of them if allowed.
 // See https://github.com/goreleaser/goreleaser/pull/809
-var ErrMultipleTokens = errors.New("multiple tokens defined. Only one is allowed")
+type ErrMultipleTokens struct {
+	tokens []string
+}
+
+func (e ErrMultipleTokens) Error() string {
+	return fmt.Sprintf("multiple tokens found, but only one is allowed: %s\n\nLearn more at https://goreleaser.com/errors/multiple-tokens\n", strings.Join(e.tokens, ", "))
+}
 
 // Pipe for env.
 type Pipe struct{}
@@ -61,18 +68,18 @@ func (Pipe) Run(ctx *context.Context) error {
 	gitlabToken, gitlabTokenErr := loadEnv("GITLAB_TOKEN", ctx.Config.EnvFiles.GitLabToken)
 	giteaToken, giteaTokenErr := loadEnv("GITEA_TOKEN", ctx.Config.EnvFiles.GiteaToken)
 
-	numOfTokens := 0
+	var tokens []string
 	if githubToken != "" {
-		numOfTokens++
+		tokens = append(tokens, "GITHUB_TOKEN")
 	}
 	if gitlabToken != "" {
-		numOfTokens++
+		tokens = append(tokens, "GITLAB_TOKEN")
 	}
 	if giteaToken != "" {
-		numOfTokens++
+		tokens = append(tokens, "GITEA_TOKEN")
 	}
-	if numOfTokens > 1 {
-		return ErrMultipleTokens
+	if len(tokens) > 1 {
+		return ErrMultipleTokens{tokens}
 	}
 
 	noTokens := githubToken == "" && gitlabToken == "" && giteaToken == ""
diff --git a/www/docs/errors/multiple-tokens.md b/www/docs/errors/multiple-tokens.md
new file mode 100644
index 00000000..49dda230
--- /dev/null
+++ b/www/docs/errors/multiple-tokens.md
@@ -0,0 +1,15 @@
+# Multiple tokens found, but only one is allowed
+
+GoReleaser infers if you are using GitHub, GitLab or Gitea by which tokens are provided.
+If you have multiple tokens set, you'll get this error.
+
+Here's an example:
+
+```sh
+   ⨯ release failed after 0.02s error=gmultiple tokens found, but only one is allowed: GITHUB_TOKEN, GITLAB_TOKEN
+
+Learn more at https://goreleaser.com/errors/multiple-tokens
+```
+
+In this case, you either unset `GITHUB_TOKEN` or `GITLAB_TOKEN`.
+You can read more about it in the [SCM docs](/scm/github/).
diff --git a/www/mkdocs.yml b/www/mkdocs.yml
index 935ea9d9..9a57b474 100644
--- a/www/mkdocs.yml
+++ b/www/mkdocs.yml
@@ -131,6 +131,7 @@ nav:
     - goreleaser jsonschema: cmd/goreleaser_jsonschema.md
 - Common errors:
   - errors/dirty.md
+  - errors/multiple-tokens.md
   - errors/no-main.md
 - deprecations.md
 - Cookbooks:


Test patch:
diff --git a/internal/pipe/env/env_test.go b/internal/pipe/env/env_test.go
index bd198dfc..c4b98172 100644
--- a/internal/pipe/env/env_test.go
+++ b/internal/pipe/env/env_test.go
@@ -120,7 +120,7 @@ func TestMultipleEnvTokens(t *testing.T) {
 		Config: config.Project{},
 	}
 	require.Error(t, Pipe{}.Run(ctx))
-	require.EqualError(t, Pipe{}.Run(ctx), ErrMultipleTokens.Error())
+	require.EqualError(t, Pipe{}.Run(ctx), "multiple tokens found, but only one is allowed: GITHUB_TOKEN, GITLAB_TOKEN, GITEA_TOKEN\n\nLearn more at https://goreleaser.com/errors/multiple-tokens\n")
 	// so the tests do not depend on each other
 	require.NoError(t, os.Unsetenv("GITHUB_TOKEN"))
 	require.NoError(t, os.Unsetenv("GITLAB_TOKEN"))