【问题标题】:gh api pass array fieldgh api 传递数组字段
【发布时间】:2022-12-26 06:03:03
【问题描述】:

我正在尝试使用接受 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]"

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

【问题讨论】:

  • 你能展示一下你现在是如何传递这些数据的吗?
  • @wkl 完成,添加到问题中

标签: github-api github-cli


【解决方案1】:

gh api --field 不接受 JSON 数组,这是 cli/cli issue #1484 项目的未决问题。

您需要做的是通过直接将其指定为 --input 来提供 JSON 负载,因此您必须创建与命令分开的 JSON,如下所示,我将其创建为 heredoc 字符串,然后将其通过管道传输到 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,这可能会稍微不那么笨重,但我假设你没有额外的工具。

【讨论】:

  • 进行了一些修补,但现在我可以正常工作了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2015-03-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
相关资源
最近更新 更多