Skip to content

Commit 0e29201

Browse files
authored
Merge pull request #136 from fluxcd/gitlab-custom-domain
Ensure that the baseURL for the gitlab client includes a scheme
2 parents 91f89c4 + 9573fa1 commit 0e29201

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

gitlab/auth.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package gitlab
1818

1919
import (
20+
"net/url"
21+
2022
"github.com/fluxcd/go-git-providers/gitprovider"
2123
gogitlab "github.com/xanzy/go-gitlab"
2224
)
@@ -37,6 +39,21 @@ func NewClient(token string, tokenType string, optFns ...gitprovider.ClientOptio
3739
return nil, err
3840
}
3941

42+
// Ensure that the URL includes a scheme when using a custom domain.
43+
if opts.Domain != nil && *opts.Domain != "" {
44+
d, err := url.Parse(*opts.Domain)
45+
if err != nil {
46+
return nil, err
47+
}
48+
if d.Scheme != "http" && d.Scheme != "https" {
49+
u := url.URL{
50+
Scheme: "https",
51+
Host: *opts.Domain,
52+
}
53+
*opts.Domain = u.String()
54+
}
55+
}
56+
4057
// Create a *http.Client using the transport chain
4158
httpClient, err := gitprovider.BuildClientFromTransportChain(opts.GetTransportChain())
4259
if err != nil {

gitlab/auth_test.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020
"testing"
2121

2222
"github.com/fluxcd/go-git-providers/gitprovider"
23+
gogitlab "github.com/xanzy/go-gitlab"
2324
)
2425

25-
func Test_DomainVariations(t *testing.T) {
26+
func TestSupportedDomain(t *testing.T) {
2627
tests := []struct {
2728
name string
2829
opts gitprovider.ClientOption
@@ -39,16 +40,31 @@ func Test_DomainVariations(t *testing.T) {
3940
opts: gitprovider.WithDomain("my-gitlab.dev.com"),
4041
want: "https://my-gitlab.dev.com",
4142
},
43+
{
44+
name: "custom domain without protocol with port",
45+
opts: gitprovider.WithDomain("my-gitlab.dev.com:1234"),
46+
want: "https://my-gitlab.dev.com:1234",
47+
},
4248
{
4349
name: "custom domain with https protocol",
4450
opts: gitprovider.WithDomain("https://my-gitlab.dev.com"),
4551
want: "https://my-gitlab.dev.com",
4652
},
53+
{
54+
name: "custom domain with https protocol with port",
55+
opts: gitprovider.WithDomain("https://my-gitlab.dev.com:1234"),
56+
want: "https://my-gitlab.dev.com:1234",
57+
},
4758
{
4859
name: "custom domain with http protocol",
4960
opts: gitprovider.WithDomain("http://my-gitlab.dev.com"),
5061
want: "http://my-gitlab.dev.com",
5162
},
63+
{
64+
name: "custom domain with http protocol with port",
65+
opts: gitprovider.WithDomain("http://my-gitlab.dev.com:1234"),
66+
want: "http://my-gitlab.dev.com:1234",
67+
},
5268
}
5369
for _, tt := range tests {
5470
t.Run(tt.name, func(t *testing.T) {
@@ -61,6 +77,61 @@ func Test_DomainVariations(t *testing.T) {
6177
}
6278
}
6379

80+
func TestBaseURL(t *testing.T) {
81+
tests := []struct {
82+
name string
83+
opts gitprovider.ClientOption
84+
expected string
85+
}{
86+
{
87+
name: "gitlab.com domain",
88+
opts: gitprovider.WithDomain("gitlab.com"),
89+
expected: "https://gitlab.com/api/v4/",
90+
},
91+
{
92+
name: "custom domain without protocol",
93+
opts: gitprovider.WithDomain("my-gitlab.dev.com"),
94+
expected: "https://my-gitlab.dev.com/api/v4/",
95+
},
96+
{
97+
name: "custom domain without protocol with port",
98+
opts: gitprovider.WithDomain("my-gitlab.dev.com:1234"),
99+
expected: "https://my-gitlab.dev.com:1234/api/v4/",
100+
},
101+
{
102+
name: "custom domain with https protocol",
103+
opts: gitprovider.WithDomain("https://my-gitlab.dev.com"),
104+
expected: "https://my-gitlab.dev.com/api/v4/",
105+
},
106+
{
107+
name: "custom domain with https protocol with port",
108+
opts: gitprovider.WithDomain("https://my-gitlab.dev.com:1234"),
109+
expected: "https://my-gitlab.dev.com:1234/api/v4/",
110+
},
111+
{
112+
name: "custom domain with http protocol",
113+
opts: gitprovider.WithDomain("http://my-gitlab.dev.com"),
114+
expected: "http://my-gitlab.dev.com/api/v4/",
115+
},
116+
{
117+
name: "custom domain with http protocol with port",
118+
opts: gitprovider.WithDomain("http://my-gitlab.dev.com:1234"),
119+
expected: "http://my-gitlab.dev.com:1234/api/v4/",
120+
},
121+
}
122+
for _, tt := range tests {
123+
t.Run(tt.name, func(t *testing.T) {
124+
c1, _ := NewClient("token", "oauth2", tt.opts)
125+
gc1 := c1.Raw().(*gogitlab.Client)
126+
assertEqual(t, tt.expected, gc1.BaseURL().String())
127+
128+
c2, _ := NewClient("token", "pat", tt.opts)
129+
gc2 := c2.Raw().(*gogitlab.Client)
130+
assertEqual(t, tt.expected, gc2.BaseURL().String())
131+
})
132+
}
133+
}
134+
64135
func assertEqual(t *testing.T, a interface{}, b interface{}) {
65136
if a != b {
66137
t.Fatalf("%s != %s", a, b)

gitlab/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7+
"net/url"
78
"strings"
89

910
"github.com/fluxcd/go-git-providers/gitprovider"
@@ -191,6 +192,17 @@ func validateOrganizationRef(ref gitprovider.OrganizationRef, expectedDomain str
191192
// validateIdentityFields makes sure the type of the IdentityRef is supported, and the domain is as expected.
192193
func validateIdentityFields(ref gitprovider.IdentityRef, expectedDomain string) error {
193194
// Make sure the expected domain is used
195+
196+
// For custom domains ensure the expected domain only includes host
197+
// and port information but not the scheme.
198+
if expectedDomain != DefaultDomain {
199+
d, err := url.Parse(expectedDomain)
200+
if err != nil {
201+
return err
202+
}
203+
expectedDomain = d.Host
204+
}
205+
194206
if ref.GetDomain() != expectedDomain {
195207
return fmt.Errorf("domain %q not supported by this client: %w", ref.GetDomain(), gitprovider.ErrDomainUnsupported)
196208
}

0 commit comments

Comments
 (0)