Skip to content

Commit d78884e

Browse files
committed
parse pipeline as code tekton
1 parent 573ab0b commit d78884e

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

models/package_insights.go

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type PackageInsights struct {
3131
GithubActionsMetadata []GithubActionsMetadata `json:"github_actions_metadata"`
3232
GitlabciConfigs []GitlabciConfig `json:"gitlabci_configs"`
3333
AzurePipelines []AzurePipeline `json:"azure_pipelines"`
34+
PipelineAsCodeTekton []PipelineAsCodeTekton `json:"pipeline_as_code_tekton"`
3435
}
3536

3637
func (p *PackageInsights) GetSourceGitRepoURI() string {

models/pipeline_as_code_tekton.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package models
2+
3+
type PipelineAsCodeTekton struct {
4+
ApiVersion string `json:"apiVersion" yaml:"apiVersion"`
5+
Kind string `json:"kind"`
6+
Metadata struct {
7+
Name string `json:"name"`
8+
Annotations map[string]string `json:"annotations"`
9+
} `json:"metadata"`
10+
}

scanner/scanner.go

+24
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ func parseGitlabCi(scanner *Scanner, filePath string, fileInfo fs.FileInfo) erro
154154
return nil
155155
}
156156

157+
func parsePipelineAsCodeTekton(scanner *Scanner, filePath string, fileInfo fs.FileInfo) error {
158+
relPath, err := filepath.Rel(scanner.Path, filePath)
159+
if err != nil {
160+
return err
161+
}
162+
163+
data, err := os.ReadFile(filePath)
164+
if err != nil {
165+
return err
166+
}
167+
168+
pipelineAsCode := models.PipelineAsCodeTekton{}
169+
err = yaml.Unmarshal(data, &pipelineAsCode)
170+
if err != nil {
171+
log.Debug().Err(err).Str("file", relPath).Msg("failed to unmarshal pipeline as code yaml file")
172+
return nil
173+
}
174+
175+
scanner.Package.PipelineAsCodeTekton = append(scanner.Package.PipelineAsCodeTekton, pipelineAsCode)
176+
177+
return nil
178+
}
179+
157180
type Scanner struct {
158181
Path string
159182
Package *models.PackageInsights
@@ -169,6 +192,7 @@ func NewScanner(path string) Scanner {
169192
ParseFuncs: map[*regexp.Regexp]parseFunc{
170193
regexp.MustCompile(`(\b|/)action\.ya?ml$`): parseGithubActionsMetadata,
171194
regexp.MustCompile(`^\.github/workflows/[^/]+\.ya?ml$`): parseGithubWorkflows,
195+
regexp.MustCompile(`^\.tekton/[^/]+\.ya?ml$`): parsePipelineAsCodeTekton,
172196
regexp.MustCompile(`\.?azure-pipelines(-.+)?\.ya?ml$`): parseAzurePipelines,
173197
regexp.MustCompile(`\.?gitlab-ci(-.+)?\.ya?ml$`): parseGitlabCi,
174198
},

scanner/scanner_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scanner
22

33
import (
44
"context"
5+
"github.com/boostsecurityio/poutine/models"
56
"github.com/boostsecurityio/poutine/opa"
67
"github.com/stretchr/testify/assert"
78
"testing"
@@ -69,3 +70,31 @@ func TestRun(t *testing.T) {
6970
assert.Contains(t, s.Package.PackageDependencies, "pkg:docker/alpine%3Alatest")
7071
assert.Equal(t, 3, len(s.Package.GitlabciConfigs))
7172
}
73+
74+
func TestPipelineAsCodeTekton(t *testing.T) {
75+
s := NewScanner("testdata")
76+
o, _ := opa.NewOpa()
77+
err := s.Run(context.TODO(), o)
78+
assert.NoError(t, err)
79+
80+
pipelines := s.Package.PipelineAsCodeTekton
81+
82+
assert.Len(t, pipelines, 1)
83+
expectedAnnotations := map[string]string{
84+
"pipelinesascode.tekton.dev/on-event": "[push, pull_request]",
85+
"pipelinesascode.tekton.dev/on-target-branch": "[*]",
86+
"pipelinesascode.tekton.dev/task": "[git-clone]",
87+
}
88+
expectedPipeline := models.PipelineAsCodeTekton{
89+
ApiVersion: "tekton.dev/v1beta1",
90+
Kind: "PipelineRun",
91+
Metadata: struct {
92+
Name string `json:"name"`
93+
Annotations map[string]string `json:"annotations"`
94+
}{
95+
Name: "linters",
96+
Annotations: expectedAnnotations,
97+
},
98+
}
99+
assert.Equal(t, expectedPipeline, pipelines[0])
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: PipelineRun
3+
metadata:
4+
name: linters
5+
annotations:
6+
pipelinesascode.tekton.dev/on-event: "[push, pull_request]"
7+
pipelinesascode.tekton.dev/on-target-branch: "[*]"
8+
pipelinesascode.tekton.dev/task: "[git-clone]"
9+
spec:
10+
params:
11+
- name: repo_url
12+
value: "{{repo_url}}"
13+
- name: revision
14+
value: "{{revision}}"
15+
pipelineSpec:
16+
params:
17+
- name: repo_url
18+
- name: revision
19+
tasks:
20+
- name: fetchit
21+
displayName: "Fetch git repository"
22+
params:
23+
- name: url
24+
value: $(params.repo_url)
25+
- name: revision
26+
value: $(params.revision)
27+
taskRef:
28+
name: git-clone
29+
workspaces:
30+
- name: output
31+
workspace: source
32+
- name: vale
33+
displayName: "Spelling and Grammar"
34+
runAfter:
35+
- fetchit
36+
taskSpec:
37+
workspaces:
38+
- name: source
39+
steps:
40+
- name: vale-lint
41+
image: jdkato/vale
42+
workingDir: $(workspaces.source.path)
43+
script: |
44+
vale docs
45+
workspaces:
46+
- name: source
47+
workspace: source
48+
workspaces:
49+
- name: source
50+
workspaces:
51+
- name: source
52+
volumeClaimTemplate:
53+
spec:
54+
accessModes:
55+
- ReadWriteOnce
56+
resources:
57+
requests:
58+
storage: 5Gi

0 commit comments

Comments
 (0)