Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit f271e71

Browse files
committed
No matching rules does not generate a PipelineRun.
This changes the Convert function for the dsl to return a nil PipelineRun if there are no tasks (apart from the git-clone task).
1 parent 2b356b7 commit f271e71

File tree

6 files changed

+55
-50
lines changed

6 files changed

+55
-50
lines changed

pkg/dsl/dsl_handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ func (h *Handler) push(ctx context.Context, evt *scm.PushHook, w http.ResponseWr
105105
http.Error(w, err.Error(), http.StatusInternalServerError)
106106
return
107107
}
108+
if pr == nil {
109+
return
110+
}
108111
created, err := h.pipelineClient.TektonV1beta1().PipelineRuns(h.namespace).Create(pr)
109112
if err != nil {
110113
h.log.Errorf("error creating pipelinerun file: %s", err)

pkg/dsl/dsl_handler_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,34 @@ func TestHandlePushEventNoPipeline(t *testing.T) {
127127
}
128128
}
129129

130+
func TestHandlePushEventNoMatchingRules(t *testing.T) {
131+
as := test.MakeAPIServer(t, "/api/v3/repos/Codertocat/Hello-World/contents/.tekton_ci.yaml", "6113728f27ae82c7b1a177c8d03f9e96e0adf246", "testdata/content_match_only_master.json")
132+
defer as.Close()
133+
scmClient, err := factory.NewClient("github", as.URL, "", factory.Client(as.Client()))
134+
if err != nil {
135+
t.Fatal(err)
136+
}
137+
gitClient := git.New(scmClient, secrets.NewMock(), metrics.NewMock())
138+
fakeTektonClient := fakeclientset.NewSimpleClientset()
139+
fakeClient := fake.NewSimpleClientset()
140+
vc := volumes.New(fakeClient)
141+
logger := zaptest.NewLogger(t, zaptest.Level(zap.WarnLevel))
142+
h := New(gitClient, fakeTektonClient, vc, metrics.NewMock(), testConfiguration(), testNS, logger.Sugar())
143+
req := test.MakeHookRequest(t, "../testdata/github_push.json", "push")
144+
rec := httptest.NewRecorder()
145+
146+
h.ServeHTTP(rec, req)
147+
148+
w := rec.Result()
149+
if w.StatusCode != http.StatusOK {
150+
t.Fatalf("got %d, want %d: %s", w.StatusCode, http.StatusOK, mustReadBody(t, w))
151+
}
152+
_, err = fakeTektonClient.TektonV1beta1().PipelineRuns(testNS).Get("", metav1.GetOptions{})
153+
if !errors.IsNotFound(err) {
154+
t.Fatalf("pipelinerun was created with no matching rules")
155+
}
156+
}
157+
130158
func mustReadBody(t *testing.T, req *http.Response) []byte {
131159
t.Helper()
132160
b, err := ioutil.ReadAll(req.Body)

pkg/dsl/scripts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ func Convert(p *ci.Pipeline, log logger.Logger, config *Configuration, src *Sour
9191
if len(p.AfterScript) > 0 {
9292
tasks = append(tasks, makeScriptTask(afterStepTaskName, previous, env, p.Image, p.AfterScript))
9393
}
94+
if len(tasks) == 1 {
95+
return nil, nil
96+
}
9497
spec := pipelinev1.PipelineRunSpec{
9598
ServiceAccountName: config.DefaultServiceAccountName,
9699
Workspaces: []pipelinev1.WorkspaceBinding{

pkg/dsl/scripts_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ func readPipelineRunFixture(t *testing.T, filename string) *pipelinev1.PipelineR
425425
if err != nil {
426426
t.Fatalf("failed to read %s: %s", filename, err)
427427
}
428-
var pr pipelinev1.PipelineRun
428+
var pr *pipelinev1.PipelineRun
429429
err = yaml.Unmarshal(b, &pr)
430430
if err != nil {
431431
t.Fatalf("failed to unmarshal %s: %s", filename, err)
432432
}
433-
return &pr
433+
return pr
434434
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": ".tekton_ci.yml",
3+
"path": ".tekton_ci.yml",
4+
"sha": "980a0d5f19a64b4b30a87d4206aade58726b60e3",
5+
"size": 13,
6+
"url": "https://api.github.com/repos/octocat/Hello-World/contents/README?ref=7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
7+
"html_url": "https://github.com/octocat/Hello-World/blob/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/README",
8+
"git_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/980a0d5f19a64b4b30a87d4206aade58726b60e3",
9+
"download_url": "https://raw.githubusercontent.com/octocat/Hello-World/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/README",
10+
"type": "file",
11+
"content": "aW1hZ2U6IGdvbGFuZzpsYXRlc3QKdmFyaWFibGVzOgogIFJFUE9fTkFNRTogZ2l0aHViLmNvbS9iaWdrZXZtY2QvZ2l0aHViLXRvb2wKCnN0YWdlczoKICAtIHRlc3QKCmZvcm1hdDoKICBzdGFnZTogdGVzdAogIHNjcmlwdDoKICAgIC0gZ28gdGVzdCAtcmFjZSAkKGdvIGxpc3QgLi8uLi4gfCBncmVwIC12IC92ZW5kb3IvKQogIHJ1bGVzOgogICAgLSBpZjogdmFycy5DSV9DT01NSVRfQlJBTkNIICE9ICJtYXN0ZXIiCiAgICAgIHdoZW46IG5ldmVyCg==",
12+
"encoding": "base64",
13+
"_links": {
14+
"self": "https://api.github.com/repos/octocat/Hello-World/contents/README?ref=7fd1a60b01f91b314f59955a4e4d4e80d8edf11d",
15+
"git": "https://api.github.com/repos/octocat/Hello-World/git/blobs/980a0d5f19a64b4b30a87d4206aade58726b60e3",
16+
"html": "https://github.com/octocat/Hello-World/blob/7fd1a60b01f91b314f59955a4e4d4e80d8edf11d/README"
17+
}
18+
}
Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1 @@
1-
apiVersion: tekton.dev/v1beta1
2-
kind: PipelineRun
3-
metadata:
4-
annotations:
5-
tekton.dev/ci-source-ref: refs/pulls/4
6-
tekton.dev/ci-source-url: https://github.com/bigkevmcd/github-tool.git
7-
tekton.dev/ci-hook-id: "26400635-d8f4-4cf5-a45f-bd03856bdf2b"
8-
creationTimestamp: null
9-
generateName: my-pipeline-run-
10-
labels:
11-
app.kubernetes.io/managed-by: dsl
12-
app.kubernetes.io/part-of: Tekton-CI
13-
spec:
14-
pipelineSpec:
15-
tasks:
16-
- name: git-clone
17-
taskSpec:
18-
steps:
19-
- command:
20-
- /ko-app/git-init
21-
- -url
22-
- https://github.com/bigkevmcd/github-tool.git
23-
- -revision
24-
- refs/pulls/4
25-
- -path
26-
- $(workspaces.source.path)
27-
env:
28-
- name: REPO_NAME
29-
value: github.com/bigkevmcd/github-tool
30-
- name: CI_PROJECT_DIR
31-
value: $(workspaces.source.path)
32-
- name: TEKTON_RESOURCE_NAME
33-
value: tekton-ci-git-clone
34-
image: gcr.io/tekton-releases/github.com/tektoncd/pipeline/cmd/git-init
35-
name: git-clone
36-
resources: {}
37-
workspaces:
38-
- name: source
39-
workspaces:
40-
- name: source
41-
workspace: git-checkout
42-
workspaces:
43-
- name: git-checkout
44-
serviceAccountName: test-account
45-
workspaces:
46-
- name: git-checkout
47-
persistentVolumeClaim:
48-
claimName: my-volume-claim-123
1+
null

0 commit comments

Comments
 (0)