| 1 |
# pragma: no cover |
| 2 |
# ruff: noqa: PERF401 |
| 3 |
|
| 4 |
# /// script |
| 5 |
# requires-python = ">=3.11" |
| 6 |
# dependencies = [] |
| 7 |
# /// |
| 8 |
|
| 9 |
import re |
| 10 |
import sys |
| 11 |
|
| 12 |
GITHUB_COMPARE_URL_START_RE = r"https?://github.com/[^/]+/[^/]+/compare/" |
| 13 |
GITHUB_COMPARE_URL_RE = GITHUB_COMPARE_URL_START_RE + r"([\w.]+)\.\.\.([\w.]+)" |
| 14 |
GITHUB_RELEASE_TAG_URL_START_RE = r"https?://github.com/[^/]+/[^/]+/releases/tag/" |
| 15 |
GITHUB_RELEASE_TAG_URL_RE = GITHUB_RELEASE_TAG_URL_START_RE + r"([\w.]+)" |
| 16 |
UNRELEASED_HEADER = "## [Unreleased]\n" |
| 17 |
VERSION_HEADER_START_RE = r"## \[([\w.]+)\]" |
| 18 |
VERSION_HEADER_FULL_RE = VERSION_HEADER_START_RE + r" - (\d{4}-\d{2}-\d{2})\n" |
| 19 |
UNRELEASED_HYPERLINK_RE = r"\[Unreleased\]: " + GITHUB_COMPARE_URL_RE + r"\n" |
| 20 |
VERSION_HYPERLINK_START_RE = r"\[([\w.]+)\]: " |
| 21 |
VERSION_HYPERLINK_RE = VERSION_HYPERLINK_START_RE + GITHUB_COMPARE_URL_RE + r"\n" |
| 22 |
INITIAL_VERSION_RE = VERSION_HYPERLINK_START_RE + GITHUB_RELEASE_TAG_URL_RE + r"\n" |
| 23 |
SECTION_HEADER_RE = r"### ([^\n]+)\n" |
| 24 |
HTML_COMMENT_RE = r"<!--.*?-->", re.DOTALL |
| 25 |
|
| 26 |
|
| 27 |
def validate_changelog(changelog_path="CHANGELOG.md"): |
| 28 |
errors = [] |
| 29 |
# Read the contents of the changelog file |
| 30 |
with open(changelog_path, encoding="UTF-8") as file: |
| 31 |
changelog = file.read() |
| 32 |
|
| 33 |
# Remove markdown comments |
| 34 |
changelog = re.sub(HTML_COMMENT_RE[0], "", changelog, flags=HTML_COMMENT_RE[1]) |
| 35 |
# Replace duplicate newlines with a single newline |
| 36 |
changelog = re.sub(r"\n+", "\n", changelog) |
| 37 |
# Replace duplicate spaces with a single space |
| 38 |
changelog = re.sub(r" +", " ", changelog) |
| 39 |
|
| 40 |
# Ensure `## [Unreleased]\n` is present |
| 41 |
if changelog.find(UNRELEASED_HEADER) == -1: |
| 42 |
errors.append("Changelog does contain '## [Unreleased]'") |
| 43 |
|
| 44 |
# Ensure unreleased has a URL |
| 45 |
unreleased_url = re.search(UNRELEASED_HYPERLINK_RE, changelog) |
| 46 |
if unreleased_url is None: |
| 47 |
errors.append("Unreleased does not have a URL") |
| 48 |
|
| 49 |
# Ensure UNRELEASED_URL_REGEX ends in "HEAD" |
| 50 |
if unreleased_url and unreleased_url[2] != "HEAD": |
| 51 |
errors.append( |
| 52 |
f"The hyperlink for [Unreleased] was expected to contain 'HEAD' but instead found '{unreleased_url[2]}'" |
| 53 |
) |
| 54 |
|
| 55 |
# Ensure the unreleased URL's version is the previous version (version text proceeding [Unreleased]) |
| 56 |
if unreleased_url: |
| 57 |
previous_version_linked_in_unreleased = unreleased_url[1] |
| 58 |
previous_version = re.search(r"\[([^\]]+)\] -", changelog) |
| 59 |
if previous_version and previous_version[1] != previous_version_linked_in_unreleased: |
| 60 |
errors.append( |
| 61 |
f"The hyperlink for [Unreleased] was expected to contain '{previous_version[1]}' but instead found '{previous_version_linked_in_unreleased}'" |
| 62 |
) |
| 63 |
|
| 64 |
# Gather info from version headers. Note that the 'Unreleased' hyperlink is validated separately. |
| 65 |
versions_from_headers = re.findall(VERSION_HEADER_START_RE, changelog) |
| 66 |
versions_from_headers = [header for header in versions_from_headers if header != "Unreleased"] |
| 67 |
dates_from_headers = re.findall(VERSION_HEADER_FULL_RE, changelog) |
| 68 |
dates_from_headers = [header[1] for header in dates_from_headers if header[0] != "Unreleased"] |
| 69 |
|
| 70 |
# Ensure each version header has a hyperlink |
| 71 |
for version in versions_from_headers: |
| 72 |
if re.search(VERSION_HYPERLINK_START_RE.replace(r"[\w.]+", version), changelog) is None: |
| 73 |
errors.append(f"Version '{version}' does not have a URL") |
| 74 |
|
| 75 |
# Gather all hyperlinks. Note that the 'Unreleased' hyperlink is validated separately |
| 76 |
hyperlinks = re.findall(VERSION_HYPERLINK_RE, changelog) |
| 77 |
hyperlinks = [hyperlink for hyperlink in hyperlinks if hyperlink[0] != "Unreleased"] |
| 78 |
|
| 79 |
# Ensure each hyperlink has a header |
| 80 |
for hyperlink in hyperlinks: |
| 81 |
if hyperlink[0] not in versions_from_headers: |
| 82 |
errors.append(f"Hyperlink '{hyperlink[0]}' does not have a version title '## [{hyperlink[0]}]'") |
| 83 |
|
| 84 |
# Ensure there is only one initial version |
| 85 |
initial_version = re.findall(INITIAL_VERSION_RE, changelog) |
| 86 |
if len(initial_version) > 1: |
| 87 |
errors.append( |
| 88 |
"There is more than one link to a '.../releases/tag/' URL " |
| 89 |
"when this is reserved for only the initial version." |
| 90 |
) |
| 91 |
|
| 92 |
# Ensure the initial version's tag name matches the version name |
| 93 |
if initial_version: |
| 94 |
initial_version_tag = initial_version[0][0] |
| 95 |
if initial_version_tag != initial_version[0][1]: |
| 96 |
errors.append( |
| 97 |
f"The initial version tag name '{initial_version[0][1]}' does " |
| 98 |
f"not match the version header '{initial_version[0][0]}'" |
| 99 |
) |
| 100 |
|
| 101 |
# Ensure the initial version has a header |
| 102 |
if ( |
| 103 |
initial_version |
| 104 |
and re.search(VERSION_HEADER_START_RE.replace(r"[\w.]+", initial_version[0][0]), changelog) is None |
| 105 |
): |
| 106 |
errors.append(f"Initial version '{initial_version[0][0]}' does not have a version header") |
| 107 |
|
| 108 |
# Ensure all versions headers have dates |
| 109 |
full_version_headers = re.findall(VERSION_HEADER_FULL_RE, changelog) |
| 110 |
if len(full_version_headers) != len(versions_from_headers): |
| 111 |
for version in versions_from_headers: |
| 112 |
if re.search(VERSION_HEADER_FULL_RE.replace(r"([\w.]+)", rf"({version})"), changelog) is None: |
| 113 |
errors.append(f"Version header '## [{version}]' does not have a date in the correct format") |
| 114 |
|
| 115 |
# Ensure version links always diff to the previous version |
| 116 |
versions_from_hyperlinks = [hyperlinks[0] for hyperlinks in hyperlinks] |
| 117 |
versions_from_hyperlinks.append(initial_version[0][0]) |
| 118 |
for position, version in enumerate(versions_from_hyperlinks): |
| 119 |
if position == len(versions_from_hyperlinks) - 1: |
| 120 |
break |
| 121 |
|
| 122 |
pattern = ( |
| 123 |
rf"\[{version}\]: {GITHUB_COMPARE_URL_START_RE}{versions_from_hyperlinks[position + 1]}\.\.\.{version}" |
| 124 |
) |
| 125 |
if re.search(pattern, changelog) is None: |
| 126 |
errors.append( |
| 127 |
f"Based on hyperlink order, the URL for version '{version}' was expected to contain '.../compare/{versions_from_hyperlinks[position + 1]}...{version}'" |
| 128 |
) |
| 129 |
|
| 130 |
# Ensure the versions in the headers are in descending order |
| 131 |
for position, version in enumerate(versions_from_headers): |
| 132 |
if position == len(versions_from_headers) - 1: |
| 133 |
break |
| 134 |
|
| 135 |
if version <= versions_from_headers[position + 1]: |
| 136 |
errors.append(f"Version '{version}' should be listed before '{versions_from_headers[position + 1]}'") |
| 137 |
|
| 138 |
# Ensure the order of versions from headers matches the hyperlinks |
| 139 |
for position, version in enumerate(versions_from_headers): |
| 140 |
if position == len(versions_from_headers) - 1: |
| 141 |
break |
| 142 |
|
| 143 |
if version != versions_from_hyperlinks[position]: |
| 144 |
errors.append( |
| 145 |
f"The order of the version headers does not match your hyperlinks. " |
| 146 |
f"Found '{versions_from_hyperlinks[position]}' in hyperlinks but expected '{version}'" |
| 147 |
) |
| 148 |
|
| 149 |
# Ensure the release dates are in descending order |
| 150 |
for position, date in enumerate(dates_from_headers): |
| 151 |
if position == len(dates_from_headers) - 1: |
| 152 |
break |
| 153 |
|
| 154 |
if date < dates_from_headers[position + 1]: |
| 155 |
errors.append(f"Header with date '{date}' should be listed before '{dates_from_headers[position + 1]}'") |
| 156 |
|
| 157 |
# Check if the user is using something other than <Added||Changed||Deprecated||Removed||Fixed||Security> |
| 158 |
section_headers = re.findall(SECTION_HEADER_RE, changelog) |
| 159 |
for header in section_headers: |
| 160 |
if header not in {"Added", "Changed", "Deprecated", "Removed", "Fixed", "Security"}: |
| 161 |
errors.append(f"Using non-standard section header '{header}'") |
| 162 |
|
| 163 |
# Check the order of the sections |
| 164 |
# Simplify the changelog into a list of `##` and `###` headers |
| 165 |
changelog_header_lines = [line for line in changelog.split("\n") if line.startswith(("###", "##"))] |
| 166 |
order = ["### Added", "### Changed", "### Deprecated", "### Removed", "### Fixed", "### Security"] |
| 167 |
current_position_in_order = -1 |
| 168 |
version_header = "UNKNOWN" |
| 169 |
seen_sections = set() |
| 170 |
for _line in changelog_header_lines: |
| 171 |
line = _line.strip() |
| 172 |
# Reset current position if we are at a version header |
| 173 |
if line.startswith("## "): |
| 174 |
version_header = line |
| 175 |
current_position_in_order = -1 |
| 176 |
seen_sections = set() |
| 177 |
|
| 178 |
# Check if the current section is in the correct order |
| 179 |
if line in order: |
| 180 |
section_position = order.index(line) |
| 181 |
# Duplicate detection (any repeat within the same version regardless of order) |
| 182 |
if line in seen_sections: |
| 183 |
errors.append(f"Duplicate section '{line}' found in version '{version_header}'.") |
| 184 |
else: |
| 185 |
seen_sections.add(line) |
| 186 |
if section_position < current_position_in_order: |
| 187 |
errors.append( |
| 188 |
f"Section '{line}' is out of order in version '{version_header}'. " |
| 189 |
"Expected section order: [Added, Changed, Deprecated, Removed, Fixed, Security]" |
| 190 |
) |
| 191 |
current_position_in_order = section_position |
| 192 |
|
| 193 |
# Find sections with missing bullet points |
| 194 |
changelog_header_and_bullet_lines = [ |
| 195 |
line for line in changelog.split("\n") if line.startswith(("### ", "## ", "-")) |
| 196 |
] |
| 197 |
current_version = "UNKNOWN" |
| 198 |
for position, line in enumerate(changelog_header_and_bullet_lines): |
| 199 |
if line.startswith("## "): |
| 200 |
current_version = line |
| 201 |
# If it's an h3 header, report an error if the next line is not a bullet point, or if there is no next line |
| 202 |
if line.startswith("### ") and ( |
| 203 |
position + 1 == len(changelog_header_and_bullet_lines) |
| 204 |
or not changelog_header_and_bullet_lines[position + 1].startswith("-") |
| 205 |
): |
| 206 |
errors.append(f"Section '{line}' in version '{current_version}' is missing bullet points") |
| 207 |
|
| 208 |
return errors |
| 209 |
|
| 210 |
|
| 211 |
if __name__ == "__main__": |
| 212 |
if len(sys.argv) == 2: |
| 213 |
validate_changelog(sys.argv[1]) |
| 214 |
if len(sys.argv) > 2: |
| 215 |
print("Usage: python validate_changelog.py [changelog_path]") |
| 216 |
sys.exit(1) |
| 217 |
|
| 218 |
errors = validate_changelog() |
| 219 |
if errors: |
| 220 |
print("Changelog has formatting errors!") |
| 221 |
for error in errors: |
| 222 |
print(f" - {error}") |
| 223 |
sys.exit(1) |
| 224 |
|
| 225 |
print("Changelog is valid!") |
| 226 |
sys.exit(0) |