-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Validate Slack field config and only allow the necessary input #1334
Conversation
config/notifiers.go
Outdated
return err | ||
} | ||
if c.Title == "" { | ||
return fmt.Errorf("missing title in Slack field configuration, value=%v", c.Value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't log c.Value
in the error.
config/notifiers.go
Outdated
return fmt.Errorf("missing title in Slack field configuration, value=%v", c.Value) | ||
} | ||
if c.Value == "" { | ||
return fmt.Errorf("missing value in Slack field configuration, title=%v", c.Title) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for c.Title
.
@@ -253,3 +253,43 @@ token: '' | |||
t.Errorf("\nexpected:\n%v\ngot:\n%v", expected, err.Error()) | |||
} | |||
} | |||
|
|||
func TestSlackFieldConfigValidation(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test case with a valid YAML connfiguration?
notify/impl.go
Outdated
for index, field := range n.conf.Fields { | ||
// Check if short was defined for the field otherwise fallback to the global setting | ||
var short bool | ||
if field.Short { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work as expected when field.Short
is set to false
on purpose. Short
needs to be a *bool
instead.
1e64169
to
7197159
Compare
@simonpasquier I've resolved all of the comments you made. Thank you for reviewing! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 small remarks, LGTM otherwise.
value: hello | ||
short: true | ||
- title: second | ||
value: world |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to validate that the unmarshaled data is matching what is expected when there's no error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I'll get this added.
notify/impl.go
Outdated
var fields = make([]config.SlackField, numFields) | ||
for index, field := range n.conf.Fields { | ||
// Check if short was defined for the field otherwise fallback to the global setting | ||
var short *bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit but I would do:
var short bool
if field.Short != nil {
short = *field.Short
} else {
short = n.conf.ShortFields
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main reason it is being set as it is currently is that I reused SlackField
from the config. package, which has Short
defined as a *bool
now. I initially had it like that, but changed it after the tests failed and caught the error. I can change the if statement to be as above, and then pass a pointer to the variable short
when building the SlackField
.
7197159
to
ddb8032
Compare
Signed-off-by: Trevor Wood <Trevor.G.Wood@gmail.com>
ddb8032
to
4d046e1
Compare
@simonpasquier I resolved your latest comments. If there's anything else you see in the latest batches of changes let me know and I'll get them pushed up. Thanks! |
LGTM |
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
Resolves the comments on the PR #1002 to update the documentation for Slack fields. Currently, any arbitrary key/value pairs can be defined; however, per the Slack documentation for defining fields [1] only the properties
title
,value
, andshort
can be defined with the first two being required for each field. This updates the Slack field configuration to only allow the aforementioned three properties as well as validates that both a title and value are defined for each property in theyaml.Unmarshaler
interface. Moreover, this allows the value forshort
to be defined on a per-field basis.