Skip to content

Commit ba2565b

Browse files
authored
support private vcs in cli (#1583)
1 parent 83dc11f commit ba2565b

File tree

10 files changed

+56
-16
lines changed

10 files changed

+56
-16
lines changed

cli/cmd/digger/default.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/diggerhq/digger/cli/pkg/github"
88
"github.com/diggerhq/digger/cli/pkg/usage"
99
comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary"
10+
dg_github "github.com/diggerhq/digger/libs/orchestrator/github"
1011
"github.com/spf13/cobra"
1112
"log"
1213
"os"
@@ -22,7 +23,7 @@ var defaultCmd = &cobra.Command{
2223
switch ci {
2324
case digger.GitHub:
2425
logLeader = os.Getenv("GITHUB_ACTOR")
25-
github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, comment_updater.CommentUpdaterProviderBasic{}, drift.DriftNotificationProviderBasic{})
26+
github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, dg_github.GithubServiceProviderBasic{}, comment_updater.CommentUpdaterProviderBasic{}, drift.DriftNotificationProviderBasic{})
2627
case digger.None:
2728
print("No CI detected.")
2829
os.Exit(10)

cli/cmd/digger/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (r *RunConfig) GetServices() (*orchestrator.PullRequestService, *orchestrat
3636
switch r.Reporter {
3737
case "github":
3838
repoOwner, repositoryName := utils.ParseRepoNamespace(r.RepoNamespace)
39-
prService = orchestrator_github.NewGitHubService(r.GithubToken, repositoryName, repoOwner)
40-
orgService = orchestrator_github.NewGitHubService(r.GithubToken, r.RepoNamespace, r.Actor)
39+
prService = orchestrator_github.GithubServiceProviderBasic{}.NewService(r.GithubToken, repositoryName, repoOwner)
40+
orgService = orchestrator_github.GithubServiceProviderBasic{}.NewService(r.GithubToken, r.RepoNamespace, r.Actor)
4141
reporter = &reporting.CiReporter{
4242
CiService: prService,
4343
ReportStrategy: ReportStrategy,

cli/pkg/github/github.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"time"
3232
)
3333

34-
func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backendApi core_backend.Api, reportingStrategy reporting.ReportStrategy, commentUpdaterProvider comment_updater.CommentUpdaterProvider, driftNotifcationProvider drift.DriftNotificationProvider) {
34+
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) {
3535
log.Printf("Using GitHub.\n")
3636
githubActor := os.Getenv("GITHUB_ACTOR")
3737
if githubActor != "" {
@@ -80,7 +80,7 @@ func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backend
8080
}
8181

8282
repoOwner, repositoryName := utils.ParseRepoNamespace(ghRepository)
83-
githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner)
83+
githubPrService := githubServiceProvider.NewService(ghToken, repositoryName, repoOwner)
8484

8585
currentDir, err := os.Getwd()
8686
if err != nil {
@@ -139,7 +139,7 @@ func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backend
139139
if err != nil {
140140
usage.ReportErrorAndExit(githubActor, fmt.Sprintf("could not get changed files: %v", err), 4)
141141
}
142-
142+
143143
diggerConfig, _, _, err := digger_config.LoadDiggerConfig("./", false, files)
144144
if err != nil {
145145
usage.ReportErrorAndExit(githubActor, fmt.Sprintf("Failed to read Digger digger_config. %s", err), 4)

cli/pkg/integration/integration_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func getProjectLockForTests() (error, *locking.PullRequestLock) {
4343
repoOwner := "diggerhq"
4444
repositoryName := "test_dynamodb_lock"
4545
ghToken := "token"
46-
githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner)
46+
githubPrService := dg_github.GithubServiceProviderBasic{}.NewService(ghToken, repositoryName, repoOwner)
4747
reporter := reporting.CiReporter{
4848
CiService: &githubPrService,
4949
PrNumber: 1,
@@ -388,7 +388,7 @@ func TestHappyPath(t *testing.T) {
388388
ghEvent := parsedNewPullRequestContext.Event
389389
repoOwner := parsedNewPullRequestContext.RepositoryOwner
390390
repositoryName := parsedNewPullRequestContext.Repository
391-
githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner)
391+
githubPrService := dg_github.GithubServiceProviderBasic{}.NewService(ghToken, repositoryName, repoOwner)
392392

393393
assert.Equal(t, "pull_request", parsedNewPullRequestContext.EventName)
394394

@@ -545,7 +545,7 @@ func TestMultiEnvHappyPath(t *testing.T) {
545545
repoOwner := parsedNewPullRequestContext.RepositoryOwner
546546
repositoryName := parsedNewPullRequestContext.Repository
547547
diggerProjectNamespace := repoOwner + "/" + repositoryName
548-
githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner)
548+
githubPrService := dg_github.GithubServiceProviderBasic{}.NewService(ghToken, repositoryName, repoOwner)
549549

550550
assert.Equal(t, "pull_request", parsedNewPullRequestContext.EventName)
551551

@@ -765,7 +765,7 @@ workflows:
765765
ghEvent := parsedNewPullRequestContext.Event
766766
repoOwner := parsedNewPullRequestContext.RepositoryOwner
767767
repositoryName := parsedNewPullRequestContext.Repository
768-
githubPrService := dg_github.NewGitHubService(ghToken, repositoryName, repoOwner)
768+
githubPrService := dg_github.GithubServiceProviderBasic{}.NewService(ghToken, repositoryName, repoOwner)
769769
diggerProjectNamespace := repoOwner + "/" + repositoryName
770770

771771
assert.Equal(t, "pull_request", parsedNewPullRequestContext.EventName)

ee/cli/cmd/digger/default.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/diggerhq/digger/cli/pkg/usage"
88
"github.com/diggerhq/digger/ee/cli/pkg/comment_updater"
99
"github.com/diggerhq/digger/ee/cli/pkg/drift"
10+
github2 "github.com/diggerhq/digger/ee/cli/pkg/github"
1011
"github.com/spf13/cobra"
1112
"log"
1213
"os"
@@ -22,7 +23,7 @@ var defaultCmd = &cobra.Command{
2223
switch ci {
2324
case digger.GitHub:
2425
logLeader = os.Getenv("GITHUB_ACTOR")
25-
github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, comment_updater.CommentUpdaterProviderAdvanced{}, drift.DriftNotificationProviderAdvanced{})
26+
github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, github2.GithubServiceProviderAdvanced{}, comment_updater.CommentUpdaterProviderAdvanced{}, drift.DriftNotificationProviderAdvanced{})
2627
case digger.None:
2728
print("No CI detected.")
2829
os.Exit(10)

ee/cli/cmd/digger/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (r *RunConfig) GetServices() (*orchestrator.PullRequestService, *orchestrat
3636
switch r.Reporter {
3737
case "github":
3838
repoOwner, repositoryName := utils.ParseRepoNamespace(r.RepoNamespace)
39-
prService = orchestrator_github.NewGitHubService(r.GithubToken, repositoryName, repoOwner)
40-
orgService = orchestrator_github.NewGitHubService(r.GithubToken, r.RepoNamespace, r.Actor)
39+
prService = orchestrator_github.GithubServiceProviderBasic{}.NewService(r.GithubToken, repositoryName, repoOwner)
40+
orgService = orchestrator_github.GithubServiceProviderBasic{}.NewService(r.GithubToken, r.RepoNamespace, r.Actor)
4141
reporter = &reporting.CiReporter{
4242
CiService: prService,
4343
ReportStrategy: ReportStrategy,

ee/cli/pkg/github/providers.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
dg_github "github.com/diggerhq/digger/libs/orchestrator/github"
6+
"github.com/google/go-github/v61/github"
7+
"log"
8+
"os"
9+
)
10+
11+
type GithubServiceProviderAdvanced struct{}
12+
13+
func (_ GithubServiceProviderAdvanced) NewService(ghToken string, repoName string, owner string) dg_github.GithubService {
14+
client := github.NewClient(nil)
15+
if ghToken != "" {
16+
client = client.WithAuthToken(ghToken)
17+
}
18+
19+
githubHostname := os.Getenv("DIGGER_GITHUB_HOSTNAME")
20+
if githubHostname != "" {
21+
log.Printf("info: using github hostname: %v", githubHostname)
22+
githubEnterpriseBaseUrl := fmt.Sprintf("https://%v/api/v3/", githubHostname)
23+
githubEnterpriseUploadUrl := fmt.Sprintf("https://%v/api/uploads/", githubHostname)
24+
client.WithEnterpriseURLs(githubEnterpriseBaseUrl, githubEnterpriseUploadUrl)
25+
}
26+
27+
return dg_github.GithubService{
28+
Client: client,
29+
RepoName: repoName,
30+
Owner: owner,
31+
}
32+
}

libs/orchestrator/github/github.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import (
1414
"github.com/google/go-github/v61/github"
1515
)
1616

17-
func NewGitHubService(ghToken string, repoName string, owner string) GithubService {
17+
type GithubServiceProvider interface {
18+
NewService(ghToken string, repoName string, owner string) GithubService
19+
}
20+
21+
type GithubServiceProviderBasic struct{}
22+
23+
func (_ GithubServiceProviderBasic) NewService(ghToken string, repoName string, owner string) GithubService {
1824
client := github.NewClient(nil)
1925
if ghToken != "" {
2026
client = client.WithAuthToken(ghToken)

libs/orchestrator/github/github_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestFindAllProjectsDependantOnImpactedProjects(t *testing.T) {
115115
}
116116

117117
func TestFindAllChangedFilesOfPR(t *testing.T) {
118-
githubPrService := NewGitHubService("", "digger", "diggerhq")
118+
githubPrService := GithubServiceProviderBasic{}.NewService("", "digger", "diggerhq")
119119
files, _ := githubPrService.GetChangedFiles(98)
120120
// 45 changed files including 1 renamed file so the previous filename is included
121121
assert.Equal(t, 46, len(files))

libs/spec/providers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (v VCSProvider) GetPrService(vcsSpec VcsSpec) (orchestrator.PullRequestServ
175175
if token == "" {
176176
return nil, fmt.Errorf("failed to get githbu service: GITHUB_TOKEN not specified")
177177
}
178-
return github.NewGitHubService(token, vcsSpec.RepoName, vcsSpec.RepoOwner), nil
178+
return github.GithubServiceProviderBasic{}.NewService(token, vcsSpec.RepoName, vcsSpec.RepoOwner), nil
179179
default:
180180
return nil, fmt.Errorf("could not get PRService, unknown type %v", vcsSpec.VcsType)
181181
}

0 commit comments

Comments
 (0)