0

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...

Emil
  • 2,196
  • 2
  • 25
  • 24

2 Answers2

0

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...

Emil
  • 2,196
  • 2
  • 25
  • 24
0

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/

Jonny
  • 81
  • 1
  • 6
  • thanks for the link, however the article states the advantage of here strings is NOT having to escape the double quotes inside the string and I don't see an explanation why I had to escape them in order to overcome the error that prompted my question. – Emil Aug 01 '22 at 13:30