Skip to content

Commit

Permalink
fix: regex support for http credentials embedded in remote url
Browse files Browse the repository at this point in the history
Introduce a modification to the regular expression for repository remote
url parsing.

Remove parser failure with credentials embedded into http remote urls.
  • Loading branch information
niderhoff committed Sep 14, 2024
1 parent f1faf60 commit 6e0ab46
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/app/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewGitData(remote string, g GitManager) (GitData, error) {
https://[email protected]/namespace/subnamespace/dummy-test-repo.git
git@[email protected]:namespace/subnamespace/dummy-test-repo.git
*/
re := regexp.MustCompile(`(?:^https?:\/\/|^ssh:\/\/|^git@)(?:[^\/:]+)(?::\d+)?[\/:](.*)\/([^\/]+?)(?:\.git)?$`)
re := regexp.MustCompile(`^(?:git@[^\/:]*|https?:\/\/[^\/]+|ssh:\/\/[^\/:]+)(?::\d+)?[\/:](.*)\/([^\/]+?)(?:\.git)?\/?$`)
matches := re.FindStringSubmatch(url)
if len(matches) != 3 {
return GitData{}, fmt.Errorf("Invalid Git URL format: %s", url)
Expand Down
28 changes: 28 additions & 0 deletions cmd/app/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,55 @@ func TestExtractGitInfo_Success(t *testing.T) {
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in HTTP and under a single folder without .git extension (with embedded credentials)",
remote: "http://username:[email protected]/namespace-1/project-name",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in HTTPS and under a single folder",
remote: "https://custom-gitlab.com/namespace-1/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in HTTPS and under a single folder (with embedded credentials)",
remote: "https://username:[email protected]/namespace-1/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in HTTPS and under a nested folder",
remote: "https://custom-gitlab.com/namespace-1/namespace-2/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1/namespace-2",
},
{
desc: "Project configured in HTTPS and under a nested folder (with embedded credentials)",
remote: "https://username:[email protected]/namespace-1/namespace-2/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1/namespace-2",
},
{
desc: "Project configured in HTTPS and under two nested folders",
remote: "https://custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1/namespace-2/namespace-3",
},
{
desc: "Project configured in HTTPS and under two nested folders (with embedded credentials)",
remote: "https://username:[email protected]/namespace-1/namespace-2/namespace-3/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1/namespace-2/namespace-3",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
Expand Down

0 comments on commit 6e0ab46

Please sign in to comment.