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

Fix issue with method defaults #447

Merged
merged 5 commits into from
Jul 6, 2022
Merged

Conversation

shravan1k
Copy link
Contributor

When certain attributes are defined under 'methods', the other undefined attributes don't line up with the promised defaults. This PR resolves this issue by checking whether certain attributes were not specified and then adds defaults for them.

Closes #434

GithubReview: &defaultBool,
}
} else {
if len(methods.Comments) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

I think you could simplify both the if and the else into one. Effectively, if methods == nil, then methods.Comments and methods.GithubReview will also be nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, will make the change.

policy/common/methods.go Show resolved Hide resolved
@@ -57,15 +57,19 @@ type RequestReview struct {

func (opts *Options) GetMethods() *common.Methods {
methods := opts.Methods
defaultBool := true
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use a more descriptive name here and in the other instances where defaultBool has been added. Perhaps defaultGithubReview?

policy/approval/approve.go Show resolved Hide resolved
@shravan1k shravan1k requested review from derekjobst and asvoboda July 5, 2022 16:32
Copy link
Member

@bluekeyes bluekeyes left a comment

Choose a reason for hiding this comment

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

One comment about the slice handling, but otherwise this looks good.

We'll want to be careful about noting this in the release notes because it is technically a breaking change. As Derek alluded to, if I have a policy like:

options:
  methods:
    github_review: true

it currently disables comments. In order to keep the same behavior after this PR, I'll have to change the policy to:

options:
  methods:
    comments: []
    github_review:  true

This is consistent with what the docs have always claimed, so I'd count this a bug fix, but people might have been relying on the old broken behavior. We might want to audit at least our internal policies to see if this will impact anything.

CommentPatterns []Regexp `yaml:"comment_patterns,omitempty"`
GithubReview bool `yaml:"github_review,omitempty"`
GithubReviewCommentPatterns []Regexp `yaml:"github_review_comment_patterns,omitempty"`
Comments *[]string `yaml:"comments,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Making this a pointer is not necessary. In Go, slices implicitly work like pointers but we don't usually think about it because a nil slice behaves the same as a non-nil empty slice. When a user does not specify the comments field in configuration, the value will be nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Will revert that back.

@@ -82,10 +87,10 @@ func (r *Rule) Trigger() common.Trigger {

if r.Requires.Count > 0 {
m := r.Options.GetMethods()
if len(m.Comments) > 0 || len(m.CommentPatterns) > 0 {
if m.Comments != nil && len(*m.Comments) > 0 || len(m.CommentPatterns) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

These nil checks and dereferencing are not necessary once m.Comments is reverted back to a []string because len(nil) == 0

for _, comment := range m.Comments {
if strings.Contains(commentBody, comment) {
return true
if m.Comments != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Same idea here with the nil check and dereferencing: you can range over a nil slice without checking first.

@shravan1k shravan1k requested a review from bluekeyes July 6, 2022 16:41
@bluekeyes bluekeyes merged commit 30d816f into develop Jul 6, 2022
@bluekeyes bluekeyes deleted the skonduru/fix-method-defaults branch July 6, 2022 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Overriding approval methods clears all defaults
4 participants