使用powerShell执行github cli graphql查询:\&quot上的解析错误。 (lbracket)在[4,22]
我已经使用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查询的变量,我可以收集解析错误与>
有关模式
值,但是逃脱方括号没有意义?...我缺少什么...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
接受一个人的答案,他可以解释为什么引号需要逃脱此处的弦乐,
以回答您有关此处字符串的最后一个问题以及为什么,这可能会有所帮助。请分享您的发现!
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/
该问题与逃避此处的双引号有关,这是 fix :
我会接受一个可以解释为什么需要逃脱此处弦乐作为应对的人的答案正如我在问题中所说的那样,内容很好!
谢谢,我希望这种错误可以帮助某人...
The issue was related to escaping the double quotes inside the here-string and this is the fix:
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...