1

I am trying to create a pull request comment automatically whenever CI is run. The output of a given command is written to a file (could also just be stored inside an environment variable though). The problem is, I usually get the following response:

curl -XPOST -d "{'body':'$RESULT'}" https://api.github.com/repo/name/issues/number/comment

{
   "message": "Problems parsing JSON",
   "documentation_url": "https://developer.github.com/v3/issues/comments/#create-a-comment"
}

This is usually due to unescpaed characters, like \n, \t, " etc.

Is there any easy way to achieve this on the command line or in bash, sh, with jq or Python? Using the Octokit.rb library is works straight away, but I don't want to install Ruby in the build environment.

Mahoni
  • 6,598
  • 13
  • 52
  • 108

2 Answers2

0

You can use jq to create your JSON object. Supposing you have your comment content in RESULT variable, the full request would be :

DATA=$(echo '{}' | jq --arg val "$RESULT" '.| {"body": $val}')

curl -s -H 'Content-Type: application/json' \
        -H 'Authorization: token YOUR_TOKEN' \
        -d "$DATA" \
        "https://api.github.com/repos/:owner/:repo/issues/:number/comments"
Bertrand Martel
  • 38,018
  • 15
  • 115
  • 140
0

The post "Using curl POST with variables defined in bash script functions" proposes multiple techniques for passing a varialbe like $RESULT in a curl POST parameter.

generate_post_data()
{
  cat <<EOF
{
  "body": "$RESULT"
}
EOF 
}

Then, following "A curl tutorial using GitHub's API ":

curl -X POST \
-H "authToken: <yourToken" \
-H "Content-Type: application/json" \
--data "$(generate_post_data)" https://api.github.com/repo/name/issues/number/comment
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755