|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# ensure asciidoctor is installed |
| 6 | +if ! command -v asciidoctor &>/dev/null ; |
| 7 | +then |
| 8 | + echo "Asciidoctor is not installed. Please install it and try again." |
| 9 | + exit 127 |
| 10 | +fi |
| 11 | + |
| 12 | +# get the *.adoc modules and assemblies in the pull request |
| 13 | +FILES=$(git diff --name-only HEAD~1 HEAD --diff-filter=d "*.adoc") |
| 14 | + |
| 15 | +REPO_PATH=$(git rev-parse --show-toplevel) |
| 16 | + |
| 17 | +# get the modules in the PR, search for assemblies that include them, and concat with any updated assemblies files |
| 18 | +check_updated_assemblies () { |
| 19 | + MODULES=$(echo "$FILES" | awk '/modules\/(.*)\.adoc/') |
| 20 | + if [ "${MODULES}" ] |
| 21 | + then |
| 22 | + # $UPDATED_ASSEMBLIES is the list of assemblies that contains changed modules |
| 23 | + UPDATED_ASSEMBLIES=$(grep -rnwl "$REPO_PATH" --include=\*.adoc --exclude-dir={snippets,modules} -e "$MODULES") |
| 24 | + # subtract $REPO_PATH from path with bash substring replacement |
| 25 | + UPDATED_ASSEMBLIES=${UPDATED_ASSEMBLIES//"$REPO_PATH/"/} |
| 26 | + fi |
| 27 | + # $ASSEMBLIES is the list of modifed assemblies |
| 28 | + ASSEMBLIES=$(echo "$FILES" | awk '!/modules\/(.*)\.adoc/') |
| 29 | + # concatenate both lists and remove dupe entries |
| 30 | + ALL_ASSEMBLIES=$(echo "$UPDATED_ASSEMBLIES $ASSEMBLIES" | tr ' ' '\n' | sort -u) |
| 31 | + # validate the assemblies |
| 32 | + echo "Validating updated assemblies. Validation will fail with FAILED, ERROR, or WARNING messages..." |
| 33 | + asciidoctor $ALL_ASSEMBLIES -a icons! -o /dev/null -v --failure-level WARN |
| 34 | +} |
| 35 | + |
| 36 | +# check assemblies and fail if errors are reported |
| 37 | +if [ -n "${FILES}" ] ; |
| 38 | +then |
| 39 | + check_updated_assemblies |
| 40 | +else |
| 41 | + echo "No modified AsciiDoc files found." |
| 42 | +fi |
0 commit comments