Skip to content

Commit

Permalink
[bot] Update main from openshift-pipelines/pipelines-as-code to 425d4f2
Browse files Browse the repository at this point in the history
    $ git diff --stat 425d4f2..e9a96ad
     pkg/matcher/annotation_matcher.go                  |  6 --
     pkg/matcher/annotation_matcher_test.go             | 40 ----------
     pkg/provider/bitbucketcloud/bitbucket.go           |  6 +-
     pkg/provider/bitbucketcloud/bitbucket_test.go      |  2 +-
     pkg/provider/bitbucketserver/bitbucketserver.go    |  4 -
     .../bitbucketserver/bitbucketserver_test.go        | 20 ++---
     pkg/provider/gitea/gitea.go                        |  7 +-
     pkg/provider/gitea/gitea_test.go                   | 55 -------------
     pkg/provider/gitea/test/setup.go                   | 93 ----------------------
     .../testdata/tree/badyaml/.tekton/badyaml.yaml     |  3 -
     pkg/provider/github/github.go                      |  7 +-
     pkg/provider/github/github_test.go                 |  4 +-
     pkg/provider/gitlab/gitlab.go                      |  7 +-
     pkg/provider/gitlab/gitlab_test.go                 |  2 +-
     pkg/provider/provider.go                           | 10 ---
     .../TestGithubPullRequestSecondBadYaml.golden      |  2 +-
     16 files changed, 32 insertions(+), 236 deletions(-)

https://github.com/openshift-pipelines/pipelines-as-code/compare/425d4f2e29555da6a1d56458ba38e538967b743a..e9a96ad1b1c2a04188fd38dd1dc3b951f938c22c
  • Loading branch information
openshift-pipelines-bot authored and openshift-pipelines-bot committed Dec 6, 2024
1 parent 7d44589 commit bd7fd16
Show file tree
Hide file tree
Showing 17 changed files with 237 additions and 33 deletions.
2 changes: 1 addition & 1 deletion head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e9a96ad1b1c2a04188fd38dd1dc3b951f938c22c
425d4f2e29555da6a1d56458ba38e538967b743a
6 changes: 6 additions & 0 deletions upstream/pkg/matcher/annotation_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func getAnnotationValues(annotation string) ([]string, error) {
func getTargetBranch(prun *tektonv1.PipelineRun, event *info.Event) (bool, string, string, error) {
var targetEvent, targetBranch string
if key, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnEvent]; ok {
if key == "[]" {
return false, "", "", fmt.Errorf("annotation %s is empty", keys.OnEvent)
}
targetEvents := []string{event.TriggerTarget.String()}
if event.EventType == triggertype.Incoming.String() {
// if we have a incoming event, we want to match pipelineruns on both incoming and push
Expand All @@ -107,6 +110,9 @@ func getTargetBranch(prun *tektonv1.PipelineRun, event *info.Event) (bool, strin
}
}
if key, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnTargetBranch]; ok {
if key == "[]" {
return false, "", "", fmt.Errorf("annotation %s is empty", keys.OnTargetBranch)
}
targetEvents := []string{event.BaseBranch}
matched, err := matchOnAnnotation(key, targetEvents, true)
targetBranch = key
Expand Down
40 changes: 40 additions & 0 deletions upstream/pkg/matcher/annotation_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,7 @@ func TestGetTargetBranch(t *testing.T) {
expectedMatch bool
expectedEvent string
expectedBranch string
expectedError string
}{
{
name: "Test with pull_request event",
Expand Down Expand Up @@ -2353,11 +2354,50 @@ func TestGetTargetBranch(t *testing.T) {
expectedEvent: "",
expectedBranch: "",
},
{
name: "Test empty array onEvent",
prun: &tektonv1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
keys.OnEvent: "[]",
keys.OnTargetBranch: "main",
},
},
},
event: &info.Event{
TriggerTarget: triggertype.PullRequest,
EventType: "pull_request",
BaseBranch: "main",
},
expectedError: fmt.Sprintf("annotation %s is empty", keys.OnEvent),
},
{
name: "Test empty array onTargetBranch",
prun: &tektonv1.PipelineRun{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
keys.OnEvent: "pull_request",
keys.OnTargetBranch: "[]",
},
},
},
event: &info.Event{
TriggerTarget: triggertype.PullRequest,
EventType: "pull_request",
BaseBranch: "main",
},
expectedError: fmt.Sprintf("annotation %s is empty", keys.OnTargetBranch),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
matched, targetEvent, targetBranch, err := getTargetBranch(tt.prun, tt.event)
if tt.expectedError != "" {
assert.Assert(t, err != nil)
assert.Error(t, err, tt.expectedError, err.Error())
return
}
assert.NilError(t, err)
assert.Equal(t, tt.expectedMatch, matched)
assert.Equal(t, tt.expectedEvent, targetEvent)
Expand Down
6 changes: 2 additions & 4 deletions upstream/pkg/provider/bitbucketcloud/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud/types"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)

