-
-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathcheck_changelog_entries.py
More file actions
70 lines (58 loc) · 2.21 KB
/
Copy pathcheck_changelog_entries.py
File metadata and controls
70 lines (58 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Check changelog entries have the correct filename structure.
Usage:
python check_changelog_entries.py [DIRECTORY]
DIRECTORY defaults to the repo-root `changes/`.
"""
import sys
from pathlib import Path
VALID_CHANGELOG_TYPES = ["feature", "changed", "bugfix", "doc", "removal", "misc"]
REPO_ROOT = Path(__file__).parent.parent.resolve()
DEFAULT_DIRECTORY = REPO_ROOT / "changes"
def is_int(s: str) -> bool:
try:
int(s)
except ValueError:
return False
else:
return True
def check(directory: Path) -> int:
print(f"Looking for changelog entries in {directory}")
entries = list(directory.glob("*"))
entries = [e for e in entries if e.name not in [".gitignore", "README.md"]]
print(f"Found {len(entries)} entries")
print()
bad_suffix = [e for e in entries if e.suffix != ".md"]
bad_issue_no = [e for e in entries if not is_int(e.name.split(".")[0])]
# Only flag bad_type for files that have already passed the prior two
# checks; otherwise `e.name.split(".")[1]` may raise IndexError on a
# malformed name like `notes.md`.
bad_type = [
e
for e in entries
if e.suffix == ".md"
and is_int(e.name.split(".")[0])
and e.name.split(".")[1] not in VALID_CHANGELOG_TYPES
]
if bad_suffix or bad_issue_no or bad_type:
if bad_suffix:
print("Changelog entries without .md suffix")
print("-------------------------------------")
print("\n".join(p.name for p in bad_suffix))
print()
if bad_issue_no:
print("Changelog entries without integer issue number")
print("----------------------------------------------")
print("\n".join(p.name for p in bad_issue_no))
print()
if bad_type:
print("Changelog entries without valid type")
print("------------------------------------")
print("\n".join(p.name for p in bad_type))
print(f"Valid types are: {VALID_CHANGELOG_TYPES}")
print()
return 1
return 0
if __name__ == "__main__":
directory = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else DEFAULT_DIRECTORY
sys.exit(check(directory))