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

pullrequest-init: Add getters to make PullRequest nil-safe. #1017

Closed
wants to merge 1 commit into from

Conversation

wlynch
Copy link
Member

@wlynch wlynch commented Jun 26, 2019

Changes

Adds getters to all funcs so that libraries can safely handle the struct
without needing to worry about doing nil checks to avoid panics. This is
heavily influenced by the behavior of Go protobufs and other libraries
like https://github.com/google/go-github.

Also adds test coverage for uploadComments and uploadLabels for nil
lists.

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

None.

Adds getters to all funcs so that libraries can safely handle the struct
without needing to worry about doing nil checks to avoid panics. This is
heavily influenced by the behavior of Go protobufs and other libraries
like https://github.com/google/go-github.

Also adds test coverage for uploadComments and uploadLabels for nil
lists.
@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: wlynch
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: imjasonh

If they are not already assigned, you can assign the PR to them by writing /assign @imjasonh in a comment when ready.

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

@googlebot googlebot added the cla: yes Trying to make the CLA bot happy with ppl from different companies work on one commit label Jun 26, 2019
@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 26, 2019
@wlynch
Copy link
Member Author

wlynch commented Jun 26, 2019

/assign @imjasonh

Copy link
Member

@afrittoli afrittoli left a comment

Choose a reason for hiding this comment

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

Thank you for this, it looks good. The only question I have is weather returning a default value when invoking the getting on a nil may not have unwanted side effects.
It's good to have a nil-safe getters but it might be good in specific cases to check for nil values before using the the getter, to avoid the side effects.

@@ -265,14 +270,14 @@ func (h *GitHubHandler) updateExistingComments(ctx context.Context, comments map
merr = multierror.Append(merr, err)
continue
}
} else if dc.Text != ec.GetBody() {
} else if dc.GetText() != ec.GetBody() {
Copy link
Member

Choose a reason for hiding this comment

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

If L264 dc, ok := comments[ec.GetID()] returns nil, true the comment ID is in comments but the value is nil, so we're probably in an error situation, since it shouldn't be possible in normal conditions to for the desired comment to be nil. With the nil-safe getter this condition here will be met and we will update the comment to "". I think this is not the desired behaviour.

if _, _, err := h.Issues.CreateComment(ctx, h.owner, h.repo, h.prNum, c); err != nil {
h.Logger.Warnf("Error creating comment: %v", err)
merr = multierror.Append(merr, err)
}
}
return merr
}

func githubCommentToTekton(c *github.IssueComment, pathPrefix string) *Comment {
Copy link
Member

Choose a reason for hiding this comment

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

Just curious, why did you place this when it's only used to build Comment for testing?

Copy link
Member

Choose a reason for hiding this comment

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

right, if it's only used for the test, let's add it in test files.

Copy link
Collaborator

@bobcatfish bobcatfish left a comment

Choose a reason for hiding this comment

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

hmm I have some mixed feelings about this! I have definitely fallen victim to not checking for nil before, so this seems pretty useful, but I think if we're going to do it, we should commit to doing it for all our types. And like @afrittoli said, it may not always make sense to return a default value, tho we could be careful about choosing it. It would probably clean up a lot of code to be able to iterate over lists without worrying they are nil, e.g.

if inputs != nil {
return validateResources(inputs.Resources, providedResources)
}

What do other OWNERS think @vdemeester @dlorenc @imjasonh (and soon) @abayer ?

@@ -41,7 +41,10 @@ func TestFakeGitHubComments(t *testing.T) {
t.Fatalf("List Issues: wanted [], got %+v, %+v, %v", got, resp, err)
}

if _, _, err := client.Issues.CreateComment(ctx, owner, repo, prNum, comment); err != nil {
comment, _, err := client.Issues.CreateComment(ctx, owner, repo, prNum, &github.IssueComment{
Copy link
Collaborator

Choose a reason for hiding this comment

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

i might just not be seeing it, but I can't find where the comment var was declared before this change :O Was this a bug in the tests? If so that would mean they aren't running in CI and there's no linting happening... 😨

Copy link
Member

Choose a reason for hiding this comment

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

😨 😨 😨 😨 😨 😨 😨 😨

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

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

So, in general, I am not a huge fan of Getter, especially in go 👼

@@ -41,7 +41,10 @@ func TestFakeGitHubComments(t *testing.T) {
t.Fatalf("List Issues: wanted [], got %+v, %+v, %v", got, resp, err)
}

if _, _, err := client.Issues.CreateComment(ctx, owner, repo, prNum, comment); err != nil {
comment, _, err := client.Issues.CreateComment(ctx, owner, repo, prNum, &github.IssueComment{
Copy link
Member

Choose a reason for hiding this comment

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

😨 😨 😨 😨 😨 😨 😨 😨

if _, _, err := h.Issues.CreateComment(ctx, h.owner, h.repo, h.prNum, c); err != nil {
h.Logger.Warnf("Error creating comment: %v", err)
merr = multierror.Append(merr, err)
}
}
return merr
}

func githubCommentToTekton(c *github.IssueComment, pathPrefix string) *Comment {
Copy link
Member

Choose a reason for hiding this comment

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

right, if it's only used for the test, let's add it in test files.

@bobcatfish
Copy link
Collaborator

We ended up running into an issue in our 0.5.0 release (#1075 ) that was due to trying to access an attribute on a nil value - I agree with what @vdemeester said about using getters not being very golang-y (is there a proper word for that?) but I also wonder if we can make our types a bit more safe.

Seems like this isn't the direction we want to go at the moment tho 🤔

@bobcatfish
Copy link
Collaborator

I'm going to close this for now assuming we don't want to go in this direction but please re-open if I'm wrong!

@bobcatfish bobcatfish closed this Aug 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Trying to make the CLA bot happy with ppl from different companies work on one commit 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.

7 participants