-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tomoya Fujita <[email protected]>
- Loading branch information
1 parent
b0a330a
commit 621e2a2
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/check_sentences.py /tmp/changed_lines.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters