Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 fallback to directly fetching a project if not found #1946

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion motor/discovery/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ func (r *Resolver) Resolve(ctx context.Context, root *asset.Asset, pCfg *provide
if err != nil {
return nil, err
}
clonedConfig := pCfg.Clone()
if clonedConfig.Options == nil {
clonedConfig.Options = map[string]string{}
}
clonedConfig.Options["project-id"] = strconv.Itoa(project.ID)

projectAsset := &asset.Asset{
PlatformIds: []string{identifier},
Name: name,
Platform: pf,
Connections: []*providers.Config{pCfg}, // pass-in the current config
Connections: []*providers.Config{clonedConfig}, // pass-in the current config
State: asset.State_STATE_ONLINE,
}

Expand Down
10 changes: 9 additions & 1 deletion motor/providers/gitlab/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,22 @@ func (t *Provider) Project() (*gitlab.Project, error) {
pid = t.ProjectId
}

project, _, err := t.Client().Projects.GetProject(pid, nil)
project, err := t.GetProjectById(pid)
if err != nil {
return nil, err
}
t.ProjectId = project.ID
return project, err
}

func (t *Provider) GetProjectById(pid interface{}) (*gitlab.Project, error) {
project, _, err := t.Client().Projects.GetProject(pid, nil)
if err != nil {
return nil, err
}
return project, err
}

func (p *Provider) PlatformInfo() (*platform.Platform, error) {
if projectName := p.opts["project"]; projectName != "" {
return GitLabProjectPlatform, nil
Expand Down
19 changes: 17 additions & 2 deletions resources/packs/gitlab/gitlab.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package gitlab

import (
"errors"
"strconv"

"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/motor/providers"
provider "go.mondoo.com/cnquery/motor/providers/gitlab"
Expand Down Expand Up @@ -151,8 +151,23 @@ func (g *mqlGitlabProject) init(args *resources.Args) (*resources.Args, GitlabPr
return args, proj, nil
}
}
// try to fetch it directly
gt, err := gitlabProvider(g.MotorRuntime.Motor.Provider)
if err != nil {
return nil, nil, err
}
prj, err := gt.GetProjectById(projectId)
if err != nil {
return nil, nil, errors.Wrap(err, "project not found")
}

return nil, nil, errors.New("project not found")
(*args)["id"] = int64(prj.ID)
(*args)["name"] = prj.Name
(*args)["path"] = prj.Path
(*args)["namespace"] = prj.Namespace.Name
(*args)["description"] = prj.Description
(*args)["visibility"] = string(prj.Visibility)
return args, nil, nil
}

func getAssetIdentifier(runtime *resources.Runtime, t string) *string {
Expand Down