Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions obsbot/cogs/public/utils/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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)
Expand All @@ -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]

Expand All @@ -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("###")
Expand Down
4 changes: 2 additions & 2 deletions obsbot/cogs/public/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading