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

Support repository and deployment URL of GHES #878

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pkg/github/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Deployment struct {
Id int64
}

var patternDeploymentURL = regexp.MustCompile(`^https://api\.github\.com/repos/(.+?)/(.+?)/deployments/(\d+)$`)
var patternDeploymentURL = regexp.MustCompile(`^https://[^/]+?/repos/([^/]+?)/([^/]+?)/deployments/(\d+)$`)

// ParseDeploymentURL parses the URL.
// For example, https://api.github.com/repos/int128/sandbox/deployments/422988781
Expand Down
15 changes: 14 additions & 1 deletion pkg/github/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package github
import "testing"

func TestParseDeploymentURL(t *testing.T) {
t.Run("valid", func(t *testing.T) {
t.Run("valid deployment of GitHub", func(t *testing.T) {
d := ParseDeploymentURL("https://api.github.com/repos/int128/sandbox/deployments/422988781")
if d == nil {
t.Fatalf("deployment was nil")
Expand All @@ -16,6 +16,19 @@ func TestParseDeploymentURL(t *testing.T) {
}
})

t.Run("valid deployment of GitHub Enterprise Server", func(t *testing.T) {
d := ParseDeploymentURL("https://api.ghes.example.com/repos/int128/sandbox/deployments/422988781")
if d == nil {
t.Fatalf("deployment was nil")
}
if want := (Repository{Owner: "int128", Name: "sandbox"}); d.Repository != want {
t.Errorf("want %+v but was %+v", want, d.Repository)
}
if d.Id != 422988781 {
t.Errorf("want %d but was %d", 422988781, d.Id)
}
})

t.Run("not deployment", func(t *testing.T) {
d := ParseDeploymentURL("https://api.github.com/repos/int128/sandbox")
if d != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/github/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Repository struct {
}

var (
patternRepositoryHTTPS = regexp.MustCompile(`^https://github\.com/([^/]+?)/([^/]+?)(\.git)?$`)
patternRepositorySSH = regexp.MustCompile(`^git@github\.com:([^/]+?)/([^/]+?)(\.git)?$`)
patternRepositoryHTTPS = regexp.MustCompile(`^https://[^/]+?/([^/]+?)/([^/]+?)(\.git)?$`)
patternRepositorySSH = regexp.MustCompile(`^git@[^:]+?:([^/]+?)/([^/]+?)(\.git)?$`)
)

func ParseRepositoryURL(s string) *Repository {
Expand Down
41 changes: 32 additions & 9 deletions pkg/github/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package github
import "testing"

func TestParseRepositoryURL(t *testing.T) {
t.Run("valid HTTPS", func(t *testing.T) {
t.Run("valid HTTPS of GitHub", func(t *testing.T) {
r := ParseRepositoryURL("https://github.com/int128/sandbox")
if r == nil {
t.Fatalf("repository was nil")
Expand All @@ -13,7 +13,7 @@ func TestParseRepositoryURL(t *testing.T) {
}
})

t.Run("HTTPS with .git", func(t *testing.T) {
t.Run("valid HTTPS of GitHub with .git", func(t *testing.T) {
r := ParseRepositoryURL("https://github.com/int128/sandbox.git")
if r == nil {
t.Fatalf("repository was nil")
Expand All @@ -23,15 +23,35 @@ func TestParseRepositoryURL(t *testing.T) {
}
})

t.Run("HTTPS but not repository", func(t *testing.T) {
r := ParseRepositoryURL("https://github.com/int128/sandbox/commits")
t.Run("valid HTTPS of GitHub Enterprise Server", func(t *testing.T) {
r := ParseRepositoryURL("https://ghes.example.com/int128/sandbox")
if r == nil {
t.Fatalf("repository was nil")
}
if want := (Repository{Owner: "int128", Name: "sandbox"}); *r != want {
t.Errorf("want %+v but was %+v", &want, r)
}
})

t.Run("valid HTTPS of GitHub Enterprise Server with .git", func(t *testing.T) {
r := ParseRepositoryURL("https://ghes.example.com/int128/sandbox.git")
if r == nil {
t.Fatalf("repository was nil")
}
if want := (Repository{Owner: "int128", Name: "sandbox"}); *r != want {
t.Errorf("want %+v but was %+v", &want, r)
}
})

t.Run("invalid HTTPS", func(t *testing.T) {
r := ParseRepositoryURL("https://example.com")
if r != nil {
t.Errorf("want nil but was %+v", r)
}
})

// https://github.com/argoproj/argo-cd/blob/master/docs/user-guide/private-repositories.md
t.Run("valid SSH", func(t *testing.T) {
t.Run("valid SSH of GitHub", func(t *testing.T) {
r := ParseRepositoryURL("git@github.com:argoproj/argocd-example-apps.git")
if r == nil {
t.Fatalf("repository was nil")
Expand All @@ -41,10 +61,13 @@ func TestParseRepositoryURL(t *testing.T) {
}
})

t.Run("SSH but not GitHub", func(t *testing.T) {
r := ParseRepositoryURL("git@example.com:argoproj/argocd-example-apps.git")
if r != nil {
t.Errorf("want nil but was %+v", r)
t.Run("valid SSH of GitHub Enterprise Server", func(t *testing.T) {
r := ParseRepositoryURL("git@ghes.example.com:argoproj/argocd-example-apps.git")
if r == nil {
t.Fatalf("repository was nil")
}
if want := (Repository{Owner: "argoproj", Name: "argocd-example-apps"}); *r != want {
t.Errorf("want %+v but was %+v", &want, r)
}
})

Expand Down