2018-11-14 23:05:20 +00:00
#!/usr/bin/env bash
2021-01-15 20:09:45 +00:00
ERROR_LOG = " $1 "
2018-11-14 23:05:20 +00:00
function report_error_to_github
{
2021-01-28 11:35:44 +00:00
[ [ $CIRCLE_PR_NUMBER != "" ] ] || CIRCLE_PR_NUMBER = " ${ CIRCLE_PULL_REQUEST //[^0-9]/ } "
2018-11-14 23:05:20 +00:00
2021-01-15 20:12:19 +00:00
if [ [ $CI = = "true" ] ]
2018-11-14 23:05:20 +00:00
then
echo "posting error message to github"
post_error_to_github
2021-01-15 20:09:45 +00:00
post_review_comment_to_github
2018-11-14 23:05:20 +00:00
fi
}
function post_error_to_github
{
GITHUB_API_URL = " https://api.github.com/repos/ $CIRCLE_PROJECT_USERNAME / $CIRCLE_PROJECT_REPONAME /issues/ $CIRCLE_PR_NUMBER /comments "
2021-01-15 20:11:57 +00:00
ESCAPED_ERROR_MSG = $( cat -e " $ERROR_LOG " | sed 's/\\/\\\\/g' | sed 's/"/\\\"/g' )
2018-11-14 23:05:20 +00:00
2021-01-15 20:11:57 +00:00
FORMATTED_ERROR_MSG = $( echo " $ESCAPED_ERROR_MSG " | sed 's/\$/\\n/g' | tr -d '\n' )
2018-11-14 23:05:20 +00:00
curl --request POST \
2021-01-15 20:11:57 +00:00
--url " $GITHUB_API_URL " \
2020-04-17 12:32:38 +00:00
--header 'accept: application/vnd.github.v3+json' \
--header 'content-type: application/json' \
2021-01-15 20:11:57 +00:00
-u " stackenbotten: $GITHUB_ACCESS_TOKEN " \
2020-04-17 12:32:38 +00:00
--data " {\"body\": \"There was an error when running \` $CIRCLE_JOB \` for commit \` $CIRCLE_SHA1 \`:\n\`\`\`\n $FORMATTED_ERROR_MSG \n\`\`\`\nPlease check that your changes are working as intended.\"} "
2020-03-16 13:23:12 +00:00
}
function post_review_comment_to_github
{
GITHUB_API_URL = " https://api.github.com/repos/ $CIRCLE_PROJECT_USERNAME / $CIRCLE_PROJECT_REPONAME /pulls/ $CIRCLE_PR_NUMBER /comments "
2021-01-15 20:11:57 +00:00
sed -i 1d " $ERROR_LOG "
2020-03-16 13:23:12 +00:00
2021-01-15 20:12:19 +00:00
while read -r line
2020-03-16 13:23:12 +00:00
do
2021-01-15 20:11:57 +00:00
ERROR_PATH = $( echo " $line " | grep -oE ".*\.cpp" )
ERROR_LINE = $( echo " $line " | grep -oE "[0-9]*" )
2021-01-15 20:26:39 +00:00
[ [ $ERROR_PATH != "" ] ] || { echo "ERROR: Error message does not contain file path." ; exit 1; }
[ [ $ERROR_LINE != "" ] ] || { echo "ERROR: Error message does not contain line number." ; exit 1; }
2020-03-16 13:23:12 +00:00
curl --request POST \
2021-01-15 20:11:57 +00:00
--url " $GITHUB_API_URL " \
2020-04-17 12:32:38 +00:00
--header 'accept: application/vnd.github.v3+json, application/vnd.github.comfort-fade-preview+json' \
--header 'content-type: application/json' \
2021-01-15 20:11:57 +00:00
-u " stackenbotten: $GITHUB_ACCESS_TOKEN " \
2020-04-17 12:32:38 +00:00
--data " {\"commit_id\": \" $CIRCLE_SHA1 \", \"path\": \" $ERROR_PATH \", \"line\": $ERROR_LINE , \"side\": \"RIGHT\", \"body\": \"Coding style error\"} "
2021-01-15 20:11:57 +00:00
done < " $ERROR_LOG "
2018-11-14 23:05:20 +00:00
}
2021-01-15 20:09:45 +00:00
report_error_to_github