GitHub API, posting new comment using a variable

Viewed 87

I have a file with a bunch of output from some performance tests. It looks similar to the following:

index | master | performance-fix | change %
--- | --- | --- | ---
load | 26212.8 | 28223.6 | 7.67%
type | 67.5 | 75.41 | 11.72%
minType | 56.91 | 59.6 | 4.73%
maxInserterSearch | 185.45 | 283.25 | 52.74%
minInserterHover | 25.97 | 27.55 | 6.08%
maxInserterHover | 44.47 | 44.7 | 0.52%

I am trying to submit a new comment on a Github issue using that table data. Standard text works fine, but when I try and pass the table along I'm getting the error:

{
  "message": "Problems parsing JSON",
  "documentation_url": "https://docs.github.com/rest/reference/issues#update-an-issue-comment"
}

My cURL request is as follows:

NEW_COMMENT=$(curl -sS \
  -X PATCH \
  -u $GH_LOGIN:$GH_AUTH_TOKEN \
  -H "Accept: application/vnd.github.v3+json" \
  "https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/issues/comments/$COMMENT_ID" \
  -d '{"body": "Results: <br />'"$TEST_RESULTS"'"}')

I have also tried creating the {"body": ...} using jq, and using the --data-urlencode flag. Both return the same "Problems parsing JSON" error.

1 Answers

It looks like $TEST_RESULTS contains characters that make the JSON not what you think it is, like including quotation marks and newlines

Maybe escaping the JSON output like this will help

escaped="$(printf '%s' "$TEST_RESULTS" | jq -Rs '.')"

... \
-d '{"body": "Results: <br />'"$escaped"'"}')
Related