From ed8028a5f875978af96e703a4dab7c1f2a7eace8 Mon Sep 17 00:00:00 2001 From: prgmitchell <86465454+prgmitchell@users.noreply.github.com> Date: Sun, 27 Sep 2026 14:46:31 -0400 Subject: [PATCH] cogs.util.github: Show merged PRs for multi-commit pushes Show the matching merged PR for pushes containing multiple commits. --- obsbot/cogs/public/utils/github.py | 58 ++++++++++++++++++++++-------- obsbot/cogs/public/webhooks.py | 4 +-- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/obsbot/cogs/public/utils/github.py b/obsbot/cogs/public/utils/github.py index ab872cd..51fdf86 100644 --- a/obsbot/cogs/public/utils/github.py +++ b/obsbot/cogs/public/utils/github.py @@ -32,7 +32,7 @@ def __init__(self, session, config, state): self.user_cache = dict() self.user_cache_max_age = 3600 * 24 * 7 - async def get_commit_messages(self, event_body, brief=False): + async def get_commit_messages(self, event_body): embed_commits = [] branch = event_body['ref'].split('/', 2)[2] project = event_body['repository']['full_name'] @@ -46,18 +46,6 @@ async def get_commit_messages(self, event_body, brief=False): elif project == 'obsproject/design' and branch != 'main': return embed_commits - if brief and len(commits) > self.config['commit_truncation_limit']: - first_hash = commits[0]['id'] - last_hash = commits[-2]['id'] - compare_url = f'https://github.com/{project}/compare/{first_hash}^...{last_hash}' - embed = Embed( - title=f'Skipped {len(commits) - 1} commits... (click link for diff)', - colour=Colour(self._skipped_commit_colour), - url=compare_url, - ) - embed_commits.append((embed, None)) - commits = commits[-1:] - for commit in commits: author_username = commit['author'].get('username', None) author_name = commit['author'].get('name', None) @@ -67,7 +55,7 @@ async def get_commit_messages(self, event_body, brief=False): title=commit_message[0], colour=Colour(self._commit_colour), url=commit['url'], timestamp=timestamp ) - if len(commit_message) > 2 and not brief: + if len(commit_message) > 2: commit_body = '\n'.join(commit_message[2:]) embed.description = commit_body[:4096] @@ -92,6 +80,48 @@ async def get_commit_messages(self, event_body, brief=False): return embed_commits + async def get_brief_commit_messages(self, event_body, commit_messages): + embed_commits = [] + if len(commit_messages) > self.config['commit_truncation_limit']: + branch = event_body['ref'].split('/', 2)[2] + project = event_body['repository']['full_name'] + head_hash = event_body['after'] + prs = await self.get_with_retry(f'https://api.github.com/repos/{project}/commits/{head_hash}/pulls') + for pr in prs or []: + if ( + pr['merged_at'] + and pr['merge_commit_sha'] == head_hash + and pr['base']['ref'] == branch + and pr['base']['repo']['full_name'] == project + ): + embed, _ = await self.get_pr_messages( + dict(number=pr['number'], pull_request=pr, repository=event_body['repository']) + ) + embed.title = f'Merged {embed.title}'[:256] + embed.colour = Colour(self._commit_colour) + embed.timestamp = dateutil.parser.parse(pr['merged_at']) + embed.set_footer(text='Merged Pull Request') + embed.add_field(name='Branch', value=branch, inline=True) + return [(embed, head_hash)] + + first_hash = commit_messages[0][1] + last_hash = commit_messages[-2][1] + compare_url = f'https://github.com/{project}/compare/{first_hash}^...{last_hash}' + embed = Embed( + title=f'Skipped {len(commit_messages) - 1} commits... (click link for diff)', + colour=Colour(self._skipped_commit_colour), + url=compare_url, + ) + embed_commits.append((embed, None)) + commit_messages = commit_messages[-1:] + + for embed, commit_hash in commit_messages: + embed = embed.copy() + embed.description = None + embed_commits.append((embed, commit_hash)) + + return embed_commits + @staticmethod def _format_embed(message_body) -> Generator[Tuple[str, str], None, None]: sections = message_body.split("###") diff --git a/obsbot/cogs/public/webhooks.py b/obsbot/cogs/public/webhooks.py index 1d0f6be..797988b 100644 --- a/obsbot/cogs/public/webhooks.py +++ b/obsbot/cogs/public/webhooks.py @@ -53,13 +53,13 @@ async def github_handler(self, request): if event == 'push': messages = [] # this gets the short and long embeds to send to the respective channels - brief = await self.gh_helper.get_commit_messages(body, True) + full = await self.gh_helper.get_commit_messages(body) + brief = await self.gh_helper.get_brief_commit_messages(body, full) for embed, commit_hash in brief: msg = await self.brief_channel.send(embed=embed) if commit_hash: messages.append((msg, commit_hash)) - full = await self.gh_helper.get_commit_messages(body, False) for embed, commit_hash in full: msg = await self.commits_channel.send(embed=embed) if commit_hash: