-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentrypoint.sh
executable file
·121 lines (98 loc) · 3.5 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
#!/bin/bash
set -euo pipefail
PATH="/opt/venv/bin:${PATH}"
default_files_regex='^.*\.(j2|jinja2|jinja)$'
default_ignore_rules=""
default_warning_rules=""
default_validate_all_codebase="false"
default_branch="main"
J2LINT_FILES_REGEX="${J2LINT_FILES_REGEX:-$default_files_regex}"
J2LINT_IGNORE_RULES="${J2LINT_IGNORE_RULES:-$default_ignore_rules}"
J2LINT_WARNING_RULES="${J2LINT_WARNING_RULES:-$default_warning_rules}"
VALIDATE_ALL_CODEBASE="${VALIDATE_ALL_CODEBASE:-$default_validate_all_codebase}"
DEFAULT_BRANCH="${DEFAULT_BRANCH:-$default_branch}"
lint_cmd="j2lint --stdin --json"
if [ -n "${J2LINT_IGNORE_RULES}" ]; then
lint_cmd="${lint_cmd} --ignore ${J2LINT_IGNORE_RULES}"
fi
if [ -n "${J2LINT_WARNING_RULES}" ]; then
lint_cmd="${lint_cmd} --warn ${J2LINT_WARNING_RULES}"
fi
if ! lint_cmd_check="$(echo "" | ${lint_cmd} 2>&1)"; then
echo "Invalid arguments provided for lint command: ${lint_cmd}"
echo "Command output:"
echo "------"
echo "${lint_cmd_check}"
echo "------"
echo ""
echo "Invalid j2lint arguments provided, exiting" >&2
exit 3
fi
if [ "${VALIDATE_ALL_CODEBASE}" = "true" ]; then
echo "Linting entire code base"
all_files="$(git ls-tree --name-only -r HEAD)"
else
echo "Linting new or changed files"
if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then
GITHUB_SHA="$(jq -r .pull_request.head.sha < "${GITHUB_EVENT_PATH}")"
fi
git config --global --add safe.directory "${GITHUB_WORKSPACE}"
git checkout --quiet "${DEFAULT_BRANCH}"
git checkout --quiet "${GITHUB_SHA}"
if [ "${GITHUB_EVENT_NAME}" = "push" ]; then
all_files="$(git diff-tree --no-commit-id --name-only -r "${GITHUB_SHA}")"
if [ -z "${all_files}" ]; then
all_files="$(git diff --name-only --diff-filter=d "${DEFAULT_BRANCH}...${GITHUB_SHA}")"
fi
else
all_files="$(git diff --name-only --diff-filter=d "${DEFAULT_BRANCH}...${GITHUB_SHA}")"
fi
echo ""
echo "New or changed files detected by git:"
echo "${all_files}"
fi
echo ""
echo "Jijna2 files to lint:"
check_files=""
for file in ${all_files}; do
if [[ "${file}" =~ ${J2LINT_FILES_REGEX} ]] && [ -f "${file}" ]; then
echo " * ${file}"
check_files="${check_files} ${file}"
fi
done
if [ -z "${check_files}" ]; then
echo "No jinja2 files to check"
exit 0
else
echo ""
echo "Checking each jinja2 file with lint command: ${lint_cmd}"
fi
lint_errors=0
lint_warnings=0
for file in ${check_files}; do
echo ""
echo "-----------------------------------------"
echo "Checking file: ${file}"
echo "Command output:"
echo "------"
lint_result="$(${lint_cmd} < "${file}" || true)"
echo "${lint_result}"
echo "------"
echo "${lint_result}" | \
jq -r '.ERRORS[] | "::error file='"${file}"',line=\(.line_number)::\(.message) (\(.id))"'
echo "${lint_result}" | \
jq -r '.WARNINGS[] | "::warning file='"${file}"',line=\(.line_number)::\(.message) (\(.id))"'
errors="$(echo "${lint_result}" | jq -r '.ERRORS | length')"
lint_errors=$((lint_errors + errors))
warnings="$(echo "${lint_result}" | jq -r '.WARNINGS | length')"
lint_warnings=$((lint_warnings + warnings))
echo "Linted ${file} with ${errors} error(s) and ${warnings} warning(s)"
echo "-----------------------------------------"
done
echo ""
echo "Total errors: ${lint_errors}"
echo "Total warnings: ${lint_warnings}"
if [ "${lint_errors}" -gt 0 ]; then
echo "Exiting with jinja2 linting errors"
exit 2
fi