fix: Parse string arrays passed to glab api --field command

This commit is contained in:
Alby Hernández 2023-09-29 15:32:54 +00:00 committed by Oscar Tovar
parent 005e2ebf82
commit 95e95ee85f
2 changed files with 51 additions and 0 deletions

View File

@ -7,12 +7,20 @@ import (
"io"
"net/http"
"net/url"
"regexp"
"strings"
"gitlab.com/gitlab-org/cli/api"
"gitlab.com/gitlab-org/cli/internal/config"
)
const (
// stringArrayRegexPattern represents a pattern to find strings like: [item, item_two]
stringArrayRegexPattern = `^\[\s*([[:lower:]_]+(\s*,\s*[[:lower:]_]+)*)?\s*\]$`
)
var strArrayRegex = regexp.MustCompile(stringArrayRegexPattern)
func httpRequest(client *api.Client, config config.Config, hostname string, method string, p string, params interface{}, headers []string) (*http.Response, error) {
var err error
isGraphQL := p == "graphql"
@ -33,6 +41,7 @@ func httpRequest(client *api.Client, config config.Config, hostname string, meth
} else {
baseURLStr = baseURLStr + strings.TrimPrefix(p, "/")
}
var body io.Reader
var bodyIsJSON bool
switch pp := params.(type) {
@ -47,10 +56,15 @@ func httpRequest(client *api.Client, config config.Config, hostname string, meth
if vv, ok := value.([]byte); ok {
pp[key] = string(vv)
}
if strValue, ok := value.(string); ok && strArrayRegex.MatchString(strValue) {
pp[key] = parseStringArrayField(strValue)
}
}
if isGraphQL {
pp = groupGraphQLVariables(pp)
}
b, err := json.Marshal(pp)
if err != nil {
return nil, fmt.Errorf("error serializing parameters: %w", err)
@ -121,3 +135,18 @@ func parseQuery(path string, params map[string]interface{}) (string, error) {
}
return path + sep + q.Encode(), nil
}
func parseStringArrayField(strValue string) []string {
strValue = strings.TrimPrefix(strValue, "[")
strValue = strings.TrimSuffix(strValue, "]")
strArrayElements := strings.Split(strValue, ",")
var strSlice []string
for _, element := range strArrayElements {
element = strings.TrimSpace(element)
element = strings.Trim(element, `"`)
strSlice = append(strSlice, element)
}
return strSlice
}

View File

@ -8,6 +8,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/api"
"gitlab.com/gitlab-org/cli/internal/config"
)
@ -228,6 +229,27 @@ hosts:
headers: fmt.Sprintf("Accept: application/json\r\nContent-Type: text/plain\r\nPrivate-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString),
},
},
{
name: "POST with string array field and type",
args: args{
host: "gitlab.com",
method: http.MethodPost,
p: "projects",
params: map[string]interface{}{"scopes": "[api, read_api]"},
headers: []string{
"content-type: application/json",
"accept: application/json",
},
},
wantErr: false,
isGraphQL: false,
want: expects{
method: http.MethodPost,
u: "https://gitlab.com/api/v4/projects",
body: `{"scopes":["api","read_api"]}`,
headers: fmt.Sprintf("Accept: application/json\r\nContent-Type: application/json\r\nPrivate-Token: OTOKEN\r\nUser-Agent: %s\r\n", versionString),
},
},
}
for _, tt := range tests {
httpClient, err := api.TestClient(client, "OTOKEN", "gitlab.com", tt.isGraphQL)