Create branch protection rule error from curl

I am trying to create a branch protection rule using cURL command in a manually dispatched GitHub workflow, but it always fails with “Problem parsing JSON” error.

Same request works from Postman.

Curl command to make the request body along with headers values.

curl -v --compressed -H "Accept: */*" -H "Content-Type: application/graphql" -H "Authorization: Bearer $SECRETS_TOKEN" -X POST https://org-enterprise-server/api/graphql -d @release_rule_file.txt

release_rule_file.txt file is generated using cat command to preserve formatting:

cat <<EOT >> release_rule_file.txt
mutation createBranchProtectionRule {
             createBranchProtectionRule(input: {
                repositoryId: `echo $repo_id`,
                pattern: "`echo $release_branch_pattern`",
				requiresStatusChecks: true,
				requiresStrictStatusChecks: true, 
				requiredStatusCheckContexts: ["Approver Validation", "CodeQL", "build"]
				}) {
                branchProtectionRule {
                        allowsDeletions
                        allowsForcePushes
                        creator { login }
                        databaseId
                        dismissesStaleReviews
                        isAdminEnforced
                        pattern
                        repository { nameWithOwner }
                        requiredApprovingReviewCount
                        requiresApprovingReviews
                        requiredStatusCheckContexts
                        requiresCodeOwnerReviews
                        requiresCommitSignatures
                        requiresLinearHistory
                        requiresStatusChecks
                        requiresStrictStatusChecks
                        restrictsPushes
                        restrictsReviewDismissals
                }
                clientMutationId
        }
	}
EOT

repo_id required for GraphQL API is base_64 encoded value retrieved from get repo REST API:

repo_id=`curl -s -H "Accept: application/vnd.github.v3+json" -H "Authorization: Bearer $SECRETS_TOKEN"  https://org-enterprise-server/api/v3/repos/org-name/test-repo | jq '.["node_id"]'`

I have verified the same header values are set in Postman except for “User-Agent” and “Cache-Control” headers.

I am using GitHub Enterprise server 3.2.1.

Please help.

Resolved it by using octokit/graphql-action instead of cURL

1 Like