-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathcheck-asciidoctor-build.sh
executable file
·86 lines (81 loc) · 3.36 KB
/
check-asciidoctor-build.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
#!/bin/bash
# ensure asciidoctor is installed
if ! command -v asciidoctor &>/dev/null ;
then
echo "Asciidoctor is not installed. Please install it and try again. 👻"
exit 127
fi
# get the *.adoc modules and assemblies in the pull request
FILES=$(git diff --name-only HEAD~1 HEAD --diff-filter=d "*.adoc" ':(exclude)_unused_topics/*')
REPO_PATH=$(git rev-parse --show-toplevel)
# get the modules in the PR, search for assemblies that include them, and concat with any updated assemblies files
check_updated_assemblies () {
MODULES=$(echo "$FILES" | awk '/modules\/(.*)\.adoc/')
if [ "${MODULES}" ]
then
# $UPDATED_ASSEMBLIES is the list of assemblies that contains changed modules
UPDATED_ASSEMBLIES=$(grep -rnwl "$REPO_PATH" --include=\*.adoc --exclude-dir={snippets,modules} -e "$MODULES")
# Exit 0 if there are no modified assemblies
if [[ -z "${UPDATED_ASSEMBLIES}" ]]
then
echo "No updated assemblies found! ✅"
exit 0
fi
update_log
# subtract $REPO_PATH from path with bash substring replacement
UPDATED_ASSEMBLIES=${UPDATED_ASSEMBLIES//"$REPO_PATH/"/}
fi
# $ASSEMBLIES is the list of modifed assemblies
ASSEMBLIES=$(echo "$FILES" | awk '!/modules\/(.*)\.adoc/')
# concatenate both lists and remove dupe entries
ALL_ASSEMBLIES=$(echo "$UPDATED_ASSEMBLIES $ASSEMBLIES" | tr ' ' '\n' | sort -u)
# check that assemblies are in a topic_map
for ASSEMBLY in ${ALL_ASSEMBLIES}; do
# get the page name to search the topic_map
# search for files only, not folders
PAGE="File: $(basename "$ASSEMBLY" .adoc)"
# don't validate the assembly if it is not in a topic map
if grep -rq "$PAGE" --include "*.yml" _topic_maps ; then
# validate the assembly
echo "Validating $ASSEMBLY ..."
RED='\033[0;31m'
NC='\033[0m'
OUTPUT=$(asciidoctor "$ASSEMBLY" -a source-highlighter=rouge -a icons! -o /tmp/out.html -v --failure-level WARN --trace 2>&1)
# check assemblies and fail if errors are reported
if [[ $? != 0 ]];
then
echo -e "${RED}$OUTPUT${NC}"
ERRORS=true
else
echo "No errors found for $ASSEMBLY! ✅"
fi
else
echo "$ASSEMBLY is not in a topic_map, skipping validation... 😙"
fi
done
if [ "$ERRORS" = true ]; then
echo "Validation errors found! ❌"
exit 1
fi
}
update_log () {
echo ""
echo "****************************************************************************"
echo ""
echo "Validating all AsciiDoc assemblies that are modifed by the pull request. 🕵"
echo "All assemblies that include modifed modules are validated. 🙀"
echo "This might include assemblies that are not in the pull request! 🤬"
echo "Validation will fail with FAILED, ERROR, or WARNING messages. ❌"
echo "Correct all reported AsciiDoc errors to pass the validation build. 🤟"
echo ""
echo "****************************************************************************"
echo ""
}
# check assemblies and fail if errors are reported
if [ -n "${FILES}" ] ;
then
check_updated_assemblies
else
echo "No modified AsciiDoc files found! 🥳"
exit 0
fi