From 81b804f8c2194485b49737286066d931ad8c8e0c Mon Sep 17 00:00:00 2001 From: Allen Wittenauer Date: Wed, 27 Apr 2022 08:45:27 -0700 Subject: [PATCH] YETUS-1145. add rstcheck --- .../in-progress/precommit/index.html.md | 1 + .../precommit/plugins/rstcheck.html.md | 48 ++++++ .../src/main/shell/plugins.d/rstcheck.sh | 147 ++++++++++++++++++ .../main/shell/test-patch-docker/Dockerfile | 2 + .../src/test/resources/i18n/\342\210\202.rst" | 4 + 5 files changed, 202 insertions(+) create mode 100644 asf-site-src/source/documentation/in-progress/precommit/plugins/rstcheck.html.md create mode 100644 precommit/src/main/shell/plugins.d/rstcheck.sh create mode 100644 "precommit/src/test/resources/i18n/\342\210\202.rst" diff --git a/asf-site-src/source/documentation/in-progress/precommit/index.html.md b/asf-site-src/source/documentation/in-progress/precommit/index.html.md index 34d27dea0..f943fda3a 100644 --- a/asf-site-src/source/documentation/in-progress/precommit/index.html.md +++ b/asf-site-src/source/documentation/in-progress/precommit/index.html.md @@ -136,6 +136,7 @@ Commonly Parsed File Formats: * .md: [markdownlint-cli](plugins/markdownlint) * json: [jsonlint](plugins/jsonlint) * .proto: [buf](plugins/buf) +* .rst: [rstcheck](plugins/rstcheck) * .yaml/.yml: [yamllint](plugins/yamllint) * .html/.xml: [xml](plugins/xml) diff --git a/asf-site-src/source/documentation/in-progress/precommit/plugins/rstcheck.html.md b/asf-site-src/source/documentation/in-progress/precommit/plugins/rstcheck.html.md new file mode 100644 index 000000000..f3609b5e4 --- /dev/null +++ b/asf-site-src/source/documentation/in-progress/precommit/plugins/rstcheck.html.md @@ -0,0 +1,48 @@ + + +# Name + +rstcheck + +# Category + +Test + +# Description + +Runs [rstcheck](https://github.com/myint/rstcheck) when a reStructuredText (.rst) file is found. + +# Environment Variables + +None + +# Options + +| Option | Notes | +|:---------|:------| +| `--rstcheck=` | path to `rstcheck` executable if it is not on the path | + +# Docker Notes + +None + +# Developer Notes + +None diff --git a/precommit/src/main/shell/plugins.d/rstcheck.sh b/precommit/src/main/shell/plugins.d/rstcheck.sh new file mode 100644 index 000000000..2989f48f6 --- /dev/null +++ b/precommit/src/main/shell/plugins.d/rstcheck.sh @@ -0,0 +1,147 @@ +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# SHELLDOC-IGNORE + +add_test_type rstcheck + +RSTCHECK_TIMER=0 + +RSTCHECK=${RSTCHECK:-$(command -v rstcheck 2>/dev/null)} + +function rstcheck_usage +{ + yetus_add_option "--rstcheck=" "path to rstcheck executable" +} + +function rstcheck_parse_args +{ + local i + + for i in "$@"; do + case ${i} in + --rstcheck=*) + RSTCHECK=${i#*=} + delete_parameter "${i}" + ;; + esac + done +} + +function rstcheck_filefilter +{ + local filename=$1 + + if [[ ${filename} =~ \.rst$ ]]; then + add_test rstcheck + fi +} + +function rstcheck_precheck +{ + if ! verify_command rstcheck "${RSTCHECK}"; then + add_vote_table_v2 0 rstcheck "" "rstcheck was not available." + delete_test rstcheck + return 0 + fi +} + +function rstcheck_exec +{ + declare i + declare repostatus=$1 + declare output="${PATCH_DIR}/${repostatus}-rstcheck-result.txt" + + pushd "${BASEDIR}" >/dev/null || return 1 + + for i in "${CHANGED_FILES[@]}"; do + if [[ ${i} =~ \.rst$ ]]; then + if [[ -f ${i} ]]; then + "${RSTCHECK}" "${i}" >> "${output}" 2>&1 + fi + fi + done + + popd >/dev/null || return 1 + return 0 +} + +function rstcheck_preapply +{ + declare i + + if ! verify_needed_test rstcheck; then + return 0 + fi + + big_console_header "rstcheck plugin: ${PATCH_BRANCH}" + + start_clock + + rstcheck_exec branch + + RSTCHECK_TIMER=$(stop_clock) + return 0 +} + +## @description Wrapper to call error_calcdiffs +## @audience private +## @stability evolving +## @replaceable no +## @param branchlog +## @param patchlog +## @return differences +function rstcheck_calcdiffs +{ + error_calcdiffs "$@" +} + +function rstcheck_postapply +{ + if ! verify_needed_test rstcheck; then + return 0 + fi + + big_console_header "rstcheck plugin: ${BUILDMODE}" + + start_clock + + # add our previous elapsed to our new timer + # by setting the clock back + offset_clock "${RSTCHECK_TIMER}" + + rstcheck_exec patch + + # shellcheck disable=SC2016 + RSTCHECK_VERSION=$("${RSTCHECK}" --version | "${AWK}" '{print $NF}') + add_version_data rstcheck "${RSTCHECK_VERSION}" + + root_postlog_compare \ + rstcheck \ + "${PATCH_DIR}/branch-rstcheck-result.txt" \ + "${PATCH_DIR}/patch-rstcheck-result.txt" +} + +function rstcheck_postcompile +{ + declare repostatus=$1 + + if [[ "${repostatus}" = branch ]]; then + rstcheck_preapply + else + rstcheck_postapply + fi +} diff --git a/precommit/src/main/shell/test-patch-docker/Dockerfile b/precommit/src/main/shell/test-patch-docker/Dockerfile index e5e408ca3..1be05df2e 100644 --- a/precommit/src/main/shell/test-patch-docker/Dockerfile +++ b/precommit/src/main/shell/test-patch-docker/Dockerfile @@ -319,6 +319,7 @@ ARG PY3_ASTROID_VERSION=2.11.2 ARG PY3_CODESPELL_VERSION=2.1.0 ARG PY3_DETECT_SECRETS=1.2.0 ARG PY3_DOCKER_COMPOSE=1.29.2 +ARG PY3_RSTCHECK_VERSION=5.0.0 ARG PY3_PYLINT_VERSION=2.13.4 ARG PY3_YAMLLINT_VERSION=1.26.3 # hadolint ignore=DL3008 @@ -352,6 +353,7 @@ RUN apt-get -q update && apt-get -q install --no-install-recommends -y \ detect-secrets==$PY3_DETECT_SECRETS \ docker-compose==$PY3_DOCKER_COMPOSE \ pylint==$PY3_PYLINT_VERSION \ + rstcheck==$PY3_RSTCHECK_VERSION \ yamllint==$PY3_YAMLLINT_VERSION \ && rm -rf /root/.cache \ && mv /usr/local/bin/pylint /usr/local/bin/pylint3 \ diff --git "a/precommit/src/test/resources/i18n/\342\210\202.rst" "b/precommit/src/test/resources/i18n/\342\210\202.rst" new file mode 100644 index 000000000..72d40a525 --- /dev/null +++ "b/precommit/src/test/resources/i18n/\342\210\202.rst" @@ -0,0 +1,4 @@ +hello +===== + +**Apache Yetus** is here!