Skip to content

cli syntax issue #1732

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

Open
angegar opened this issue Dec 6, 2021 · 11 comments
Open

cli syntax issue #1732

angegar opened this issue Dec 6, 2021 · 11 comments
Labels

Comments

@angegar
Copy link

angegar commented Dec 6, 2021

Description of the problem, including code/CLI snippet

Hello,
First of all thank you for the work done in this project, it is helpful.

I am trying to manage some gitlab configuration with python-gitlab CLI, however I do not find the right syntax to protect a branch. I am struggling with the parameters :

--allowed-to-push
--allowed-to-merge

Can someone help me ?

**The syntax below does not work

   gitlab project-protected-branch create --project-id xxx\
      --name master \
      --allowed-to-merge='user_id: xxx' \
      --code-owner-approval-required=true \
      --push-access-level=0 \
      --merge-access-level=0
@JohnVillalovos
Copy link
Member

**The syntax below does not work

How does it not work? Any messages? Silently fails?

@angegar
Copy link
Author

angegar commented Dec 6, 2021

The error message is Impossible to create object (400: allowed_to_merge is invalid)

Probably because of a wrong synxtax, i do not know how to set the --allowed-to-merge parameter.

@JohnVillalovos
Copy link
Member

The upstream docs ( https://docs.gitlab.com/ee/api/protected_branches.html#protect-repository-branches ) say that allowed_to_merge is a list of integers.

So I would think on the command line it would be: --allowed-to-merge=1 your example above says user_id: xxx

@JohnVillalovos
Copy link
Member

Sorry, the upstream docs say:

allowed_to_merge:

Array of access levels allowed to merge, with each described by a hash 

So maybe: --allowed-to-merge=40 would be for maintainer access.

I'm not quite sure what they mean by "described by a hash"

@angegar
Copy link
Author

angegar commented Dec 6, 2021

Thank you for your help :D

I tried gitlab project-protected-branch create --project-id yyy --name master --code-owner-approval-required=true --allowed-to-merge=xxx but it failed with the same error

Impossible to create object (400: allowed_to_merge is invalid)

It should be a complex structure as it can be a list of user_id or a list of group_id.

@JohnVillalovos
Copy link
Member

as it can be a list of user_id or a list of group_id.

That doesn't seem to be what the docs say 😟

@JohnVillalovos
Copy link
Member

If you can figure out how to make it work with a curl command then maybe we can then try to figure out how to make it work with the CLI.

@JohnVillalovos
Copy link
Member

JohnVillalovos commented Dec 6, 2021

as it can be a list of user_id or a list of group_id.

That doesn't seem to be what the docs say 😟

But it seems like it might be what the code says. Confusing 😟

https://gitlab.com/gitlab-org/gitlab/-/blob/1171f6fb3b81c3f7ebaf0a1295eee239e4ea9499/ee/lib/ee/api/helpers/protected_branches_helpers.rb#L21

@JohnVillalovos
Copy link
Member

Looking more closely at the docs I found:

https://docs.gitlab.com/ee/api/protected_branches.html#example-with-user--group-level-access

Which says:

Elements in the allowed_to_push / allowed_to_merge / allowed_to_unprotect array should take the form {user_id: integer}, {group_id: integer}, or {access_level: integer}. Each user must have access to the project and each group must have this project shared. These access levels allow more granular control over protected branch access.

An example curl command:

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "\
https://gitlab.example.com/api/v4/projects/5/protected_branches?name=*-stable&allowed_to_push%5B%5D%5Buser_id%5D=1"

Which putting part of it into Python:

>>> urllib.parse.unquote("allowed_to_push%5B%5D%5Buser_id%5D=1")
'allowed_to_push[][user_id]=1'

So we for sure don't support that data type yet 😟 Right now I have a PR up to better support the array type: #1699

We will need to add support for the hash type in the future and the array of hashes.
https://docs.gitlab.com/ee/api/#hash

@angegar
Copy link
Author

angegar commented Dec 7, 2021

OK, thus i have i will have to change my code. Thank you for your investigation.

@nejch nejch added the cli label Jan 13, 2022
@w7089
Copy link

w7089 commented Mar 20, 2023

I see that 1735 got merged, but i'm still getting Impossible to create object (400: allowed_to_push is invalid) error on gitlab --debug --verbose project-protected-branch create --project-id 29 --name main --allowed-to-push '[{"access_level": 0}]'.

I thought the array ALLOWED_TO_MERGE has to follow the API

Am I wrong?

The debug mode of the above cli command shows:

send: b'{"name": "main", "allowed_to_push": "[{\\"access_level\\": 0}]"}'

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

No branches or pull requests

4 participants