From 83d8a17ffb953ab6992714a0f0b827b1c29b6e2d Mon Sep 17 00:00:00 2001 From: Kent Rancourt Date: Tue, 11 Jun 2024 16:37:17 -0400 Subject: [PATCH 1/2] support parsing github enterprise urls Signed-off-by: Kent Rancourt --- internal/gitprovider/github/github.go | 18 +++++--- internal/gitprovider/github/github_test.go | 54 ++++++++++++++++++++++ 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 internal/gitprovider/github/github_test.go diff --git a/internal/gitprovider/github/github.go b/internal/gitprovider/github/github.go index c71bc07a7..ea8dda5e2 100644 --- a/internal/gitprovider/github/github.go +++ b/internal/gitprovider/github/github.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/url" - "regexp" "strings" "github.com/google/go-github/v56/github" @@ -146,13 +145,18 @@ func convertGithubPR(ghPR *github.PullRequest) *gitprovider.PullRequest { } } -func parseGitHubURL(u string) (string, string, error) { - regex := regexp.MustCompile(`^https\://github\.com/([\w-]+)/([\w-]+).*`) - parts := regex.FindStringSubmatch(u) - if len(parts) != 3 { - return "", "", fmt.Errorf("error parsing github repository URL %q", u) +func parseGitHubURL(repoURL string) (string, string, error) { + u, err := url.Parse(repoURL) + if err != nil { + return "", "", fmt.Errorf("error parsing github repository URL %q: %w", u, err) + } + path := strings.TrimPrefix(u.Path, "/") + path = strings.TrimSuffix(path, ".git") + parts := strings.Split(path, "/") + if len(parts) != 2 { + return "", "", fmt.Errorf("could not parse github repository URL %q", u) } - return parts[1], parts[2], nil + return parts[0], parts[1], nil } func (g *GitHubProvider) IsPullRequestMerged(ctx context.Context, repoURL string, id int64) (bool, error) { diff --git a/internal/gitprovider/github/github_test.go b/internal/gitprovider/github/github_test.go new file mode 100644 index 000000000..487fa1475 --- /dev/null +++ b/internal/gitprovider/github/github_test.go @@ -0,0 +1,54 @@ +package github + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestParseGitHubURL(t *testing.T) { + testCases := []struct { + url string + expectedOwner string + expectedRepo string + errExpected bool + }{ + { + url: "not-a-url", + errExpected: true, + }, + { + url: "https://github.com/akuity", + errExpected: true, + }, + { + url: "https://github.com/akuity/kargo", + expectedOwner: "akuity", + expectedRepo: "kargo", + }, + { + url: "https://github.com/akuity/kargo.git", + expectedOwner: "akuity", + expectedRepo: "kargo", + }, + { + // This isn't a real URL. It's just to validate that the function can + // handle GitHub Enterprise URLs. + url: "https://github.akuity.io/akuity/kargo.git", + expectedOwner: "akuity", + expectedRepo: "kargo", + }, + } + for _, testCase := range testCases { + t.Run(testCase.url, func(t *testing.T) { + owner, repo, err := parseGitHubURL(testCase.url) + if testCase.errExpected { + require.Error(t, err) + } else { + require.NoError(t, err) + require.Equal(t, testCase.expectedOwner, owner) + require.Equal(t, testCase.expectedRepo, repo) + } + }) + } +} From 7ace2156e3e2aeff74320702ef44781fd58359e2 Mon Sep 17 00:00:00 2001 From: Kent Rancourt Date: Tue, 11 Jun 2024 17:44:22 -0400 Subject: [PATCH 2/2] Update internal/gitprovider/github/github.go Signed-off-by: Kent Rancourt Co-authored-by: Hidde Beydals --- internal/gitprovider/github/github.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/gitprovider/github/github.go b/internal/gitprovider/github/github.go index ea8dda5e2..c53b1c68d 100644 --- a/internal/gitprovider/github/github.go +++ b/internal/gitprovider/github/github.go @@ -154,7 +154,7 @@ func parseGitHubURL(repoURL string) (string, string, error) { path = strings.TrimSuffix(path, ".git") parts := strings.Split(path, "/") if len(parts) != 2 { - return "", "", fmt.Errorf("could not parse github repository URL %q", u) + return "", "", fmt.Errorf("could not extract repository owner and name from URL %q", u) } return parts[0], parts[1], nil }