-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathentrypoint.sh
executable file
·68 lines (54 loc) · 1.47 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
#!/bin/bash
BASE="${INPUT_PATH}"
COVERAGE="${INPUT_COVERAGE%%%}" # trim % (e.g. 90% -> 90)
FILES=( "${INPUT_FILES}" )
run_opa() {
local -a targets
targets=( $(find ${BASE} -name '*.rego') )
if (( ${#FILES[@]} > 0 )); then
targets=( "${FILES[@]}" )
fi
local error=false
local rego
for rego in ${targets[@]}
do
# target is only .rego file
if [[ ${rego} != *.rego ]]; then
continue
fi
# target is only .rego file
if [[ ${rego} =~ _test.rego$ ]]; then
continue
fi
rego_test="${rego%.rego}_test.rego"
if [[ ! -f ${rego_test} ]]; then
echo "[ERROR] ${rego}: test file not found" >&2
error=true
continue
fi
echo "[INFO] Testing for ${rego}..."
opa test ${rego} ${rego_test} || error=true
echo "[INFO] Checking test coverage for ${rego}..."
local coverage
coverage="$(opa test --coverage --format=json ${rego} ${rego_test} | jq .coverage 2>/dev/null)"
if (( ${coverage%.*} > ${COVERAGE} )); then
echo "PASS: coverage ${coverage}%"
else
echo "[ERROR] current test coverage ${coverage}%: must to be ${COVERAGE}.0% or greater" >&2
error=true
continue
fi
done
if ${error}; then
return 1
fi
}
main() {
run_opa "$@" 2>&1 | tee -a result
result="$(cat result)"
# https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/td-p/37870
echo "result=${result//$'\n'/'%0A'}" >> $GITHUB_OUTPUT
}
set -o pipefail
main "$@"
exit $?