-
Notifications
You must be signed in to change notification settings - Fork 1
/
bicep-lint.sh
executable file
·62 lines (55 loc) · 2.27 KB
/
bicep-lint.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
# shellcheck shell=sh disable=SC3011,SC3014
## Download Azure CLI
if ! command -v az > /dev/null 2>&1
then
echo "Azure CLI not found - installing..."
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
exit
fi
## Install Bicep CLI
if [ "$BICEP_VERSION" != 'latest' ]; then
az bicep install -v "$BICEP_VERSION"
else
az bicep install
fi
# Now either run the full analysis or files changed based on the settings defined
if [ "$ANALYSE_ALL_FILES" == 'true' ]; then
while read -r file; do
az bicep build --file "$file" 2>> errors.txt
done <<< "$(find . -type f -name '*.bicep')"
else
if [ "$ACTION_EVENT_NAME" == 'pull_request' ]; then
while read -r file; do
az bicep build --file "$file" 2>> errors.txt
done <<< "$(git diff --name-only --diff-filter=d origin/"$CURRENT_CODE"..origin/"${CHANGED_CODE#"refs/heads/"}")"
else
while read -r file; do
az bicep build --file "$file" 2>> errors.txt
done <<< "$(git diff --name-only --diff-filter=d "$CURRENT_CODE".."$CHANGED_CODE")"
fi
fi
## Clean empty lines
sed -i '/^[[:blank:]]*$/ d' errors.txt
## Clean the notifications that a linter configuration was found
sed -i '/Linter Configuration/d' errors.txt
## Loop through each line identified in linter and set a GitHub message
while read -r message; do
SEVERITY="$(echo "$message" | grep -E ' Info | Warning | Error ' -o | sed 's/ //g')"
FILENAME="$(sed "s#.*$WORKSPACE\([^(]*\).*#\1#" <<<"$message")"
LINE="$(sed "s#.*(\([^,]*\).*#\1#" <<<"$message")"
COLUMN="$(sed "s#.*,\([^)]*\).*#\1#" <<<"$message")"
REMOVE_TITLE_START="${message#* : }"
REMOVE_TITLE_END="${REMOVE_TITLE_START%%: *}"
TITLE="${REMOVE_TITLE_END#*"$SEVERITY"}"
MESSAGE="${message##*: }"
## Sets GitHub Notifications based on severity set
if [ "$SEVERITY" == 'Info' ]; then
echo "::notice file=$FILENAME,line=$LINE,col=$COLUMN,title=$TITLE::$MESSAGE"
elif [ "$SEVERITY" == 'Error' ]; then
echo "::error file=$FILENAME,line=$LINE,col=$COLUMN,title=$TITLE::$MESSAGE"
elif expr "$MESSAGE" : "az bicep upgrade" > /dev/null; then
true # do not add upgrade prompts
else
echo "::warning file=$FILENAME,line=$LINE,col=$COLUMN,title=$TITLE::$MESSAGE"
fi
done < errors.txt