Skip to content

Commit

Permalink
fix(account): handle invalid repository domain matches (#137)
Browse files Browse the repository at this point in the history
fixes #136 

Signed-off-by: Jacob LeGrone <[email protected]>
  • Loading branch information
jlegrone authored and unguiculus committed Mar 28, 2019
1 parent c97c790 commit ea18dc6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
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

0 comments on commit ea18dc6

Please sign in to comment.