Skip to content

Commit

Permalink
🧹 more gitlab group and project fetching cleanup (#1922)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeffrey authored Sep 26, 2023
1 parent 06be5cb commit e36b094
Showing 1 changed file with 28 additions and 27 deletions.
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")
}

0 comments on commit e36b094

Please sign in to comment.