Skip to content

Commit

Permalink
feat: improve multiple tokens error (#2733)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Dec 6, 2021
1 parent f9687b4 commit 7386773
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
21 changes: 14 additions & 7 deletions internal/pipe/env/env.go
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/tmpl"
Expand All @@ -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{}
Expand Down Expand Up @@ -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 == ""
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/env/env_test.go
Expand Up @@ -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"))
Expand Down
15 changes: 15 additions & 0 deletions 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/).
1 change: 1 addition & 0 deletions www/mkdocs.yml
Expand Up @@ -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:
Expand Down

0 comments on commit 7386773

Please sign in to comment.