From e36b094d5e255e7e9d5bdec9f75e408081ec711c Mon Sep 17 00:00:00 2001 From: vjeffrey Date: Mon, 25 Sep 2023 18:35:47 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20more=20gitlab=20group=20and=20pr?= =?UTF-8?q?oject=20fetching=20cleanup=20(#1922)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/packs/gitlab/gitlab.go | 55 ++++++++++++++++---------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/resources/packs/gitlab/gitlab.go b/resources/packs/gitlab/gitlab.go index 65e52cbcaa..f863cec2d9 100644 --- a/resources/packs/gitlab/gitlab.go +++ b/resources/packs/gitlab/gitlab.go @@ -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" @@ -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] @@ -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 @@ -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") }