Skip to content

Commit

Permalink
wip gitlab updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeffrey committed Sep 26, 2023
1 parent e36b094 commit c27a543
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ require (
github.com/vmware/go-vcloud-director/v2 v2.17.0
github.com/vmware/goipmi v0.0.0-20181114221114-2333cd82d702
github.com/vmware/govmomi v0.29.0
github.com/xanzy/go-gitlab v0.74.0
github.com/xanzy/go-gitlab v0.92.1
github.com/zclconf/go-cty v1.10.0
go.mondoo.com/ranger-rpc v0.0.0-20230328135530-12135c17095f
go.opentelemetry.io/otel v1.14.0
Expand Down Expand Up @@ -360,7 +360,7 @@ require (
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.2 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-secure-stdlib/mlock v0.1.1 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,8 @@ github.com/hashicorp/go-plugin v1.4.8/go.mod h1:viDMjcLJuDui6pXb8U4HVfb8AamCWhHG
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-retryablehttp v0.7.2 h1:AcYqCvkpalPnPF2pn0KamgwamS42TqUDDYFRKq/RAd0=
github.com/hashicorp/go-retryablehttp v0.7.2/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
github.com/hashicorp/go-secure-stdlib/mlock v0.1.1 h1:cCRo8gK7oq6A2L6LICkUZ+/a5rLiRXFMf1Qd4xSwxTc=
Expand Down Expand Up @@ -1424,6 +1426,8 @@ github.com/vmware/govmomi v0.29.0 h1:SHJQ7DUc4fltFZv16znJNGHR1/XhiDK5iKxm2OqwkuU
github.com/vmware/govmomi v0.29.0/go.mod h1:F7adsVewLNHsW/IIm7ziFURaXDaHEwcc+ym4r3INMdY=
github.com/xanzy/go-gitlab v0.74.0 h1:Ha1cokbjn0PXy6B19t3W324dwM4AOT52fuHr7nERPrc=
github.com/xanzy/go-gitlab v0.74.0/go.mod h1:d/a0vswScO7Agg1CZNz15Ic6SSvBG9vfw8egL99t4kA=
github.com/xanzy/go-gitlab v0.92.1 h1:4HfRQtGtGd1M/Xn3G6hOikfWaysL7/G6y4EEzVKINPs=
github.com/xanzy/go-gitlab v0.92.1/go.mod h1:5ryv+MnpZStBH8I/77HuQBsMbBGANtVpLWC15qOjWAw=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
Expand Down
20 changes: 13 additions & 7 deletions motor/discovery/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
case "gitlab-project":
if pCfg.IncludesOneOfDiscoveryTarget(common.DiscoveryAuto, common.DiscoveryAll, DiscoveryProject) {
name := defaultName
project, _ := p.Project()
grp, _ := p.Group()
project, err := p.Project()
if err != nil {
return nil, err
}

if name == "" {
if project != nil {
Expand All @@ -89,7 +91,7 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
list = append(list, projectAsset)

if pCfg.IncludesOneOfDiscoveryTarget(common.DiscoveryAuto, common.DiscoveryAll, DiscoveryTerraform) {
terraformFiles, err := discoverTerraformHcl(ctx, p.Client(), grp.Path, project.Path)
terraformFiles, err := discoverTerraformHcl(ctx, p.Client(), project.ID)
if err != nil {
log.Error().Err(err).Msg("error discovering terraform")
} else if len(terraformFiles) > 0 {
Expand Down Expand Up @@ -131,7 +133,11 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
})

if pCfg.IncludesOneOfDiscoveryTarget(common.DiscoveryAuto, common.DiscoveryAll, DiscoveryProject) {
for _, project := range grp.Projects {
proj, err := p.GroupProjects()
if err != nil {
return nil, err
}
for _, project := range proj {
clonedConfig := pCfg.Clone()
if clonedConfig.Options == nil {
clonedConfig.Options = map[string]string{}
Expand All @@ -150,7 +156,7 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
list = append(list, projectAsset)

if pCfg.IncludesOneOfDiscoveryTarget(common.DiscoveryAuto, common.DiscoveryAll, DiscoveryTerraform) {
terraformFiles, err := discoverTerraformHcl(ctx, p.Client(), grp.Path, project.Path)
terraformFiles, err := discoverTerraformHcl(ctx, p.Client(), project.ID)
if err == nil && len(terraformFiles) > 0 {
terraformCfg := terraformConfig(pCfg, project.HTTPURLToRepo)
terraformCfg.Credentials = credentials(pCfg)
Expand All @@ -171,7 +177,7 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
}

// discoverTerraformHcl will check if the repository contains terraform files and return the terraform asset
func discoverTerraformHcl(ctx context.Context, client *gitlab_lib.Client, group string, project string) ([]string, error) {
func discoverTerraformHcl(ctx context.Context, client *gitlab_lib.Client, projectId int) ([]string, error) {
opts := &gitlab_lib.ListTreeOptions{
ListOptions: gitlab_lib.ListOptions{
PerPage: 100,
Expand All @@ -181,7 +187,7 @@ func discoverTerraformHcl(ctx context.Context, client *gitlab_lib.Client, group

nodes := []*gitlab_lib.TreeNode{}
for {
data, resp, err := client.Repositories.ListTree(group+"/"+project, opts)
data, resp, err := client.Repositories.ListTree(projectId, opts)
if err != nil {
return nil, err
}
Expand Down
18 changes: 17 additions & 1 deletion motor/providers/gitlab/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,30 @@ func (t *Provider) Group() (*gitlab.Group, error) {
if err != nil {
return nil, err
}
t.GroupId = grp.ID
return grp, err
}

func (t *Provider) GroupProjects() ([]*gitlab.Project, error) {
grp, _, err := t.Client().Groups.ListGroupProjects(t.GroupId, nil)
if err != nil {
return nil, err
}
return grp, err
}

func (t *Provider) Project() (*gitlab.Project, error) {
project, _, err := t.Client().Projects.GetProject(url.QueryEscape(t.GroupPath)+"/"+url.QueryEscape(t.ProjectPath), nil)
var project *gitlab.Project
var err error
if t.ProjectId == 0 {
project, _, err = t.Client().Projects.GetProject(url.QueryEscape(t.GroupPath)+"/"+url.QueryEscape(t.ProjectPath), nil)
} else {
project, _, err = t.Client().Projects.GetProject(t.ProjectId, nil)
}
if err != nil {
return nil, err
}
t.ProjectId = project.ID
return project, err
}

Expand Down
2 changes: 2 additions & 0 deletions motor/providers/gitlab/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type Provider struct {
opts map[string]string
GroupPath string
ProjectPath string
GroupId int
ProjectId int
}

func (p *Provider) Close() {}
Expand Down

0 comments on commit c27a543

Please sign in to comment.