Skip to content

Commit

Permalink
Support setting a hostname specific GitLab token environment variable
Browse files Browse the repository at this point in the history
Signed-off-by: Sune Keller <absukl@almbrand.dk>
  • Loading branch information
sirlatrom committed Mar 15, 2021
1 parent e4d130e commit 13bab83
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/providers/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type gitLab struct {
url *url.URL
client *gitlab.Client
token string
owner string
repo string
tag string
Expand Down Expand Up @@ -154,11 +155,11 @@ func (g *gitLab) Fetch() (*File, error) {
return nil, err
}

if token, exists := os.LookupEnv("GITLAB_TOKEN"); exists {
if g.token != "" {
if gf.ExtraHeaders == nil {
gf.ExtraHeaders = map[string]string{}
}
gf.ExtraHeaders["PRIVATE-TOKEN"] = token
gf.ExtraHeaders["PRIVATE-TOKEN"] = g.token
}

name, outputFile, err := assets.ProcessURL(gf)
Expand Down Expand Up @@ -239,9 +240,14 @@ func newGitLab(u *url.URL) (Provider, error) {
}

token := os.Getenv("GITLAB_TOKEN")
hostnameSpecificEnvVarName := fmt.Sprintf("GITLAB_TOKEN_%s", strings.ReplaceAll(u.Hostname(), `.`, "_"))
hostnameSpecificToken := os.Getenv(hostnameSpecificEnvVarName)
if hostnameSpecificToken != "" {
token = hostnameSpecificToken
}
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(fmt.Sprintf("https://%s/api/v4", u.Hostname())))
if err != nil {
return nil, err
}
return &gitLab{url: u, client: client, owner: s[1], repo: s[2], tag: tag}, nil
return &gitLab{url: u, client: client, token: token, owner: s[1], repo: s[2], tag: tag}, nil
}

0 comments on commit 13bab83

Please sign in to comment.