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

Error when creating branch protection #2467

Closed
luisdavim opened this issue Sep 17, 2022 · 27 comments · Fixed by #2468
Closed

Error when creating branch protection #2467

luisdavim opened this issue Sep 17, 2022 · 27 comments · Fixed by #2468

Comments

@luisdavim
Copy link
Contributor

luisdavim commented Sep 17, 2022

Since we updated our code to use the latest version of this library, we can't create a branch protection that requires branches to be up to date before merging but don't require any status checks:

func (r *RepoSettings) protectionRequestOptions() *github.ProtectionRequest {
	return &github.ProtectionRequest{
		EnforceAdmins: r.EnforceAdmins,
		RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
			DismissStaleReviews:          r.DismissStaleReviews,
			RequiredApprovingReviewCount: r.RequiredApprovingReviewCount,
			RequireCodeOwnerReviews:      r.RequireCodeOwnerReviews,
		},
		RequiredStatusChecks: &github.RequiredStatusChecks{
			Strict: true,
		},
	}
}

The above results in an error:

PUT https://api.github.com/repos/****/****/branches/main/protection: 422 Invalid request.

No subschema in "anyOf" matched.
No subschema in "oneOf" matched.
Not all subschemas of "allOf" matched.
For 'anyOf/1', {"strict"=>true} is not a null. []

Such rule is possible to be created from the UI:
image

This is also affecting the Github Terraform provider: integrations/terraform-provider-github#1147

@luisdavim
Copy link
Contributor Author

Looking at https://github.com/orgs/community/discussions/24642 removing omitempty from the Checks property could solve the problem but I guess that for that you'll have to fully deprecate the Contexts or maybe you could force Checks to be null if len(Contexts) > 0 && len(Contexts) == 0

@luisdavim
Copy link
Contributor Author

luisdavim commented Sep 17, 2022

I've tested this locally, removing omitempty from the Checks property in https://github.com/google/go-github/blob/master/github/repos.go#L908 and passing:

github.ProtectionRequest{
	EnforceAdmins: r.EnforceAdmins,
	RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
		DismissStaleReviews:          r.DismissStaleReviews,
		RequiredApprovingReviewCount: r.RequiredApprovingReviewCount,
		RequireCodeOwnerReviews:      r.RequireCodeOwnerReviews,
	},
	RequiredStatusChecks: &github.RequiredStatusChecks{
		Strict: true,
		Checks: []*github.RequiredStatusCheck{},
	},
}

Works, and you might not need to fully deprecate Contexts as if Checks is not set it's passed as null and that should be ok if Contexts is set...

I've opened #2468 to remove the omitempty.

@luisdavim
Copy link
Contributor Author

Thank you very much for the quick turnaround on this @gmlewis 🚀
Any chances of getting a new tag released so we can get the fix downstream?

@gmlewis
Copy link
Collaborator

gmlewis commented Sep 19, 2022

Thank you very much for the quick turnaround on this @gmlewis rocket Any chances of getting a new tag released so we can get the fix downstream?

Yes, I can work on a new release today.

@luisdavim
Copy link
Contributor Author

Awesome, thanks. Really appreciate it 👍

@gmlewis
Copy link
Collaborator

gmlewis commented Sep 19, 2022

@luisdavim - this issue has now been covered in the latest release:
https://github.com/google/go-github/releases/tag/v47.1.0

@alock
Copy link

alock commented Dec 1, 2022

When attempting to update I ran into the same issue as integrations/terraform-provider-github#1307. It appears that golang is setting the value to nil and not null as the github api requires. Is there a simple way to override that before the value is sent?

For 'anyOf/1', {"strict"=>true, "contexts"=>["build"], "checks"=>nil} is not a null. []

@gmlewis
Copy link
Collaborator

gmlewis commented Dec 2, 2022

@luisdavim - do you have any insights to this new issue?

