Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(account): handle invalid repository domain matches #137

Merged
merged 1 commit into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions pkg/tool/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 13 additions & 8 deletions pkg/tool/account_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
package tool

import (
"github.com/stretchr/testify/assert"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseOutGitDomain(t *testing.T) {
var testDataSlice = []struct {
name string
repoUrl string
expected string
err error
}{
{"GitHub SSH", "[email protected]:foo/bar", "github.com"},
{"GitHub HTTPS", "https://github.com/foo/bar", "github.com"},
{"Gitlab SSH", "[email protected]:foo/bar", "gitlab.com"},
{"Gitlab HTTPS", "https://gitlab.com/foo/bar", "gitlab.com"},
{"Bitbucket SSH", "[email protected]:foo/bar", "bitbucket.com"},
{"Bitbucket HTTPS", "https://bitbucket.com/foo/bar", "bitbucket.com"},
{"GitHub SSH", "[email protected]:foo/bar", "github.com", nil},
{"GitHub HTTPS", "https://github.com/foo/bar", "github.com", nil},
{"Gitlab SSH", "[email protected]:foo/bar", "gitlab.com", nil},
{"Gitlab HTTPS", "https://gitlab.com/foo/bar", "gitlab.com", nil},
{"Bitbucket SSH", "[email protected]: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)
})
}
Expand Down