Skip to content

Commit

Permalink
main: Set module names in the PR title
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bbhtt committed Jun 10, 2024
1 parent 7f99008 commit 2d5d112
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 26 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 2d5d112

Please sign in to comment.