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

apply triggertemplate param default value if body/header based triggerbinding param missing from body/header #761

Merged

Conversation

gabemontero
Copy link
Contributor

Changes

Fixes #568

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

See the contribution guide for more details.

Release Notes

```release-note
NONE
```

/assign @dibyom

@tekton-robot tekton-robot added the release-note-none Denotes a PR that doesnt merit a release note. label Sep 18, 2020
@tekton-robot tekton-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 18, 2020
Copy link
Contributor

@savitaashture savitaashture left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding sample example to examples to show usage of this or any doc update which provide this information that there is a support of default values if not found from binding

if err != nil {
return nil, fmt.Errorf("failed to ApplyEventValuesToParams: %w", err)
}

return MergeInDefaultParams(out, ttParams), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need to call MergeInDefaultParams since we are now adding default values in applyEventValuesToParams itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we do. Unit tests fail without it:

using default defaultVal since failed to replace JSONPath value for param p2: $(body.p2): error: p2 is not found
--- FAIL: TestResolveParams (0.00s)
    --- FAIL: TestResolveParams/add_default_values_for_params_with_missing_values (0.00s)
        event_test.go:351: didn't get expected params -want + got:   []v1alpha1.Param{
              	{Name: "p1", Value: "val1"},
            - 	{Name: "p2", Value: "defaultVal"},
              }
FAIL
FAIL	github.com/tektoncd/triggers/pkg/template	0.062s
FAIL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, interesting. I think that makes sense. But makes me wonder if we can simplify this code somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok @dibyom I've pushed a separate commit that consolidates applyEventValuesToParams and mergeInDefaultParams

it is possible the changes pulled in with the rebase helped things (the method was minimally switched from public to package only)

otherwise, the biggest takeaway is that an explicit for loop on the default array has been replaced with a golang map lookup.... that is positive.

If you like this approach better I'll squash the commits.

// if the header or body was not supplied or was malformed, go with a default if it exists
for _, ps := range defaults {
if ps.Name == p.Name && ps.Default != nil {
fmt.Fprintf(os.Stdout, "using default %s since failed to replace JSONPath value for param %s: %s: error: %s\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useful information, but I'm not sure we need to print it out. If we do, maybe we can pass through the logger that we for other logging instead of using fmt.Fprintf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I looked at that and found it a bit onerous to pass the logger through all the call levels to get it here.

But if this is not an tenable compromise, then sure. I can go one way or the other. I don't have a strong opinion. Will it be feasible for a user to figure out that if the default is used vs. the body/header reference, there was an issue with how it was constructed?

You got a final call here @dibyom ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think its fine to remove the log statement here given that this is documented/expected behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok just pushed removal of log statement as part of rebasing this PR on top of recent changes

@gabemontero
Copy link
Contributor Author

How about adding sample example to examples to show usage of this or any doc update which provide this information that there is a support of default values if not found from binding

Per #568 (comment) the existing example could be tweaked to leverage this, where the fact that the default is getting used is highlighted. Would that be amenable vs. creating a separate example?

I did scan the docs and was unclear on where to update. Suggestions on how to cross reference https://github.com/tektoncd/triggers/blob/master/docs/triggertemplates.md with https://github.com/tektoncd/triggers/blob/master/docs/triggerbindings.md to connect the dots @savitaashture @dibyom ?

@tekton-robot tekton-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 23, 2020
@dibyom
Copy link
Member

dibyom commented Oct 5, 2020

Per #568 (comment) the existing example could be tweaked to leverage this, where the fact that the default is getting used is highlighted. Would that be amenable vs. creating a separate example?

Sure as long as we have an example that shows that we substitute the default value when one from the event is missing, I'm ok!

I did scan the docs and was unclear on where to update. Suggestions on how to cross reference https://github.com/tektoncd/triggers/blob/master/docs/triggertemplates.md with https://github.com/tektoncd/triggers/blob/master/docs/triggerbindings.md to connect the dots

In the parameters section of TriggerTemplates, we can add something about the default field and when the default value is applied.

Similarly, in the TriggerBindings docs, we can add a something about the behavior maybe, as sub-section inside the Event Variable Interpolation section -- if we fail to resolve the param value from the event, we'll look to see if the trigger template param has a default value. If it doesn't we throw an error.

@gabemontero gabemontero force-pushed the apply-default-if-param-missing branch from 5df7ef2 to 300539d Compare October 7, 2020 20:04
@tekton-robot tekton-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 7, 2020
@gabemontero gabemontero force-pushed the apply-default-if-param-missing branch from bfe8d6c to 69d7a97 Compare October 7, 2020 21:01
@gabemontero
Copy link
Contributor Author

Per #568 (comment) the existing example could be tweaked to leverage this, where the fact that the default is getting used is highlighted. Would that be amenable vs. creating a separate example?

Sure as long as we have an example that shows that we substitute the default value when one from the event is missing, I'm ok!

OK pushed commit 64735d2 to try and capture this

@gabemontero
Copy link
Contributor Author

In the parameters section of TriggerTemplates, we can add something about the default field and when the default value is applied.

Similarly, in the TriggerBindings docs, we can add a something about the behavior maybe, as sub-section inside the Event Variable Interpolation section -- if we fail to resolve the param value from the event, we'll look to see if the trigger template param has a default value. If it doesn't we throw an error.

doc updates following these recommendations in 774fb5c

tests have been passing previously, so I'm assuming this doc only change will not affect things

PTAL @dibyom @savitaashture

thanks!!

@dibyom
Copy link
Member

dibyom commented Oct 8, 2020

Nice cleanup! Would you mind squashing up the commits?

@dibyom
Copy link
Member

dibyom commented Oct 8, 2020

/approve

@tekton-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dibyom

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 8, 2020
@gabemontero gabemontero force-pushed the apply-default-if-param-missing branch from 774fb5c to f1ea098 Compare October 8, 2020 16:36
@gabemontero
Copy link
Contributor Author

commits squashed @dibyom

@dibyom
Copy link
Member

dibyom commented Oct 8, 2020

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Oct 8, 2020
@tekton-robot tekton-robot merged commit 6420b23 into tektoncd:master Oct 8, 2020
@gabemontero gabemontero deleted the apply-default-if-param-missing branch October 8, 2020 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesnt merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TriggerTemplate default value is not applied if the param is missing from Binding
4 participants