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

Check for nil pointer in update rule parameters #2854

Merged
merged 4 commits into from Aug 3, 2023

Conversation

o-sama
Copy link
Contributor

@o-sama o-sama commented Aug 2, 2023

Since discussing this with the GitHub folks, I’ve been told that the update_allows_fetch_and_merge parameter for the update rule only applies to forked repos. Note: I was told this is still bugged for forked repos when sending a request through the API, but will be fixed in the near future.

Since it only applies to forked repos, the API returns a ruleset which contains:

{
   "type": “update"
}

This doesn't have a parameters field, so when we try to unmarshal the JSON we run into a nil pointer dereference error. No functionality is changed here except to check for a nil pointer before we try to unmarshal the JSON, and if it isn’t included then we just set the parameters to nil.

I didn’t think this needed a change in testing because it doesn’t change any functionality, but I’ll be happy to go and add something if you think otherwise.

@gmlewis I’d really appreciate if this can be part of the upcoming release (which I know is short notice) since I’d love for it to be included in integrations/terraform-provider-github#1808. No worries if it’s too late though, I can just keep it out for now. Thanks!!

@codecov
Copy link

codecov bot commented Aug 2, 2023

Codecov Report

Merging #2854 (8238725) into master (f897b2c) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2854   +/-   ##
=======================================
  Coverage   98.06%   98.06%           
=======================================
  Files         138      138           
  Lines       12318    12327    +9     
=======================================
+ Hits        12080    12089    +9     
  Misses        162      162           
  Partials       76       76           
Files Changed Coverage Δ
github/repos_rules.go 100.00% <100.00%> (ø)

Copy link
Collaborator

@gmlewis gmlewis 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, @o-sama!
Please add a new unit test to cover this case.
Yes, we can hold off on the next release until this PR is merged.

if err := json.Unmarshal(*RepositoryRule.Parameters, &params); err != nil {
return err
}
if RepositoryRule.Parameters != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's please change this to make it easier to read:

if RepositoryRule.Parameters == nil {
  r.Parameters = nil
  return 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 wrote it this way since there’s a return nil at the end of the function, the rest of the switch statement also only returns if there’s an error, and just sets r.Parameters to nil and lets the return nil at the end of the function take care of it. Do you think I should keep it this way or break the convention for the update case only?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, I understand, but I want to highlight the "happy path" and provide the early return which is idiomatic Go.
Please switch to my suggestion, and feel free to put a blank line after the new block if you think it reads better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Works for me, I’ll make the change in a bit 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, also added a bit in the update rule constructor to check for empty params since this specific rule is a bit different than others 🙂

if RepositoryRule.Parameters == nil {
r.Parameters = nil
return nil
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

One of the points was to remove the "else" and un-indent the following section, please:

Suggested change
} else {
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You’re totally right, I think I had a brain freeze. Don’t know why I kept an else with a return in there.

Copy link
Collaborator

@gmlewis gmlewis 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, @o-sama !
LGTM.

Awaiting second LGTM+Approval from any other contributor to this repo before merging.

@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Aug 3, 2023
@o-sama
Copy link
Contributor Author

o-sama commented Aug 3, 2023

Thanks @gmlewis! sorry about the sloppiness earlier with the requested change 🙏

Copy link
Contributor

@liaodaniel liaodaniel left a comment

Choose a reason for hiding this comment

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

LGTM

@gmlewis
Copy link
Collaborator

gmlewis commented Aug 3, 2023

Thanks @gmlewis! sorry about the sloppiness earlier with the requested change 🙏

No problem! Sorry for the delayed responses... Work sometimes gets in the way. 😂

@gmlewis
Copy link
Collaborator

gmlewis commented Aug 3, 2023

Thank you, @liaodaniel !
Merging.

@gmlewis gmlewis removed the NeedsReview PR is awaiting a review before merging. label Aug 3, 2023
@gmlewis gmlewis merged commit a8da03b into google:master Aug 3, 2023
9 checks passed
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.

None yet

3 participants