Skip to content

Commit 84a9b39

Browse files
committed
Add wiki page updated, push and commit comment event handling
1 parent cfbdf84 commit 84a9b39

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

bot/github.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def _iter_repos(self, repository):
5757
if repo.id == repo_id:
5858
yield chat_id, repo
5959

60-
def _send(self, repo, text, check_repo: Callable[[Repo], bool]):
61-
text = truncate(text, TRUNCATED_MESSAGE, REPLY_MESSAGE)
60+
def _send(self, repo, text, check_repo: Callable[[Repo], bool], suffix=REPLY_MESSAGE):
61+
text = truncate(text, TRUNCATED_MESSAGE, suffix)
6262

6363
for chat_id, repo in self._iter_repos(repo):
6464
if check_repo(repo):
@@ -179,6 +179,62 @@ def pull_request_review_comment(self, update, context):
179179

180180
self._send(repo, text, lambda r: r.pull_review_comments)
181181

182+
def push(self, update, context):
183+
# Triggered on a push to a repository branch.
184+
# Branch pushes and repository tag pushes also trigger webhook push events.
185+
commits = update.payload['commits']
186+
ref = update.payload['ref']
187+
188+
if commits and ref.startswith('refs/heads/'):
189+
branch = ref[len('refs/heads/'):]
190+
repo = update.payload['repository']
191+
compare = update.payload['compare']
192+
193+
text = f'🔨 <a href="{compare}">{len(commits)} new commits</a> to {repo["full_name"]}:{branch}\n\n'
194+
195+
for commit in commits:
196+
text += f'<a href="{commit["url"]}">{commit["id"][:7]}</a>: {commit["message"]} by {commit["author"]["name"]}'
197+
198+
self._send(repo, text, lambda r: (r.push_main or r.push) if branch == repo["default_branch"] else r.push,
199+
suffix='')
200+
201+
def gollum(self, update, context):
202+
# Wiki page is created or updated.
203+
pages = update.payload['pages']
204+
repo = update.payload['repository']
205+
sender = update.payload['sender']
206+
207+
text = f'🔨 {len(pages)} {repo["full_name"]} wiki page{"s" if len(pages) > 1 else ""} were updated '
208+
sender_link = link(sender['html_url'], '@' + sender['login'])
209+
text += f'by {sender_link}\n\n'
210+
211+
for page in pages:
212+
text += f'<a href="{page["html_url"]}">{page["title"]}</a>'
213+
214+
self._send(repo, text, lambda r: r.wiki_pages, suffix='')
215+
216+
def commit_comment(self, update, context):
217+
if update.payload['action'] == 'created':
218+
repo = update.payload['repository']
219+
comment = update.payload['comment']
220+
author = comment['user']
221+
222+
author_link = link(author['html_url'], '@' + author['login'])
223+
text = f'💬 <a href="{comment["html_url"]}">New comment</a> on commit {comment["commit_id"][:7]} by {author_link}'
224+
position, line, path = comment['position'], comment['line'], comment['path']
225+
if path:
226+
text += f'\nPath: {path}'
227+
if line:
228+
text += f'\nLine: {line}'
229+
if position == 1:
230+
text += ' (before)'
231+
elif position == 2:
232+
text += ' (after)'
233+
234+
text += f'\n\n{comment["body"]}'
235+
236+
self._send(repo, text, lambda r: r.commit_comments, suffix='')
237+
182238
# def integration_installation_repositories(self, update, context):
183239
# new_repos = [{'id': repo['id'], 'full_name': repo['full_name']} for repo in
184240
# update.payload['repositories_added']]

bot/repo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ class Repo:
1111
pull_comments: bool = True
1212
pull_reviews: bool = True
1313
pull_review_comments: bool = True
14+
wiki_pages: bool = False
15+
push: bool = False
16+
push_main: bool = True
17+
commit_comments: bool = True

bot/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ def repo_buttons(update, context):
144144
[ToggleButton('pull_comments', value=repo.pull_comments, text='Comments on pull requests')],
145145
[ToggleButton('pull_reviews', value=repo.pull_reviews, text='New pull request reviews')],
146146
[ToggleButton('pull_review_comments', value=repo.pull_review_comments, text='Pull request review comments')],
147+
[ToggleButton('commit_comments', value=repo.commit_comments, text='Comment on a commit/diff')],
148+
[ToggleButton('wiki_pages', value=repo.wiki_pages, text='Wiki page updated')],
149+
[ToggleButton('push', value=repo.push, text='Commits pushed to any branch')],
150+
[ToggleButton('push_main', value=repo.push_main, text='Commits pushed to default branch')],
147151
[SetButton('remove', None, '❌ Remove')],
148152
[BackButton(BACK)]
149153
]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy