1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-11 14:39:28 +02:00

feat: improve multiple tokens error (#2733)

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>
This commit is contained in:
Carlos Alexandro Becker 2021-12-05 22:25:29 -03:00 committed by GitHub
parent f9687b482a
commit 73867736a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 8 deletions

View File

@ -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 == ""

View File

@ -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"))

View File

@ -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/).

View File

@ -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: