Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to search for additional files #62

Merged
merged 1 commit into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ jobs:
uses: ./
with:
use-quiet-mode: 'yes'
folder-path: 'md'
folder-path: 'md/dir1, md/dir2'
file-path: './README.md, ./LICENSE, ./md/file4.markdown'
shellcheck:
runs-on: [ubuntu-latest]
steps:
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ You customize the action by using the following variables:
|`use-quiet-mode`| Specify `yes` to only show errors in output.| `no`|
|`use-verbose-mode`|Specify `yes` to show detailed HTTP status for checked links. |`no` |
|`config-file`|Specify a [custom configuration file](https://github.com/tcort/markdown-link-check#config-file-format) for markdown-link-check. You can use it to remove false-positives by specifying replacement patterns and ignore patterns.|`mlc_config.json`|
|`folder-path` |By default the `github-action-markdown-link-check` action checks for all markdown files in your repository. Use this option to limit checks to only specific folders. |`.` |
|`folder-path` |By default the `github-action-markdown-link-check` action checks for all markdown files in your repository. Use this option to limit checks to only specific folders. Use comma separated values for checking multiple folders. |`.` |
|`max-depth` |Specify how many levels deep you want to check in the directory structure. The default value is `-1` which means check all levels.|`-1` |
|`check-modified-files-only` |Use this variable to only check modified markdown files instead of checking all markdown files. The action uses `git` to find modified markdown files. Only use this variable when you run the action to check pull requests.|`no`|
|`base-branch`|Use this variable to specify the branch to compare when finding modified markdown files. |`master`|
|`file-extension`|By default the `github-action-markdown-link-check` action checks files in your repository with the `.md` extension. Use this option to specify a different file extension such as `.markdown` or `.mdx`.|`.md`|
|`file-path` | Specify additional files (with complete path and extension) you want to check. Use comma separated values for checking multiple files. | - |

#### Sample workflow with variables

Expand Down Expand Up @@ -142,6 +143,23 @@ jobs:

```

### Check multiple directories and files

```yml
on: [pull_request]
name: Check links for modified files
jobs:
markdown-link-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: gaurav-nelson/github-action-markdown-link-check@v1
with:
use-quiet-mode: 'yes'
folder-path: 'md/dir1, md/dir2'
file-path: './README.md, ./LICENSE, ./md/file4.markdown'
```

## Versioning
GitHub Action - Markdown link check follows the [GitHub recommended versioning strategy](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md).

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ inputs:
description: 'Use this to specify the file extension of Markdown files.'
required: true
default: '.md'
file-path:
description: 'Specify additional files you want to check'
required: true
default: ''

runs:
using: 'docker'
Expand All @@ -53,3 +57,4 @@ runs:
- ${{ inputs.check-modified-files-only }}
- ${{ inputs.base-branch }}
- ${{ inputs.file-extension }}
- ${{ inputs.file-path }}
159 changes: 113 additions & 46 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ BLUE='\033[0;34m'
npm i -g markdown-link-check@3.8.1

declare -a FIND_CALL
declare -a COMMAND_DIRS COMMAND_FILES
declare -a COMMAND_FILES

USE_QUIET_MODE="$1"
USE_VERBOSE_MODE="$2"
Expand All @@ -23,50 +25,81 @@ if [ -z "$8" ]; then
else
FILE_EXTENSION="$8"
fi
FILE_PATH="$9"

if [ -f "$CONFIG_FILE" ]; then
echo -e "${BLUE}Using markdown-link-check configuration file: ${YELLOW}$CONFIG_FILE${NC}"
else
echo -e "${BLUE}Cannot find ${YELLOW}$CONFIG_FILE${NC}"
echo -e "${YELLOW}NOTE: See https://github.com/tcort/markdown-link-check#config-file-format to know more about"
echo -e "customizing markdown-link-check by using a configuration file.${NC}"
fi

FOLDERS=""
FILES=""

echo -e "${BLUE}USE_QUIET_MODE: $1${NC}"
echo -e "${BLUE}USE_VERBOSE_MODE: $2${NC}"
echo -e "${BLUE}FOLDER_PATH: $4${NC}"
echo -e "${BLUE}MAX_DEPTH: $5${NC}"
echo -e "${BLUE}CHECK_MODIFIED_FILES: $6${NC}"
echo -e "${BLUE}FILE_EXTENSION: $8${NC}"
echo -e "${BLUE}FILE_PATH: $9${NC}"

handle_dirs () {

IFS=', ' read -r -a DIRLIST <<< "$FOLDER_PATH"

for index in "${!DIRLIST[@]}"
do
COMMAND_DIRS+=("${DIRLIST[index]}")
done
FOLDERS="${COMMAND_DIRS[*]}"

check_errors () {
if [ -e error.txt ] ; then
if grep -q "ERROR:" error.txt; then
echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}"
cat error.txt
printf "\n"
echo -e "${YELLOW}=========================================================================${NC}"
exit 113
else
echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}"
printf "\n"
echo -e "${GREEN}[✔] All links are good!${NC}"
printf "\n"
echo -e "${YELLOW}=========================================================================${NC}"
fi
else
echo -e "${GREEN}All good!${NC}"
fi
}

if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then
handle_files () {

echo -e "${BLUE}BASE_BRANCH: $7${NC}"
IFS=', ' read -r -a FILELIST <<< "$FILE_PATH"

git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null
MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}")
for index in "${!FILELIST[@]}"
do
if [ $index == 0 ]; then
COMMAND_FILES+=("-wholename ${FILELIST[index]}")
else
COMMAND_FILES+=("-o -wholename ${FILELIST[index]}")
fi
done
FILES="${COMMAND_FILES[*]}"

FIND_CALL=('markdown-link-check')
}

check_errors () {

if [ -e error.txt ] ; then
if grep -q "ERROR:" error.txt; then
echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}"
cat error.txt
printf "\n"
echo -e "${YELLOW}=========================================================================${NC}"
exit 113
else
echo -e "${YELLOW}=========================> MARKDOWN LINK CHECK <=========================${NC}"
printf "\n"
echo -e "${GREEN}[✔] All links are good!${NC}"
printf "\n"
echo -e "${YELLOW}=========================================================================${NC}"
fi
else
echo -e "${GREEN}All good!${NC}"
fi

}

add_options () {

if [ -f "$CONFIG_FILE" ]; then
echo -e "${BLUE}Using markdown-link-check configuration file: ${YELLOW}$CONFIG_FILE${NC}"
FIND_CALL+=('--config' "${CONFIG_FILE}")
else
echo -e "${BLUE}Cannot find ${YELLOW}$CONFIG_FILE${NC}"
echo -e "${YELLOW}NOTE: See https://github.com/tcort/markdown-link-check#config-file-format to know more about"
echo -e "customizing markdown-link-check by using a configuration file.${NC}"
fi

if [ "$USE_QUIET_MODE" = "yes" ]; then
Expand All @@ -77,6 +110,50 @@ if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then
FIND_CALL+=('-v')
fi

}

check_additional_files () {

if [ -n "$FILES" ]; then
if [ "$MAX_DEPTH" -ne -1 ]; then
FIND_CALL=('find' '.' '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
else
FIND_CALL=('find' '.' '-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

if [ -n "$9" ]; then
handle_files
fi

if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then

echo -e "${BLUE}BASE_BRANCH: $7${NC}"

git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null
MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}")

FIND_CALL=('markdown-link-check')

add_options

mapfile -t FILE_ARRAY < <( git diff --name-only "$MASTER_HASH" )

for i in "${FILE_ARRAY[@]}"
Expand All @@ -88,39 +165,29 @@ if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then
unset 'FIND_CALL[${#FIND_CALL[@]}-1]'
fi
done

check_additional_files

check_errors

else

if [ "$5" -ne -1 ]; then
FIND_CALL=('find' "${FOLDER_PATH}" '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
else
FIND_CALL=('find' "${FOLDER_PATH}" '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}')
fi

if [ -f "$CONFIG_FILE" ]; then
echo -e "${BLUE}Using markdown-link-check configuration file: ${YELLOW}$CONFIG_FILE${NC}"
FIND_CALL+=('--config' "${CONFIG_FILE}")
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
else
echo -e "${BLUE}Cannot find ${YELLOW}$CONFIG_FILE${NC}"
echo -e "${YELLOW}NOTE: See https://github.com/tcort/markdown-link-check#config-file-format to know more about"
echo -e "customizing markdown-link-check by using a configuration file.${NC}"
fi

if [ "$USE_QUIET_MODE" = "yes" ]; then
FIND_CALL+=('-q')
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}')
fi

if [ "$USE_VERBOSE_MODE" = "yes" ]; then
FIND_CALL+=('-v')
fi
add_options

FIND_CALL+=(';')

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

check_additional_files

check_errors

fi
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions md/dir2/dir2level2/level-2a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

## Test internal and external links

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.

### Alpha

This [exists](#alpha).
This [one does not](#does-not).
References and definitions are [checked][alpha] [too][charlie].

### Bravo

Headings in `readme.md` are [not checked](file1.md#bravo).
But [missing files are reported](missing-example.js).

[alpha]: #alpha
[charlie]: #charlie

External file: [Charlie](./file2.md/#charlie)
10 changes: 10 additions & 0 deletions md/dir2/dir2level2/level-2b.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Checking more links

## Bravo

This [doesn't exists](#alpha).
This [one does](#bravo).

## Charlie

This is linked from file1.
25 changes: 25 additions & 0 deletions md/dir2/level-1a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

## Test internal and external links

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.

### Alpha

This [exists](#alpha).
This [one does not](#does-not).
References and definitions are [checked][alpha] [too][charlie].

### Bravo

Headings in `readme.md` are [not checked](file1.md#bravo).
But [missing files are reported](missing-example.js).

[alpha]: #alpha
[charlie]: #charlie

External file: [Charlie](./file2.md/#charlie)
10 changes: 10 additions & 0 deletions md/dir2/level-1b.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Checking more links

## Bravo

This [doesn't exists](#alpha).
This [one does](#bravo).

## Charlie

This is linked from file1.
25 changes: 25 additions & 0 deletions md/dir3/dir3level2/level-2a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

## Test internal and external links

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.

### Alpha

This [exists](#alpha).
This [one does not](#does-not).
References and definitions are [checked][alpha] [too][charlie].

### Bravo

Headings in `readme.md` are [not checked](file1.md#bravo).
But [missing files are reported](missing-example.js).

[alpha]: #alpha
[charlie]: #charlie

External file: [Charlie](./file2.md/#charlie)
10 changes: 10 additions & 0 deletions md/dir3/dir3level2/level-2b.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Checking more links

## Bravo

This [doesn't exists](#alpha).
This [one does](#bravo).

## Charlie

This is linked from file1.