From ea18dc6af1d2b91d10f5aef9a5d8002a317255db Mon Sep 17 00:00:00 2001 From: Jacob LeGrone Date: Thu, 28 Mar 2019 15:55:55 -0400 Subject: [PATCH] fix(account): handle invalid repository domain matches (#137) fixes #136 Signed-off-by: Jacob LeGrone --- pkg/tool/account.go | 21 ++++++++++++++------- pkg/tool/account_test.go | 21 +++++++++++++-------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/pkg/tool/account.go b/pkg/tool/account.go index 6ab6f2c2..0a8e5c49 100644 --- a/pkg/tool/account.go +++ b/pkg/tool/account.go @@ -16,30 +16,37 @@ package tool import ( "fmt" - "github.com/pkg/errors" "net/http" "regexp" + + "github.com/pkg/errors" ) type AccountValidator struct{} var repoDomainPattern = regexp.MustCompile("(?:https://|git@)([^/:]+)") -func (v AccountValidator) Validate(repoUrl string, account string) error { - domain := parseOutGitRepoDomain(repoUrl) +func (v AccountValidator) Validate(repoURL string, account string) error { + domain, err := parseOutGitRepoDomain(repoURL) + if err != nil { + return err + } url := fmt.Sprintf("https://%s/%s", domain, account) response, err := http.Head(url) if err != nil { return errors.Wrap(err, "Error validating maintainers") } if response.StatusCode != 200 { - return errors.New(fmt.Sprintf("Error validating maintainer '%s': %s", account, response.Status)) + return fmt.Errorf("Error validating maintainer '%s': %s", account, response.Status) } return nil } -func parseOutGitRepoDomain(repoUrl string) string { +func parseOutGitRepoDomain(repoURL string) (string, error) { // This works for GitHub, Bitbucket, and Gitlab - submatch := repoDomainPattern.FindStringSubmatch(repoUrl) - return submatch[1] + submatch := repoDomainPattern.FindStringSubmatch(repoURL) + if submatch == nil || len(submatch) < 2 { + return "", fmt.Errorf("Could not parse git repository domain for '%s'", repoURL) + } + return submatch[1], nil } diff --git a/pkg/tool/account_test.go b/pkg/tool/account_test.go index 59eaae2c..a19f174a 100644 --- a/pkg/tool/account_test.go +++ b/pkg/tool/account_test.go @@ -1,8 +1,10 @@ package tool import ( - "github.com/stretchr/testify/assert" + "fmt" "testing" + + "github.com/stretchr/testify/assert" ) func TestParseOutGitDomain(t *testing.T) { @@ -10,18 +12,21 @@ func TestParseOutGitDomain(t *testing.T) { name string repoUrl string expected string + err error }{ - {"GitHub SSH", "git@github.com:foo/bar", "github.com"}, - {"GitHub HTTPS", "https://github.com/foo/bar", "github.com"}, - {"Gitlab SSH", "git@gitlab.com:foo/bar", "gitlab.com"}, - {"Gitlab HTTPS", "https://gitlab.com/foo/bar", "gitlab.com"}, - {"Bitbucket SSH", "git@bitbucket.com:foo/bar", "bitbucket.com"}, - {"Bitbucket HTTPS", "https://bitbucket.com/foo/bar", "bitbucket.com"}, + {"GitHub SSH", "git@github.com:foo/bar", "github.com", nil}, + {"GitHub HTTPS", "https://github.com/foo/bar", "github.com", nil}, + {"Gitlab SSH", "git@gitlab.com:foo/bar", "gitlab.com", nil}, + {"Gitlab HTTPS", "https://gitlab.com/foo/bar", "gitlab.com", nil}, + {"Bitbucket SSH", "git@bitbucket.com:foo/bar", "bitbucket.com", nil}, + {"Bitbucket HTTPS", "https://bitbucket.com/foo/bar", "bitbucket.com", nil}, + {"Invalid", "foo/bar", "", fmt.Errorf("Could not parse git repository domain for 'foo/bar'")}, } for _, testData := range testDataSlice { t.Run(testData.name, func(t *testing.T) { - actual := parseOutGitRepoDomain(testData.repoUrl) + actual, err := parseOutGitRepoDomain(testData.repoUrl) + assert.Equal(t, err, testData.err) assert.Equal(t, testData.expected, actual) }) }