-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(detector): provide a detector for repository hosted on GitLab.com (
#259) (#281) * feat(detector): provide a detector for repository hosted on GitLab.com (#259) * remove hg test after #276 Co-authored-by: Davin Kevin <[email protected]>
- Loading branch information
1 parent
da52711
commit d436e03
Showing
5 changed files
with
94 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package getter | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// GitLabDetector implements Detector to detect GitLab URLs and turn | ||
// them into URLs that the Git Getter can understand. | ||
type GitLabDetector struct{} | ||
|
||
func (d *GitLabDetector) Detect(src, _ string) (string, bool, error) { | ||
if len(src) == 0 { | ||
return "", false, nil | ||
} | ||
|
||
if strings.HasPrefix(src, "gitlab.com/") { | ||
return d.detectHTTP(src) | ||
} | ||
|
||
return "", false, nil | ||
} | ||
|
||
func (d *GitLabDetector) detectHTTP(src string) (string, bool, error) { | ||
parts := strings.Split(src, "/") | ||
if len(parts) < 3 { | ||
return "", false, fmt.Errorf( | ||
"GitLab URLs should be gitlab.com/username/repo") | ||
} | ||
|
||
urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/")) | ||
repoUrl, err := url.Parse(urlStr) | ||
if err != nil { | ||
return "", true, fmt.Errorf("error parsing GitLab URL: %s", err) | ||
} | ||
|
||
if !strings.HasSuffix(repoUrl.Path, ".git") { | ||
repoUrl.Path += ".git" | ||
} | ||
|
||
if len(parts) > 3 { | ||
repoUrl.Path += "//" + strings.Join(parts[3:], "/") | ||
} | ||
|
||
return "git::" + repoUrl.String(), true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package getter | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGitLabDetector(t *testing.T) { | ||
cases := []struct { | ||
Input string | ||
Output string | ||
}{ | ||
// HTTP | ||
{"gitlab.com/hashicorp/foo", "git::https://gitlab.com/hashicorp/foo.git"}, | ||
{"gitlab.com/hashicorp/foo.git", "git::https://gitlab.com/hashicorp/foo.git"}, | ||
{ | ||
"gitlab.com/hashicorp/foo/bar", | ||
"git::https://gitlab.com/hashicorp/foo.git//bar", | ||
}, | ||
{ | ||
"gitlab.com/hashicorp/foo?foo=bar", | ||
"git::https://gitlab.com/hashicorp/foo.git?foo=bar", | ||
}, | ||
{ | ||
"gitlab.com/hashicorp/foo.git?foo=bar", | ||
"git::https://gitlab.com/hashicorp/foo.git?foo=bar", | ||
}, | ||
} | ||
|
||
pwd := "/pwd" | ||
f := new(GitLabDetector) | ||
for i, tc := range cases { | ||
output, ok, err := f.Detect(tc.Input, pwd) | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
if !ok { | ||
t.Fatal("not ok") | ||
} | ||
|
||
if output != tc.Output { | ||
t.Fatalf("%d: bad: %#v", i, output) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters