Skip to content

Commit ea18dc6

Browse files
jlegroneunguiculus
authored andcommitted
fix(account): handle invalid repository domain matches (#137)
fixes #136 Signed-off-by: Jacob LeGrone <[email protected]>
1 parent c97c790 commit ea18dc6

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

pkg/tool/account.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,37 @@ package tool
1616

1717
import (
1818
"fmt"
19-
"github.com/pkg/errors"
2019
"net/http"
2120
"regexp"
21+
22+
"github.com/pkg/errors"
2223
)
2324

2425
type AccountValidator struct{}
2526

2627
var repoDomainPattern = regexp.MustCompile("(?:https://|git@)([^/:]+)")
2728

28-
func (v AccountValidator) Validate(repoUrl string, account string) error {
29-
domain := parseOutGitRepoDomain(repoUrl)
29+
func (v AccountValidator) Validate(repoURL string, account string) error {
30+
domain, err := parseOutGitRepoDomain(repoURL)
31+
if err != nil {
32+
return err
33+
}
3034
url := fmt.Sprintf("https://%s/%s", domain, account)
3135
response, err := http.Head(url)
3236
if err != nil {
3337
return errors.Wrap(err, "Error validating maintainers")
3438
}
3539
if response.StatusCode != 200 {
36-
return errors.New(fmt.Sprintf("Error validating maintainer '%s': %s", account, response.Status))
40+
return fmt.Errorf("Error validating maintainer '%s': %s", account, response.Status)
3741
}
3842
return nil
3943
}
4044

41-
func parseOutGitRepoDomain(repoUrl string) string {
45+
func parseOutGitRepoDomain(repoURL string) (string, error) {
4246
// This works for GitHub, Bitbucket, and Gitlab
43-
submatch := repoDomainPattern.FindStringSubmatch(repoUrl)
44-
return submatch[1]
47+
submatch := repoDomainPattern.FindStringSubmatch(repoURL)
48+
if submatch == nil || len(submatch) < 2 {
49+
return "", fmt.Errorf("Could not parse git repository domain for '%s'", repoURL)
50+
}
51+
return submatch[1], nil
4552
}

pkg/tool/account_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
package tool
22

33
import (
4-
"github.com/stretchr/testify/assert"
4+
"fmt"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestParseOutGitDomain(t *testing.T) {
911
var testDataSlice = []struct {
1012
name string
1113
repoUrl string
1214
expected string
15+
err error
1316
}{
14-
{"GitHub SSH", "[email protected]:foo/bar", "github.com"},
15-
{"GitHub HTTPS", "https://github.com/foo/bar", "github.com"},
16-
{"Gitlab SSH", "[email protected]:foo/bar", "gitlab.com"},
17-
{"Gitlab HTTPS", "https://gitlab.com/foo/bar", "gitlab.com"},
18-
{"Bitbucket SSH", "[email protected]:foo/bar", "bitbucket.com"},
19-
{"Bitbucket HTTPS", "https://bitbucket.com/foo/bar", "bitbucket.com"},
17+
{"GitHub SSH", "[email protected]:foo/bar", "github.com", nil},
18+
{"GitHub HTTPS", "https://github.com/foo/bar", "github.com", nil},
19+
{"Gitlab SSH", "[email protected]:foo/bar", "gitlab.com", nil},
20+
{"Gitlab HTTPS", "https://gitlab.com/foo/bar", "gitlab.com", nil},
21+
{"Bitbucket SSH", "[email protected]:foo/bar", "bitbucket.com", nil},
22+
{"Bitbucket HTTPS", "https://bitbucket.com/foo/bar", "bitbucket.com", nil},
23+
{"Invalid", "foo/bar", "", fmt.Errorf("Could not parse git repository domain for 'foo/bar'")},
2024
}
2125

2226
for _, testData := range testDataSlice {
2327
t.Run(testData.name, func(t *testing.T) {
24-
actual := parseOutGitRepoDomain(testData.repoUrl)
28+
actual, err := parseOutGitRepoDomain(testData.repoUrl)
29+
assert.Equal(t, err, testData.err)
2530
assert.Equal(t, testData.expected, actual)
2631
})
2732
}

0 commit comments

Comments
 (0)