Skip to content

Commit

Permalink
feat: add github enterprise support (#80)
Browse files Browse the repository at this point in the history
* add github enterprise support

Signed-off-by: Zach Aller <[email protected]>

* add github enterprise support

Signed-off-by: Zach Aller <[email protected]>

---------

Signed-off-by: Zach Aller <[email protected]>
  • Loading branch information
zachaller authored Oct 24, 2024
1 parent 6bace8f commit 76f0367
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 30 deletions.
2 changes: 1 addition & 1 deletion internal/controller/commitstatus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (r *CommitStatusReconciler) getCommitStatusProvider(ctx context.Context, co

switch {
case scmProvider.Spec.GitHub != nil:
return github.NewGithubCommitStatusProvider(r.Client, *secret)
return github.NewGithubCommitStatusProvider(r.Client, *secret, scmProvider.Spec.GitHub.Domain)
case scmProvider.Spec.Fake != nil:
return fake.NewFakeCommitStatusProvider(*secret)
default:
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/pullrequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (r *PullRequestReconciler) getPullRequestProvider(ctx context.Context, pr p

switch {
case scmProvider.Spec.GitHub != nil:
return github.NewGithubPullRequestProvider(r.Client, *secret)
return github.NewGithubPullRequestProvider(r.Client, *secret, scmProvider.Spec.GitHub.Domain)
case scmProvider.Spec.Fake != nil:
return fake.NewFakePullRequestProvider(r.Client), nil
default:
Expand Down
4 changes: 2 additions & 2 deletions internal/scms/github/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type CommitStatus struct {

var _ scms.CommitStatusProvider = &CommitStatus{}

func NewGithubCommitStatusProvider(k8sClient client.Client, secret v1.Secret) (*CommitStatus, error) {
client, err := GetClient(secret)
func NewGithubCommitStatusProvider(k8sClient client.Client, secret v1.Secret, domain string) (*CommitStatus, error) {
client, err := GetClient(secret, domain)
if err != nil {
return nil, err
}
Expand Down
38 changes: 14 additions & 24 deletions internal/scms/github/git_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (gh GitAuthenticationProvider) GetUser(ctx context.Context) (string, error)
return "git", nil
}

func GetClient(secret v1.Secret) (*github.Client, error) {
func GetClient(secret v1.Secret, domain string) (*github.Client, error) {

appID, err := strconv.ParseInt(string(secret.Data["appID"]), 10, 64)
if err != nil {
Expand All @@ -74,28 +74,18 @@ func GetClient(secret v1.Secret) (*github.Client, error) {
if err != nil {
return nil, err
}
client := github.NewClient(&http.Client{Transport: itr})

var client *github.Client
if domain == "" || domain == "github.com" {
client = github.NewClient(&http.Client{Transport: itr})
} else {
baseURL := fmt.Sprintf("https://%s/api/v3", domain)
uploadsURL := fmt.Sprintf("https://%s/api/uploads", domain)
client, err = github.NewClient(&http.Client{Transport: itr}).WithEnterpriseURLs(baseURL, uploadsURL)
if err != nil {
return nil, err
}
}

return client, nil
}

//func GetEnterpriseClient(secret v1.Secret) (*github.Client, error) {
//
// appID, err := strconv.ParseInt(string(secret.Data["appID"]), 10, 64)
// if err != nil {
// panic(err)
// }
//
// installationID, err := strconv.ParseInt(string(secret.Data["installationID"]), 10, 64)
// if err != nil {
// panic(err)
// }
//
// itr, _ := ghinstallation.New(http.DefaultTransport, appID, installationID, secret.Data["privateKey"])
//
// client, err := github.NewClient(&http.Client{Transport: itr}).WithEnterpriseURLs("https://github.example.com/api/v3", "https://github.example.com/api/v3")
// if err != nil {
// return nil, err
// }
//
// return client, nil
//}
4 changes: 2 additions & 2 deletions internal/scms/github/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type PullRequest struct {

var _ scms.PullRequestProvider = &PullRequest{}

func NewGithubPullRequestProvider(k8sClient client.Client, secret v1.Secret) (*PullRequest, error) {
client, err := GetClient(secret)
func NewGithubPullRequestProvider(k8sClient client.Client, secret v1.Secret, domain string) (*PullRequest, error) {
client, err := GetClient(secret, domain)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 76f0367

Please sign in to comment.