Skip to content

Commit

Permalink
feat: added additional GH tests
Browse files Browse the repository at this point in the history
Added additional GH tests from Coala's git-url-parse.
Also, quick return from parser when a regexp match is found.
  • Loading branch information
retr0h committed Jun 23, 2024
1 parent 47047d5 commit d5d608f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 66 deletions.
14 changes: 4 additions & 10 deletions internal/repositories/github/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ const (

// ChatGPT-4 generated regexp
var patterns = []string{
`^(?P<scheme>https?)://(?P<resource>[^/]+)/(?P<owner>[^/]+)/(?P<repo>[^/]+)(/(?:tree|blob)/(?P<branch>[^/]+)/(?P<path>.*)?)?$`,
`^(?P<scheme>https?)://(?P<resource>[^/]+)/(?P<owner>[^/]+)/(?P<repo>[^/]+?)(?:\.git)?(/(?:tree|blob)/(?P<branch>[^/]+)/(?P<path>.*)?)?$`,
`^(?P<scheme>https?)://(?P<resource>raw\.githubusercontent\.com)/(?P<owner>[^/]+)/(?P<repo>[^/]+)/(?P<branch>[^/]+)/(?P<path>.*)$`,
`^(?P<scheme>git)@(?P<resource>github\.com):(?P<owner>[^/]+)/(?P<repo>[^/]+)\.git$`,
}

// Parse the provided GitHub URL.
func (gh *GitHub) Parse(url string) (*api.Repository, error) {
response := &api.Repository{}

for _, pattern := range patterns {
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(url)
Expand All @@ -55,7 +53,7 @@ func (gh *GitHub) Parse(url string) (*api.Repository, error) {
)

if matches != nil {
response = &api.Repository{
return &api.Repository{
Protocol: mm["scheme"],
Host: mm["resource"],
Provider: providerName,
Expand All @@ -65,15 +63,11 @@ func (gh *GitHub) Parse(url string) (*api.Repository, error) {
Path: mm["path"],
Branch: mm["branch"],
HREF: url,
}
}, nil
}
}

if (api.Repository{}) == *response {
return nil, fmt.Errorf("could match url: %s to any pattern", url)
}

return response, nil
return nil, fmt.Errorf("could match url: %s to any pattern", url)
}

func makeMatchMap(re *regexp.Regexp, matches []string) map[string]string {
Expand Down
48 changes: 48 additions & 0 deletions internal/repositories/github/parser_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,54 @@ func (suite *ParserPublicTestSuite) TestParse() {
},
wantErr: false,
},
// https://github.com/coala/git-url-parse/issues/29
{
input: "https://github.com/sphinx-doc/sphinx.git",
want: &repository{
protocol: "https",
protocols: []string{"https"},
resource: "github.com",
owner: "sphinx-doc",
repo: "sphinx",
path: "",
branch: "",
provider: "github",
href: "https://github.com/sphinx-doc/sphinx.git",
},
wantErr: false,
},
// https://github.com/retr0h/git-url-parse/issues/33
{
input: "https://github.com/tterranigma/Stouts.openvpn",
want: &repository{
protocol: "https",
protocols: []string{"https"},
resource: "github.com",
owner: "tterranigma",
repo: "Stouts.openvpn",
path: "",
branch: "",
provider: "github",
href: "https://github.com/tterranigma/Stouts.openvpn",
},
wantErr: false,
},
// https://github.com/retr0h/git-url-parse/issues/33
{
input: "https://github.com/tterranigma/Stouts.openvpn.git",
want: &repository{
protocol: "https",
protocols: []string{"https"},
resource: "github.com",
owner: "tterranigma",
repo: "Stouts.openvpn",
path: "",
branch: "",
provider: "github",
href: "https://github.com/tterranigma/Stouts.openvpn.git",
},
wantErr: false,
},
{
input: "https://github.com/owner/repository/blob/main/files/file0.json",
want: &repository{
Expand Down
12 changes: 3 additions & 9 deletions internal/repositories/gitlab/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ var patterns = []string{

// Parse the provided GitLab URL.
func (gh *GitLab) Parse(url string) (*api.Repository, error) {
response := &api.Repository{}

for _, pattern := range patterns {
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(url)
Expand All @@ -60,7 +58,7 @@ func (gh *GitLab) Parse(url string) (*api.Repository, error) {
)

if matches != nil {
response = &api.Repository{
return &api.Repository{
Protocol: mm["scheme"],
Host: mm["resource"],
Provider: providerName,
Expand All @@ -70,15 +68,11 @@ func (gh *GitLab) Parse(url string) (*api.Repository, error) {
Path: mm["path"],
Branch: mm["branch"],
HREF: url,
}
}, nil
}
}

if (api.Repository{}) == *response {
return nil, fmt.Errorf("could match url: %s to any pattern", url)
}

return response, nil
return nil, fmt.Errorf("could match url: %s to any pattern", url)
}

func makeMatchMap(re *regexp.Regexp, matches []string) map[string]string {
Expand Down
47 changes: 0 additions & 47 deletions python/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,53 +268,6 @@ def first_match_urls():
'name': 'repo',
'owner': 'owner',
},
# https://github.com/retr0h/git-url-parse/issues/29
'https://github.com/sphinx-doc/sphinx.git': {
'pathname': '/sphinx-doc/sphinx.git',
'protocols': ['https'],
'protocol': 'https',
'href': 'https://github.com/sphinx-doc/sphinx.git',
'resource': 'github.com',
'user': None,
'port': None,
'name': 'sphinx',
'owner': 'sphinx-doc',
},
# https://github.com/retr0h/git-url-parse/issues/33
'https://github.com/tterranigma/Stouts.openvpn': {
'pathname': '/tterranigma/Stouts.openvpn',
'protocols': ['https'],
'protocol': 'https',
'href': 'https://github.com/tterranigma/Stouts.openvpn',
'resource': 'github.com',
'user': None,
'port': None,
'name': 'Stouts.openvpn',
'owner': 'tterranigma',
},
'https://github.com/tterranigma/Stouts.openvpn/': {
'pathname': '/tterranigma/Stouts.openvpn/',
'protocols': ['https'],
'protocol': 'https',
'href': 'https://github.com/tterranigma/Stouts.openvpn/',
'resource': 'github.com',
'user': None,
'port': None,
'name': 'Stouts.openvpn',
'owner': 'tterranigma',
},
# https://github.com/retr0h/git-url-parse/issues/33
'https://github.com/tterranigma/Stouts.openvpn.git': {
'pathname': '/tterranigma/Stouts.openvpn.git',
'protocols': ['https'],
'protocol': 'https',
'href': 'https://github.com/tterranigma/Stouts.openvpn.git',
'resource': 'github.com',
'user': None,
'port': None,
'name': 'Stouts.openvpn',
'owner': 'tterranigma',
},
}


Expand Down

0 comments on commit d5d608f

Please sign in to comment.