-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentrypoint.sh
executable file
·177 lines (125 loc) · 4.25 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
set -exo pipefail
# Author: Rickard Löfström <[email protected]>
function print_message() {
echo "$1" >&2
}
function parse_exitcode() {
local tflint_exitcode=$1
local tflint_status="Failed"
# TFLint returns the following exit statuses on exit: 0: No issues foun 2: Errors occurred 3: No errors occurred, but issues found
if [ "${tflint_exitcode}" -eq 0 ]; then
tflint_status="Success"
elif [ "${tflint_exitcode}" -eq 2 ]; then
tflint_status="Failed"
else
[ "${tflint_exitcode}" -eq 3 ]
tflint_status="Warning"
fi
echo "$tflint_status"
}
function format_comment() {
local tflint_output="$1"
local tflint_status_code="$2"
local tflint_status_state
tflint_status_state=$(parse_exitcode "${tflint_status_code}")
local comment
comment="#### \`Terraform TFlint\` ${tflint_status_state} <details><summary>Show Output</summary>
<p>
\`\`\`hcl
${tflint_output}
\`\`\`
</p>
</details>"
echo "$comment"
}
function get_url() {
local url=""
local github_event_path=$1
url=$(jq -r .pull_request.comments_url "${github_event_path}")
echo "$url"
}
function is_pull_request() {
local github_event_name
github_event_name="$1"
echo [ "${github_event_name}" == "pull_request" ]
}
function is_comment_enabled() {
local is_enabled="$1"
# Comment on the pull request if necessary.
echo [ "${is_enabled}" == "1" ] || [ "${is_enabled}" == "true" ]
}
function post_comment() {
local comment=$1
local github_event_name=$2
local github_token=$3
local url=$4
local dry_run="${5:-0}"
local payload
if [ -n "${github_token}" ] && [ -n "${url}" ] && [ -n "${comment}" ]; then
payload=$(echo "${comment}" | jq -R --slurp '{body: .}')
if [ "${dry_run}" -eq 1 ]; then
echo "::debug::sending comment ${comment} to ${url}"
echo "${payload}" | curl -s -S -H "Authorization: token ${github_token}" --header "Content-Type: application/json" --data @- "${url}" >/dev/null
else
echo "::debug::dry-run sending comment ${comment} to ${url}"
fi
else
echo "::debug::check token, comment is ${comment} and url is ${url}"
echo 1
fi
}
function is_comment_available() {
local tflint_exitcode="$1"
echo [ "${tflint_exitcode}" != "0" ]
}
function main() {
declare GITHUB_EVENT_NAME GITHUB_EVENT_PATH GITHUB_TOKEN
local terraform_location="${INPUT_TFLINT_ACTION_FOLDER:-$GITHUB_WORKSPACE}"
local tflint_opts="${INPUT_TFLINT_ACTION__OPTS:-}"
local tflint_action_comment="${INPUT_TFLINT_ACTION_COMMENT:-0}"
local github_event_type="${GITHUB_EVENT_NAME}"
local github_event_path="${GITHUB_EVENT_PATH}"
local github_token="${GITHUB_TOKEN}"
local tflint_output
local tflint_exitcode
if [ -x "$(command -v tflint)" ]; then
local tflint_parameters
# shellcheck disable=SC2086,SC2116
tflint_parameters=$(echo ${tflint_opts} ${terraform_location})
# shellcheck disable=SC2086,SC2116
tflint_output=$(tflint --no-color $tflint_parameters)
tflint_exitcode=${?}
comment_enabled=$(is_comment_enabled "${tflint_action_comment}")
pull_request=$(is_pull_request "${github_event_type}")
## We should only send a comment if we have comments enabled, it's a pull-request and we have a github token
if [ "$pull_request" -eq 1 ]; then
if [ -n "$github_token" ]; then
local comment_available
comment_available=$(is_comment_available "${tflint_exitcode}")
if [ "$comment_available" -eq 1 ] && [ "$comment_enabled" -eq 1 ]; then
local comment
comment=$(format_comment "${tflint_output}" "${tflint_exitcode}")
url=$(get_url "$GITHUB_EVENT_PATH")
post_comment "${comment}" "${github_event_type}" "${github_token}" "${url}"
else
echo "::debug::No comment available"
fi
else
echo "::debug::GITHUB_TOKEN is required to perform this action"
fi
else
echo "::debug::The event name was ${GITHUB_EVENT}"
fi
echo "::set-output name=tf_lint_output::${tflint_output}"
echo "::set-output name=tf_lint_status::${tflint_status}"
exit "$tflint_exitcode"
else
echo "::debug::tflint is required to perform this action"
exit 1
fi
}
# shellcheck disable=SC1234
if [ "${1}" != "--source-only" ]; then
main "${@}"
fi