Exclude files and folders

This commit is contained in:
Gaurav Nelson 2023-05-24 18:10:45 +10:00
parent 322b231568
commit 26605b0edd
3 changed files with 117 additions and 118 deletions

View File

@ -17,8 +17,10 @@ jobs:
uses: ./ uses: ./
with: with:
# Add a test to restrict the test to just dir4 and dir5. # Add a test to restrict the test to just dir4 and dir5.
folder-path: './md/dir4, ./md/dir5' folder-path: '.'
file-path: './md/AdditionalFileTest1.md, ./md/AdditionalFileTest2.md' file-path: './md/AdditionalFileTest1.md, ./md/AdditionalFileTest2.md'
exclude-folder-path: './md/dir3, ./md/dir4'
exclude-file: './md/dir1/level-1a.md, ./md/dir2/dir2level2/level-2b.md'
shellcheck: shellcheck:
runs-on: [ubuntu-latest] runs-on: [ubuntu-latest]
steps: steps:

View File

@ -2,11 +2,11 @@ name: 'markdown-link-check'
description: 'Check if all links are valid in markdown files.' description: 'Check if all links are valid in markdown files.'
author: 'Gaurav Nelson' author: 'Gaurav Nelson'
branding: branding:
icon: 'link' icon: 'link'
color: 'green' color: 'green'
inputs: inputs:
use-quiet-mode: use-quiet-mode:
description: 'Use yes to enable markdown-link-check quiet mode which only list errors.' description: 'Use yes to enable markdown-link-check quiet mode which only lists errors.'
required: true required: true
default: 'no' default: 'no'
use-verbose-mode: use-verbose-mode:
@ -14,11 +14,11 @@ inputs:
required: true required: true
default: 'no' default: 'no'
config-file: config-file:
description: 'Specify path to a markdown-link-check JSON configuration file.' description: 'Specify the path to a markdown-link-check JSON configuration file.'
required: true required: true
default: 'mlc_config.json' default: 'mlc_config.json'
folder-path: folder-path:
description: 'Specify path to a custom folder where your markdown files are located.' description: 'Specify the path to a custom folder where your markdown files are located.'
required: true required: true
default: '.' default: '.'
max-depth: max-depth:
@ -30,7 +30,7 @@ inputs:
required: true required: true
default: 'no' default: 'no'
base-branch: base-branch:
description: 'Use this to specify the base branch against which the action finds the modififed files.' description: 'Use this to specify the base branch against which the action finds the modified files.'
required: true required: true
default: 'master' default: 'master'
file-extension: file-extension:
@ -38,9 +38,17 @@ inputs:
required: true required: true
default: '.md' default: '.md'
file-path: file-path:
description: 'Specify additional files you want to check' description: 'Specify additional files you want to check.'
required: true required: true
default: '' default: ''
exclude-folder-path:
description: 'Specify the path to exclude folders from the search.'
required: false
default: ''
exclude-file:
description: 'Specify additional files to exclude from the check.'
required: false
default: ''
runs: runs:
using: 'docker' using: 'docker'
@ -55,3 +63,5 @@ runs:
- ${{ inputs.base-branch }} - ${{ inputs.base-branch }}
- ${{ inputs.file-extension }} - ${{ inputs.file-extension }}
- ${{ inputs.file-path }} - ${{ inputs.file-path }}
- ${{ inputs.exclude-folder-path }}
- ${{ inputs.exclude-file }}

View File

