gh api 传递数组字段

问题描述 投票:0回答:2

我正在尝试使用接受 3 个字段的

gh api --method PUT orgs/<ORG>/actions/permissions/selected-actions
:其中之一
patterns_allowed
。我尝试了多种方式来传递数据,但总是不行

{
  "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"
}

其中一个尝试如下所示:

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]"

传递数组类型字段的正确方法是什么?

github-api github-cli
2个回答
2
投票

gh api --field
不接受 JSON 数组,这是该项目的一个开放问题,如 cli/cli 问题 #1484

您需要做的是通过直接将其指定为

--input
来提供 JSON 有效负载,因此您必须与命令分开创建 JSON,如下所示,我将其创建为定界符字符串,并且然后将其输入到
gh api
命令中:


#!/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 -

如果你有

jq
,这可能会稍微不那么笨重,但我假设你没有额外的工具。


2
投票

检查 gh 2.21.0(2022 年 12 月) 是否有帮助:PR 6614 确实修复了 问题 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 }

您的情况:

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]}"

2024 年第 2 季度:gh 2.48.0 附带 issue 8761PR 8762

gh api
魔法字段中添加对深度嵌套数组的支持

# update allowed values of the "environment" custom property in a deeply nested array 
gh api --PATCH /orgs/{org}/properties/schema \
    -F 'properties[][property_name]=environment' \
    -F 'properties[][default_value]=production' \
    -F 'properties[][allowed_values][]=staging' \
    -F 'properties[][allowed_values][]=production'
© www.soinside.com 2019 - 2024. All rights reserved.