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

🧹 more gitlab group and project fetching cleanup #1922

Merged
merged 1 commit into from
Sep 26, 2023
Merged
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
55 changes: 28 additions & 27 deletions resources/packs/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"

"github.com/rs/zerolog/log"
"github.com/xanzy/go-gitlab"
"go.mondoo.com/cnquery/motor/providers"
provider "go.mondoo.com/cnquery/motor/providers/gitlab"
"go.mondoo.com/cnquery/resources"
Expand Down Expand Up @@ -46,35 +47,27 @@ func (g *mqlGitlabGroup) init(args *resources.Args) (*resources.Args, GitlabGrou
if err != nil {
return nil, nil, err
}

(*args)["id"] = int64(grp.ID)
(*args)["name"] = grp.Name
(*args)["path"] = grp.Path
(*args)["description"] = grp.Description
(*args)["visibility"] = string(grp.Visibility)
(*args)["requireTwoFactorAuthentication"] = grp.RequireTwoFactorAuth

return args, nil, nil
}

// GetProjects list all projects that belong to a group
// see https://docs.gitlab.com/ee/api/projects.html
func (g *mqlGitlabGroup) GetProjects() ([]interface{}, error) {
gt, err := gitlabProvider(g.MotorRuntime.Motor.Provider)
if err != nil {
return nil, err
resArgs := []interface{}{
"id", int64(grp.ID),
"name", grp.Name,
"path", grp.Path,
"description", grp.Description,
"visibility", string(grp.Visibility),
"requireTwoFactorAuthentication", grp.RequireTwoFactorAuth,
}

path, err := g.Path()
projects, err := g.createProjectResources(grp)
if err != nil {
return nil, err
return nil, nil, err
}

grp, _, err := gt.Client().Groups.GetGroup(path, nil)
resArgs = append(resArgs, "projects", projects)
mqlGroup, err := g.MotorRuntime.CreateResource("gitlab.group", resArgs...)
if err != nil {
return nil, err
return nil, nil, err
}
return args, mqlGroup.(*mqlGitlabGroup), nil
}

func (g *mqlGitlabGroup) createProjectResources(grp *gitlab.Group) ([]interface{}, error) {
var mqlProjects []interface{}
for i := range grp.Projects {
prj := grp.Projects[i]
Expand All @@ -88,14 +81,22 @@ func (g *mqlGitlabGroup) GetProjects() ([]interface{}, error) {
"visibility", string(prj.Visibility),
)
if err != nil {
return nil, err
// log the err. we're seeing weird behavior with these apis. lets log if we have
// issues here
log.Error().Err(err).Str("path", prj.Path).Msg("cannot create gitlab project asset")
} else {
mqlProjects = append(mqlProjects, mqlProject)
}
mqlProjects = append(mqlProjects, mqlProject)
}

return mqlProjects, nil
}

// GetProjects list all projects that belong to a group
// see https://docs.gitlab.com/ee/api/projects.html
func (g *mqlGitlabGroup) GetProjects() ([]interface{}, error) {
return g.Projects()
}

func (g *mqlGitlabProject) id() (string, error) {
id, _ := g.Id()
return "gitlab.project/" + strconv.FormatInt(id, 10), nil
Expand Down Expand Up @@ -134,5 +135,5 @@ func (g *mqlGitlabProject) init(args *resources.Args) (*resources.Args, GitlabPr
}
}

return args, nil, nil
return nil, nil, errors.New("project not found")
}