Skip to content

Commit

Permalink
Merge pull request #10 from mantidproject/release_note_check
Browse files Browse the repository at this point in the history
New pre commit check for release note bullet point format
  • Loading branch information
cailafinn authored Oct 25, 2023
2 parents 82e4b72 + 0686ab7 commit 5e10ad0
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
docker/build_logs
.idea
clang-format/build/linux/build_logs/
*/__pycache__
*.egg-info
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@
language: python
additional_dependencies: [gitpython]
types: [python]

- id: mantid-release-note-check
name: Release note bullet point style check
description: Checks that release note bullet points are the correct style
entry: mantid-release-note-check
language: python
stages: [commit]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Custom pre-commit hooks for the pre-commit framework
The hooks present in this repository are:
* clang-format (self-contained hook not reliant on clang-format being available externally)
* mantid_release_note_check (checks and edits mantid release notes so that they use the ``-`` bullet point style)

To use the hooks copy this to your .pre-commit-config.yaml:
```yaml
- repo: https://github.com/mantidproject/pre-commit-hooks
rev: main
hooks:
- id: clang-format
- id: mantid-release-note-check
files: docs\/source\/release\/v\d\.\d\.\d\/.*\/.*\.rst
```
Binary file modified clang-format/bin/clang-format-darwin
Binary file not shown.
Binary file modified clang-format/bin/clang-format.exe
Binary file not shown.
Empty file.
54 changes: 54 additions & 0 deletions mantid_release_note_check/mantid_release_note_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import argparse

from pathlib import Path
from typing import Sequence


def filter_files(filenames: set[str]) -> set[str]:
files_to_keep = set()
for filename in filenames:
path = Path(filename)
if path.suffix == '.rst' and 'release' in path.parts:
files_to_keep.add(filename)

return filenames & files_to_keep


def check_bullet_points(filenames: set[str]) -> int:
retv = 0

for filename in filenames:
with open(filename, 'r') as rn:
lines = rn.readlines()
for i in range(len(lines)):
line = lines[i].lstrip()
if len(lines[i]) - len(line) > 1 or len(line) == 0:
# ignore nested bullet points and blank lines
continue
bullet = line[0]
if bullet in ['*', '+']:
print(f"{filename} has incorrect bullet point style '{bullet}'. Please only use '-'.")
print(f"Fixing {filename}")
lines[i] = lines[i].replace(bullet, '-', 1)
retv = 1

if retv != 0:
with open(filename, 'w') as rn:
rn.writelines(lines)

return retv


def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument(
'filenames', nargs='*',
help='Filenames pre-commit believes are changed.',
)
args = parser.parse_args(argv)
release_notes = filter_files(set(args.filenames))
return check_bullet_points(release_notes)


if __name__ == '__main__':
raise SystemExit(main())
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = mantid_pre_commit_hooks
version = 1.0.0
version = 1.1.0
description = Various Mantid Pre-commit hooks

[options]
Expand All @@ -15,3 +15,4 @@ exclude =
[options.entry_points]
console_scripts =
cmake-missing-pytest-files = cmake_missing_pytest_files.check_for_missing_py_tests:main
mantid-release-note-check = mantid_release_note_check.mantid_release_note_check:main
Empty file added tests/__init__.py
Empty file.
74 changes: 74 additions & 0 deletions tests/mantid_release_note_check_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import unittest

from typing import List

from mantid_release_note_check.mantid_release_note_check import check_bullet_points


class MantidReleaseNoteCheckTest(unittest.TestCase):

RELEASE_NOTE_PATH = "release_note.rst"

def setUp(self) -> None:
open(self.RELEASE_NOTE_PATH, 'a').close()

def tearDown(self) -> None:
os.remove(self.RELEASE_NOTE_PATH)

def _write_release_note(self, lines: List[str]) -> None:
with open(self.RELEASE_NOTE_PATH, 'w') as rn:
rn.writelines(l + '\n' for l in lines)

def _match_file_content(self, lines: List[str]):
with open(self.RELEASE_NOTE_PATH, 'r') as rn:
self.assertEqual([l + '\n' for l in lines], rn.readlines())

def test_good_single_release_note(self):
lines = ["- good note"]
self._write_release_note(lines)
self.assertEqual(check_bullet_points({self.RELEASE_NOTE_PATH}), 0)
self._match_file_content(lines)

def test_bad_single_release_note_star(self):
self._write_release_note(["* bad note"])
self.assertEqual(check_bullet_points({self.RELEASE_NOTE_PATH}), 1)
self._match_file_content(["- bad note"])

def test_bad_single_release_note_plus(self):
self._write_release_note(["+ bad note"])
self.assertEqual(check_bullet_points({self.RELEASE_NOTE_PATH}), 1)
self._match_file_content(["- bad note"])

def test_mixed_bullet_points(self):
lines = ["* note one",
"- note two",
"+ note three"]
self._write_release_note(lines)
self.assertEqual(check_bullet_points({self.RELEASE_NOTE_PATH}), 1)
fixed_lines = ["- note one",
"- note two",
"- note three"]
self._match_file_content(fixed_lines)

def test_nested_bullet_lists(self):
lines = ["- note 1",
"",
" - note 1a",
" - note 1b",
"",
"- note 2"]
self._write_release_note(lines)
self.assertEqual(check_bullet_points({self.RELEASE_NOTE_PATH}), 0)
self._match_file_content(lines)

def test_different_nested_bullet_lists(self):
lines = ["- note 1",
"",
" * note 1a",
" * note 1b",
"",
"- note 2"]
self._write_release_note(lines)
self.assertEqual(check_bullet_points({self.RELEASE_NOTE_PATH}), 0)
self._match_file_content(lines)

0 comments on commit 5e10ad0

Please sign in to comment.