Skip to content

feat: add gitlab-ci include parser #144

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
64 changes: 64 additions & 0 deletions pkg/gitlab/include/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package include

import (
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

dio "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/aquasecurity/go-dep-parser/pkg/utils"
)

type Include struct {
Project string `yaml:"project,omitempty"`
Ref string `yaml:"ref,omitempty"`
}

type GitlabCiFile struct {
Includes []Include `yaml:"include,omitempty"`
}

type Parser struct{}

func NewParser() types.Parser {
return &Parser{}
}

func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var gitlabCiFile GitlabCiFile
decoder := yaml.NewDecoder(r)
err := decoder.Decode(&gitlabCiFile)
if err != nil {
// Currently only parses data when we can unmarshal from array project includes
// See https://docs.gitlab.com/ee/ci/yaml/includes.html for other options
return nil, nil, xerrors.Errorf("failed to parse gitlab-ci file: %w", err)
}

libs, deps := p.parse(&gitlabCiFile)

return libs, deps, nil
}

func (p *Parser) parse(gitlabCiFile *GitlabCiFile) ([]types.Library, []types.Dependency) {
var libs []types.Library

for _, include := range gitlabCiFile.Includes {
name := include.Project
if name == "" {
continue
}

version := include.Ref
if version == "" {
version = "latest"
}

libs = append(libs, types.Library{
ID: utils.PackageID(name, version),
Name: name,
Version: version,
})
}

return libs, nil
}
40 changes: 40 additions & 0 deletions pkg/gitlab/include/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package include

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/aquasecurity/go-dep-parser/pkg/types"
)

func TestParse(t *testing.T) {
vectors := []struct {
file string
want []types.Library
}{
{
file: "testdata/.gitlab-ci.yml",
want: gitlabCiInclude,
},
{
file: "testdata/no-ref.gitlab-ci.yml",
want: gitlabCiIncludeLatest,
},
}

for _, v := range vectors {
t.Run(path.Base(v.file), func(t *testing.T) {
f, err := os.Open(v.file)
require.NoError(t, err)

got, _, err := NewParser().Parse(f)
require.NoError(t, err)

assert.Equal(t, v.want, got)
})
}
}
21 changes: 21 additions & 0 deletions pkg/gitlab/include/parse_testcase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package include

import "github.com/aquasecurity/go-dep-parser/pkg/types"

var (
gitlabCiInclude = []types.Library{
{
ID: "my-group/[email protected]",
Name: "my-group/my-project",
Version: "1.0.0",
},
}

gitlabCiIncludeLatest = []types.Library{
{
ID: "my-group/my-project@latest",
Name: "my-group/my-project",
Version: "latest",
},
}
)
4 changes: 4 additions & 0 deletions pkg/gitlab/include/testdata/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include:
- project: my-group/my-project
ref: 1.0.0
file: /templates/.gitlab-ci-template.yml
3 changes: 3 additions & 0 deletions pkg/gitlab/include/testdata/no-ref.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include:
- project: my-group/my-project
file: /templates/.gitlab-ci-template.yml