Skip to content

Commit

Permalink
chore(backport release-0.7): fix(controller): support parsing github …
Browse files Browse the repository at this point in the history
…enterprise urls (#2148)

Co-authored-by: Kent Rancourt <[email protected]>
  • Loading branch information
akuitybot and krancour authored Jun 11, 2024
1 parent 0f2f4d5 commit fd6816c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
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 @@ import (
"context"
"fmt"
"net/url"
"regexp"
"strings"

"github.com/google/go-github/v56/github"
Expand Down Expand Up @@ -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 extract repository owner and name from 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) {
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)
}
})
}
}

0 comments on commit fd6816c

Please sign in to comment.