You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
gh api -F <key=val> not accepting int or boolean on ruleset creation #8983
bugSomething isn't workinggh-apirelating to the gh api commandmore-info-neededMore info needed from user/contributorplatformProblems with the GitHub platform rather than the CLI client
Using the gh api command to create ruleset, it is impossible to pass anything other than a string as a "key=value" pair. I tried using -F flag but it does not work either.
{
"message": "Invalid request.\n\nInvalid property /rules/0: data matches no possible input. See `documentation_url`.",
"documentation_url": "https://docs.github.com/rest/repos/rules#create-a-repository-ruleset"
}
I get the same behavior when i try with a property that should have a boolean value. For example:
Is there anything that looks wrong in these request to you? I must admit I'm not an expert on the ruleset API. Also, the example gh snippet in those docs is definitely broken in some ways.
Is there anything that looks wrong in these request to you?
The requests look good to me. This is exactly what i would expect. To get the latest structure of the ruleset object i used the export tool of rulesets available in the GitHub UI and looked at the json file it gave.
Also, the example gh snippet in those docs is definitely broken in some ways.
It definitely is broken. I should have probably reported this too but here's what I found that is broken:
The ref_name[include] is not valid, what needs to be used is something like conditions[ref_name][include]
same goes for ref_name[exclude]
I would assume bypass_actors[][actor_id]=234 is also broken for the same reason as my 2 examples above but I have not tested this particular key in my use cases.
I left a note for the team that owns this API endpoint on Saturday but given that it was the weekend I wouldn't expect a reply. In terms of your original comment, the way to have the most control is to craft your own JSON body and go from there:
gh api --verbose -X POST \
--input <FILE_CONTAINING_JSON_ABOVE> \
repos/{owner}/{repo}/rulesets
The -F flags for fields are mostly suited for simple cases and can get janky very quickly with the syntax that was chosen. I'm not sure there's a way to represent this JSON structure via -F, so you may end up having to do some preprocessing to produce the JSON yourself (it can be passed on stdin with --input -. Have asked @andyfeller to see if he can massage -F because he knows things 🤓 . Does it matter to you whether -F works for this case?
Limitation of #8761 involving nested objects within arrays
@williammartin : I believe we're experiencing a limitation behind parseFields design as it unclear when to create new nested objects over multiple fields within an array over multiple field flags.
Explanation
The following explanation resolves around the following 2 blocks of code:
The problem occurs because parseFields and related logic doesn't know when to start a new object within an array. It parses field keys one step at a time without taking later keys into account before deciding if this is setting an existing object or a new object.
Assuming the following state before rules field is being parsed:
parseFields generates a stack of keys to set field values while detecting when an object is nested in an array:
-F "rules[][type]=pull_request"
parseFields sees rules as the previous key and type as the new key and is aware it is in an array but not how deep the array is. Because test doesn't exist within the map, addParamsSlice creates a new map and appends it to the array before being returned to set the value resulting in:
Again, parseFields sees rules as the previous key and parameters as the new key and is aware it is in an array. Because parameters doesn't exist within the map, addParamsSlice creates a new map and appends it to the array before being returned to set the value resulting in:
This is where we get into the problem: parseFields sees rules and parameters just like before. addParamsSlice sees parameters DOES exist however it doesn't fall into any existing use case to be reused as it doesn't have enough information to know we will set a totally different field than the last time.
The -F flags for fields are mostly suited for simple cases and can get janky very quickly with the syntax that was chosen. I'm not sure there's a way to represent this JSON structure via -F, so you may end up having to do some preprocessing to produce the JSON yourself (it can be passed on stdin with --input -. Have asked @andyfeller to see if he can massage -F because he knows things 🤓 . Does it matter to you whether -F works for this case?
Your suggested approach is answering all my needs. I don't specifically needed the -F flag to work, I just needed a way to pass the whole ruleset data to the creation API and what you suggested is 100% doing the job for me; I just tested it. You can either close this issue or leave it open if you think this still needs to be fixed. I would suggest maybe reworking the API documentation before closing this.
I tripped up over this bug, trying to ( ultimately set up a ruleset as described above ). I noticed that the examples given in the API documentation didn't work either. I'm currently using gh version 2.49.2 (2024-05-13) on Ubuntu 23.10
The workaround worked perfectly for me to create a ruleset
and I learnt a lot about the --input - flag, as well as the --verbose flag.
I can't help but wonder if we should include in the gh API manual page an example of how to use the --input - flag to correctly pass the json in when all else fails. Also add a --verbose example too, as this provides useful debugging information when it comes to finding out what is going wrong.
I created a zendesk ticket 2777197 for myself, in the hope that if a customer logs a support ticket, then support will search and find the ticket - which will point them here.
I can't help but wonder if we should include in the gh API manual page an example of how to use the --input - flag to correctly pass the json in when all else fails. Also add a --verbose example too, as this provides useful debugging information when it comes to finding out what is going wrong.
@erichanson1 : I think that is an excellent idea especially because how you do it is likely to differ by person. For example, my andyfeller/gh-repo-export extension builds dynamic JSON inputs using jq which is a bit more advanced but possibly necessary depending:
Created #9104 to track this example addition and have marked it help wanted so if you're interested in adding an example that would have helped you @erichanson1, that would be much appreciated. Cheers.
bugSomething isn't workinggh-apirelating to the gh api commandmore-info-neededMore info needed from user/contributorplatformProblems with the GitHub platform rather than the CLI client
Activity
williammartin commentedon Apr 20, 2024
I see the same behaviour on
v2.48.0andv2.47.0but I wonder if this is a platform bug.Looking at your examples using the
--verboseflag, the body looks correct to me:{ "enforcement": "active", "name": "Test", "rules": [ { "parameters": { "required_approving_review_count": 1 }, "type": "pull_request" } ], "target": "branch" }{ "enforcement": "active", "name": "Test", "rules": [ { "parameters": { "dismiss_stale_reviews_on_push": true }, "type": "pull_request" } ], "target": "branch" }Is there anything that looks wrong in these request to you? I must admit I'm not an expert on the ruleset API. Also, the example
ghsnippet in those docs is definitely broken in some ways.andyfeller commentedon Apr 20, 2024
@AlexVermette-Eaton : Just to share how I was testing #8762 by creating rule:
Let me be clear, this took a bit of reversing by manually creating a rule and playing with it.
AshVz commentedon Apr 20, 2024
@williammartin
The requests look good to me. This is exactly what i would expect. To get the latest structure of the ruleset object i used the export tool of rulesets available in the GitHub UI and looked at the json file it gave.
It definitely is broken. I should have probably reported this too but here's what I found that is broken:
ref_name[include]is not valid, what needs to be used is something likeconditions[ref_name][include]ref_name[exclude]I would assume
bypass_actors[][actor_id]=234is also broken for the same reason as my 2 examples above but I have not tested this particular key in my use cases.AshVz commentedon Apr 21, 2024
If anyone has a work around I would gladly take it, as this is blocking for what
I am trying to achieve.
williammartin commentedon Apr 22, 2024
I left a note for the team that owns this API endpoint on Saturday but given that it was the weekend I wouldn't expect a reply. In terms of your original comment, the way to have the most control is to craft your own JSON body and go from there:
{ "query": "mutation($input:CreateRepositoryRulesetInput!){\n createRepositoryRuleset(input:$input){\n ruleset{\n id\n name\n createdAt\n }\n }\n}", "variables": { "input": { "enforcement": "ACTIVE", "name": "Test", "conditions": {}, "rules": [ { "parameters": { "pullRequest": { "dismissStaleReviewsOnPush": false, "requireCodeOwnerReview": false, "requireLastPushApproval": false, "requiredApprovingReviewCount": 1, "requiredReviewThreadResolution": false } }, "type": "PULL_REQUEST" } ], "sourceId": "<REPOSITORY_GQL_ID>", "target": "BRANCH" } } }You can get the
<REPOSITORY_GQL_ID>via:I appreciate this is not particularly pleasant. I will get back to you when we hear back from the team that own the ruleset REST API.
williammartin commentedon Apr 22, 2024
Looks like the issue is that for the
parametersof arule, all fields are required as in the docs:What this means is that you need a body like so:
{ "enforcement": "active", "name": "Test", "rules": [ { "parameters": { "dismiss_stale_reviews_on_push": false, "require_code_owner_review": false, "require_last_push_approval": false, "required_approving_review_count": 1, "required_review_thread_resolution": false }, "type": "pull_request" } ], "target": "branch" }Could be used like so:
The
-Fflags for fields are mostly suited for simple cases and can get janky very quickly with the syntax that was chosen. I'm not sure there's a way to represent this JSON structure via-F, so you may end up having to do some preprocessing to produce the JSON yourself (it can be passed on stdin with--input -. Have asked @andyfeller to see if he can massage-Fbecause he knows things 🤓 . Does it matter to you whether-Fworks for this case?andyfeller commentedon Apr 22, 2024
Limitation of #8761 involving nested objects within arrays
@williammartin : I believe we're experiencing a limitation behind
parseFieldsdesign as it unclear when to create new nested objects over multiple fields within an array over multiple field flags.Explanation
The following explanation resolves around the following 2 blocks of code:
cli/pkg/cmd/api/fields.go
Lines 63 to 84 in 8181c62
cli/pkg/cmd/api/fields.go
Lines 135 to 150 in 8181c62
Take the following example:
The problem occurs because
parseFieldsand related logic doesn't know when to start a new object within an array. It parses field keys one step at a time without taking later keys into account before deciding if this is setting an existing object or a new object.Assuming the following state before
rulesfield is being parsed:{ "name": "Test", "target": "branch", "enforcement": "active", }parseFieldsgenerates a stack of keys to set field values while detecting when an object is nested in an array:-F "rules[][type]=pull_request"parseFieldsseesrulesas the previous key andtypeas the new key and is aware it is in an array but not how deep the array is. Becausetestdoesn't exist within the map,addParamsSlicecreates a new map and appends it to the array before being returned to set the value resulting in:{ "name": "Test", "target": "branch", "enforcement": "active", "rules": [ { "type": "pull_request" } ] }-F "rules[][parameters][dismiss_stale_reviews_on_push]=false"Again,
parseFieldsseesrulesas the previous key andparametersas the new key and is aware it is in an array. Becauseparametersdoesn't exist within the map,addParamsSlicecreates a new map and appends it to the array before being returned to set the value resulting in:{ "name": "Test", "target": "branch", "enforcement": "active", "rules": [ { "type": "pull_request", "parameters": { "dismiss_stale_reviews_on_push": false } } ] }-F "rules[][parameters][require_code_owner_review]=false"This is where we get into the problem:
parseFieldsseesrulesandparametersjust like before.addParamsSliceseesparametersDOES exist however it doesn't fall into any existing use case to be reused as it doesn't have enough information to know we will set a totally different field than the last time.{ "name": "Test", "target": "branch", "enforcement": "active", "rules": [ { "type": "pull_request", "parameters": { "dismiss_stale_reviews_on_push": false } }, { "parameters": { "require_code_owner_review": false } } ] }This feels like a problem with the field set design as users cannot set a whole object in a single flag.
Test for debugging
Locally, I created the following test within
pkg/cmd/api/fields_test.go:AshVz commentedon Apr 22, 2024
@williammartin
Your suggested approach is answering all my needs. I don't specifically needed the
-Fflag to work, I just needed a way to pass the whole ruleset data to the creation API and what you suggested is 100% doing the job for me; I just tested it. You can either close this issue or leave it open if you think this still needs to be fixed. I would suggest maybe reworking the API documentation before closing this.erichanson1 commentedon May 14, 2024
@williammartin @andyfeller
I tripped up over this bug, trying to ( ultimately set up a ruleset as described above ). I noticed that the examples given in the API documentation didn't work either. I'm currently using
gh version 2.49.2 (2024-05-13)on Ubuntu 23.10The workaround worked perfectly for me to create a ruleset
and I learnt a lot about the --input - flag, as well as the --verbose flag.
I can't help but wonder if we should include in the gh API manual page an example of how to use the --input - flag to correctly pass the json in when all else fails. Also add a --verbose example too, as this provides useful debugging information when it comes to finding out what is going wrong.
I created a zendesk ticket 2777197 for myself, in the hope that if a customer logs a support ticket, then support will search and find the ticket - which will point them here.
andyfeller commentedon May 14, 2024
@erichanson1 : I think that is an excellent idea especially because how you do it is likely to differ by person. For example, my
andyfeller/gh-repo-exportextension builds dynamic JSON inputs usingjqwhich is a bit more advanced but possibly necessary depending:williammartin commentedon May 21, 2024
Created #9104 to track this example addition and have marked it
help wantedso if you're interested in adding an example that would have helped you @erichanson1, that would be much appreciated. Cheers.williammartin commentedon Jun 4, 2024
I'm going to close this because #9104 tracks the action item for this work. Cheers everyone for your input!