Skip to content

Commit 7422cce

Browse files
committed
Support both forms of GitLab access token
1 parent a72049a commit 7422cce

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

api/gitlab.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,22 @@ func gitlabDirector(r *http.Request) {
4949
// explicitly disable User-Agent so it's not set to default value
5050
r.Header.Set("User-Agent", "")
5151
}
52-
r.Header.Del("Authorization")
53-
if r.Method != http.MethodOptions {
54-
r.Header.Set("Private-Token", accessToken)
52+
53+
config := getConfig(ctx)
54+
tokenType := config.GitLab.AccessTokenType
55+
56+
if tokenType == "personal_access" {
57+
// Private access token
58+
r.Header.Del("Authorization")
59+
if r.Method != http.MethodOptions {
60+
r.Header.Set("Private-Token", accessToken)
61+
}
62+
} else {
63+
// OAuth token
64+
r.Header.Del("Authorization")
65+
if r.Method != http.MethodOptions {
66+
r.Header.Set("Authorization", "Bearer "+accessToken)
67+
}
5568
}
5669

5770
log := getLogEntry(r)

conf/configuration.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
const DefaultGitHubEndpoint = "https://api.github.com"
1212
const DefaultGitLabEndpoint = "https://gitlab.com/api/v4"
13+
const DefaultGitLabTokenType = "oauth"
1314

1415
type GitHubConfig struct {
1516
AccessToken string `envconfig:"ACCESS_TOKEN" json:"access_token,omitempty"`
@@ -18,9 +19,10 @@ type GitHubConfig struct {
1819
}
1920

2021
type GitLabConfig struct {
21-
AccessToken string `envconfig:"ACCESS_TOKEN" json:"access_token,omitempty"`
22-
Endpoint string `envconfig:"ENDPOINT" json:"endpoint"`
23-
Repo string `envconfig:"REPO" json:"repo"` // Should be "owner/repo" format
22+
AccessToken string `envconfig:"ACCESS_TOKEN" json:"access_token,omitempty"`
23+
AccessTokenType string `envconfig:"ACCESS_TOKEN_TYPE" json:"access_token_type"`
24+
Endpoint string `envconfig:"ENDPOINT" json:"endpoint"`
25+
Repo string `envconfig:"REPO" json:"repo"` // Should be "owner/repo" format
2426
}
2527

2628
// DBConfiguration holds all the database related configuration.
@@ -106,6 +108,11 @@ func LoadConfig(filename string) (*Configuration, error) {
106108
func (config *Configuration) ApplyDefaults() {
107109
if config.GitHub.Endpoint == "" {
108110
config.GitHub.Endpoint = DefaultGitHubEndpoint
111+
}
112+
if config.GitLab.Endpoint == "" {
109113
config.GitLab.Endpoint = DefaultGitLabEndpoint
110114
}
115+
if config.GitLab.AccessTokenType == "" {
116+
config.GitLab.AccessTokenType = DefaultGitLabTokenType
117+
}
111118
}

0 commit comments

Comments
 (0)