Skip to content

Commit 038d48a

Browse files
committed
scripts: many changes to build_changelog.py
- added 'Full Changelog' link to botton, - fix wrong submodule names - filter empty sections - added path improvements
1 parent 6b67ec6 commit 038d48a

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

scripts/build_changelog.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#!/usr/bin/env python3
22
"""
3-
Script that outputs a changelog for the repository in the current directory and its submodules.
3+
Script that generates a changelog for the repository and its submodules, and outputs it in the current directory.
4+
5+
NOTE: This script can be downloaded as-is and run from your repository.
6+
7+
Repos using this script:
8+
- ActivityWatch/activitywatch
9+
- ErikBjare/gptme
410
511
Manual actions needed to clean up for changelog:
612
- Reorder modules in a logical order (aw-webui, aw-server, aw-server-rust, aw-watcher-window, aw-watcher-afk, ...)
@@ -14,6 +20,7 @@
1420
from collections import defaultdict
1521
from collections.abc import Collection
1622
from dataclasses import dataclass
23+
from pathlib import Path
1724
from subprocess import PIPE, STDOUT
1825
from subprocess import run as _run
1926
from time import sleep
@@ -30,27 +37,34 @@
3037
logger = logging.getLogger(__name__)
3138

3239

33-
def main():
34-
prev_release = run("git describe --tags --abbrev=0").strip()
35-
next_release = "master"
40+
script_dir = Path(__file__).parent.resolve()
3641

42+
43+
def main():
3744
parser = argparse.ArgumentParser(description="Generate changelog from git history")
3845

46+
# repo info
3947
parser.add_argument("--org", default="ActivityWatch", help="GitHub organization")
4048
parser.add_argument("--repo", default="activitywatch", help="GitHub repository")
4149
parser.add_argument(
4250
"--project-title", default="ActivityWatch", help="Project title"
4351
)
4452

53+
# settings
54+
last_tag = run("git describe --tags --abbrev=0").strip() # get latest tag
55+
branch = run("git rev-parse --abbrev-ref HEAD").strip() # get current branch name
4556
parser.add_argument(
46-
"--range", default=f"{prev_release}...{next_release}", help="Git commit range"
57+
"--range", default=f"{last_tag}...{branch}", help="Git commit range"
4758
)
4859
parser.add_argument("--path", default=".", help="Path to git repo")
60+
61+
# output
4962
parser.add_argument(
5063
"--output", default="changelog.md", help="Path to output changelog"
5164
)
52-
args = parser.parse_args()
5365

66+
# parse args
67+
args = parser.parse_args()
5468
since, until = args.range.split("...", 1)
5569

5670
# preferred output order for submodules
@@ -164,9 +178,9 @@ def wrap_details(title, body, wraplines=5):
164178
wrap = body.strip().count("\n") > wraplines
165179
if wrap:
166180
out += "\n<details><summary>Click to expand</summary>\n<p>"
167-
out += f"\n{body.rstrip()}\n\n"
181+
out += f"\n{body.rstrip()}"
168182
if wrap:
169-
out += "</p>\n</details>"
183+
out += "\n\n</p>\n</details>"
170184
return out
171185

172186

@@ -223,7 +237,7 @@ def summary_repo(
223237
if "Misc" in name or "Fixes" in name:
224238
out += wrap_details(title, entries)
225239
else:
226-
out += f"\n\n### {title}"
240+
out += f"\n\n### {title}\n"
227241
out += entries
228242

229243
# NOTE: For now, these TODOs can be manually fixed for each changelog.
@@ -252,13 +266,20 @@ def summary_repo(
252266

253267
subrepos[name] = summary_repo(
254268
org,
255-
repo,
269+
name,
256270
f"{path}/{name}",
257271
commit_range,
258272
filter_types=filter_types,
259273
repo_order=repo_order,
260274
)
261275

276+
# filter out subrepos with no commits (single line after stripping whitespace)
277+
subrepos = {
278+
name: output
279+
for name, output in subrepos.items()
280+
if len(output.strip().splitlines()) > 1
281+
}
282+
262283
# pick subrepos in repo_order, and remove from dict
263284
for name in repo_order:
264285
if name in subrepos:
@@ -380,6 +401,9 @@ def build(
380401

381402
output += output_contributors.strip() + "\n\n"
382403
output += output_changelog.strip() + "\n\n"
404+
output += (
405+
f"**Full Changelog**: https://github.com/{org}/{repo}/compare/{since}...{tag}"
406+
)
383407

384408
if repo == "activitywatch":
385409
output = output.replace("# activitywatch", "# activitywatch (bundle repo)")
@@ -444,7 +468,7 @@ def get_all_contributors() -> set[str]:
444468
logger.info("Getting all contributors")
445469

446470
# We will commit this file, to act as a cache (preventing us from querying GitHub API every time)
447-
filename = "scripts/changelog_contributors.csv"
471+
filename = script_dir / "changelog_contributors.csv"
448472

449473
# mapping from username to one or more emails
450474
usernames: Dict[str, set] = defaultdict(set)
@@ -502,7 +526,7 @@ def get_twitter_of_ghusers(ghusers: Collection[str]):
502526
logger.info("Getting twitter of GitHub usernames")
503527

504528
# We will commit this file, to act as a cache (preventing us from querying GitHub API every time)
505-
filename = "scripts/changelog_contributors_twitter.csv"
529+
filename = script_dir / "changelog_contributors_twitter.csv"
506530

507531
twitter = {}
508532

0 commit comments

Comments
 (0)