Skip to content

Commit

Permalink
support private vcs in cli (#1583)
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Jun 19, 2024
1 parent 83dc11f commit ba2565b
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 16 deletions.
3 changes: 2 additions & 1 deletion cli/cmd/digger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cli/cmd/digger/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions cli/pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions cli/pkg/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion ee/cli/cmd/digger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions ee/cli/cmd/digger/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions ee/cli/pkg/github/providers.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
8 changes: 7 additions & 1 deletion libs/orchestrator/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion libs/orchestrator/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion libs/spec/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit ba2565b

Please sign in to comment.