From 2d5d112c4b7cf8eb2200412bd7f18ee7eea963ff Mon Sep 17 00:00:00 2001 From: bbhtt Date: Tue, 4 Jun 2024 09:25:11 +0530 Subject: [PATCH] main: Set module names in the PR title Choose the "best" title with most module names while making sure it does not cross 70 characters. The limit is to prevent too much verbosity in commit messages and PR titles when a large set of modules are getting updated. If it crosses 70 characters, start deducting module names and add the number of remaining updated modules. --- src/main.py | 28 ++++++++++++++++++++++++++-- tests/test_main.py | 4 ++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main.py b/src/main.py index 06f56833..254a1d37 100755 --- a/src/main.py +++ b/src/main.py @@ -177,15 +177,39 @@ class CommittedChanges(t.NamedTuple): base_branch: t.Optional[str] +def commit_message(changes: t.List[str]) -> str: + assert len(changes) >= 1 + + if len(changes) == 1: + return changes[0] + + module_names = list(dict.fromkeys(list(i.split(":", 1)[0] for i in changes))) + for i in reversed(range(2, len(module_names) + 1)): + xs = module_names[: i - 1] + y = module_names[i - 1] + zs = module_names[i:] + + if zs: + tail = f" and {len(zs)} more modules" + xs.append(y) + else: + tail = f" and {y} modules" + + subject = "Update " + ", ".join(xs) + tail + if len(subject) <= 70: + return subject + + return f"Update {len(module_names)} modules" + + def commit_changes(changes: t.List[str]) -> CommittedChanges: log.info("Committing updates") body: t.Optional[str] + subject = commit_message(changes) if len(changes) > 1: - subject = "Update {} modules".format(len(changes)) body = "\n".join(changes) message = subject + "\n\n" + body else: - subject = changes[0] body = None message = subject diff --git a/tests/test_main.py b/tests/test_main.py index 5c00b31a..d647a9dc 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -74,7 +74,7 @@ async def test_full_run(self): self.assertEqual(await main.run_with_args(args1), (2, 0, True)) commit_data = self._get_commit_data() - self.assertEqual(commit_data["subject"], "Update 2 modules") + self.assertEqual(commit_data["subject"], "Update libXaw and xterm modules") self.assertEqual(commit_data["author_name"], "Test Runner") self.assertEqual(commit_data["author_email"], "test@localhost") self.assertEqual(commit_data["committer_name"], "Test Runner") @@ -95,7 +95,7 @@ async def test_git_envvars(self): self.assertEqual(await main.run_with_args(args1), (2, 0, True)) commit_data = self._get_commit_data() - self.assertEqual(commit_data["subject"], "Update 2 modules") + self.assertEqual(commit_data["subject"], "Update libXaw and xterm modules") self.assertEqual(commit_data["author_name"], "Some Guy") self.assertEqual(commit_data["author_email"], "someguy@localhost") self.assertEqual(commit_data["committer_name"], "Test Runner")