使用powerShell执行github cli graphql查询:\&quot上的解析错误。 (lbracket)在[4,22]

发布于 2025-02-02 21:44:16 字数 2989 浏览 5 评论 0 原文

我已经使用github cli创建了PowerShell脚本来创建一个存储库分支规则:

[cmdletbinding()]
param(
    [parameter(Mandatory=$true)]
    [string]$Organisation,
    [parameter(Mandatory=$true)]
    [string]$Repository,    
    [hashtable]$Properties = [ordered]@{
        allowsDeletions='false'
        allowsForcePushes='false'
        blocksCreations='false'
        dismissesStaleReviews='true'
        isAdminEnforced='true'
        requiredApprovingReviewCount=1 
        requiresApprovingReviews='true'
        requiresCodeOwnerReviews='false'
        requiresCommitSignatures='false'
        requiresConversationResolution='true'
        requiresLinearHistory='false'
        requiresStatusChecks='true'
        requiresStrictStatusChecks='true'
        restrictsPushes='false'
        restrictsReviewDismissals='false'
    },
    [parameter(Mandatory=$false)]
    [string]$BranchPattern = '[mp][ar][ise][ntp]*',
    [string]$Hostname = 'github.com'
)

$repo = "repos/$Organisation/$Repository"
$repoNodeId = $(gh api --hostname "$hostname" $repo --jq .node_id)
Write-Debug "`$repo = '$repo'; `$repoNodeId = $repoNodeId"

$graphQl = @"
mutation createBranchProtectionRule {
    createBranchProtectionRule(input: {
        repositoryId: "$repoNodeId"
        pattern: "$BranchPattern"
        $($(foreach($p in $Properties.GetEnumerator()){"$($p.Name): $($p.Value)"}) -join "`n`t")
    }){
        branchProtectionRule {
            allowsDeletions
            allowsForcePushes
            creator { login }
            databaseId
            dismissesStaleReviews
            isAdminEnforced
            pattern
            repository { nameWithOwner }
            requiredApprovingReviewCount
            requiresApprovingReviews
            requiredStatusCheckContexts
            requiresCodeOwnerReviews
            requiresCommitSignatures
            requiresLinearHistory
            requiresStatusChecks
            requiresStrictStatusChecks
            restrictsPushes
            restrictsReviewDismissals
        }
        clientMutationId
    }
}
"@

Write-Debug $graphQl

gh api --hostname "$Hostname" graphql -F query="$graphQl"

在句法上是正确的,因为当我复制 $ graphql 变量的内容时/概述/资源管理器“ rel =“ nofollow noreferrer”> github graphql explorer 它的工作正常。但是,执行 gh api -hostName“ graphql -f query =” $ graphql“ ”,我一直在遇到以下解析错误:

{
  "errors": [
    {
      "message": "Parse error on \"[\" (LBRACKET) at [4, 22]",
      "locations": [
        {
          "line": 4,
          "column": 22
        }
      ]
    }
  ]
}

注意我正在使用 there-string 图形QL查询的变量,我可以收集解析错误与> 有关模式值,但是逃脱方括号没有意义?...我缺少什么...

I have created PowerShell script using GitHub CLI to create a repository branching rule:

[cmdletbinding()]
param(
    [parameter(Mandatory=$true)]
    [string]$Organisation,
    [parameter(Mandatory=$true)]
    [string]$Repository,    
    [hashtable]$Properties = [ordered]@{
        allowsDeletions='false'
        allowsForcePushes='false'
        blocksCreations='false'
        dismissesStaleReviews='true'
        isAdminEnforced='true'
        requiredApprovingReviewCount=1 
        requiresApprovingReviews='true'
        requiresCodeOwnerReviews='false'
        requiresCommitSignatures='false'
        requiresConversationResolution='true'
        requiresLinearHistory='false'
        requiresStatusChecks='true'
        requiresStrictStatusChecks='true'
        restrictsPushes='false'
        restrictsReviewDismissals='false'
    },
    [parameter(Mandatory=$false)]
    [string]$BranchPattern = '[mp][ar][ise][ntp]*',
    [string]$Hostname = 'github.com'
)

$repo = "repos/$Organisation/$Repository"
$repoNodeId = $(gh api --hostname "$hostname" $repo --jq .node_id)
Write-Debug "`$repo = '$repo'; `$repoNodeId = $repoNodeId"

$graphQl = @"
mutation createBranchProtectionRule {
    createBranchProtectionRule(input: {
        repositoryId: "$repoNodeId"
        pattern: "$BranchPattern"
        $($(foreach($p in $Properties.GetEnumerator()){"$($p.Name): $($p.Value)"}) -join "`n`t")
    }){
        branchProtectionRule {
            allowsDeletions
            allowsForcePushes
            creator { login }
            databaseId
            dismissesStaleReviews
            isAdminEnforced
            pattern
            repository { nameWithOwner }
            requiredApprovingReviewCount
            requiresApprovingReviews
            requiredStatusCheckContexts
            requiresCodeOwnerReviews
            requiresCommitSignatures
            requiresLinearHistory
            requiresStatusChecks
            requiresStrictStatusChecks
            restrictsPushes
            restrictsReviewDismissals
        }
        clientMutationId
    }
}
"@

Write-Debug $graphQl

gh api --hostname "$Hostname" graphql -F query="$graphQl"

The mutation createBranchProtectionRule is syntactically correct, because when I copy the content of the $graphQl variable and execute it in GitHub GraphQL explorer it works just fine. However, I keep getting the following parsing error when gh api --hostname "$Hostname" graphql -F query="$graphQl" is executed:

{
  "errors": [
    {
      "message": "Parse error on \"[\" (LBRACKET) at [4, 22]",
      "locations": [
        {
          "line": 4,
          "column": 22
        }
      ]
    }
  ]
}

Note I am using here-string variable for the graph QL query and I can gather the parsing error is to do with the pattern value, however it makes no sense to escape the square brackets?... What am I missing...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十级心震 2025-02-09 21:44:17

接受一个人的答案,他可以解释为什么引号需要逃脱此处的弦乐,

以回答您有关此处字符串的最后一个问题以及为什么,这可能会有所帮助。请分享您的发现!

accept an Answer from someone who can explain why the quotes needed escaping inside the here-string

In answer to your last question about here strings and why, this may help. Please share your findings!

https://devblogs.microsoft.com/scripting/powershell-for-programmers-here-strings-there-strings-everywhere-some-string-strings/

却一份温柔 2025-02-09 21:44:16

该问题与逃避此处的双引号有关,这是 fix

$graphQl = @"
mutation createBranchProtectionRule {
    createBranchProtectionRule(input: {
        repositoryId: \"$repoNodeId\"
        pattern: \"$BranchPattern\"
#... rest omitted for brevity ...
"@

我会接受一个可以解释为什么需要逃脱此处弦乐作为应对的人的答案正如我在问题中所说的那样,内容很好!

谢谢,我希望这种错误可以帮助某人...

The issue was related to escaping the double quotes inside the here-string and this is the fix:

$graphQl = @"
mutation createBranchProtectionRule {
    createBranchProtectionRule(input: {
        repositoryId: \"$repoNodeId\"
        pattern: \"$BranchPattern\"
#... rest omitted for brevity ...
"@

I would accept an Answer from someone who can explain why the quotes needed escaping inside the here-string as coping the content as I said in the question works just fine!?

Thanks and I hope this kind of error helps someone...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文