From d5d608f46eb6b4212624eaf9922d2052a7adf14b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=A0=CF=85=CE=B1=CE=B7=20=D7=A0=CF=85=CE=B1=CE=B7=D1=95?= =?UTF-8?q?=CF=83=CE=B7?= Date: Sat, 22 Jun 2024 22:24:04 -0700 Subject: [PATCH] feat: added additional GH tests Added additional GH tests from Coala's git-url-parse. Also, quick return from parser when a regexp match is found. --- internal/repositories/github/parser.go | 14 ++---- .../repositories/github/parser_public_test.go | 48 +++++++++++++++++++ internal/repositories/gitlab/parser.go | 12 ++--- python/test/conftest.py | 47 ------------------ 4 files changed, 55 insertions(+), 66 deletions(-) diff --git a/internal/repositories/github/parser.go b/internal/repositories/github/parser.go index af23972..bef6d7f 100644 --- a/internal/repositories/github/parser.go +++ b/internal/repositories/github/parser.go @@ -34,15 +34,13 @@ const ( // ChatGPT-4 generated regexp var patterns = []string{ - `^(?Phttps?)://(?P[^/]+)/(?P[^/]+)/(?P[^/]+)(/(?:tree|blob)/(?P[^/]+)/(?P.*)?)?$`, + `^(?Phttps?)://(?P[^/]+)/(?P[^/]+)/(?P[^/]+?)(?:\.git)?(/(?:tree|blob)/(?P[^/]+)/(?P.*)?)?$`, `^(?Phttps?)://(?Praw\.githubusercontent\.com)/(?P[^/]+)/(?P[^/]+)/(?P[^/]+)/(?P.*)$`, `^(?Pgit)@(?Pgithub\.com):(?P[^/]+)/(?P[^/]+)\.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) @@ -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, @@ -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 { diff --git a/internal/repositories/github/parser_public_test.go b/internal/repositories/github/parser_public_test.go index 6c9ce92..08a700d 100644 --- a/internal/repositories/github/parser_public_test.go +++ b/internal/repositories/github/parser_public_test.go @@ -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{ diff --git a/internal/repositories/gitlab/parser.go b/internal/repositories/gitlab/parser.go index 5ab149b..bc301e5 100644 --- a/internal/repositories/gitlab/parser.go +++ b/internal/repositories/gitlab/parser.go @@ -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) @@ -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, @@ -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 { diff --git a/python/test/conftest.py b/python/test/conftest.py index 0315071..d79b16e 100644 --- a/python/test/conftest.py +++ b/python/test/conftest.py @@ -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', - }, }