2016-08-08 08:42:20 -04:00
#!/usr/bin/env bash
2020-11-09 07:09:24 -05:00
set -o pipefail
2016-08-08 08:42:20 -04:00
2020-10-29 14:09:11 -04:00
cd " $( dirname " $0 " ) /.. " || exit 1
2019-10-22 05:06:14 -04:00
echo " => Linting documents at path $( pwd ) as $( whoami) ... "
2020-03-12 17:09:45 -04:00
echo
ERRORCODE = 0
2016-08-08 08:42:20 -04:00
# Use long options (e.g. --header instead of -H) for curl examples in documentation.
2018-03-26 09:05:33 -04:00
echo '=> Checking for cURL short options...'
2020-03-12 17:09:45 -04:00
echo
2017-09-25 04:50:06 -04:00
grep --extended-regexp --recursive --color= auto 'curl (.+ )?-[^- ].*' doc/ >/dev/null 2>& 1
2017-10-03 11:15:40 -04:00
if [ $? -eq 0 ]
2016-08-08 08:42:20 -04:00
then
2017-09-25 04:50:06 -04:00
echo ' ✖ ERROR: Short options for curl should not be used in documentation!
Use long options ( e.g., --header instead of -H) :' >& 2
grep --extended-regexp --recursive --color= auto 'curl (.+ )?-[^- ].*' doc/
2020-03-12 17:09:45 -04:00
( ( ERRORCODE++) )
2016-08-08 08:42:20 -04:00
fi
2021-02-08 16:09:25 -05:00
# Documentation pages need front matter for tracking purposes.
echo '=> Checking documentation for front matter...'
echo
2021-08-08 23:10:10 -04:00
if ! scripts/lint-docs-metadata.sh
2021-02-08 16:09:25 -05:00
then
echo '✖ ERROR: These documentation pages need front matter. See https://docs.gitlab.com/ee/development/documentation/index.html#stage-and-group-metadata for how to add it.' >& 2
echo
( ( ERRORCODE++) )
fi
2021-03-12 13:09:23 -05:00
# Test for non-standard spaces (NBSP, NNBSP, ZWSP) in documentation.
2020-12-15 04:10:00 -05:00
echo '=> Checking for non-standard spaces...'
echo
2021-03-12 13:09:23 -05:00
grep --extended-regexp --binary-file= without-match --recursive '[ ]' doc/ >/dev/null 2>& 1
2020-12-15 04:10:00 -05:00
if [ $? -eq 0 ]
then
2021-03-12 13:09:23 -05:00
echo ' ✖ ERROR: Non-standard spaces ( NBSP, NNBSP, ZWSP) should not be used in documentation.
2020-12-15 04:10:00 -05:00
https://docs.gitlab.com/ee/development/documentation/styleguide/index.html#spaces-between-words
Replace with standard spaces:' >& 2
# Find the spaces, then add color codes with sed to highlight each NBSP or NNBSP in the output.
grep --extended-regexp --binary-file= without-match --recursive --color= auto '[ ]' doc \
| sed -e '' / /s//` printf "\033[0;101m \033[0m" ` /'' -e '' / /s//` printf "\033[0;101m \033[0m" ` /''
( ( ERRORCODE++) )
fi
2016-10-16 14:29:45 -04:00
# Ensure that the CHANGELOG.md does not contain duplicate versions
DUPLICATE_CHANGELOG_VERSIONS = $( grep --extended-regexp '^## .+' CHANGELOG.md | sed -E 's| \(.+\)||' | sort -r | uniq -d)
2018-03-26 09:05:33 -04:00
echo '=> Checking for CHANGELOG.md duplicate entries...'
2020-03-12 17:09:45 -04:00
echo
2016-08-25 16:48:17 -04:00
if [ " ${ DUPLICATE_CHANGELOG_VERSIONS } " != "" ]
then
2016-10-16 14:29:45 -04:00
echo '✖ ERROR: Duplicate versions in CHANGELOG.md:' >& 2
2016-08-25 16:48:17 -04:00
echo " ${ DUPLICATE_CHANGELOG_VERSIONS } " >& 2
2020-03-12 17:09:45 -04:00
( ( ERRORCODE++) )
2016-08-25 16:48:17 -04:00
fi
2017-09-25 04:50:06 -04:00
# Make sure no files in doc/ are executable
2019-10-11 02:06:27 -04:00
EXEC_PERM_COUNT = $( find doc/ -type f -perm 755 | wc -l)
2019-10-22 05:06:14 -04:00
echo " => Checking $( pwd ) /doc for executable permissions... "
2020-03-12 17:09:45 -04:00
echo
2017-09-25 04:50:06 -04:00
if [ " ${ EXEC_PERM_COUNT } " -ne 0 ]
then
echo '✖ ERROR: Executable permissions should not be used in documentation! Use `chmod 644` to the files in question:' >& 2
2019-10-11 02:06:27 -04:00
find doc/ -type f -perm 755
2020-03-12 17:09:45 -04:00
( ( ERRORCODE++) )
2017-09-25 04:50:06 -04:00
fi
2018-03-26 09:05:33 -04:00
# Do not use 'README.md', instead use 'index.md'
2021-08-17 20:11:18 -04:00
# Number of 'README.md's as of 2021-08-17
2021-09-29 11:11:47 -04:00
NUMBER_READMES = 0
2018-03-26 09:05:33 -04:00
FIND_READMES = $( find doc/ -name "README.md" | wc -l)
echo '=> Checking for new README.md files...'
2020-03-12 17:09:45 -04:00
echo
2019-09-13 14:06:03 -04:00
if [ ${ FIND_READMES } -ne $NUMBER_READMES ]
2018-03-26 09:05:33 -04:00
then
2019-09-13 14:06:03 -04:00
echo
2020-07-10 14:09:45 -04:00
echo ' ✖ ERROR: The number of README.md file(s) has changed. Use index.md instead of README.md.' >& 2
echo ' ✖ If removing a README.md file, update NUMBER_READMES in lint-doc.sh.' >& 2
2021-03-10 04:09:29 -05:00
echo ' https://docs.gitlab.com/ee/development/documentation/styleguide/index.html#work-with-directories-and-files'
2019-09-13 14:06:03 -04:00
echo
2020-03-12 17:09:45 -04:00
( ( ERRORCODE++) )
2018-03-26 09:05:33 -04:00
fi
2021-09-20 02:09:32 -04:00
# Do not use dashes (-) in directory names, use underscores (_) instead.
# Number of directories with dashes as of 2021-09-17
NUMBER_DASHES = 2
FIND_DASHES = $( find doc -type d -name "*-*" | wc -l)
echo '=> Checking for directory names containing dashes...'
echo
if [ ${ FIND_DASHES } -ne $NUMBER_DASHES ]
then
echo
echo ' ✖ ERROR: The number of directory names containing dashes has changed. Use underscores instead of dashes for the directory names.' >& 2
echo ' ✖ If removing a directory containing dashes, update NUMBER_DASHES in lint-doc.sh.' >& 2
echo ' https://docs.gitlab.com/ee/development/documentation/styleguide/index.html#work-with-directories-and-files'
echo
( ( ERRORCODE++) )
fi
2020-09-02 08:10:35 -04:00
# Run Vale and Markdownlint only on changed files. Only works on merged results
# pipelines, so first checks if a merged results CI variable is present. If not present,
# runs test on all files.
if [ -z " ${ CI_MERGE_REQUEST_TARGET_BRANCH_SHA } " ]
then
MD_DOC_PATH = ${ MD_DOC_PATH :- doc }
echo "Merge request pipeline (detached) detected. Testing all files."
else
MERGE_BASE = $( git merge-base ${ CI_MERGE_REQUEST_TARGET_BRANCH_SHA } ${ CI_MERGE_REQUEST_SOURCE_BRANCH_SHA } )
2021-02-09 13:09:59 -05:00
if git diff --diff-filter= d --name-only " ${ MERGE_BASE } .. ${ CI_MERGE_REQUEST_SOURCE_BRANCH_SHA } " | grep -E "\.vale|\.markdownlint|lint-doc\.sh"
2020-11-10 13:09:07 -05:00
then
2020-11-26 13:09:18 -05:00
MD_DOC_PATH = ${ MD_DOC_PATH :- doc }
echo "Vale, Markdownlint, or lint-doc.sh configuration changed. Testing all files."
else
2021-02-09 13:09:59 -05:00
MD_DOC_PATH = $( git diff --diff-filter= d --name-only " ${ MERGE_BASE } .. ${ CI_MERGE_REQUEST_SOURCE_BRANCH_SHA } " -- 'doc/*.md' )
2020-11-26 13:09:18 -05:00
if [ -n " ${ MD_DOC_PATH } " ]
then
echo -e " Merged results pipeline detected. Testing only the following files:\n ${ MD_DOC_PATH } "
fi
2020-11-10 13:09:07 -05:00
fi
fi
2020-03-05 07:07:48 -05:00
function run_locally_or_in_docker( ) {
local cmd = $1
local args = $2
if hash ${ cmd } 2>/dev/null
then
2020-11-16 22:09:06 -05:00
$cmd $args
2020-03-05 07:07:48 -05:00
elif hash docker 2>/dev/null
then
2021-11-10 22:10:33 -05:00
docker run -t -v ${ PWD } :/gitlab -w /gitlab --rm registry.gitlab.com/gitlab-org/gitlab-docs/lint-markdown:alpine-3.14-vale-2.12.0-markdownlint-0.29.0 ${ cmd } ${ args }
2020-03-05 07:07:48 -05:00
else
echo
echo " ✖ ERROR: ' ${ cmd } ' not found. Install ' ${ cmd } ' or Docker to proceed. " >& 2
echo
2020-03-12 17:09:45 -04:00
( ( ERRORCODE++) )
2020-03-05 07:07:48 -05:00
fi
if [ $? -ne 0 ]
then
echo
echo " ✖ ERROR: ' ${ cmd } ' failed with errors. " >& 2
echo
2020-03-12 17:09:45 -04:00
( ( ERRORCODE++) )
2020-03-05 07:07:48 -05:00
fi
}
echo '=> Linting markdown style...'
2020-03-12 17:09:45 -04:00
echo
2020-11-10 13:09:07 -05:00
if [ -z " ${ MD_DOC_PATH } " ]
then
echo "Merged results pipeline detected, but no markdown files found. Skipping."
else
2021-04-29 02:09:58 -04:00
run_locally_or_in_docker 'markdownlint' " --config .markdownlint.yml ${ MD_DOC_PATH } "
2020-11-10 13:09:07 -05:00
fi
2020-03-05 07:07:48 -05:00
echo '=> Linting prose...'
2020-11-16 22:09:06 -05:00
run_locally_or_in_docker 'vale' " --minAlertLevel error --output=doc/.vale/vale.tmpl ${ MD_DOC_PATH } "
2020-03-05 07:07:48 -05:00
2020-03-12 17:09:45 -04:00
if [ $ERRORCODE -ne 0 ]
then
echo " ✖ ${ ERRORCODE } lint test(s) failed. Review the log carefully to see full listing. "
exit 1
else
echo "✔ Linting passed"
exit 0
fi