From c5e6c7647294e5addbf74e3c65b339aee90ff3d3 Mon Sep 17 00:00:00 2001 From: Gaurav Nelson Date: Sun, 5 Apr 2020 20:20:35 +1000 Subject: [PATCH] Modified files check --- .github/workflows/push.yml | 11 +--- Dockerfile | 2 +- README.md | 56 ++++++++++++---- action.yml | 12 ++++ entrypoint.sh | 132 +++++++++++++++++++++++++------------ md/file1.md | 10 +-- md/file2.md | 6 +- 7 files changed, 158 insertions(+), 71 deletions(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index da6f853..84f5c73 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -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 @@ -16,6 +10,7 @@ jobs: with: use-quiet-mode: 'yes' folder-path: 'md' + check-modified-files-only: 'yes' shellcheck: runs-on: [ubuntu-latest] steps: diff --git a/Dockerfile b/Dockerfile index 4b729c0..8c227e2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/README.md b/README.md index 796a87b..286e3c1 100644 --- a/README.md +++ b/README.md @@ -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 @@ -110,3 +108,33 @@ checking for certain links in a markdown document. 2. `` Use this comment to disable link checking for the next line. 3. `` 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' + +``` diff --git a/action.yml b/action.yml index 930f614..f1fa87c 100644 --- a/action.yml +++ b/action.yml @@ -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' @@ -36,3 +46,5 @@ runs: - ${{ inputs.config-file }} - ${{ inputs.folder-path }} - ${{ inputs.max-depth }} + - ${{ inputs.check-modified-files-only }} + - ${{ inputs.base-branch }} diff --git a/entrypoint.sh b/entrypoint.sh index b205ee0..d902649 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -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 diff --git a/md/file1.md b/md/file1.md index 1930c4d..9c3d61b 100644 --- a/md/file1.md +++ b/md/file1.md @@ -3,23 +3,23 @@ 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]. +References and definitions are [checked][alpha]. ### 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) \ No newline at end of file diff --git a/md/file2.md b/md/file2.md index cb065f9..aa7bf69 100644 --- a/md/file2.md +++ b/md/file2.md @@ -1,10 +1,12 @@ # Checking more links ## Bravo - + This [doesn't exists](#alpha). This [one does](#bravo). ## Charlie -This is linked from file1. \ No newline at end of file +This is linked from file1. + +## change \ No newline at end of file