github-action-markdown-link.../entrypoint.sh

219 lines
5.2 KiB
Bash
Raw Normal View History

2019-04-03 08:16:17 +03:00
#!/usr/bin/env bash
set -eu
NC='\033[0m' # No Color
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
2020-08-19 07:44:02 +03:00
RED='\033[0;31m'
2023-04-17 14:20:47 +03:00
npm i -g markdown-link-check@3.11.1
2022-04-29 03:53:40 +03:00
echo "::group::Debug information"
npm -g list --depth=1
echo "::endgroup::"
2019-04-03 08:16:17 +03:00
2020-04-05 13:20:35 +03:00
declare -a FIND_CALL
2020-08-09 15:01:29 +03:00
declare -a COMMAND_DIRS COMMAND_FILES
declare -a COMMAND_FILES
2020-04-05 13:20:35 +03:00
USE_QUIET_MODE="$1"
USE_VERBOSE_MODE="$2"
CONFIG_FILE="$3"
FOLDER_PATH="$4"
2020-04-01 13:12:32 +03:00
MAX_DEPTH="$5"
2020-04-05 13:20:35 +03:00
CHECK_MODIFIED_FILES="$6"
BASE_BRANCH="$7"
2022-08-09 11:16:23 +03:00
2020-06-16 02:19:01 +03:00
if [ -z "$8" ]; then
FILE_EXTENSION=".md"
2020-06-16 11:36:25 +03:00
else
FILE_EXTENSION="$8"
2020-06-06 14:21:20 +03:00
fi
2020-08-09 15:01:29 +03:00
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}"
2020-04-01 13:12:32 +03:00
echo -e "${BLUE}MAX_DEPTH: $5${NC}"
2020-04-05 13:20:35 +03:00
echo -e "${BLUE}CHECK_MODIFIED_FILES: $6${NC}"
echo -e "${BLUE}FILE_EXTENSION: $8${NC}"
2020-08-09 15:01:29 +03:00
echo -e "${BLUE}FILE_PATH: $9${NC}"
handle_dirs () {
IFS=', ' read -r -a DIRLIST <<< "$FOLDER_PATH"
for index in "${!DIRLIST[@]}"
do
2020-08-19 07:44:02 +03:00
if [ ! -d "${DIRLIST[index]}" ]; then
echo -e "${RED}ERROR [✖] Can't find the directory: ${YELLOW}${DIRLIST[index]}${NC}"
exit 2
fi
2020-08-09 15:01:29 +03:00
COMMAND_DIRS+=("${DIRLIST[index]}")
done
FOLDERS="${COMMAND_DIRS[*]}"
2020-04-05 13:20:35 +03:00
}
2020-04-03 05:26:57 +03:00
2020-08-09 15:01:29 +03:00
handle_files () {
2020-08-09 15:01:29 +03:00
IFS=', ' read -r -a FILELIST <<< "$FILE_PATH"
2020-08-09 15:01:29 +03:00
for index in "${!FILELIST[@]}"
do
2020-08-19 07:44:02 +03:00
if [ ! -f "${FILELIST[index]}" ]; then
echo -e "${RED}ERROR [✖] Can't find the file: ${YELLOW}${FILELIST[index]}${NC}"
exit 2
fi
2020-12-09 16:33:00 +03:00
if [ "$index" == 0 ]; then
2020-08-09 15:01:29 +03:00
COMMAND_FILES+=("-wholename ${FILELIST[index]}")
else
COMMAND_FILES+=("-o -wholename ${FILELIST[index]}")
fi
done
FILES="${COMMAND_FILES[*]}"
2020-08-09 15:01:29 +03:00
}
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}"
2022-08-09 11:16:23 +03:00
echo "MLC_OUTPUT<<EOF" >> "$GITHUB_ENV"
cat error.txt >> "$GITHUB_ENV"
echo "EOF" >> "$GITHUB_ENV"
2020-08-09 15:01:29 +03:00
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}"
2022-08-09 11:16:23 +03:00
echo "MLC_OUTPUT=[✔] All links are good!" >> "$GITHUB_OUTPUT"
2020-08-09 15:01:29 +03:00
fi
else
echo -e "${GREEN}All good!${NC}"
2022-08-09 11:16:23 +03:00
echo "MLC_OUTPUT=All good!" >> "$GITHUB_OUTPUT"
2020-08-09 15:01:29 +03:00
fi
}
add_options () {
2020-04-05 13:20:35 +03:00
if [ -f "$CONFIG_FILE" ]; then
FIND_CALL+=('--config' "${CONFIG_FILE}")
fi
if [ "$USE_QUIET_MODE" = "yes" ]; then
FIND_CALL+=('-q')
fi
if [ "$USE_VERBOSE_MODE" = "yes" ]; then
FIND_CALL+=('-v')
fi
2020-08-09 15:01:29 +03:00
}
check_additional_files () {
if [ -n "$FILES" ]; then
if [ "$MAX_DEPTH" -ne -1 ]; then
FIND_CALL=('find' ${FOLDERS} '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
2020-08-09 15:01:29 +03:00
else
FIND_CALL=('find' ${FOLDERS} '-type' 'f' '(' ${FILES} ')' '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}')
2020-08-09 15:01:29 +03:00
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}"
2022-04-29 03:53:40 +03:00
2022-07-28 19:33:02 +03:00
git config --global --add safe.directory '*'
2022-04-29 03:53:40 +03:00
2020-08-09 15:01:29 +03:00
git fetch origin "${BASE_BRANCH}" --depth=1 > /dev/null
MASTER_HASH=$(git rev-parse origin/"${BASE_BRANCH}")
if [ -z "$FOLDERS" ]; then
FOLDERS="."
fi
2020-08-09 15:01:29 +03:00
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[@]}")
2020-04-05 13:20:35 +03:00
for i in "${FILE_ARRAY[@]}"
do
2020-08-07 05:21:48 +03:00
if [ "${i##*.}" == "${FILE_EXTENSION#.}" ]; then
2020-04-05 13:20:35 +03:00
FIND_CALL+=("${i}")
2020-06-09 10:57:51 +03:00
COMMAND="${FIND_CALL[*]}"
2020-04-05 13:20:35 +03:00
$COMMAND &>> error.txt || true
unset 'FIND_CALL[${#FIND_CALL[@]}-1]'
fi
done
2020-08-09 15:01:29 +03:00
check_additional_files
2022-04-29 03:53:40 +03:00
2020-04-05 13:20:35 +03:00
check_errors
else
2020-04-05 13:20:35 +03:00
if [ "$5" -ne -1 ]; then
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-maxdepth' "${MAX_DEPTH}" '-exec' 'markdown-link-check' '{}')
2020-04-05 13:20:35 +03:00
else
FIND_CALL=('find' ${FOLDERS} '-name' '*'"${FILE_EXTENSION}" '-not' '-path' './node_modules/*' '-exec' 'markdown-link-check' '{}')
2020-04-05 13:20:35 +03:00
fi
2020-08-09 15:01:29 +03:00
add_options
2020-04-05 13:20:35 +03:00
FIND_CALL+=(';')
set -x
"${FIND_CALL[@]}" &>> error.txt
set +x
2020-08-09 15:01:29 +03:00
check_additional_files
2020-04-05 13:20:35 +03:00
check_errors
2019-04-03 08:16:17 +03:00
fi