Skip to content

Commit

Permalink
Exclude files and folders
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-nelson committed May 24, 2023
1 parent 6eb9643 commit 1410b35
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 33 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/push.yml
Expand Up @@ -9,16 +9,18 @@ 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:
- uses: actions/checkout@master
- 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:
Expand Down
12 changes: 11 additions & 1 deletion action.yml
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -55,3 +63,5 @@ runs:
- ${{ inputs.base-branch }}
- ${{ inputs.file-extension }}
- ${{ inputs.file-path }}
- ${{ inputs.exclude-folder-path }}
- ${{ inputs.exclude-file-path }}
93 changes: 65 additions & 28 deletions entrypoint.sh
Expand Up @@ -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}"
Expand All @@ -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}"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -108,7 +138,7 @@ check_errors () {

}

add_options () {
add_options() {

if [ -f "$CONFIG_FILE" ]; then
FIND_CALL+=('--config' "${CONFIG_FILE}")
Expand All @@ -124,7 +154,7 @@ add_options () {

}

check_additional_files () {
check_additional_files() {

if [ -n "$FILES" ]; then
if [ "$MAX_DEPTH" -ne -1 ]; then
Expand All @@ -138,7 +168,7 @@ check_additional_files () {
FIND_CALL+=(';')

set -x
"${FIND_CALL[@]}" &>> error.txt
"${FIND_CALL[@]}" &>>error.txt
set +x

fi
Expand All @@ -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
Expand All @@ -161,25 +199,24 @@ 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')

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

Expand All @@ -188,17 +225,17 @@ 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

FIND_CALL+=(';')

set -x
"${FIND_CALL[@]}" &>> error.txt
"${FIND_CALL[@]}" &>>error.txt
set +x

check_additional_files
Expand Down
4 changes: 3 additions & 1 deletion md/dir1/level-1a.md
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion md/dir2/level-1a.md
Expand Up @@ -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

Expand Down

0 comments on commit 1410b35

Please sign in to comment.