3

I am trying to use gh api --method PUT orgs/<ORG>/actions/permissions/selected-actions that accepts 3 fields: one of them patterns_allowed. I have tried multiple ways to pass the data, but I always get

{
  "message": "Invalid request.\n\nFor 'properties/patterns_allowed', \"...\" is not an array.",
  "documentation_url": "https://docs.github.com/rest/reference/actions#set-allowed-actions-for-an-organization"
}

One of this attempts looks like this:

ALLOWED="foo,bar"
gh api --method PUT \
    orgs/${{github.repository_owner}}/actions/permissions/selected-actions \
    --field "github_owned_allowed=true" \
    --field "verified_allowed=false" \
    --field "patterns_allowed=[$ALLOWED]"

What is the proper way to pass array type field?

gsf
  • 6,612
  • 7
  • 35
  • 64

2 Answers2

2

gh api --field doesn't accept JSON arrays, it's an open issue for the project as cli/cli issue #1484.

What you'd have to do instead is provide the JSON payload by specifying it directly as --input, so you have to create the JSON separate from the command, like in the following where I'm creating it as a heredoc string and then piping it into the gh api command:


#!/bin/bash

# This needs to be quoted correctly for substituting
# as the elements of a JSON list
ALLOWED='"foo","bar"'

# Using a heredoc to make our JSON payload
read -r -d '' PAYLOAD <<-JSON
{
    "github_owned_allowed": true,
    "verified_allowed": false,
    "patterns_allowed": [${ALLOWED}]
}
JSON

echo "$PAYLOAD" | gh api --method PUT \
    orgs/YOUR_ORG/actions/permissions/selected-actions \
    --input -

This could be made slightly less clunky if you have jq, but I assumed no extra tools on your part.

wkl
  • 77,184
  • 16
  • 165
  • 176
2

Check if gh 2.21.0 (Dec. 2022) would help: PR 6614 does fix issue 1484

gh api -f labels[]=bug -f labels[]=p1
#=> { "labels": ["bug", "p1"] }

gh api -f branch[name]=patch-1 -F branch[protected]=true
#=> { "branch": { "name": "patch-1", "protected": true }

In your case:

ALLOWED=("foo" "bar")
gh api --method PUT \
    orgs/${{github.repository_owner}}/actions/permissions/selected-actions \
    --field "github_owned_allowed=true" \
    --field "verified_allowed=false" \
    --field "patterns_allowed[]=${ALLOWED[0]}" \
    --field "patterns_allowed[]=${ALLOWED[1]}"
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Wow that is not obvious! And not mentioned in [the docs](https://docs.github.com/en/enterprise-server@3.7/rest/teams/teams#create-a-team) i.e. `repo_names array of strings The full name (e.g., "organization-name/repository-name") of repositories to add the team to.` – Martin Apr 12 '23 at 16:57