Skip to content

Commit

Permalink
Modified files check
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav Nelson committed Apr 5, 2020
1 parent 0f043f6 commit c5e6c76
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 71 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
on:
push:
branches:
- master
pull_request:
branches:
- master
name: Link Check
on: [pull_request]
name: PR Checks
jobs:
markdown-link-check:
runs-on: ubuntu-latest
Expand All @@ -16,6 +10,7 @@ jobs:
with:
use-quiet-mode: 'yes'
folder-path: 'md'
check-modified-files-only: 'yes'
shellcheck:
runs-on: [ubuntu-latest]
steps:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM node:alpine
RUN apk add --no-cache bash>5.0.16-r0
RUN apk add --no-cache bash>5.0.16-r0 git>2.26.0-r0
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
56 changes: 42 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@ This GitHub action checks all Markdown files in your repository for broken links
- [Custom variables](#custom-variables)
- [Scheduled runs](#scheduled-runs)
- [Disable check for some links](#disable-check-for-some-links)
- [Check only modified files in a pull request](#check-only-modified-files-in-a-pull-request)
### Custom variables
You cancustomize the action by using the following variables:
- `use-quiet-mode`: Specify `yes` to only show errors in output.
- `use-verbose-mode`: Specify `yes` to show detailed HTTP status for checked links.
- `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.
- `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.
- `max-depth`: Specify how many levels deep you want to check in the directory
structure. By default this is not set, using e.g. `1` will limit to
top-level directory only or `folder-path` only, if set.
You customize the action by using the following variables:
| Variable | Description | Default value |
|:----------|:--------------|:-----------|
|`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. |`.` |
|`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`|

#### Sample workflow with variables

Expand Down Expand Up @@ -110,3 +108,33 @@ checking for certain links in a markdown document.
2. `<!-- markdown-link-check-disable-next-line -->` Use this comment to disable link checking for the next line.
3. `<!-- markdown-link-check-disable-line -->` Use this comment to disable link
checking for the current line.

### Check only modified files in a pull request

Use the following workflow to only check links in modified markdown files in a
pull request.

When
you use this variable, the action finds modififed files between two commits:
- latest commit in you PR
- latest commit in the `master` branch. If you are suing a different branch to
merge PRs, specify the branch using `base-branch`.

> **NOTE**: We can also use GitHub API to get all modified files in a PR, but that
> would require tokens and stuff, create an issue or PR if you need that.

```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@0.6.0
with:
use-quiet-mode: 'yes'
use-verbose-mode: 'yes'
check-modified-files-only: 'yes'
```
12 changes: 12 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ inputs:
description: 'Specify a max-depth of directories you want to search for markdown files.'
required: true
default: '-1'
check-modified-files-only:
description: 'Use yes to only check for modified markdown files instead of checking all markdown files.'
required: true
default: 'no'
base-branch:
description: 'Use this to specify the base branch against which the action
finds the modififed files.'
required: true
default: 'master'

runs:
using: 'docker'
image: 'Dockerfile'
Expand All @@ -36,3 +46,5 @@ runs:
- ${{ inputs.config-file }}
- ${{ inputs.folder-path }}
- ${{ inputs.max-depth }}
- ${{ inputs.check-modified-files-only }}
- ${{ inputs.base-branch }}
132 changes: 91 additions & 41 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,112 @@ BLUE='\033[0;34m'

npm i -g markdown-link-check

declare -a FIND_CALL

USE_QUIET_MODE="$1"
USE_VERBOSE_MODE="$2"
CONFIG_FILE="$3"
FOLDER_PATH="$4"
MAX_DEPTH="$5"
CHECK_MODIFIED_FILES="$6"
BASE_BRANCH="$7"

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}"

declare -a FIND_CALL
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 [ "$5" -ne -1 ]; then
FIND_CALL=('find' "${FOLDER_PATH}" '-name' '*.md' '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
else
FIND_CALL=('find' "${FOLDER_PATH}" '-name' '*.md' '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}')
fi
if [ "$CHECK_MODIFIED_FILES" = "yes" ]; then

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
echo -e "${BLUE}BASE_BRANCH: $7${NC}"

if [ "$USE_QUIET_MODE" = "yes" ]; then
FIND_CALL+=('-q')
fi
git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null
MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}")

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

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
FIND_CALL+=('-q')
fi

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

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

for i in "${FILE_ARRAY[@]}"
do
if [ ${i: -3} = ".md" ]; then
FIND_CALL+=("${i}")
COMMAND="${FIND_CALL[@]}"
$COMMAND &>> error.txt || true
unset 'FIND_CALL[${#FIND_CALL[@]}-1]'
fi
done

check_errors

FIND_CALL+=(';')

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

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}"

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

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

FIND_CALL+=(';')

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

fi
10 changes: 5 additions & 5 deletions md/file1.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

www.google.com

<!-- markdown-link-check-disable-next-line -->
[This is a broken link](https://www.exampleexample.cox)

<!-- markdown-link-check-disable-next-line -->
[This is another broken link](http://ignored-domain.com) but its ignored using a
configuration file.

### Alpha

This [exists](#alpha).
<!-- markdown-link-check-disable-next-line -->
This [one does not](#does-not).
References and definitions are [checked][alpha] [too][charlie].
References and definitions are [checked][alpha].

### Bravo

Headings in `readme.md` are [not checked](file1.md#bravo).
<!-- markdown-link-check-disable-next-line -->
But [missing files are reported](missing-example.js).

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

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

## Bravo

<!-- markdown-link-check-disable-next-line -->
This [doesn't exists](#alpha).
This [one does](#bravo).

## Charlie

This is linked from file1.
This is linked from file1.

## change

0 comments on commit c5e6c76

Please sign in to comment.