From 1ca12bae08bf36825599f63c6a07cde60408321c Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Tue, 4 Feb 2025 21:41:43 -0800 Subject: [PATCH] test. Signed-off-by: Tomoya Fujita --- .github/workflows/rst_check.yml | 28 ++++++++++++++++++ scripts/sentence_line_checker.py | 51 ++++++++++++++++++++++++++++++++ source/Citations.rst | 7 +++++ 3 files changed, 86 insertions(+) create mode 100644 .github/workflows/rst_check.yml create mode 100644 scripts/sentence_line_checker.py diff --git a/.github/workflows/rst_check.yml b/.github/workflows/rst_check.yml new file mode 100644 index 00000000000..af8ac18696d --- /dev/null +++ b/.github/workflows/rst_check.yml @@ -0,0 +1,28 @@ +name: Specific checks for rst files + +on: + pull_request: + paths: + - "**/*.rst" # trigger only for .rst files + +jobs: + one_sentence_per_line: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Get changed lines in .rst files + run: | + git fetch origin ${{ github.base_ref }} --depth=1 + git diff --unified=0 origin/${{ github.base_ref }} HEAD -- '*.rst' | grep '^[+-]' | grep -Ev '^(---|\+\+\+)' > /tmp/changed_lines.txt || true + + - name: Run one-sentence-per-line checker on changed lines + run: | + python3 ./scripts/sentence_line_checker.py /tmp/changed_lines.txt diff --git a/scripts/sentence_line_checker.py b/scripts/sentence_line_checker.py new file mode 100644 index 00000000000..f312f37135d --- /dev/null +++ b/scripts/sentence_line_checker.py @@ -0,0 +1,51 @@ +import argparse +import re +import sys + +def is_one_sentence_per_line(line) -> bool: + """ + Check if a line contains only one complete sentence. + + :param line: The line of file to check. + """ + # this assumes a sentence ends with a period, question mark, or exclamation mark. + sentence_endings = re.findall(r'[.!?]', line) + # allow empty lines or lines with only one sentence + return len(sentence_endings) <= 1 + +def check_changed_lines(filename) -> None: + """ + Check only changed lines for violations. + + .. warning:: It checks only added / modified lines for the viaolation in the file, + and ignores everything else including removed lines. + + :param filename: The name of the file to check. + """ + with open(filename, 'r', encoding='utf-8') as file: + lines = file.readlines() + + violations = [] + for lineno, line in enumerate(lines, start=1): + line = line.strip() + # check only added lines, ignore everything else + if line.startswith("+") and not is_one_sentence_per_line(line[1:]): + violations.append((lineno, line[1:])) + + if violations: + print(f"\n⚠️ Found {len(violations)} violations:") + for lineno, line in violations: + print(f" ❌ Line {lineno}: {line}") + # exit with non-zero status code to fail github actions + sys.exit(1) + else: + print("✅ No violations found.") + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description="Check if modified contents contain only one complete sentence per line.") + parser.add_argument( + 'filename', type=str, help='The name of the file to check.') + args = parser.parse_args() + + check_changed_lines(args.filename) diff --git a/source/Citations.rst b/source/Citations.rst index a433d9fc937..2f4e3a2764d 100644 --- a/source/Citations.rst +++ b/source/Citations.rst @@ -3,6 +3,13 @@ Citations ========= +This is a test. +This is a test. This should fail. +Is this a test? +Is this a test? Shoud this fail? +This is a test! +This is a test! This should fail! + If you use ROS 2 in your work please cite the 2022 Science Robotics paper `Robot Operating System 2: Design, architecture, and uses in the wild `_. | S. Macenski, T. Foote, B. Gerkey, C. Lalancette, W. Woodall, "Robot Operating System 2: Design, architecture, and uses in the wild," Science Robotics vol. 7, May 2022.