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

Commit 70e3a08

Browse files
authored
Merge pull request #53 from nathandines/hotfix/no-curl-on-non-pr-builds
Do Not Try to Comment on Non-PR Builds (fixes #50)
2 parents 5162a59 + 8c6d029 commit 70e3a08

File tree

5 files changed

+70
-59
lines changed

5 files changed

+70
-59
lines changed

apply/entrypoint.sh

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,30 @@ if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ] || [ "$P
5656
exit $SUCCESS
5757
fi
5858

59-
# Build the comment we'll post to the PR.
60-
OUTPUT=$(stripcolors "$OUTPUT")
61-
COMMENT=""
62-
if [ $SUCCESS -ne 0 ]; then
63-
OUTPUT=$(wrap "$OUTPUT")
64-
COMMENT="#### \`terraform apply\` Failed
59+
if [[ "$GITHUB_EVENT_NAME" == 'pull_request' ]]; then
60+
# Build the comment we'll post to the PR.
61+
OUTPUT=$(stripcolors "$OUTPUT")
62+
COMMENT=""
63+
if [ $SUCCESS -ne 0 ]; then
64+
OUTPUT=$(wrap "$OUTPUT")
65+
COMMENT="#### \`terraform apply\` Failed
6566
$OUTPUT
6667
6768
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
68-
else
69-
# Call wrap to optionally wrap our output in a collapsible markdown section.
70-
OUTPUT=$(wrap "$OUTPUT")
71-
COMMENT="#### \`terraform apply\` Success
69+
else
70+
# Call wrap to optionally wrap our output in a collapsible markdown section.
71+
OUTPUT=$(wrap "$OUTPUT")
72+
COMMENT="#### \`terraform apply\` Success
7273
$OUTPUT
7374
7475
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
75-
fi
76+
fi
7677

77-
# Post the comment.
78-
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
79-
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
80-
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
78+
# Post the comment.
79+
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
80+
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
81+
82+
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
83+
fi
8184

8285
exit $SUCCESS

fmt/entrypoint.sh

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,45 @@ if [ $SUCCESS -eq 0 ]; then
1818
exit 0
1919
fi
2020

21-
if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
22-
exit $SUCCESS
23-
fi
24-
25-
OUTPUT=$(stripcolors "$OUTPUT")
26-
if [ $SUCCESS -eq 2 ]; then
27-
# If it exits with 2, then there was a parse error and the command won't have
28-
# printed out the files that have failed. In this case we comment back with the
29-
# whole parse error.
30-
COMMENT="\`\`\`
21+
if [[ "$GITHUB_EVENT_NAME" == 'pull_request' ]]; then
22+
if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
23+
exit $SUCCESS
24+
fi
25+
26+
OUTPUT=$(stripcolors "$OUTPUT")
27+
if [ $SUCCESS -eq 2 ]; then
28+
# If it exits with 2, then there was a parse error and the command won't have
29+
# printed out the files that have failed. In this case we comment back with the
30+
# whole parse error.
31+
COMMENT="\`\`\`
3132
$OUTPUT
3233
\`\`\`
3334
"
34-
else
35-
# Otherwise the output will contain a list of unformatted filenames.
36-
# Iterate through each file and build up a comment containing the diff
37-
# of each file.
38-
COMMENT=""
39-
for file in $OUTPUT; do
40-
FILE_DIFF=$(terraform fmt -no-color -write=false -diff "$file" | sed -n '/@@.*/,//{/@@.*/d;p}')
41-
COMMENT="$COMMENT
35+
else
36+
# Otherwise the output will contain a list of unformatted filenames.
37+
# Iterate through each file and build up a comment containing the diff
38+
# of each file.
39+
COMMENT=""
40+
for file in $OUTPUT; do
41+
FILE_DIFF=$(terraform fmt -no-color -write=false -diff "$file" | sed -n '/@@.*/,//{/@@.*/d;p}')
42+
COMMENT="$COMMENT
4243
<details><summary><code>$file</code></summary>
4344
4445
\`\`\`diff
4546
$FILE_DIFF
4647
\`\`\`
4748
</details>
4849
"
49-
done
50-
fi
50+
done
51+
fi
5152

52-
COMMENT_WRAPPER="#### \`terraform fmt\` Failed
53+
COMMENT_WRAPPER="#### \`terraform fmt\` Failed
5354
$COMMENT
5455
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*
5556
"
56-
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT_WRAPPER" '.body = $body')
57-
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
58-
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
57+
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT_WRAPPER" '.body = $body')
58+
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
59+
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
60+
fi
5961

6062
exit $SUCCESS

init/entrypoint.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ if [ $SUCCESS -eq 0 ]; then
2727
exit 0
2828
fi
2929

30-
if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
31-
exit $SUCCESS
32-
fi
30+
if [[ "$GITHUB_EVENT_NAME" == 'pull_request' ]]; then
31+
if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
32+
exit $SUCCESS
33+
fi
3334

34-
OUTPUT=$(stripcolors "$OUTPUT")
35-
COMMENT="#### \`terraform init\` Failed
35+
OUTPUT=$(stripcolors "$OUTPUT")
36+
COMMENT="#### \`terraform init\` Failed
3637
\`\`\`
3738
$OUTPUT
3839
\`\`\`
3940
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
40-
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
41-
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
42-
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
41+
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
42+
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
43+
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
44+
fi
4345

4446
exit $SUCCESS
4547

plan/entrypoint.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ $OUTPUT
8383
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
8484
fi
8585

86-
# Post the comment.
87-
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
88-
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
89-
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
86+
if [[ "$GITHUB_EVENT_NAME" == 'pull_request' ]]; then
87+
# Post the comment.
88+
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
89+
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
90+
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
91+
fi
9092

9193
exit $SUCCESS

validate/entrypoint.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ if [ $SUCCESS -eq 0 ]; then
2222
exit 0
2323
fi
2424

25-
if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
26-
exit $SUCCESS
27-
fi
25+
if [[ "$GITHUB_EVENT_NAME" == 'pull_request' ]]; then
26+
if [ "$TF_ACTION_COMMENT" = "1" ] || [ "$TF_ACTION_COMMENT" = "false" ]; then
27+
exit $SUCCESS
28+
fi
2829

29-
OUTPUT=$(stripcolors "$OUTPUT")
30-
COMMENT="#### \`terraform validate\` Failed
30+
OUTPUT=$(stripcolors "$OUTPUT")
31+
COMMENT="#### \`terraform validate\` Failed
3132
\`\`\`
3233
$OUTPUT
3334
\`\`\`
3435
*Workflow: \`$GITHUB_WORKFLOW\`, Action: \`$GITHUB_ACTION\`*"
35-
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
36-
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
37-
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
36+
PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body')
37+
COMMENTS_URL=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.comments_url)
38+
curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null
39+
fi
3840

3941
exit $SUCCESS

0 commit comments

Comments
 (0)