File: make_changelog.py

package info (click to toggle)
pybind11 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,448 kB
  • sloc: cpp: 27,239; python: 13,512; ansic: 4,244; makefile: 204; sh: 36
file content (121 lines) | stat: -rwxr-xr-x 3,151 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env -S uv run

# /// script
# dependencies = ["ghapi", "rich"]
# ///

from __future__ import annotations

import re

import ghapi.all
from rich import print
from rich.syntax import Syntax

MD_ENTRY = re.compile(
    r"""
    \#\#\ Suggested\ changelog\ entry:     # Match the heading exactly
    (?:\s*<!--.*?-->)?                     # Optionally match one comment
    (?P<content>.*?)                       # Lazily capture content until...
    (?=                                    # Lookahead for one of the following:
        ^-{3,}\s*$                         #   A line with 3 or more dashes
      | ^<!--\s*readthedocs                #   A comment starting with 'readthedocs'
      | ^\#\#                              #   A new heading
      | \Z                                 #   End of string
    )
    """,
    re.DOTALL | re.VERBOSE | re.MULTILINE,
)
print()


api = ghapi.all.GhApi(owner="pybind", repo="pybind11")

issues_pages = ghapi.page.paged(
    api.issues.list_for_repo, labels="needs changelog", state="closed"
)
issues = (issue for page in issues_pages for issue in page)
missing = []
old = []
cats_descr = {
    "feat": "New Features",
    "feat(types)": "",
    "feat(cmake)": "",
    "fix": "Bug fixes",
    "fix(types)": "",
    "fix(cmake)": "",
    "fix(free-threading)": "",
    "docs": "Documentation",
    "tests": "Tests",
    "ci": "CI",
    "chore": "Other",
    "chore(cmake)": "",
    "unknown": "Uncategorised",
}
cats: dict[str, list[str]] = {c: [] for c in cats_descr}

for issue in issues:
    if "```rst" in issue.body:
        old.append(issue)
        continue

    changelog = MD_ENTRY.search(issue.body or "")
    if not changelog:
        missing.append(issue)
        continue

    msg = changelog.group("content").strip()
    if not msg:
        missing.append(issue)
        continue
    if msg.startswith("* "):
        msg = msg[2:]
    if not msg.startswith("- "):
        msg = "- " + msg
    if not msg.endswith("."):
        msg += "."
    if msg == "- Placeholder.":
        missing.append(issue)
        continue

    msg += f"\n  [#{issue.number}]({issue.html_url})"
    for cat, cat_list in cats.items():
        if issue.title.lower().startswith(f"{cat}:"):
            cat_list.append(msg)
            break
    else:
        cats["unknown"].append(msg)

for cat, msgs in cats.items():
    if msgs:
        desc = cats_descr[cat]
        print(f"[bold]{desc}:" if desc else f"<!-- {cat} -->")
        print()
        for msg in msgs:
            print(Syntax(msg, "md", theme="ansi_light", word_wrap=True))
            print()
        print()

if missing:
    print()
    print("[blue]" + "-" * 30)
    print()

    for issue in missing:
        print(f"[red bold]Missing:[/red bold][red] {issue.title}")
        print(f"[red]  {issue.html_url}\n")

    print("[bold]Template:\n")
    msg = "## Suggested changelog entry:"
    print(Syntax(msg, "md", theme="ansi_light"))

if old:
    print()
    print("[red]" + "-" * 30)
    print()

    for issue in old:
        print(f"[red bold]Old:[/red bold][red] {issue.title}")
        print(f"[red]  {issue.html_url}\n")

print()