Skip to content

Commit

Permalink
⭐️ Adding the support to list the files in a Project (Gitlab) (#4386)
Browse files Browse the repository at this point in the history
* Adding the support to list the files in a Respository

Signed-off-by: Hossein Rouhani <h_rouhani@hotmail.com>

* Tim's comment

Signed-off-by: Hossein Rouhani <h_rouhani@hotmail.com>

* Extend the functionality to read the content as well

Signed-off-by: Hossein Rouhani <h_rouhani@hotmail.com>

* adjust manifest file

Signed-off-by: Hossein Rouhani <h_rouhani@hotmail.com>

* rebase and adjust

Signed-off-by: Hossein Rouhani <h_rouhani@hotmail.com>

* adjust

Signed-off-by: Hossein Rouhani <h_rouhani@hotmail.com>

---------

Signed-off-by: Hossein Rouhani <h_rouhani@hotmail.com>
  • Loading branch information
HRouhani authored Jul 24, 2024
1 parent 041461a commit e9d1830
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
64 changes: 64 additions & 0 deletions providers/gitlab/resources/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package resources

import (
"encoding/base64"
"fmt"
"strconv"

"github.com/xanzy/go-gitlab"
Expand Down Expand Up @@ -299,3 +301,65 @@ func (p *mqlGitlabProject) projectMembers() ([]interface{}, error) {

return mqlMembers, nil
}

// Define the id function for a unique identifier for a file resource
func (f *mqlGitlabProjectFile) id() (string, error) {
return f.Path.Data, nil
}

// To fetch the list of files in the project repository and their contents
func (p *mqlGitlabProject) projectFiles() ([]interface{}, error) {
conn := p.MqlRuntime.Connection.(*connection.GitLabConnection)

projectID := int(p.Id.Data)
defaultBranch := p.DefaultBranch.Data

ref := &defaultBranch
recursive := true

// ListTree function expects pointer to the struct
listFilesOptions := &gitlab.ListTreeOptions{
Ref: ref,
Recursive: &recursive,
}

// Fetch the list of files
files, _, err := conn.Client().Repositories.ListTree(projectID, listFilesOptions)
if err != nil {
return nil, fmt.Errorf("failed to list files in repository: %w", err)
}

var mqlFiles []interface{}
for _, file := range files {
// Making sure we only fetch file content for blobs (files) not directories
if file.Type == "blob" {
// Fetch file content
fileContent, _, err := conn.Client().RepositoryFiles.GetFile(projectID, file.Path, &gitlab.GetFileOptions{Ref: ref})
if err != nil {
return nil, err
}

// Decode base64 content
contentBytes, err := base64.StdEncoding.DecodeString(fileContent.Content)
if err != nil {
return nil, err
}

fileInfo := map[string]*llx.RawData{
"path": llx.StringData(file.Path),
"type": llx.StringData(file.Type),
"name": llx.StringData(file.Name),
"content": llx.StringData(string(contentBytes)),
}

mqlFile, err := CreateResource(p.MqlRuntime, "gitlab.project.file", fileInfo)
if err != nil {
return nil, err
}

mqlFiles = append(mqlFiles, mqlFile)
}
}

return mqlFiles, nil
}
16 changes: 15 additions & 1 deletion providers/gitlab/resources/gitlab.lr
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ gitlab.project @defaults("fullName visibility webURL") {
approvalSettings() gitlab.project.approvalSettings
// Protected branches settings for the project
protectedBranches() []gitlab.project.protectedBranch
// To fetch the list of members in the project with their roles
// List of members in the project with their roles
projectMembers() []gitlab.project.member
// List of files in the project repository
projectFiles() []gitlab.project.file
}


Expand Down Expand Up @@ -142,4 +144,16 @@ gitlab.project.member @defaults("id name role") {
name string
// Member role
role string
}

// GitLab project file
private gitlab.project.file @defaults("path type") {
// File path
path string
// File type
type string
// File name
name string
// File content
content string
}
10 changes: 10 additions & 0 deletions providers/gitlab/resources/gitlab.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ resources:
packagesEnabled:
min_mondoo_version: 9.0.0
path: {}
projectFiles:
min_mondoo_version: 9.0.0
projectMembers:
min_mondoo_version: 9.0.0
protectedBranches:
Expand Down Expand Up @@ -97,6 +99,14 @@ resources:
resetApprovalsOnPush: {}
is_private: true
min_mondoo_version: 9.0.0
gitlab.project.file:
fields:
content: {}
name: {}
path: {}
type: {}
is_private: true
min_mondoo_version: 9.0.0
gitlab.project.member:
fields:
id: {}
Expand Down

0 comments on commit e9d1830

Please sign in to comment.