diff --git a/cli/cmd/digger/default.go b/cli/cmd/digger/default.go index 2ee3c787f..74eeebb6b 100644 --- a/cli/cmd/digger/default.go +++ b/cli/cmd/digger/default.go @@ -7,6 +7,7 @@ import ( "github.com/diggerhq/digger/cli/pkg/github" "github.com/diggerhq/digger/cli/pkg/usage" comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary" + dg_github "github.com/diggerhq/digger/libs/orchestrator/github" "github.com/spf13/cobra" "log" "os" @@ -22,7 +23,7 @@ var defaultCmd = &cobra.Command{ switch ci { case digger.GitHub: logLeader = os.Getenv("GITHUB_ACTOR") - github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, comment_updater.CommentUpdaterProviderBasic{}, drift.DriftNotificationProviderBasic{}) + github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, dg_github.GithubServiceProviderBasic{}, comment_updater.CommentUpdaterProviderBasic{}, drift.DriftNotificationProviderBasic{}) case digger.None: print("No CI detected.") os.Exit(10) diff --git a/cli/cmd/digger/root.go b/cli/cmd/digger/root.go index c3b63c48d..59110fec9 100644 --- a/cli/cmd/digger/root.go +++ b/cli/cmd/digger/root.go @@ -36,8 +36,8 @@ func (r *RunConfig) GetServices() (*orchestrator.PullRequestService, *orchestrat switch r.Reporter { case "github": repoOwner, repositoryName := utils.ParseRepoNamespace(r.RepoNamespace) - prService = orchestrator_github.NewGitHubService(r.GithubToken, repositoryName, repoOwner) - orgService = orchestrator_github.NewGitHubService(r.GithubToken, r.RepoNamespace, r.Actor) + prService = orchestrator_github.GithubServiceProviderBasic{}.NewService(r.GithubToken, repositoryName, repoOwner) + orgService = orchestrator_github.GithubServiceProviderBasic{}.NewService(r.GithubToken, r.RepoNamespace, r.Actor) reporter = &reporting.CiReporter{ CiService: prService, ReportStrategy: ReportStrategy, diff --git a/cli/pkg/github/github.go b/cli/pkg/github/github.go index 0f9c0c067..99b6243bf 100644 --- a/cli/pkg/github/github.go +++ b/cli/pkg/github/github.go @@ -31,7 +31,7 @@ import ( "time" ) -func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backendApi core_backend.Api, reportingStrategy reporting.ReportStrategy, commentUpdaterProvider comment_updater.CommentUpdaterProvider, driftNotifcationProvider drift.DriftNotificationProvider) { +func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backendApi core_backend.Api, reportingStrategy reporting.ReportStrategy, githubServiceProvider dg_github.GithubServiceProvider, commentUpdaterProvider comment_updater.CommentUpdaterProvider, driftNotifcationProvider drift.DriftNotificationProvider) { log.Printf("Using GitHub.\n") githubActor := os.Getenv("GITHUB_ACTOR") if githubActor != "" { @@ -80,7 +80,7 @@ func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backend } repoOwner, repositoryName := utils.ParseRepoNamespace(ghRepository) - githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner) + githubPrService := githubServiceProvider.NewService(ghToken, repositoryName, repoOwner) currentDir, err := os.Getwd() if err != nil { @@ -139,7 +139,7 @@ func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backend if err != nil { usage.ReportErrorAndExit(githubActor, fmt.Sprintf("could not get changed files: %v", err), 4) } - + diggerConfig, _, _, err := digger_config.LoadDiggerConfig("./", false, files) if err != nil { usage.ReportErrorAndExit(githubActor, fmt.Sprintf("Failed to read Digger digger_config. %s", err), 4) diff --git a/cli/pkg/integration/integration_test.go b/cli/pkg/integration/integration_test.go index 740ac7ecb..dea71271c 100644 --- a/cli/pkg/integration/integration_test.go +++ b/cli/pkg/integration/integration_test.go @@ -43,7 +43,7 @@ func getProjectLockForTests() (error, *locking.PullRequestLock) { repoOwner := "diggerhq" repositoryName := "test_dynamodb_lock" ghToken := "token" - githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner) + githubPrService := dg_github.GithubServiceProviderBasic{}.NewService(ghToken, repositoryName, repoOwner) reporter := reporting.CiReporter{ CiService: &githubPrService, PrNumber: 1, @@ -388,7 +388,7 @@ func TestHappyPath(t *testing.T) { ghEvent := parsedNewPullRequestContext.Event repoOwner := parsedNewPullRequestContext.RepositoryOwner repositoryName := parsedNewPullRequestContext.Repository - githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner) + githubPrService := dg_github.GithubServiceProviderBasic{}.NewService(ghToken, repositoryName, repoOwner) assert.Equal(t, "pull_request", parsedNewPullRequestContext.EventName) @@ -545,7 +545,7 @@ func TestMultiEnvHappyPath(t *testing.T) { repoOwner := parsedNewPullRequestContext.RepositoryOwner repositoryName := parsedNewPullRequestContext.Repository diggerProjectNamespace := repoOwner + "/" + repositoryName - githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner) + githubPrService := dg_github.GithubServiceProviderBasic{}.NewService(ghToken, repositoryName, repoOwner) assert.Equal(t, "pull_request", parsedNewPullRequestContext.EventName) @@ -765,7 +765,7 @@ workflows: ghEvent := parsedNewPullRequestContext.Event repoOwner := parsedNewPullRequestContext.RepositoryOwner repositoryName := parsedNewPullRequestContext.Repository - githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner) + githubPrService := dg_github.GithubServiceProviderBasic{}.NewService(ghToken, repositoryName, repoOwner) diggerProjectNamespace := repoOwner + "/" + repositoryName assert.Equal(t, "pull_request", parsedNewPullRequestContext.EventName) diff --git a/ee/cli/cmd/digger/default.go b/ee/cli/cmd/digger/default.go index bbd5294f6..03a44f059 100644 --- a/ee/cli/cmd/digger/default.go +++ b/ee/cli/cmd/digger/default.go @@ -7,6 +7,7 @@ import ( "github.com/diggerhq/digger/cli/pkg/usage" "github.com/diggerhq/digger/ee/cli/pkg/comment_updater" "github.com/diggerhq/digger/ee/cli/pkg/drift" + github2 "github.com/diggerhq/digger/ee/cli/pkg/github" "github.com/spf13/cobra" "log" "os" @@ -22,7 +23,7 @@ var defaultCmd = &cobra.Command{ switch ci { case digger.GitHub: logLeader = os.Getenv("GITHUB_ACTOR") - github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, comment_updater.CommentUpdaterProviderAdvanced{}, drift.DriftNotificationProviderAdvanced{}) + github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, github2.GithubServiceProviderAdvanced{}, comment_updater.CommentUpdaterProviderAdvanced{}, drift.DriftNotificationProviderAdvanced{}) case digger.None: print("No CI detected.") os.Exit(10) diff --git a/ee/cli/cmd/digger/root.go b/ee/cli/cmd/digger/root.go index bf3d3704d..5549e6c7c 100644 --- a/ee/cli/cmd/digger/root.go +++ b/ee/cli/cmd/digger/root.go @@ -36,8 +36,8 @@ func (r *RunConfig) GetServices() (*orchestrator.PullRequestService, *orchestrat switch r.Reporter { case "github": repoOwner, repositoryName := utils.ParseRepoNamespace(r.RepoNamespace) - prService = orchestrator_github.NewGitHubService(r.GithubToken, repositoryName, repoOwner) - orgService = orchestrator_github.NewGitHubService(r.GithubToken, r.RepoNamespace, r.Actor) + prService = orchestrator_github.GithubServiceProviderBasic{}.NewService(r.GithubToken, repositoryName, repoOwner) + orgService = orchestrator_github.GithubServiceProviderBasic{}.NewService(r.GithubToken, r.RepoNamespace, r.Actor) reporter = &reporting.CiReporter{ CiService: prService, ReportStrategy: ReportStrategy, diff --git a/ee/cli/pkg/github/providers.go b/ee/cli/pkg/github/providers.go new file mode 100644 index 000000000..985d48e5f --- /dev/null +++ b/ee/cli/pkg/github/providers.go @@ -0,0 +1,32 @@ +package github + +import ( + "fmt" + dg_github "github.com/diggerhq/digger/libs/orchestrator/github" + "github.com/google/go-github/v61/github" + "log" + "os" +) + +type GithubServiceProviderAdvanced struct{} + +func (_ GithubServiceProviderAdvanced) NewService(ghToken string, repoName string, owner string) dg_github.GithubService { + client := github.NewClient(nil) + if ghToken != "" { + client = client.WithAuthToken(ghToken) + } + + githubHostname := os.Getenv("DIGGER_GITHUB_HOSTNAME") + if githubHostname != "" { + log.Printf("info: using github hostname: %v", githubHostname) + githubEnterpriseBaseUrl := fmt.Sprintf("https://%v/api/v3/", githubHostname) + githubEnterpriseUploadUrl := fmt.Sprintf("https://%v/api/uploads/", githubHostname) + client.WithEnterpriseURLs(githubEnterpriseBaseUrl, githubEnterpriseUploadUrl) + } + + return dg_github.GithubService{ + Client: client, + RepoName: repoName, + Owner: owner, + } +} diff --git a/libs/orchestrator/github/github.go b/libs/orchestrator/github/github.go index 533953e45..a4cc020da 100644 --- a/libs/orchestrator/github/github.go +++ b/libs/orchestrator/github/github.go @@ -14,7 +14,13 @@ import ( "github.com/google/go-github/v61/github" ) -func NewGitHubService(ghToken string, repoName string, owner string) GithubService { +type GithubServiceProvider interface { + NewService(ghToken string, repoName string, owner string) GithubService +} + +type GithubServiceProviderBasic struct{} + +func (_ GithubServiceProviderBasic) NewService(ghToken string, repoName string, owner string) GithubService { client := github.NewClient(nil) if ghToken != "" { client = client.WithAuthToken(ghToken) diff --git a/libs/orchestrator/github/github_test.go b/libs/orchestrator/github/github_test.go index f118fb673..db6dcb9a4 100644 --- a/libs/orchestrator/github/github_test.go +++ b/libs/orchestrator/github/github_test.go @@ -115,7 +115,7 @@ func TestFindAllProjectsDependantOnImpactedProjects(t *testing.T) { } func TestFindAllChangedFilesOfPR(t *testing.T) { - githubPrService := NewGitHubService("", "digger", "diggerhq") + githubPrService := GithubServiceProviderBasic{}.NewService("", "digger", "diggerhq") files, _ := githubPrService.GetChangedFiles(98) // 45 changed files including 1 renamed file so the previous filename is included assert.Equal(t, 46, len(files)) diff --git a/libs/spec/providers.go b/libs/spec/providers.go index 11867e8e8..89a47c0be 100644 --- a/libs/spec/providers.go +++ b/libs/spec/providers.go @@ -175,7 +175,7 @@ func (v VCSProvider) GetPrService(vcsSpec VcsSpec) (orchestrator.PullRequestServ if token == "" { return nil, fmt.Errorf("failed to get githbu service: GITHUB_TOKEN not specified") } - return github.NewGitHubService(token, vcsSpec.RepoName, vcsSpec.RepoOwner), nil + return github.GithubServiceProviderBasic{}.NewService(token, vcsSpec.RepoName, vcsSpec.RepoOwner), nil default: return nil, fmt.Errorf("could not get PRService, unknown type %v", vcsSpec.VcsType) }