Skip to content
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

Resolving newline in TriggerBinding param #953

Closed
wants to merge 1 commit into from
Closed
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
78 changes: 77 additions & 1 deletion pkg/sink/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,83 @@ func TestHandleEvent(t *testing.T) {
TaskRef: &pipelinev1.TaskRef{Name: "git-clone"},
},
}},
}}
},
{
name: "single trigger with newline resolved param",
resources: test.Resources{
EventListeners: []*triggersv1.EventListener{{
ObjectMeta: metav1.ObjectMeta{
Name: eventListenerName,
Namespace: namespace,
},
Spec: triggersv1.EventListenerSpec{
Triggers: []triggersv1.EventListenerTrigger{{
Name: "git-clone-trigger",
Bindings: []*triggersv1.EventListenerBinding{{
Ref: "git-clone",
Kind: triggersv1.NamespacedTriggerBindingKind,
}},
Template: &triggersv1.EventListenerTemplate{
Ref: ptr.String("git-clone"),
},
}},
},
}},
TriggerBindings: []*triggersv1.TriggerBinding{{
ObjectMeta: metav1.ObjectMeta{
Name: "git-clone",
Namespace: namespace,
},
Spec: triggersv1.TriggerBindingSpec{
Params: []triggersv1.Param{
{Name: "url", Value: `{"a":\n{"b":"c"}\n}`},
{Name: "revision", Value: `test\nrevision`},
{Name: "name", Value: "git-clone-run"},
{Name: "app", Value: "$(body.foo)"},
{Name: "type", Value: "$(header.Content-Type)"},
},
},
}},
TriggerTemplates: []*triggersv1.TriggerTemplate{{
ObjectMeta: metav1.ObjectMeta{
Name: "git-clone",
Namespace: namespace,
Annotations: map[string]string{
"triggers.tekton.dev/old-escape-quotes": "true",
},
},
Spec: gitCloneTTSpec,
}},
},
eventBody: eventBody,
want: []pipelinev1.TaskRun{{
TypeMeta: metav1.TypeMeta{
APIVersion: "tekton.dev/v1beta1",
Kind: "TaskRun",
},
ObjectMeta: metav1.ObjectMeta{
Name: "git-clone-run",
Namespace: namespace,
Labels: map[string]string{
"app": "bar\t\r\nbaz昨",
"type": "application/json",
"triggers.tekton.dev/eventlistener": eventListenerName,
"triggers.tekton.dev/trigger": "git-clone-trigger",
"triggers.tekton.dev/triggers-eventid": "12345",
},
},
Spec: pipelinev1.TaskRunSpec{
Params: []pipelinev1.Param{{
Name: "url",
Value: pipelinev1.ArrayOrString{Type: pipelinev1.ParamTypeString, StringVal: "{\"a\":\n{\"b\":\"c\"}\n}"},
}, {
Name: "git-revision",
Value: pipelinev1.ArrayOrString{Type: pipelinev1.ParamTypeString, StringVal: "test\nrevision"},
}},
TaskRef: &pipelinev1.TaskRef{Name: "git-clone"},
},
}},
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// TODO: Do we ever support multiple eventListeners? Maybe change test.Resources to only accept one?
Expand Down
5 changes: 3 additions & 2 deletions pkg/template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ func applyParamsToResourceTemplate(params []triggersv1.Param, rt json.RawMessage
func applyParamToResourceTemplate(param triggersv1.Param, rt json.RawMessage, oldEscape bool) json.RawMessage {
// Assume the param is valid
paramVariable := fmt.Sprintf("$(tt.params.%s)", param.Name)
paramValue := strings.Replace(param.Value, "\n", "\\n", -1)
// Escape quotes so that that JSON strings can be appended to regular strings.
// See #257 for discussion on this behavior.
if oldEscape {
paramValue := strings.Replace(param.Value, `"`, `\"`, -1)
paramValue = strings.Replace(paramValue, `"`, `\"`, -1)
return bytes.Replace(rt, []byte(paramVariable), []byte(paramValue), -1)
}
return bytes.Replace(rt, []byte(paramVariable), []byte(param.Value), -1)
return bytes.Replace(rt, []byte(paramVariable), []byte(paramValue), -1)
}

// UUID generates a Universally Unique IDentifier following RFC 4122.
Expand Down
22 changes: 22 additions & 0 deletions pkg/template/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ func Test_applyParamToResourceTemplate(t *testing.T) {
rt: json.RawMessage(`{"foo": $(tt.params.p1)}`),
},
want: json.RawMessage(`{"foo": {"a":"b"}}`),
}, {
name: "escape newline in param val with JSON requires oldescape",
args: args{
param: triggersv1.Param{
Name: "p1",
Value: `{"a":\n{"b":"c"}\n}`,
},
rt: json.RawMessage(`{"foo": "$(tt.params.p1)"}`),
},
oldEscape: true,
want: json.RawMessage(`{"foo": "{\"a\":\n{\"b\":\"c\"}\n}"}`),
}, {
name: "escape newline in param val",
args: args{
param: triggersv1.Param{
Name: "p1",
Value: `test
value`,
},
rt: json.RawMessage(`{"foo": "$(tt.params.p1)"}`),
},
want: json.RawMessage(`{"foo": "test\nvalue"}`),
}, {
name: "escape quotes in param val - old escaping",
args: args{
Expand Down