Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(controller): support parsing github enterprise urls #2145

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions internal/gitprovider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"context"
"fmt"
"net/url"
"regexp"
"strings"

"github.com/google/go-github/v56/github"
Expand Down Expand Up @@ -146,13 +145,18 @@
}
}

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)

Check warning on line 151 in internal/gitprovider/github/github.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/github/github.go#L151

Added line #L151 was not covered by tests
}
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)
krancour marked this conversation as resolved.
Show resolved Hide resolved
}
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) {
Expand Down
54 changes: 54 additions & 0 deletions internal/gitprovider/github/github_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading