Skip to content

Commit 437601e

Browse files
authored
feat(detector): provide a detector for repository hosted on GitLab.com (#259)
1 parent 81f79b4 commit 437601e

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ is built-in by default:
8080
file URLs.
8181
* GitHub URLs, such as "github.com/mitchellh/vagrant" are automatically
8282
changed to Git protocol over HTTP.
83+
* GitLab URLs, such as "gitlab.com/inkscape/inkscape" are automatically
84+
changed to Git protocol over HTTP.
8385
* BitBucket URLs, such as "bitbucket.org/mitchellh/vagrant" are automatically
8486
changed to a Git or mercurial protocol using the BitBucket API.
8587

detect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var Detectors []Detector
2323
func init() {
2424
Detectors = []Detector{
2525
new(GitHubDetector),
26+
new(GitLabDetector),
2627
new(GitDetector),
2728
new(BitBucketDetector),
2829
new(S3Detector),

detect_gitlab.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package getter
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
"strings"
7+
)
8+
9+
// GitLabDetector implements Detector to detect GitLab URLs and turn
10+
// them into URLs that the Git Getter can understand.
11+
type GitLabDetector struct{}
12+
13+
func (d *GitLabDetector) Detect(src, _ string) (string, bool, error) {
14+
if len(src) == 0 {
15+
return "", false, nil
16+
}
17+
18+
if strings.HasPrefix(src, "gitlab.com/") {
19+
return d.detectHTTP(src)
20+
}
21+
22+
return "", false, nil
23+
}
24+
25+
func (d *GitLabDetector) detectHTTP(src string) (string, bool, error) {
26+
parts := strings.Split(src, "/")
27+
if len(parts) < 3 {
28+
return "", false, fmt.Errorf(
29+
"GitLab URLs should be gitlab.com/username/repo")
30+
}
31+
32+
urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
33+
repoUrl, err := url.Parse(urlStr)
34+
if err != nil {
35+
return "", true, fmt.Errorf("error parsing GitLab URL: %s", err)
36+
}
37+
38+
if !strings.HasSuffix(repoUrl.Path, ".git") {
39+
repoUrl.Path += ".git"
40+
}
41+
42+
if len(parts) > 3 {
43+
repoUrl.Path += "//" + strings.Join(parts[3:], "/")
44+
}
45+
46+
return "git::" + repoUrl.String(), true, nil
47+
}

detect_gitlab_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package getter
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGitLabDetector(t *testing.T) {
8+
cases := []struct {
9+
Input string
10+
Output string
11+
}{
12+
// HTTP
13+
{"gitlab.com/hashicorp/foo", "git::https://gitlab.com/hashicorp/foo.git"},
14+
{"gitlab.com/hashicorp/foo.git", "git::https://gitlab.com/hashicorp/foo.git"},
15+
{
16+
"gitlab.com/hashicorp/foo/bar",
17+
"git::https://gitlab.com/hashicorp/foo.git//bar",
18+
},
19+
{
20+
"gitlab.com/hashicorp/foo?foo=bar",
21+
"git::https://gitlab.com/hashicorp/foo.git?foo=bar",
22+
},
23+
{
24+
"gitlab.com/hashicorp/foo.git?foo=bar",
25+
"git::https://gitlab.com/hashicorp/foo.git?foo=bar",
26+
},
27+
}
28+
29+
pwd := "/pwd"
30+
f := new(GitLabDetector)
31+
for i, tc := range cases {
32+
output, ok, err := f.Detect(tc.Input, pwd)
33+
if err != nil {
34+
t.Fatalf("err: %s", err)
35+
}
36+
if !ok {
37+
t.Fatal("not ok")
38+
}
39+
40+
if output != tc.Output {
41+
t.Fatalf("%d: bad: %#v", i, output)
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)