Skip to content

Commit

Permalink
feat: added git github URL parsing to internal pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h committed Jun 20, 2024
1 parent a64117f commit 00fd5f7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/github/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
var patterns = []string{
`^(?P<scheme>https?)://(?P<resource>[^/]+)/(?P<owner>[^/]+)/(?P<repo>[^/]+)(/(?: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$`,
}

// New factory to create a new GitHub instance.
Expand Down
37 changes: 33 additions & 4 deletions internal/github/parser_public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,34 @@ func (suite *GitHubPublicTestSuite) TestParseOk() {
href: "https://raw.githubusercontent.com/retr0h/git-url-parse/main/files/file0.json",
},
},
{
input: "https://www.github.com/retr0h/git-url-parse",
want: &url{
protocol: "https",
resource: "www.github.com",
owner: "retr0h",
repo: "git-url-parse",
path: "",
branch: "",
provider: "github",
href: "https://www.github.com/retr0h/git-url-parse",
},
},
{
input: "[email protected]:retr0h/git-url-parse.git",
want: &url{
protocol: "git",
resource: "github.com",
owner: "retr0h",
repo: "git-url-parse",
path: "",
branch: "",
provider: "github",
href: "[email protected]:retr0h/git-url-parse.git",
},
},
}

// "[email protected]:retr0h/git-url-parse.git"
// "[email protected]:foobar/retr0h/git-url-parse.git"
// "https://www.github.com/retr0h/git-url-parse"

for _, tc := range tests {
var got internal.RepositoryURLManager
got, err := suite.rm.Parse(tc.input)
Expand Down Expand Up @@ -154,6 +176,13 @@ func (suite *GitHubPublicTestSuite) TestParseFails() {
input: "bogus://url/",
want: fmt.Sprintf("could match url: %s to any pattern", "bogus://url/"),
},
{
input: "[email protected]:foobar/retr0h/git-url-parse.git",
want: fmt.Sprintf(
"could match url: %s to any pattern",
"[email protected]:foobar/retr0h/git-url-parse.git",
),
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 00fd5f7

Please sign in to comment.