var _ provider.Interface = (*Provider)(nil)
Expand Down Expand Up @@ -262,9 +261,8 @@ func (v *Provider) concatAllYamlFiles(objects []bitbucket.RepositoryFile, event
if err != nil {
return "", err
}
var i any
if err := yaml.Unmarshal([]byte(data), &i); err != nil {
return "", fmt.Errorf("error unmarshalling yaml file %s: %w", value.Path, err)
if err := provider.ValidateYaml([]byte(data), value.Path); err != nil {
return "", err
}

if allTemplates != "" && !strings.HasPrefix(data, "---") {
Expand Down
2 changes: 1 addition & 1 deletion upstream/pkg/provider/bitbucketcloud/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestGetTektonDir(t *testing.T) {
content, err := v.GetTektonDir(ctx, tt.event, ".tekton", tt.provenance)
if tt.wantErr != "" {
assert.Assert(t, err != nil, "expected error %s, got %v", tt.wantErr, err)
assert.Equal(t, err.Error(), tt.wantErr)
assert.ErrorContains(t, err, tt.wantErr)
return
}
if tt.contentContains == "" {
Expand Down
4 changes: 4 additions & 0 deletions upstream/pkg/provider/bitbucketserver/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func (v *Provider) concatAllYamlFiles(objects []string, runevent *info.Event) (s
return "", err
}

if err := provider.ValidateYaml([]byte(data), value); err != nil {
return "", err
}

if allTemplates != "" && !strings.HasPrefix(data, "---") {
allTemplates += "---"
}
Expand Down
20 changes: 12 additions & 8 deletions upstream/pkg/provider/bitbucketserver/bitbucketserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestGetTektonDir(t *testing.T) {
path string
testDirPath string
contentContains string
wantErr bool
wantErr string
removeSuffix bool
}{
{
Expand All @@ -49,6 +49,13 @@ func TestGetTektonDir(t *testing.T) {
testDirPath: "./",
contentContains: "",
},
{
name: "Badly formatted yaml",
event: bbtest.MakeEvent(nil),
path: ".tekton",
testDirPath: "../../pipelineascode/testdata/bad_yaml/.tekton",
wantErr: "error unmarshalling yaml file .tekton/badyaml.yaml: yaml: line 2: did not find expected key",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -60,15 +67,12 @@ func TestGetTektonDir(t *testing.T) {
v := &Provider{Logger: logger, baseURL: tURL, Client: client, projectKey: tt.event.Organization}
bbtest.MuxDirContent(t, mux, tt.event, tt.testDirPath, tt.path)
content, err := v.GetTektonDir(ctx, tt.event, tt.path, "")
if tt.wantErr {
assert.Assert(t, err != nil,
"GetTektonDir() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.contentContains == "" {
assert.Equal(t, content, "")
if tt.wantErr != "" {
assert.Assert(t, err != nil, "we should have get an error here")
assert.ErrorContains(t, err, tt.wantErr)
return
}
assert.NilError(t, err)
assert.Assert(t, strings.Contains(content, tt.contentContains), "content %s doesn't have %s", content, tt.contentContains)
})
}
Expand Down
7 changes: 2 additions & 5 deletions upstream/pkg/provider/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -258,10 +257,8 @@ func (v *Provider) concatAllYamlFiles(objects []gitea.GitEntry, event *info.Even
if err != nil {
return "", err
}
// validate yaml
var i any
if err := yaml.Unmarshal(data, &i); err != nil {
return "", fmt.Errorf("error unmarshalling yaml file %s: %w", value.Path, err)
if err := provider.ValidateYaml(data, value.Path); err != nil {
return "", err
}
if allTemplates != "" && !strings.HasPrefix(string(data), "---") {
allTemplates += "---"
Expand Down
55 changes: 55 additions & 0 deletions upstream/pkg/provider/gitea/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package gitea

import (
"context"
"crypto/sha256"
"fmt"
"io"
"net/http"
"reflect"
"sort"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -21,6 +23,7 @@ import (
tgitea "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitea/test"
"go.uber.org/zap"
zapobserver "go.uber.org/zap/zaptest/observer"
"gotest.tools/v3/assert"
rtesting "knative.dev/pkg/reconciler/testing"
)

Expand Down Expand Up @@ -470,3 +473,55 @@ func TestProvider_CreateStatusCommit(t *testing.T) {
})
}
}

func TestGetTektonDir(t *testing.T) {
testGetTektonDir := []struct {
treepath string
event *info.Event
name string
expectedString string
provenance string
filterMessageSnippet string
wantErr string
}{
{
name: "test with badly formatted yaml",
event: &info.Event{
Organization: "tekton",
Repository: "cat",
SHA: "123",
},
treepath: "testdata/tree/badyaml",
wantErr: "error unmarshalling yaml file badyaml.yaml: yaml: line 2: did not find expected key",
},
}
for _, tt := range testGetTektonDir {
t.Run(tt.name, func(t *testing.T) {
observer, _ := zapobserver.New(zap.InfoLevel)
fakelogger := zap.New(observer).Sugar()
ctx, _ := rtesting.SetupFakeContext(t)
fakeclient, mux, teardown := tgitea.Setup(t)
defer teardown()
gvcs := Provider{
Client: fakeclient,
Logger: fakelogger,
}
if tt.provenance == "default_branch" {
tt.event.SHA = tt.event.DefaultBranch
} else {
shaDir := fmt.Sprintf("%x", sha256.Sum256([]byte(tt.treepath)))
tt.event.SHA = shaDir
}

tgitea.SetupGitTree(t, mux, tt.treepath, tt.event, false)
got, err := gvcs.GetTektonDir(ctx, tt.event, ".tekton", tt.provenance)
if tt.wantErr != "" {
assert.Assert(t, err != nil, "we should have get an error here")
assert.ErrorContains(t, err, tt.wantErr)
return
}
assert.NilError(t, err)
assert.Assert(t, strings.Contains(got, tt.expectedString), "expected %s, got %s", tt.expectedString, got)
})
}
}
93 changes: 93 additions & 0 deletions upstream/pkg/provider/gitea/test/setup.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package test

import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

"code.gitea.io/sdk/gitea"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -43,3 +49,90 @@ func Setup(t *testing.T) (*gitea.Client, *http.ServeMux, func()) {
assert.NilError(t, err)
return client, mux, tearDown
}

// SetupGitTree Take a dir and fake a full GitTree Gitea api calls reply recursively over a muxer.
func SetupGitTree(t *testing.T, mux *http.ServeMux, dir string, event *info.Event, recursive bool) {
entries := []gitea.GitEntry{}
type file struct {
sha, name string
isdir bool
}
files := []file{}
if recursive {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
sha := fmt.Sprintf("%x", sha256.Sum256([]byte(path)))
if err == nil && path != dir {
files = append(files, file{name: path, isdir: info.IsDir(), sha: sha})
}
return nil
})
assert.NilError(t, err)
} else {
dfiles, err := os.ReadDir(dir)
assert.NilError(t, err)

for _, f := range dfiles {
sha := fmt.Sprintf("%x", sha256.Sum256([]byte(f.Name())))
files = append(files, file{name: filepath.Join(dir, f.Name()), sha: sha, isdir: f.IsDir()})
}
}
for _, f := range files {
etype := "blob"
mode := "100644"
if f.isdir {
etype = "tree"
mode = "040000"
if !recursive {
SetupGitTree(t, mux, f.name,
&info.Event{
Organization: event.Organization,
Repository: event.Repository,
SHA: f.sha,
},
true)
}
} else {
mux.HandleFunc(fmt.Sprintf("/repos/%v/%v/git/blobs/%v", event.Organization, event.Repository, f.sha),
func(w http.ResponseWriter, r *http.Request) {
// go over all files and match the sha to the name we want
sha := filepath.Base(r.URL.Path)
chosenf := file{}
for _, f := range files {
if f.sha == sha {
chosenf = f
break
}
}
assert.Assert(t, chosenf.name != "", "sha %s not found", sha)

s, err := os.ReadFile(chosenf.name)
assert.NilError(t, err)
// encode content as base64
blob := &gitea.GitBlobResponse{
SHA: chosenf.sha,
Content: base64.StdEncoding.EncodeToString(s),
}
b, err := json.Marshal(blob)
assert.NilError(t, err)
fmt.Fprint(w, string(b))
})
}
entries = append(entries, gitea.GitEntry{
Path: strings.TrimPrefix(f.name, dir+"/"),
Mode: mode,
Type: etype,
SHA: f.sha,
})
}
u := fmt.Sprintf("/repos/%v/%v/git/trees/%v", event.Organization, event.Repository, event.SHA)
mux.HandleFunc(u, func(rw http.ResponseWriter, _ *http.Request) {
tree := &gitea.GitTreeResponse{
SHA: event.SHA,
Entries: entries,
}
// encode tree as json
b, err := json.Marshal(tree)
assert.NilError(t, err)
fmt.Fprint(rw, string(b))
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo:
bar:
xlxlxl
7 changes: 2 additions & 5 deletions upstream/pkg/provider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"go.uber.org/zap"
"golang.org/x/oauth2"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -410,10 +409,8 @@ func (v *Provider) concatAllYamlFiles(ctx context.Context, objects []*github.Tre
if err != nil {
return "", err
}
// validate yaml
var i any
if err := yaml.Unmarshal(data, &i); err != nil {
return "", fmt.Errorf("error unmarshalling yaml file %s: %w", value.GetPath(), err)
if err := provider.ValidateYaml(data, value.GetPath()); err != nil {
return "", err
}
if allTemplates != "" && !strings.HasPrefix(string(data), "---") {
allTemplates += "---"
Expand Down
Loading

0 comments on commit bd7fd16

Please sign in to comment.