diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-30 12:09:22 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-30 12:09:22 +0300 |
commit | ef144889c1bb80372e25f38fc89c7832ce84417a (patch) | |
tree | 54fa644c7be78fa1a562ca5f97b7f295647e1ce1 /scripts | |
parent | 286bddcf3c62ca6fc499e1d5b6e678c0866fecc4 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/lint-doc.sh | 179 | ||||
-rwxr-xr-x | scripts/lint-docs-metadata.sh | 10 |
2 files changed, 101 insertions, 88 deletions
diff --git a/scripts/lint-doc.sh b/scripts/lint-doc.sh index 36b078adb48..84035ecd012 100755 --- a/scripts/lint-doc.sh +++ b/scripts/lint-doc.sh @@ -1,67 +1,75 @@ #!/usr/bin/env bash set -o pipefail +COLOR_RED="\e[31m" +COLOR_GREEN="\e[32m" +COLOR_RESET="\e[39m" + cd "$(dirname "$0")/.." || exit 1 -echo "=> Linting documents at path $(pwd) as $(whoami)..." -echo +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Linting documents at path $(pwd) as $(whoami)...${COLOR_RESET}\n" ERRORCODE=0 # Use long options (e.g. --header instead of -H) for curl examples in documentation. -echo '=> Checking for cURL short options...' -echo -grep --extended-regexp --recursive --color=auto 'curl (.+ )?-[^- ].*' doc/ >/dev/null 2>&1 -if [ $? -eq 0 ] +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking for cURL short options...${COLOR_RESET}\n" +if grep --extended-regexp --recursive --color=auto 'curl (.+ )?-[^- ].*' doc/ >/dev/null 2>&1; then - 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/ + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: Short options for curl should not be used in documentation!${COLOR_RESET}" + printf " Use long options (for example, --header instead of -H):\n" >&2 + grep --extended-regexp --recursive --color=auto 'curl (.+ )?-[^- ].*' doc ((ERRORCODE++)) fi # Documentation pages need front matter for tracking purposes. -echo '=> Checking documentation for front matter...' -echo +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking documentation for front matter...${COLOR_RESET}\n" if ! scripts/lint-docs-metadata.sh 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 + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: These documentation pages need front matter!${COLOR_RESET}" + printf " See https://docs.gitlab.com/ee/development/documentation/index.html#stage-and-group-metadata for how to add it.\n" >&2 ((ERRORCODE++)) fi # Test for non-standard spaces (NBSP, NNBSP, ZWSP) in documentation. -echo '=> Checking for non-standard spaces...' -echo -grep --extended-regexp --binary-file=without-match --recursive '[ ]' doc/ >/dev/null 2>&1 -if [ $? -eq 0 ] +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking for non-standard spaces...${COLOR_RESET}\n" +if grep --extended-regexp --binary-file=without-match --recursive '[ ]' doc/ >/dev/null 2>&1; then - echo '✖ ERROR: Non-standard spaces (NBSP, NNBSP, ZWSP) should not be used in documentation. - https://docs.gitlab.com/ee/development/documentation/styleguide/index.html#spaces-between-words - Replace with standard spaces:' >&2 + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: Non-standard spaces (NBSP, NNBSP, ZWSP) should not be used in documentation!${COLOR_RESET}" + printf " https://docs.gitlab.com/ee/development/documentation/styleguide/index.html#spaces-between-words\n" + printf "Replace with standard spaces:\n" >&2 # Find the spaces, then add color codes with sed to highlight each NBSP or NNBSP in the output. + # shellcheck disable=SC1018 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"`/'' + | sed -e ''/ /s//"$(printf "\033[0;101m \033[0m")"/'' -e ''/ /s//"$(printf "\033[0;101m \033[0m")"/'' ((ERRORCODE++)) fi # 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) -echo '=> Checking for CHANGELOG.md duplicate entries...' -echo +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking for CHANGELOG.md duplicate entries...${COLOR_RESET}\n" if [ "${DUPLICATE_CHANGELOG_VERSIONS}" != "" ] then - echo '✖ ERROR: Duplicate versions in CHANGELOG.md:' >&2 + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: Duplicate versions in CHANGELOG.md:${COLOR_RESET}\n" >&2 echo "${DUPLICATE_CHANGELOG_VERSIONS}" >&2 ((ERRORCODE++)) fi # Make sure no files in doc/ are executable EXEC_PERM_COUNT=$(find doc/ -type f -perm 755 | wc -l) -echo "=> Checking $(pwd)/doc for executable permissions..." -echo +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking $(pwd)/doc for executable permissions...${COLOR_RESET}\n" 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 - find doc/ -type f -perm 755 + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: Executable permissions should not be used in documentation!${COLOR_RESET} Use 'chmod 644' on these files:\n" >&2 + find doc -type f -perm 755 ((ERRORCODE++)) fi @@ -69,15 +77,14 @@ fi # Number of 'README.md's as of 2021-08-17 NUMBER_READMES=0 FIND_READMES=$(find doc/ -name "README.md" | wc -l) -echo '=> Checking for new README.md files...' -echo -if [ ${FIND_READMES} -ne $NUMBER_READMES ] +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking for new README.md files...${COLOR_RESET}\n" +if [ "${FIND_READMES}" -ne $NUMBER_READMES ] then - echo - 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 - echo ' https://docs.gitlab.com/ee/development/documentation/site_architecture/folder_structure.html#work-with-directories-and-files' >&2 - echo + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: The number of README.md files has changed!${COLOR_RESET} Use index.md instead of README.md.\n" >&2 + printf "If removing a README.md file, update NUMBER_READMES in lint-doc.sh.\n" >&2 + printf "https://docs.gitlab.com/ee/development/documentation/site_architecture/folder_structure.html#work-with-directories-and-files\n" ((ERRORCODE++)) fi @@ -85,42 +92,40 @@ fi # 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 ] +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking for directory names containing dashes...${COLOR_RESET}\n" +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/site_architecture/folder_structure.html#work-with-directories-and-files' >&2 - echo - ((ERRORCODE++)) + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: The number of directory names containing dashes has changed!${COLOR_RESET} Use underscores instead of dashes for the directory names.\n" >&2 + printf "If removing a directory containing dashes, update NUMBER_DASHES in lint-doc.sh.\n" >&2 + printf "https://docs.gitlab.com/ee/development/documentation/site_architecture/folder_structure.html#work-with-directories-and-files\n" + ((ERRORCODE++)) fi # Do not use uppercase letters in directory and file names, use all lowercase instead. # (find always returns 0, so we use the grep hack https://serverfault.com/a/225827) FIND_UPPERCASE_DIRS=$(find doc -type d -name "*[[:upper:]]*") -echo '=> Checking for directory names containing an uppercase letter...' -echo +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking for directory names containing an uppercase letter...${COLOR_RESET}\n" if echo "${FIND_UPPERCASE_DIRS}" | grep . &>/dev/null then - echo '✖ ERROR: Found one or more directories with an uppercase letter in their name. Use lowercase instead of uppercase for the directory names.' >&2 - echo 'https://docs.gitlab.com/ee/development/documentation/site_architecture/folder_structure.html#work-with-directories-and-files' >&2 - echo + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: Found one or more directories with an uppercase letter in their name!${COLOR_RESET} Use lowercase instead of uppercase for the directory names.\n" >&2 + printf "https://docs.gitlab.com/ee/development/documentation/site_architecture/folder_structure.html#work-with-directories-and-files\n" >&2 echo "${FIND_UPPERCASE_DIRS}" - echo ((ERRORCODE++)) fi + FIND_UPPERCASE_FILES=$(find doc -type f -name "*[[:upper:]]*.md") -echo '=> Checking for file names containing an uppercase letter...' -echo +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Checking for file names containing an uppercase letter...${COLOR_RESET}\n" if echo "${FIND_UPPERCASE_FILES}" | grep . &>/dev/null then - echo '✖ ERROR: Found one or more file names with an uppercase letter in their name. Use lowercase instead of uppercase for the file names.' >&2 - echo 'https://docs.gitlab.com/ee/development/documentation/site_architecture/folder_structure.html#work-with-directories-and-files' >&2 - echo + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: Found one or more file names with an uppercase letter in their name!${COLOR_RESET} Use lowercase instead of uppercase for the file names.\n" >&2 + printf "https://docs.gitlab.com/ee/development/documentation/site_architecture/folder_structure.html#work-with-directories-and-files\n" >&2 echo "${FIND_UPPERCASE_FILES}" - echo ((ERRORCODE++)) fi @@ -130,18 +135,21 @@ fi 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." + # shellcheck disable=2059 + printf "${COLOR_GREEN}INFO: Merge request pipeline (detached) detected. Running Markdownlint and Vale on all files...${COLOR_RESET}\n" else - MERGE_BASE=$(git merge-base ${CI_MERGE_REQUEST_TARGET_BRANCH_SHA} ${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA}) + MERGE_BASE=$(git merge-base "${CI_MERGE_REQUEST_TARGET_BRANCH_SHA}" "${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA}") if git diff --diff-filter=d --name-only "${MERGE_BASE}..${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA}" | grep -E "\.vale|\.markdownlint|lint-doc\.sh|docs\.gitlab-ci\.yml" then MD_DOC_PATH=${MD_DOC_PATH:-doc} - echo "Vale, Markdownlint, lint-doc.sh, or pipeline configuration changed. Testing all files." + # shellcheck disable=2059 + printf "${COLOR_GREEN}INFO: Vale, Markdownlint, lint-doc.sh, or pipeline configuration changed. Testing all files.${COLOR_RESET}\n" else MD_DOC_PATH=$(git diff --diff-filter=d --name-only "${MERGE_BASE}..${CI_MERGE_REQUEST_SOURCE_BRANCH_SHA}" -- 'doc/*.md') if [ -n "${MD_DOC_PATH}" ] then - echo -e "Merged results pipeline detected. Testing only the following files:\n${MD_DOC_PATH}" + # shellcheck disable=2059 + printf "${COLOR_GREEN}INFO: Merged results pipeline detected. Testing only the following files:${COLOR_RESET}\n${MD_DOC_PATH}" fi fi fi @@ -151,58 +159,63 @@ function run_locally_or_in_container() { local args=$2 local registry_url="registry.gitlab.com/gitlab-org/gitlab-docs/lint-markdown:alpine-3.16-vale-2.22.0-markdownlint-0.32.2-markdownlint2-0.6.0" - if hash ${cmd} 2>/dev/null + if hash "${cmd}" 2>/dev/null then + # shellcheck disable=2059 + printf "${COLOR_GREEN}INFO: Found locally-installed ${cmd}! Running...${COLOR_RESET}\n" $cmd $args # When using software like Rancher Desktop, both nerdctl and docker binaries are available # but only one is configured. To check which one to use, we need to probe each runtime - elif (hash nerdctl 2>/dev/null) && (nerdctl info 2>&1 1>/dev/null) + elif (hash nerdctl 2>/dev/null) && (nerdctl info > /dev/null 2>&1) then + # shellcheck disable=2059 + printf "${COLOR_GREEN}INFO: Found nerdctl! Using linting image to run ${cmd}...${COLOR_RESET}\n" nerdctl run -t -v "${PWD}:/gitlab" -w /gitlab --rm ${registry_url} ${cmd} ${args} - elif (hash docker 2>/dev/null) && (docker info 2>&1 1>/dev/null) + elif (hash docker 2>/dev/null) && (docker info > /dev/null 2>&1) then + # shellcheck disable=2059 + printf "${COLOR_GREEN}INFO: Found docker! Using linting image to run ${cmd}...${COLOR_RESET}\n" docker run -t -v "${PWD}:/gitlab" -w /gitlab --rm ${registry_url} ${cmd} ${args} else - echo - echo " ✖ ERROR: '${cmd}' not found. Install '${cmd}' or a container runtime (Docker/Nerdctl) to proceed." >&2 - echo + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: '${cmd}' not found!${COLOR_RESET} Install '${cmd}' locally, or install a container runtime (docker or nerdctl) and try again.\n" >&2 ((ERRORCODE++)) fi if [ $? -ne 0 ] then - echo - echo " ✖ ERROR: '${cmd}' failed with errors." >&2 - echo + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: '${cmd}' failed with errors!${COLOR_RESET}\n" >&2 ((ERRORCODE++)) fi } -echo '=> Linting markdown style...' -echo +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Linting markdown style...${COLOR_RESET}\n" if [ -z "${MD_DOC_PATH}" ] then - echo "Merged results pipeline detected, but no markdown files found. Skipping." + # shellcheck disable=2059 + printf "${COLOR_GREEN}INFO: Merged results pipeline detected, but no markdown files found. Skipping.${COLOR_RESET}\n" else - yarn markdownlint --config .markdownlint.yml ${MD_DOC_PATH} --rules doc/.markdownlint/rules - - if [ $? -ne 0 ] + if ! yarn markdownlint --config .markdownlint.yml "${MD_DOC_PATH}" --rules doc/.markdownlint/rules; then - echo - echo '✖ ERROR: Markdownlint failed with errors.' >&2 - echo + # shellcheck disable=2059 + printf "${COLOR_RED}ERROR: Markdownlint failed with errors!${COLOR_RESET}\n" >&2 ((ERRORCODE++)) fi fi -echo '=> Linting prose...' +# shellcheck disable=2059 +printf "${COLOR_GREEN}INFO: Looking for Vale to lint prose, either installed locally or available in documentation linting image...${COLOR_RESET}\n" run_locally_or_in_container 'vale' "--minAlertLevel error --output=doc/.vale/vale.tmpl ${MD_DOC_PATH}" -if [ $ERRORCODE -ne 0 ] +if [ "$ERRORCODE" -ne 0 ] then - echo "✖ ${ERRORCODE} lint test(s) failed. Review the log carefully to see full listing." + # shellcheck disable=2059 + printf "\n${COLOR_RED}ERROR: lint test(s) failed! Review the log carefully to see full listing.${COLOR_RESET}\n" exit 1 else - echo "✔ Linting passed" + # shellcheck disable=2059 + printf "\n${COLOR_GREEN}INFO: Linting passed.${COLOR_RESET}\n" exit 0 fi diff --git a/scripts/lint-docs-metadata.sh b/scripts/lint-docs-metadata.sh index 4212adfd3f1..cbf571729f3 100755 --- a/scripts/lint-docs-metadata.sh +++ b/scripts/lint-docs-metadata.sh @@ -38,10 +38,10 @@ function check_file { file="$1" TOTAL_FILES=$((TOTAL_FILES + 1)) if [ "$(head -n1 "$file")" != "---" ]; then - printf "${COLOR_RED}Documentation metadata missing in %s.${COLOR_RESET}\n" "$file" >&2 + printf "${COLOR_RED}ERROR: Documentation metadata missing in %s.${COLOR_RESET}\n" "$file" >&2 FAILING_FILES=$((FAILING_FILES + 1)) elif [ "$VERBOSE" == "true" ]; then - printf "Documentation metadata found in %s.\n" "$file" + printf "${COLOR_GREEN}INFO: Documentation metadata found in %s.${COLOR_RESET}\n" "$file" fi } @@ -53,7 +53,7 @@ function check_all_files { if [[ "$CHECK_ALL" = "true" ]]; then # shellcheck disable=SC2059 - printf "No files supplied. Checking all markdown files in doc/.\n" + printf "${COLOR_GREEN}INFO: No files supplied! Checking all markdown files in doc/...${COLOR_RESET}\n" check_all_files else # Takes a list of Markdown files as a parameter @@ -66,10 +66,10 @@ fi if [ "$FAILING_FILES" -gt 0 ]; then # shellcheck disable=SC2059 - printf "\n${COLOR_RED}Documentation metadata is missing in ${FAILING_FILES} of ${TOTAL_FILES} documentation files.${COLOR_RESET} For more information, see https://docs.gitlab.com/ee/development/documentation/#metadata.\n" >&2 + printf "\n${COLOR_RED}ERROR: Documentation metadata is missing in ${FAILING_FILES} of ${TOTAL_FILES} documentation files.${COLOR_RESET} For more information, see https://docs.gitlab.com/ee/development/documentation/#metadata.\n" >&2 exit 1 else # shellcheck disable=SC2059 - printf "${COLOR_GREEN}Documentation metadata found in ${TOTAL_FILES} documentation files.${COLOR_RESET}\n" + printf "${COLOR_GREEN}INFO: Documentation metadata found in ${TOTAL_FILES} documentation files.${COLOR_RESET}\n" exit 0 fi |