Skip to content

Adds required pull request reviews #512

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

Closed

Conversation

alindeman
Copy link
Contributor

required_pull_request_reviews is a recently introduced object that will soon be required:

/cc: @sr

Copy link
Member

@dmitshur dmitshur left a comment

Choose a reason for hiding this comment

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

I have one question about omitempty. Everything else looks good.

// RequiredPullRequestReviews represents the protection configuration for pull requests.
type RequiredPullRequestReviews struct {
// Enforce pull request reviews for repository administrators.
IncludeAdmins *bool `json:"include_admins,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

include_admins key is documented as:

Required. Enforce required status checks for repository administrators.

Shouldn't we not use omitempty option for required parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for bringing this up.

I was mostly following what I thought was a convention. As another example, something like creating a repository requires a name in the same manner, but name is omitempty on the Repository struct. It seems this pattern is pretty common in this codebase.

I also initially thought that in the absence of omitempty, failing to provide a value would send null along which might evaluate to false server-side, and therefore be surprising. However, on actually testing this assumption, it appears that GitHub validates that include_admins is strictly a boolean and will raise an error if null is passed. The assumption was wrong.

If you think that going forward it's a good idea for required values to, well, omit omitempty, I'm on board and will make it so for this property. Let me know what you think.

Copy link
Member

@dmitshur dmitshur Jan 9, 2017

Choose a reason for hiding this comment

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

Yeah, I saw that similar fields above that are required also have omitempty, and I figured you probably just followed the pattern. I wanted to bring it up so we could discuss the best thing to do from now on.

I do think leaving out omitempty would be a more appropriate thing to do, and it's better to start doing it late than never. There are at least a few places where it's already done, e.g., see here:

What if we go a step further to increase the mapping between a required field and it always being included by making it a value rather than pointer? E.g.:

IncludeAdmins bool `json:"include_admins"` // Required.

That eliminates any invalid value such as "null" being possible.

Thoughts?

Copy link
Contributor Author

@alindeman alindeman Jan 11, 2017

Choose a reason for hiding this comment

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

@shurcooL 👍 I like that it exposes the requirements of the API in the type itself. I've made it so.

I also pushed 2a12ccd to backport this convention to RequiredStatusChecks because it also includes similarly required parameters like include_admins. I realize this is a breaking change, but I feel like it's a good idea to remain consistent within the structures that the {Create,Update}BranchProtection functions use. Furthermore, the GitHub HTTP API is itself still beta and open to breaking changes. If you think it's a bad idea, though, I can lop that commit off.

Copy link
Member

Choose a reason for hiding this comment

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

I like that it exposes the requirements of the API in the type itself. I've made it so.

Great!

If you think it's a bad idea, though, I can lop that commit off.

Not at all, on the contrary, I think that's absolutely the way to go. Thank you for doing that.

Yes, the GitHub API is still in beta so I think it's fine to break it slightly, since it's an improvement.

/cc @varadarajana FYI, see above. You'll need to make a small change to your PR #509 as a result.

By making it a value, rather than a pointer to a value, we drive home the point
that it must be specified and is always present in responses.
Required fields should be values, not pointers, because a lack of a value is an
invalid specification.
@alindeman alindeman force-pushed the required-pull-request-reviews branch from 7634edb to 2a12ccd Compare January 11, 2017 01:47
@dmitshur
Copy link
Member

Hmm, what about BranchRestrictionsRequest?

I can see that in https://developer.github.com/v3/repos/branches/#update-branch-protection it has 2 keys, and unlike the required_status_checks and required_pull_request_reviews, they don't have Required. text. However, it does say the following for all 3:

The required_status_checks object must have the following keys

The required_pull_request_reviews object must have the following keys

The restrictions object must have the following keys

Emphasis mine. It kinda sounds like it's implied that they're also required... Not sure what to do here. Do you think it's worth clarifying with GitHub support, @alindeman?

@dmitshur
Copy link
Member

Another observation I want to point out. Both Protection and ProtectionRequest reuse the same structs for RequiredStatusChecks and RequiredPullRequestReviews.

It's true that the fields in those structs are documented as required for the https://developer.github.com/v3/repos/branches/#update-branch-protection endpoint, but the change from pointer to value will affect the Protection object used during unmarshaling. We are assuming that since the input fields are required, they should be always present as part of the output from https://developer.github.com/v3/repos/branches/#get-branch-protection endpoint too.

If that isn't the case, it would no longer be possible to detect if a zero value came from the response, or if the response didn't contain a value.

This is quite subtle, and I think it's not going to be a problem, but I just wanted to point it out for review purposes.

Copy link
Member

@dmitshur dmitshur left a comment

Choose a reason for hiding this comment

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

See 2 comments above. We might want to also change BranchRestrictionsRequest struct.

Aside from that, this change LGTM as it currently stands.

@gmlewis
Copy link
Collaborator

gmlewis commented Jan 11, 2017

Thank you, @alindeman and @shurcooL!
It seems to me like your interpretation of the GitHub API docs is totally reasonable and that we can run with it. We can always revisit if we discover issues.
LGTM.
I'll let this sit for a day if there are any other comments, then merge.

@gmlewis gmlewis added the lgtm label Jan 11, 2017
@alindeman
Copy link
Contributor Author

@shurcooL I didn't get to look into your questions yesterday like I hoped, but I'll give these a look later today my time (US East)

@alindeman
Copy link
Contributor Author

I can see that in https://developer.github.com/v3/repos/branches/#update-branch-protection it has 2 keys, and unlike the required_status_checks and required_pull_request_reviews, they don't have Required. text. However, it does say the following for all 3:

The required_status_checks object must have the following keys
The required_pull_request_reviews object must have the following keys
The restrictions object must have the following keys
Emphasis mine. It kinda sounds like it's implied that they're also required... Not sure what to do here. Do you think it's worth clarifying with GitHub support, @alindeman?

I explored the API by sending it some different permutations of requests and found that, you're right, users and teams are required if restrictions is passed a non-null value. And if it's passed, the response always contains the users and teams as well. I'll make them non-pointers as well in an upcoming commit.

@alindeman
Copy link
Contributor Author

If that isn't the case, it would no longer be possible to detect if a zero value came from the response, or if the response didn't contain a value.

This is quite subtle, and I think it's not going to be a problem, but I just wanted to point it out for review purposes.

Agreed. While I think it's true that it would be possible for the GitHub API to return only a subset of the required keys in responses, I think in practice it would be a bug given the constraints they've documented and appear to be enforcing server-side with JSON Schema.

@gmlewis
Copy link
Collaborator

gmlewis commented Jan 13, 2017

LGTM.
Merging.

@gmlewis gmlewis closed this in 5e7fada Jan 13, 2017
@gmlewis
Copy link
Collaborator

gmlewis commented Jan 13, 2017

Integrated in 5e7fada

dmitshur added a commit that referenced this pull request Jan 17, 2017
All of those fields are documented as required at
https://developer.github.com/v3/repos/branches/#update-branch-protection.

In #512, we changed the API to better reflect that. However,
it should still be documented that the fields are required. This
is consistent with the other required fields that are also documented
as required.

Additionally, note that empty slices must be non-nil. From running integration
tests, I've seen that passing nil slices (which get encoded as null in via encoding/json)
resulted in invalid request errors such as:

	No subschema in "anyOf" matched.
	For 'properties/users', nil is not an array.
	For 'properties/teams', nil is not an array.
	Not all subschemas of "allOf" matched.
	For 'anyOf/1', {"users"=>nil, "teams"=>nil} is not a null. []

Follows #512.
dmitshur added a commit that referenced this pull request Jan 17, 2017
All of those fields are documented as required at
https://developer.github.com/v3/repos/branches/#update-branch-protection.

In #512, we changed the API to better reflect that. However, it should
still be documented that the fields are required. This is consistent
with the other required fields that are also documented as required.

Additionally, note that empty slices must be non-nil. From running
integration tests, I've seen that passing nil slices (which get encoded
as null in via encoding/json) resulted in invalid request errors such
as:

	No subschema in "anyOf" matched.
	For 'properties/users', nil is not an array.
	For 'properties/teams', nil is not an array.
	Not all subschemas of "allOf" matched.
	For 'anyOf/1', {"users"=>nil, "teams"=>nil} is not a null. []

Follows #512.
dmitshur added a commit that referenced this pull request Jan 17, 2017
All of those fields are documented as required at
https://developer.github.com/v3/repos/branches/#update-branch-protection.

In #512, we changed the API to better reflect that. However, it should
still be documented that the fields are required. This is consistent
with the other required fields that are also documented as required.

Additionally, note that empty slices must be non-nil. From running
integration tests, I've seen that passing nil slices (which get encoded
as null via encoding/json) resulted in invalid request errors such as:

	No subschema in "anyOf" matched.
	For 'properties/users', nil is not an array.
	For 'properties/teams', nil is not an array.
	Not all subschemas of "allOf" matched.
	For 'anyOf/1', {"users"=>nil, "teams"=>nil} is not a null. []

Follows #512.
dmitshur added a commit that referenced this pull request Jan 18, 2017
All of those fields are documented as required at
https://developer.github.com/v3/repos/branches/#update-branch-protection.

In #512, we changed the API to better reflect that. However, it should
still be documented that the fields are required. This is consistent
with the other required fields that are also documented as required.

Additionally, note that empty slices must be non-nil. From running
integration tests, I've seen that passing nil slices (which get encoded
as null via encoding/json) resulted in invalid request errors such as:

	No subschema in "anyOf" matched.
	For 'properties/users', nil is not an array.
	For 'properties/teams', nil is not an array.
	Not all subschemas of "allOf" matched.
	For 'anyOf/1', {"users"=>nil, "teams"=>nil} is not a null. []

Follows #512.
bubg-dev pushed a commit to bubg-dev/go-github that referenced this pull request Jun 16, 2017
`required_pull_request_reviews` is a newly introduced object:
* https://developer.github.com/v3/repos/branches/#get-branch-protection
* https://developer.github.com/v3/repos/branches/#update-branch-protection

Closes google#512.

Change-Id: Ia4d3ff3e39163fe58a691a8abf0a067bf3c714e5
bubg-dev pushed a commit to bubg-dev/go-github that referenced this pull request Jun 16, 2017
All of those fields are documented as required at
https://developer.github.com/v3/repos/branches/#update-branch-protection.

In google#512, we changed the API to better reflect that. However, it should
still be documented that the fields are required. This is consistent
with the other required fields that are also documented as required.

Additionally, note that empty slices must be non-nil. From running
integration tests, I've seen that passing nil slices (which get encoded
as null via encoding/json) resulted in invalid request errors such as:

	No subschema in "anyOf" matched.
	For 'properties/users', nil is not an array.
	For 'properties/teams', nil is not an array.
	Not all subschemas of "allOf" matched.
	For 'anyOf/1', {"users"=>nil, "teams"=>nil} is not a null. []

Follows google#512.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants