Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.

Commit ceb829b

Browse files
dalensudomateo
authored andcommitted
Pass comments through pipes instead of as command line arguments (#83)
* Pass comment through pipes instead of on command line For very long comments, like terraform plan output with thousands of lines, it was not possible to fit all of it into the argument list. But by using shell built ins and passing the comment through pipes it works. * only keep last part of error output
1 parent e64ab09 commit ceb829b

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

src/terraform_apply.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ ${applyOutput}
3737

3838
applyCommentWrapper=$(stripColors "${applyCommentWrapper}")
3939
echo "apply: info: creating JSON"
40-
applyPayload=$(echo '{}' | jq --arg body "${applyCommentWrapper}" '.body = $body')
40+
applyPayload=$(echo "${applyCommentWrapper}" | jq -R --slurp '{body: .}')
4141
applyCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
4242
echo "apply: info: commenting on the pull request"
43-
curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data "${applyPayload}" "${applyCommentsURL}" > /dev/null
43+
echo "${applyPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${applyCommentsURL}" > /dev/null
4444
fi
4545

4646
exit ${applyExitCode}

src/terraform_fmt.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function terraformFmt {
1111
echo "fmt: info: checking if Terraform files in ${tfWorkingDir} are correctly formatted"
1212
fmtOutput=$(terraform fmt -check=true -write=false -diff ${fmtRecursive} 2>&1)
1313
fmtExitCode=${?}
14-
14+
1515
# Exit code of 0 indicates success. Print the output and exit.
1616
if [ ${fmtExitCode} -eq 0 ]; then
1717
echo "fmt: info: Terraform files in ${tfWorkingDir} are correctly formatted"
@@ -60,10 +60,10 @@ ${fmtComment}
6060

6161
fmtCommentWrapper=$(stripColors "${fmtCommentWrapper}")
6262
echo "fmt: info: creating JSON"
63-
fmtPayload=$(echo '{}' | jq --arg body "${fmtCommentWrapper}" '.body = $body')
63+
fmtPayload=$(echo "${fmtCommentWrapper}" | jq -R --slurp '{body: .}')
6464
fmtCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
6565
echo "fmt: info: commenting on the pull request"
66-
curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data "${fmtPayload}" "${fmtCommentsURL}" > /dev/null
66+
echo "${fmtPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${fmtCommentsURL}" > /dev/null
6767
fi
6868

6969
exit ${fmtExitCode}

src/terraform_init.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function terraformInit {
55
echo "init: info: initializing Terraform configuration in ${tfWorkingDir}"
66
initOutput=$(terraform init -input=false 2>&1)
77
initExitCode=${?}
8-
8+
99
# Exit code of 0 indicates success. Print the output and exit.
1010
if [ ${initExitCode} -eq 0 ]; then
1111
echo "init: info: successfully initialized Terraform configuration in ${tfWorkingDir}"
@@ -31,10 +31,10 @@ ${initOutput}
3131

3232
initCommentWrapper=$(stripColors "${initCommentWrapper}")
3333
echo "init: info: creating JSON"
34-
initPayload=$(echo '{}' | jq --arg body "${initCommentWrapper}" '.body = $body')
34+
initPayload=$(echo "${initCommentWrapper}" | jq -R --slurp '{body: .}')
3535
initCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
3636
echo "init: info: commenting on the pull request"
37-
curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data "${initPayload}" "${initCommentsURL}" > /dev/null
37+
echo "${initPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${initCommentsURL}" > /dev/null
3838
fi
3939

4040
exit ${initExitCode}

src/terraform_plan.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ function terraformPlan {
2929
planOutput=$(echo "${planOutput}" | sed -n -r '/-{72}/,/-{72}/{ /-{72}/d; p }')
3030
fi
3131
planOutput=$(echo "${planOutput}" | sed -r -e 's/^ \+/\+/g' | sed -r -e 's/^ ~/~/g' | sed -r -e 's/^ -/-/g')
32+
33+
# If output is longer than max length (65536 characters), keep last part
34+
planOutput=$(echo "${planOutput}" | tail -c 65000 )
3235
fi
3336

3437
# Exit code of !0 indicates failure.
@@ -40,7 +43,7 @@ function terraformPlan {
4043

4144
# Comment on the pull request if necessary.
4245
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ] && ([ "${planHasChanges}" == "true" ] || [ "${planCommentStatus}" == "Failed" ]); then
43-
planCommentWrapper="#### \`terraform plan\` ${planCommentStatus}
46+
planCommentWrapper="#### \`terraform plan\` ${planCommentStatus}
4447
<details><summary>Show Output</summary>
4548
4649
\`\`\`
@@ -53,10 +56,10 @@ ${planOutput}
5356

5457
planCommentWrapper=$(stripColors "${planCommentWrapper}")
5558
echo "plan: info: creating JSON"
56-
planPayload=$(echo '{}' | jq --arg body "${planCommentWrapper}" '.body = $body')
59+
planPayload=$(echo "${planCommentWrapper}" | jq -R --slurp '{body: .}')
5760
planCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
5861
echo "plan: info: commenting on the pull request"
59-
curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data "${planPayload}" "${planCommentsURL}" > /dev/null
62+
echo "${planPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${planCommentsURL}" > /dev/null
6063
fi
6164

6265
echo ::set-output name=tf_actions_plan_has_changes::${planHasChanges}

src/terraform_validate.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function terraformValidate {
55
echo "validate: info: validating Terraform configuration in ${tfWorkingDir}"
66
validateOutput=$(terraform validate 2>&1)
77
validateExitCode=${?}
8-
8+
99
# Exit code of 0 indicates success. Print the output and exit.
1010
if [ ${validateExitCode} -eq 0 ]; then
1111
echo "validate: info: successfully validated Terraform configuration in ${tfWorkingDir}"
@@ -31,10 +31,10 @@ ${validateOutput}
3131

3232
validateCommentWrapper=$(stripColors "${validateCommentWrapper}")
3333
echo "validate: info: creating JSON"
34-
validatePayload=$(echo '{}' | jq --arg body "${validateCommentWrapper}" '.body = $body')
34+
validatePayload=$(echo "${validateCommentWrapper}" | jq -R --slurp '{body: .}')
3535
validateCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
3636
echo "validate: info: commenting on the pull request"
37-
curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data "${validatePayload}" "${validateCommentsURL}" > /dev/null
37+
echo "${validatePayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${validateCommentsURL}" > /dev/null
3838
fi
3939

4040
exit ${validateExitCode}

0 commit comments

Comments
 (0)