@ -13,9 +13,10 @@ echo "::group::Debug information"
npm -g list --depth=1 npm -g list --depth=1
echo "::endgroup::" echo "::endgroup::"
declare -a FIND_CALL declare -a COMMANDS
declare -a COMMAND_DIRS COMMAND_FILES declare -a FOLDERS
declare -a COMMAND_FILES declare -a EXCLUDE_DIRS
declare -a EXCLUDE_FILES
USE_QUIET_MODE="$1" USE_QUIET_MODE="$1"
USE_VERBOSE_MODE="$2" USE_VERBOSE_MODE="$2"
@ -30,6 +31,8 @@ else
FILE_EXTENSION="$8" FILE_EXTENSION="$8"
fi fi
FILE_PATH="$9" FILE_PATH="$9"
EXCLUDE_FOLDERS="${10:-}"
EXCLUDE_FILES="${11:-}"
if [ -f "$CONFIG_FILE" ]; then if [ -f "$CONFIG_FILE" ]; then
echo -e "${BLUE}Using markdown-link-check configuration file: ${YELLOW}$CONFIG_FILE${NC}" echo -e "${BLUE}Using markdown-link-check configuration file: ${YELLOW}$CONFIG_FILE${NC}"
@ -39,55 +42,73 @@ else
echo -e "customizing markdown-link-check by using a configuration file.${NC}" echo -e "customizing markdown-link-check by using a configuration file.${NC}"
fi fi
FOLDERS="" echo -e "${BLUE}USE_QUIET_MODE: $USE_QUIET_MODE${NC}"
FILES="" echo -e "${BLUE}USE_VERBOSE_MODE: $USE_VERBOSE_MODE${NC}"
echo -e "${BLUE}FOLDER_PATH: $FOLDER_PATH${NC}"
echo -e "${BLUE}USE_QUIET_MODE: $1${NC}" echo -e "${BLUE}MAX_DEPTH: $MAX_DEPTH${NC}"
echo -e "${BLUE}USE_VERBOSE_MODE: $2${NC}" echo -e "${BLUE}CHECK_MODIFIED_FILES: $CHECK_MODIFIED_FILES${NC}"
echo -e "${BLUE}FOLDER_PATH: $4${NC}" echo -e "${BLUE}FILE_EXTENSION: $FILE_EXTENSION${NC}"
echo -e "${BLUE}MAX_DEPTH: $5${NC}" echo -e "${BLUE}FILE_PATH: $FILE_PATH${NC}"
echo -e "${BLUE}CHECK_MODIFIED_FILES: $6${NC}" echo -e "${BLUE}EXCLUDE_FOLDERS: $EXCLUDE_FOLDERS${NC}"
echo -e "${BLUE}FILE_EXTENSION: $8${NC}" echo -e "${BLUE}EXCLUDE_FILES: $EXCLUDE_FILES${NC}"
echo -e "${BLUE}FILE_PATH: $9${NC}"
# Helper function to handle directory paths
handle_dirs () { handle_dirs () {
IFS=',' read -r -a DIRLIST <<< "$FOLDER_PATH"
IFS=', ' read -r -a DIRLIST <<< "$FOLDER_PATH" for index in "${!DIRLIST[@]}"; do
for index in "${!DIRLIST[@]}"
do
if [ ! -d "${DIRLIST[index]}" ]; then if [ ! -d "${DIRLIST[index]}" ]; then
echo -e "${RED}ERROR [✖] Can't find the directory: ${YELLOW}${DIRLIST[index]}${NC}" echo -e "${RED}ERROR [✖] Can't find the directory: ${YELLOW}${DIRLIST[index]}${NC}"
exit 2 exit 2
fi fi
COMMAND_DIRS+=("${DIRLIST[index]}") # Add directory paths to the array
FOLDERS+=("${DIRLIST[index]}")
done done
FOLDERS="${COMMAND_DIRS[*]}"
} }
# Helper function to handle excluded directory paths
handle_exclude_dirs () {
IFS=',' read -r -a EXCLUDE_DIRLIST <<< "$EXCLUDE_FOLDERS"
for index in "${!EXCLUDE_DIRLIST[@]}"; do
if [ ! -d "${EXCLUDE_DIRLIST[index]}" ]; then
echo -e "${RED}ERROR [✖] Can't find the directory: ${YELLOW}${EXCLUDE_DIRLIST[index]}${NC}"
exit 2
fi
# Add exclusion rules for directories to the array
EXCLUDE_DIRS+=("-not -path '${EXCLUDE_DIRLIST[index]}'")
done
}
# Helper function to handle file paths
handle_files () { handle_files () {
IFS=',' read -r -a FILELIST <<< "$FILE_PATH"
IFS=', ' read -r -a FILELIST <<< "$FILE_PATH" for index in "${!FILELIST[@]}"; do
for index in "${!FILELIST[@]}"
do
if [ ! -f "${FILELIST[index]}" ]; then if [ ! -f "${FILELIST[index]}" ]; then
echo -e "${RED}ERROR [✖] Can't find the file: ${YELLOW}${FILELIST[index]}${NC}" echo -e "${RED}ERROR [✖] Can't find the file: ${YELLOW}${FILELIST[index]}${NC}"
exit 2 exit 2
fi fi
if [ "$index" == 0 ]; then # Add file paths to the array
COMMAND_FILES+=("-wholename ${FILELIST[index]}") COMMANDS+=("${FILELIST[index]}")
else
COMMAND_FILES+=("-o -wholename ${FILELIST[index]}")
fi
done done
FILES="${COMMAND_FILES[*]}" }
# Helper function to handle excluded file paths
handle_exclude_files () {
IFS=',' read -r -a EXCLUDE_FILELIST <<< "$EXCLUDE_FILES"
for index in "${!EXCLUDE_FILELIST[@]}"; do
if [ ! -f "${EXCLUDE_FILELIST[index]}" ]; then
echo -e "${RED}ERROR [✖] Can't find the file: ${YELLOW}${EXCLUDE_FILELIST[index]}${NC}"
exit 2
fi
# Add exclusion rules for files to the array
EXCLUDE_FILES+=("-not -name '${EXCLUDE_FILELIST[index]}'")
done
} }
check_errors () { check_errors () {
if [ -e error.txt ] ; then if [ -e error.txt ] ; then
if grep -q "ERROR:" error.txt; then if grep -q "ERROR:" error.txt; then
echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}" echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}"
@ -105,108 +126,74 @@ check_errors () {
else else
echo -e "${GREEN}All good!${NC}" echo -e "${GREEN}All good!${NC}"
fi fi
} }
add_options () { if [ -n "$CONFIG_FILE" ]; then
COMMANDS+=("--config" "$CONFIG_FILE")
if [ -f "$CONFIG_FILE" ]; then
FIND_CALL+=('--config' "${CONFIG_FILE}")
fi
if [ "$USE_QUIET_MODE" = "yes" ]; then
FIND_CALL+=('-q')
fi
if [ "$USE_VERBOSE_MODE" = "yes" ]; then
FIND_CALL+=('-v')
fi
}
check_additional_files () {
if [ -n "$FILES" ]; then
if [ "$MAX_DEPTH" -ne -1 ]; then
FIND_CALL=('find' ${FOLDERS} '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
else
FIND_CALL=('find' ${FOLDERS} '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}')
fi
add_options
FIND_CALL+=(';')
set -x
"${FIND_CALL[@]}" &>> error.txt
set +x
fi
}
if [ -z "$8" ]; then
FOLDERS="."
else
handle_dirs
fi fi
if [ -n "$9" ]; then if [ "$USE_QUIET_MODE" = "yes" ]; then
handle_files COMMANDS+=("-q")
fi
if [ "$USE_VERBOSE_MODE" = "yes" ]; then
COMMANDS+=("-v")
fi
if [ -n "$EXCLUDE_FOLDERS" ]; then
handle_exclude_dirs
fi
if [ -n "$EXCLUDE_FILES" ]; then
handle_exclude_files
fi fi
if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then
echo -e "${BLUE}BASE_BRANCH: $BASE_BRANCH${NC}"
echo -e "${BLUE}BASE_BRANCH: $7${NC}"
git config --global --add safe.directory '*' git config --global --add safe.directory '*'
git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null git fetch origin "$BASE_BRANCH" --depth=1 > /dev/null
MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}") MASTER_HASH=$(git rev-parse origin/"$BASE_BRANCH")
if [ -z "$FOLDERS" ]; then if [ -z "${FOLDERS[*]}" ]; then
FOLDERS="." FOLDERS=(".")
fi fi
FIND_CALL=('markdown-link-check')
add_options
FOLDER_ARRAY=(${FOLDER_PATH//,/ }) FOLDER_ARRAY=(${FOLDER_PATH//,/ })
mapfile -t FILE_ARRAY < <( git diff --name-only --diff-filter=AM "$MASTER_HASH" -- "${FOLDER_ARRAY[@]}") mapfile -t FILE_ARRAY < <(git diff --name-only --diff-filter=AM "$MASTER_HASH" -- "${FOLDER_ARRAY[@]}")
for i in "${FILE_ARRAY[@]}" for i in "${FILE_ARRAY[@]}"; do
do if [ "${i##*.}" == "${FILE_EXTENSION#.}" ]; then
if [ "${i##*.}" == "${FILE_EXTENSION#.}" ]; then COMMANDS+=("$i")
FIND_CALL+=("${i}") markdown-link-check "${COMMANDS[@]}" >> error.txt || true
COMMAND="${FIND_CALL[*]}" unset 'COMMANDS[${#COMMANDS[@]}-1]'
$COMMAND &>> error.txt || true fi
unset 'FIND_CALL[${#FIND_CALL[@]}-1]' done
fi
done
check_additional_files
check_errors check_errors
else else
if [ "$MAX_DEPTH" -eq -1 ]; then
if [ "$5" -ne -1 ]; then MAX_DEPTH=""
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
else else
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}') MAX_DEPTH="-maxdepth $MAX_DEPTH"
fi fi
add_options if [ -n "$FOLDER_PATH" ]; then
handle_dirs
FIND_CALL+=(';') # Find all files with the specified extension
mapfile -t FILES < <(find "${FOLDERS[@]}" $MAX_DEPTH -type f -name "*$FILE_EXTENSION" "${EXCLUDE_DIRS[@]}" "${EXCLUDE_FILES[@]}")
set -x for i in "${FILES[@]}"; do
"${FIND_CALL[@]}" &>> error.txt COMMANDS+=("$i")
set +x markdown-link-check "${COMMANDS[@]}" >> error.txt || true
unset 'COMMANDS[${#COMMANDS[@]}-1]'
check_additional_files done
check_errors
check_errors
else
echo -e "${RED}ERROR [✖] No folder path provided.${NC}"
exit 2
fi
fi fi