From 1410b356d48ab7c70b55700e21321723489d6f98 Mon Sep 17 00:00:00 2001 From: Gaurav Nelson Date: Wed, 24 May 2023 16:58:00 +1000 Subject: [PATCH] Exclude files and folders --- .github/workflows/push.yml | 6 ++- action.yml | 12 ++++- entrypoint.sh | 93 ++++++++++++++++++++++++++------------ md/dir1/level-1a.md | 4 +- md/dir2/level-1a.md | 4 +- 5 files changed, 86 insertions(+), 33 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 450e78f..9f72aa8 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -9,6 +9,7 @@ jobs: uses: ./ with: check-modified-files-only: 'yes' + exclude-file-path: './md/dir1/level-1a.md, md/dir2/level-1a.md' markdown-link-check-folders: runs-on: ubuntu-latest steps: @@ -16,9 +17,10 @@ jobs: - name: markdown-link-check uses: ./ with: - # Add a test to restrict the test to just dir4 and dir5. - folder-path: './md/dir4, ./md/dir5' + folder-path: './md' file-path: './md/AdditionalFileTest1.md, ./md/AdditionalFileTest2.md' + exclude-folder-path: './md/dir4, ./md/dir5' + exclude-file-path: 'file1.md, file2.md' shellcheck: runs-on: [ubuntu-latest] steps: diff --git a/action.yml b/action.yml index 1cd7665..668e9cf 100644 --- a/action.yml +++ b/action.yml @@ -2,7 +2,7 @@ name: 'markdown-link-check' description: 'Check if all links are valid in markdown files.' author: 'Gaurav Nelson' branding: - icon: 'link' + icon: 'link' color: 'green' inputs: use-quiet-mode: @@ -21,6 +21,10 @@ inputs: description: 'Specify path to a custom folder where your markdown files are located.' required: true default: '.' + exclude-folder-path: + description: 'Specify comma-separated paths of folders to be excluded from the check.' + required: false + default: '' max-depth: description: 'Specify a max-depth of directories you want to search for markdown files.' required: true @@ -41,6 +45,10 @@ inputs: description: 'Specify additional files you want to check' required: true default: '' + exclude-file-path: + description: 'Specify comma-separated paths of files to be excluded from the check.' + required: false + default: '' runs: using: 'docker' @@ -55,3 +63,5 @@ runs: - ${{ inputs.base-branch }} - ${{ inputs.file-extension }} - ${{ inputs.file-path }} + - ${{ inputs.exclude-folder-path }} + - ${{ inputs.exclude-file-path }} diff --git a/entrypoint.sh b/entrypoint.sh index ea1dc1d..8a879bf 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -30,6 +30,8 @@ else FILE_EXTENSION="$8" fi FILE_PATH="$9" +EXCLUDE_FOLDER_PATH="${10}" +EXCLUDE_FILE_PATH="${11}" if [ -f "$CONFIG_FILE" ]; then echo -e "${BLUE}Using markdown-link-check configuration file: ${YELLOW}$CONFIG_FILE${NC}" @@ -41,6 +43,8 @@ fi FOLDERS="" FILES="" +EXCLUDED_FOLDERS="" +EXCLUDED_FILES="" echo -e "${BLUE}USE_QUIET_MODE: $1${NC}" echo -e "${BLUE}USE_VERBOSE_MODE: $2${NC}" @@ -50,12 +54,11 @@ echo -e "${BLUE}CHECK_MODIFIED_FILES: $6${NC}" echo -e "${BLUE}FILE_EXTENSION: $8${NC}" echo -e "${BLUE}FILE_PATH: $9${NC}" -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 echo -e "${RED}ERROR [✖] Can't find the directory: ${YELLOW}${DIRLIST[index]}${NC}" exit 2 @@ -66,12 +69,39 @@ handle_dirs () { } -handle_files () { +handle_excluded_dirs() { - IFS=', ' read -r -a FILELIST <<< "$FILE_PATH" + IFS=', ' read -r -a EXCLUDED_DIRLIST <<<"$EXCLUDE_FOLDER_PATH" - for index in "${!FILELIST[@]}" - do + for index in "${!EXCLUDED_DIRLIST[@]}"; do + if [ ! -d "${EXCLUDED_DIRLIST[index]}" ]; then + echo -e "${RED}ERROR [✖] Can't find the directory: ${YELLOW}${EXCLUDED_DIRLIST[index]}${NC}" + exit 2 + fi + EXCLUDED_FOLDERS+=("-path ${EXCLUDED_DIRLIST[index]} -prune -o") + done + +} + +handle_excluded_files() { + + IFS=', ' read -r -a EXCLUDED_FILELIST <<<"$EXCLUDE_FILE_PATH" + + for index in "${!EXCLUDED_FILELIST[@]}"; do + if [ ! -f "${EXCLUDED_FILELIST[index]}" ]; then + echo -e "${RED}ERROR [✖] Can't find the file: ${YELLOW}${EXCLUDED_FILELIST[index]}${NC}" + exit 2 + fi + EXCLUDED_FILES+=("-name ${EXCLUDED_FILELIST[index]} -prune -o") + done + +} + +handle_files() { + + IFS=', ' read -r -a FILELIST <<<"$FILE_PATH" + + for index in "${!FILELIST[@]}"; do if [ ! -f "${FILELIST[index]}" ]; then echo -e "${RED}ERROR [✖] Can't find the file: ${YELLOW}${FILELIST[index]}${NC}" exit 2 @@ -86,9 +116,9 @@ handle_files () { } -check_errors () { +check_errors() { - if [ -e error.txt ] ; then + if [ -e error.txt ]; then if grep -q "ERROR:" error.txt; then echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}" cat error.txt @@ -108,7 +138,7 @@ check_errors () { } -add_options () { +add_options() { if [ -f "$CONFIG_FILE" ]; then FIND_CALL+=('--config' "${CONFIG_FILE}") @@ -124,7 +154,7 @@ add_options () { } -check_additional_files () { +check_additional_files() { if [ -n "$FILES" ]; then if [ "$MAX_DEPTH" -ne -1 ]; then @@ -138,7 +168,7 @@ check_additional_files () { FIND_CALL+=(';') set -x - "${FIND_CALL[@]}" &>> error.txt + "${FIND_CALL[@]}" &>>error.txt set +x fi @@ -151,6 +181,14 @@ else handle_dirs fi +if [ -n "$EXCLUDE_FOLDER_PATH" ]; then + handle_excluded_dirs +fi + +if [ -n "$EXCLUDE_FILE_PATH" ]; then + handle_excluded_files +fi + if [ -n "$9" ]; then handle_files fi @@ -161,7 +199,7 @@ if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then 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}") FIND_CALL=('markdown-link-check') @@ -169,17 +207,16 @@ if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then add_options FOLDER_ARRAY=(${FOLDER_PATH//,/ }) - mapfile -t FILE_ARRAY < <( git diff --name-only --diff-filter=AM "$MASTER_HASH" -- "${FOLDER_ARRAY[@]}") - - for i in "${FILE_ARRAY[@]}" - do - if [ "${i##*.}" == "${FILE_EXTENSION#.}" ]; then - FIND_CALL+=("${i}") - COMMAND="${FIND_CALL[*]}" - $COMMAND &>> error.txt || true - unset 'FIND_CALL[${#FIND_CALL[@]}-1]' - fi - done + mapfile -t FILE_ARRAY < <(git diff --name-only --diff-filter=AM "$MASTER_HASH" -- "${FOLDER_ARRAY[@]}") + + for i in "${FILE_ARRAY[@]}"; do + if [ "${i##*.}" == "${FILE_EXTENSION#.}" ]; then + FIND_CALL+=("${i}") + COMMAND="${FIND_CALL[*]}" + $COMMAND &>>error.txt || true + unset 'FIND_CALL[${#FIND_CALL[@]}-1]' + fi + done check_additional_files @@ -188,9 +225,9 @@ if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then else if [ "$5" -ne -1 ]; then - FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}') + FIND_CALL=('find' ${FOLDERS} ${EXCLUDED_FOLDERS[@]} ${EXCLUDED_FILES[@]} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}') else - FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}') + FIND_CALL=('find' ${FOLDERS} ${EXCLUDED_FOLDERS[@]} ${EXCLUDED_FILES[@]} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}') fi add_options @@ -198,7 +235,7 @@ else FIND_CALL+=(';') set -x - "${FIND_CALL[@]}" &>> error.txt + "${FIND_CALL[@]}" &>>error.txt set +x check_additional_files diff --git a/md/dir1/level-1a.md b/md/dir1/level-1a.md index 1930c4d..a64ff52 100644 --- a/md/dir1/level-1a.md +++ b/md/dir1/level-1a.md @@ -6,7 +6,9 @@ www.google.com [This is a broken link](https://www.exampleexample.cox) [This is another broken link](http://ignored-domain.com) but its ignored using a -configuration file. +configuration file. + +This is a change. ### Alpha diff --git a/md/dir2/level-1a.md b/md/dir2/level-1a.md index 1930c4d..a64ff52 100644 --- a/md/dir2/level-1a.md +++ b/md/dir2/level-1a.md @@ -6,7 +6,9 @@ www.google.com [This is a broken link](https://www.exampleexample.cox) [This is another broken link](http://ignored-domain.com) but its ignored using a -configuration file. +configuration file. + +This is a change. ### Alpha