fix: Parse string arrays passed to glab api --field command
Description
When using glab api
command using --field
to define fields with multiple values, such as --field scope=[api,read_repository]
the cli is not working as it is transforming the whole field into a string, which ends with "scope":"[api,read_repository]" in final JSON send to the API, and should end as "scope":[api,read_repository]
This MR fix that
Steps to reproduce
currently, some Gitlab API endpoints require arrays, for example those related to creation for project access tokens. Imagine the following request using CLI. This won't work without my fix:
glab api \
--method POST \
--header content-type:application/json \
--field name={{ .TOKEN_NAME }} \
--field scopes=[api,read_api] \
--field access_level=40 \
--field expires_at={{ .GITLAB_ACCESS_TOKEN_RW_EXPIRY_DATE }} \
projects/{{ .GITLAB_PROJECT_ID }}/access_tokens
Another option I tried, and did not work would be:
--field scopes=api,read_api
The reason for not working is the CLI transforming them to --field scopes="[api,read_api]"
when marshaling the flags into JSON object. Basically it transform an array into a string, and the API is not expecting it as string, but as a json array, as documented here
Related Issues
How has this been tested?
Personal computer calling the API and watching resulting JSON before sending the content to the API
Workaround
$ echo -n '{ "name":"test_token", "scopes":["api", "read_repository"], "expires_at":"2023-09-27", "access_level": 30}' \
| glab api --method POST --header content-type:application/json --input=- projects/<PROJECT_ID>/access_tokens
Screenshots (if appropriate):
Types of changes
-
Bug fix (non-breaking change which fixes an issue) -
New feature (non-breaking change which adds functionality) -
Breaking change (fix or feature that would cause existing functionality to change) -
Documentation -
Chore (Related to CI or Packaging to platforms) -
Test gap