-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mantidproject/release_note_check
New pre commit check for release note bullet point format
- Loading branch information
Showing
10 changed files
with
142 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
docker/build_logs | ||
.idea | ||
clang-format/build/linux/build_logs/ | ||
*/__pycache__ | ||
*.egg-info |
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
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 |
---|---|---|
@@ -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 not shown.
Binary file not shown.
Empty file.
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,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()) |
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
Empty file.
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,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) |