Skip to content

Commit

Permalink
support private VCS in ee (#1613)
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes authored Jul 11, 2024
1 parent 3ab8baf commit 18a9944
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 17 deletions.
17 changes: 12 additions & 5 deletions backend/utils/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@ import (
)

type GitlabProvider interface {
NewClient(token string) (*gitlab.Client, error)
NewClient(token string, baseUrl string) (*gitlab.Client, error)
}

type GitlabClientProvider struct{}

func (g GitlabClientProvider) NewClient(token string) (*gitlab.Client, error) {
client, err := gitlab.NewClient(token)
return client, err
func (g GitlabClientProvider) NewClient(token string, baseUrl string) (*gitlab.Client, error) {
if baseUrl == "" {
client, err := gitlab.NewClient(token)
return client, err
} else {
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl))
return client, err
}
}

func GetGitlabService(gh GitlabProvider, projectId int, repoName string, repoFullName string, prNumber int, discussionId string) (*orchestrator_gitlab.GitLabService, error) {
token := os.Getenv("DIGGER_GITLAB_ACCESS_TOKEN")
client, err := gh.NewClient(token)
baseUrl := os.Getenv("DIGGER_GITLAB_BASE_URL")

client, err := gh.NewClient(token, baseUrl)
if err != nil {
return nil, fmt.Errorf("could not get gitlab client: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/digger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var defaultCmd = &cobra.Command{
}
err = spec2.RunSpec(
spec,
lib_spec.VCSProvider{},
lib_spec.VCSProviderBasic{},
lib_spec.JobSpecProvider{},
lib_spec.LockProvider{},
lib_spec.ReporterProvider{},
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/digger/run_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var runSpecCmd = &cobra.Command{
}
err = spec2.RunSpec(
spec,
lib_spec.VCSProvider{},
lib_spec.VCSProviderBasic{},
lib_spec.JobSpecProvider{},
lib_spec.LockProvider{},
lib_spec.ReporterProvider{},
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 @@ -11,6 +11,7 @@ import (
"github.com/diggerhq/digger/ee/cli/pkg/drift"
github2 "github.com/diggerhq/digger/ee/cli/pkg/github"
"github.com/diggerhq/digger/ee/cli/pkg/policy"
"github.com/diggerhq/digger/ee/cli/pkg/vcs"
comment_summary "github.com/diggerhq/digger/libs/comment_utils/summary"
lib_spec "github.com/diggerhq/digger/libs/spec"
"github.com/spf13/cobra"
Expand All @@ -32,7 +33,7 @@ var defaultCmd = &cobra.Command{
}
err = spec2.RunSpec(
spec,
lib_spec.VCSProvider{},
vcs.VCSProviderAdvanced{},
lib_spec.JobSpecProvider{},
lib_spec.LockProvider{},
lib_spec.ReporterProvider{},
Expand Down
2 changes: 1 addition & 1 deletion ee/cli/cmd/digger/run_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var runSpecCmd = &cobra.Command{
}
err = spec2.RunSpec(
spec,
lib_spec.VCSProvider{},
lib_spec.VCSProviderBasic{},
lib_spec.JobSpecProvider{},
lib_spec.LockProvider{},
lib_spec.ReporterProvider{},
Expand Down
54 changes: 54 additions & 0 deletions ee/cli/pkg/vcs/providers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package vcs

import (
"fmt"
github2 "github.com/diggerhq/digger/ee/cli/pkg/github"
"github.com/diggerhq/digger/libs/ci"
"github.com/diggerhq/digger/libs/ci/gitlab"
"github.com/diggerhq/digger/libs/spec"
"os"
)

type VCSProviderAdvanced struct{}

func (v VCSProviderAdvanced) GetPrService(vcsSpec spec.VcsSpec) (ci.PullRequestService, error) {
switch vcsSpec.VcsType {
case "github":
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return nil, fmt.Errorf("failed to get github service: GITHUB_TOKEN not specified")
}
return github2.GithubServiceProviderAdvanced{}.NewService(token, vcsSpec.RepoName, vcsSpec.RepoOwner)
case "gitlab":
token := os.Getenv("GITLAB_TOKEN")
if token == "" {
return nil, fmt.Errorf("failed to get gitlab service: GITLAB_TOKEN not specified")
}
context, err := gitlab.ParseGitLabContext()
if err != nil {
return nil, fmt.Errorf("failed to get gitlab service, could not parse context: %v", err)
}
baseUrl := os.Getenv("DIGGER_GITLAB_BASE_URL")
return gitlab.NewGitLabService(token, context, baseUrl)
default:
return nil, fmt.Errorf("could not get PRService, unknown type %v", vcsSpec.VcsType)
}
}

func (v VCSProviderAdvanced) GetOrgService(vcsSpec spec.VcsSpec) (ci.OrgService, error) {
switch vcsSpec.VcsType {
case "gitlab":
token := os.Getenv("GITLAB_TOKEN")
if token == "" {
return nil, fmt.Errorf("failed to get gitlab service: GITLAB_TOKEN not specified")
}
context, err := gitlab.ParseGitLabContext()
if err != nil {
return nil, fmt.Errorf("failed to get gitlab service, could not parse context: %v", err)
}
baseUrl := os.Getenv("DIGGER_GITLAB_BASE_URL")
return gitlab.NewGitLabService(token, context, baseUrl)
default:
return nil, fmt.Errorf("could not get PRService, unknown type %v", vcsSpec.VcsType)
}
}
11 changes: 9 additions & 2 deletions libs/ci/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ func ParseGitLabContext() (*GitLabContext, error) {
return &parsedGitLabContext, nil
}

func NewGitLabService(token string, gitLabContext *GitLabContext) (*GitLabService, error) {
client, err := go_gitlab.NewClient(token)
func NewGitLabService(token string, gitLabContext *GitLabContext, gitlabBaseUrl string) (*GitLabService, error) {
var client *go_gitlab.Client
var err error
if gitlabBaseUrl == "" {
client, err = go_gitlab.NewClient(token, go_gitlab.WithBaseURL(gitlabBaseUrl))
} else {
client, err = go_gitlab.NewClient(token)
}

if err != nil {
log.Fatalf("failed to create gitlab client: %v", err)
}
Expand Down
16 changes: 10 additions & 6 deletions libs/spec/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,14 @@ func (b BackendApiProvider) GetBackendApi(backendSpec BackendSpec) (backend2.Api
}
}

type VCSProvider struct{}
type VCSProvider interface {
GetPrService(vcsSpec VcsSpec) (ci.PullRequestService, error)
GetOrgService(vcsSpec VcsSpec) (ci.OrgService, error)
}

type VCSProviderBasic struct{}

func (v VCSProvider) GetPrService(vcsSpec VcsSpec) (ci.PullRequestService, error) {
func (v VCSProviderBasic) GetPrService(vcsSpec VcsSpec) (ci.PullRequestService, error) {
switch vcsSpec.VcsType {
case "github":
token := os.Getenv("GITHUB_TOKEN")
Expand All @@ -186,13 +191,13 @@ func (v VCSProvider) GetPrService(vcsSpec VcsSpec) (ci.PullRequestService, error
if err != nil {
return nil, fmt.Errorf("failed to get gitlab service, could not parse context: %v", err)
}
return gitlab.NewGitLabService(token, context)
return gitlab.NewGitLabService(token, context, "")
default:
return nil, fmt.Errorf("could not get PRService, unknown type %v", vcsSpec.VcsType)
}
}

func (v VCSProvider) GetOrgService(vcsSpec VcsSpec) (ci.OrgService, error) {
func (v VCSProviderBasic) GetOrgService(vcsSpec VcsSpec) (ci.OrgService, error) {
switch vcsSpec.VcsType {
case "github":
token := os.Getenv("GITHUB_TOKEN")
Expand All @@ -209,11 +214,10 @@ func (v VCSProvider) GetOrgService(vcsSpec VcsSpec) (ci.OrgService, error) {
if err != nil {
return nil, fmt.Errorf("failed to get gitlab service, could not parse context: %v", err)
}
return gitlab.NewGitLabService(token, context)
return gitlab.NewGitLabService(token, context, "")
default:
return nil, fmt.Errorf("could not get PRService, unknown type %v", vcsSpec.VcsType)
}

}

type SpecPolicyProvider interface {
Expand Down

0 comments on commit 18a9944

Please sign in to comment.