@alock - just for the record, this repo is now sending an empty [] which that error message is reporting as nil, as evidenced by our unit test here:
23a2636#diff-1da0ffca0653d8b270e435e5e5ef6615b547c752c60b45df54c7e4307ece89e9R1589

@gmlewis gmlewis reopened this Dec 2, 2022
@luisdavim
Copy link
Contributor Author

I think the terraform provider still needs to be updated to use a version of this lib that contains the fix....

@gmlewis
Copy link
Collaborator

gmlewis commented Dec 2, 2022

I think the terraform provider still needs to be updated to use a version of this lib that contains the fix....

Ah, thank you, @luisdavim !

Then I'll close this issue to hopefully reduce the confusion... but please report back if you feel we introduced a regression.

@gmlewis gmlewis closed this as completed Dec 2, 2022
@TheQueenIsDead
Copy link

I think the terraform provider still needs to be updated to use a version of this lib that contains the fix....

It looks like the Terraform provider updated to go-github v48.1.0 17 days ago, and published those changes in their v5.11.0 release

However I still receive the same issue attempting to apply:

╷
│ Error: PUT https://api.github.com/repos/<org>/status/branches/<b>/protection: 422 Invalid request.
│ 
│ No subschema in "anyOf" matched.
│ For 'properties/checks', nil is not an array.
│ Not all subschemas of "allOf" matched.
│ For 'anyOf/1', {"strict"=>false, "checks"=>nil} is not a null. []
│ 
│   with module.repository["<service>"].github_branch_protection_v3.branch_protection[1],
│   on .terraform/modules/repository/main.tf line 240, in resource "github_branch_protection_v3" "branch_protection":240: resource "github_branch_protection_v3" "branch_protection" {
│ 
╵
╷
│ Error: PUT https://api.github.com/repos/<org>/status/branches/<a>/protection: 422 Invalid request.
│ 
│ No subschema in "anyOf" matched.
│ For 'properties/checks', nil is not an array.
│ Not all subschemas of "allOf" matched.
│ For 'anyOf/1', {"strict"=>false, "contexts"=>["go / Build"], "checks"=>nil} is not a null. []
│ 
│   with module.repository["<service>"].github_branch_protection_v3.branch_protection[0],
│   on .terraform/modules/repository/main.tf line 240, in resource "github_branch_protection_v3" "branch_protection":240: resource "github_branch_protection_v3" "branch_protection" {
│ 
╵

Versions as such, is there something I'm missing here?

$ terraform version
Terraform v1.3.5
on linux_amd64
+ provider registry.terraform.io/integrations/github v5.11.0

Any help appreciated 🙏

@gmlewis
Copy link
Collaborator

gmlewis commented Dec 4, 2022

Reopening but need assistance on this one from Terraform users.

@gmlewis gmlewis reopened this Dec 4, 2022
@luisdavim
Copy link
Contributor Author

The fix will have to be on the terraform provider side, to pass an empty array instead of nothing, the alternative could be to add logic to this library to set an empty array when it's nil....

@TheQueenIsDead
Copy link

I've tested this locally, removing omitempty from the Checks property in https://github.com/google/go-github/blob/master/github/repos.go#L908 and passing:

github.ProtectionRequest{
	EnforceAdmins: r.EnforceAdmins,
	RequiredPullRequestReviews: &github.PullRequestReviewsEnforcementRequest{
		DismissStaleReviews:          r.DismissStaleReviews,
		RequiredApprovingReviewCount: r.RequiredApprovingReviewCount,
		RequireCodeOwnerReviews:      r.RequireCodeOwnerReviews,
	},
	RequiredStatusChecks: &github.RequiredStatusChecks{
		Strict: true,
		Checks: []*github.RequiredStatusCheck{},
	},
}

Works, and you might not need to fully deprecate Contexts as if Checks is not set it's passed as null and that should be ok if Contexts is set...

I've opened #2468 to remove the omitempty.

Oh I see, per your comment above. Apologies for missing that. I'll open a PR downstream to try adding an empty array to the request in their utility file 👍
https://github.com/integrations/terraform-provider-github/blob/main/github/resource_github_branch_protection_v3_utils.go#L178

The idea to handle the nil gracefully upstream is a nice one, though you'd know best if there might be any interesting ramifications of that. Thanks for the response!

@luisdavim
Copy link
Contributor Author

Do keep on mind that if contexts is set then checks should be null. Using contexts is deprecated and terraform shouldn't be using it but I don't know if it is.

@TheQueenIsDead
Copy link

TheQueenIsDead commented Dec 6, 2022

Duly noted. The Terraform provider has not yet been switched over to use checks instead of contexts, per this utility function that builds the branch protection objects:
https://github.com/integrations/terraform-provider-github/blob/main/github/resource_github_branch_protection_v3_utils.go#L41

It seems there is a downstream issue to track this upgrade:
integrations/terraform-provider-github#985
With a WIP PR here:
integrations/terraform-provider-github#1051

In the meantime I'll see about merging the PR to include an empty Checks array to resolve the immediate issue, though it might require a little more through than I initially realised.

I'm happy to close this issue knowing that there is a downstream ticket to track deprecating Contexts. I suppose the only other reason to keep this open would be if we wanted the client to handle nil values as empty arrays per your suggestion.

I'll leave that decision to you 👍

@gmlewis
Copy link
Collaborator

gmlewis commented Dec 7, 2022

OK, then I think we are good to go and I'll close this issue (again).
Hopefully we can leave it closed this time.

@emmahsax
Copy link

emmahsax commented Jan 4, 2024

I'm trying to run v57.0.0 of this, and I'm still getting this issue:

No subschema in "anyOf" matched.
No subschema in "oneOf" matched.
Not all subschemas of "allOf" matched.
For 'anyOf/1', {"strict"=>true} is not a null. []

This is what our branch protection looks like:

defaultBranchProtection := &github.ProtectionRequest{
    RequiredStatusChecks: &github.RequiredStatusChecks{
	Checks: []*github.RequiredStatusCheck{},
	Strict: true,

    },
}

@gmlewis
Copy link
Collaborator

gmlewis commented Jan 5, 2024

Reopening.

@gmlewis gmlewis reopened this Jan 5, 2024
@emmahsax
Copy link

emmahsax commented Jan 5, 2024

I did find that v57.0.0 has the omitempty on Checks, while v56.0.0 does not.

@KatharinaSick
Copy link

fyi: I'm getting the same issue. I tried to work around it but couldn't find a way.

@gmlewis
Copy link
Collaborator

gmlewis commented Jan 8, 2024

This issue is apparently in direct conflict with #2976.

We would appreciate it if one of our amazing contributors would please volunteer to take a deep dive here and see why #2467 and #2976 are at opposition with one another and see if they can come up with a fix that solves both issues.

@KatharinaSick
Copy link

Thank you! :)
Is there anything I can do to work around this in the meantime?

@gmlewis
Copy link
Collaborator

gmlewis commented Jan 9, 2024

Thank you! :) Is there anything I can do to work around this in the meantime?

I would recommend using the version that works for you for your particular use case, since we appear to have different use cases apparently.

This difference in use cases will be part of what needs to be determined with a deep dive into fixing this issue.

@gmlewis
Copy link
Collaborator

gmlewis commented Jan 9, 2024

Please check out this comment: #2976 (comment)

@emmahsax
Copy link

emmahsax commented Feb 8, 2024

@KatharinaSick I've come with two potential workarounds that could work for your situation. If neither of them work, maybe I can try to help you find a solution. The workarounds are written here: #2976 (comment)

@gmlewis
Copy link
Collaborator

gmlewis commented Feb 11, 2024

Fixed by: #3070.

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 a pull request may close this issue.

6 participants