mirror of
https://github.com/YunoHost/webhooks.git
synced 2024-09-03 19:56:54 +02:00
[fix] notify is now an async function
This commit is contained in:
parent
c8645ab3e6
commit
348fcc07f7
1 changed files with 35 additions and 35 deletions
70
server.py
70
server.py
|
@ -30,7 +30,7 @@ other_chans = {
|
|||
# * déployer
|
||||
|
||||
|
||||
def notify(message, repository="dev"):
|
||||
async def notify(message, repository="dev"):
|
||||
if repository.endswith("_ynh"):
|
||||
chan = "apps"
|
||||
else:
|
||||
|
@ -86,10 +86,10 @@ async def github(request):
|
|||
if len(commit_message) > 120:
|
||||
commit_message = commit_message[120:] + "..."
|
||||
|
||||
notify(f"[{repository}] @{user} pushed {len(commits)} commit to {branch}: {commit_message} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} pushed {len(commits)} commit to {branch}: {commit_message} {url}", repository=repository)
|
||||
elif len(commits) > 1:
|
||||
url = request.json["compare"]
|
||||
notify(f"[{repository}] @{user} pushed {len(commits)} commits to {branch}: {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} pushed {len(commits)} commits to {branch}: {url}", repository=repository)
|
||||
for commit in commits[-3:]:
|
||||
author = commit["author"]["name"]
|
||||
commit_message = commit["message"].replace("\r\n", " ").replace("\n", " ")
|
||||
|
@ -97,7 +97,7 @@ async def github(request):
|
|||
if len(commit_message) > 120:
|
||||
commit_message = commit_message[120:] + "..."
|
||||
|
||||
notify(f"[{repository}/{branch}] {commit_message} - {author}", repository=repository)
|
||||
await notify(f"[{repository}/{branch}] {commit_message} - {author}", repository=repository)
|
||||
else:
|
||||
... # case of 0 which means branch deletion
|
||||
|
||||
|
@ -109,7 +109,7 @@ async def github(request):
|
|||
comment = request.json["comment"]["body"].replace("\r\n", " ")
|
||||
url = request.json["comment"]["html_url"]
|
||||
|
||||
notify(f"[{repository}] @{user} comment on commit {commit_short_id}: {comment} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} comment on commit {commit_short_id}: {comment} {url}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#createevent
|
||||
elif hook_type == "create":
|
||||
|
@ -118,13 +118,13 @@ async def github(request):
|
|||
repository = request.json["repository"]["name"]
|
||||
|
||||
if kind == "repository":
|
||||
notify(f"@{user} created new repository {repository}: {url}", repository=repository)
|
||||
await notify(f"@{user} created new repository {repository}: {url}", repository=repository)
|
||||
elif kind == "branch":
|
||||
branch = request.json["ref"]
|
||||
notify(f"[{repository}] @{user} created new branch {branch}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} created new branch {branch}", repository=repository)
|
||||
elif kind == "tag":
|
||||
tag = request.json["ref"]
|
||||
notify(f"[{repository}] @{user} created new tag {tag}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} created new tag {tag}", repository=repository)
|
||||
else:
|
||||
print(f"WARNING: unknown 'create' event kind: {kind}")
|
||||
|
||||
|
@ -135,7 +135,7 @@ async def github(request):
|
|||
repository = request.json["repository"]["name"]
|
||||
|
||||
ref = request.json["ref"]
|
||||
notify(f"[{repository}] @{user} deleted {kind} {ref}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} deleted {kind} {ref}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#forkevent
|
||||
elif hook_type == "fork":
|
||||
|
@ -144,7 +144,7 @@ async def github(request):
|
|||
user = request.json["sender"]["login"]
|
||||
url = request.json["forkee"]["html_url"]
|
||||
|
||||
notify(f"@{user} forked {repository} to {forked_repository}: {url}", repository=repository)
|
||||
await notify(f"@{user} forked {repository} to {forked_repository}: {url}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#issuecommentevent
|
||||
elif hook_type == "issue_comment":
|
||||
|
@ -158,7 +158,7 @@ async def github(request):
|
|||
if len(comment) > 120:
|
||||
comment = comment[:120] + "..."
|
||||
|
||||
notify(f"[{repository}] @{user} commented on issue #{issue_number} {issue_title}: {comment} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} commented on issue #{issue_number} {issue_title}: {comment} {url}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#issuesevent
|
||||
elif hook_type == "issues":
|
||||
|
@ -170,29 +170,29 @@ async def github(request):
|
|||
issue_title = request.json["issue"]["title"]
|
||||
|
||||
if action == "opened":
|
||||
notify(f"[{repository}] @{user} {action} issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
|
||||
elif action in ("edited", "deleted", "transferred", "pinned",
|
||||
"unpinned", "closed", "reopened"):
|
||||
notify(f"[{repository}] @{user} {action} issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
|
||||
elif action in ("assigned", "unassigned"):
|
||||
assigned_user = request.json["assignee"]["login"]
|
||||
notify(f"[{repository}] @{user} {action} {assigned_user} on issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} {assigned_user} on issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
|
||||
elif action in ("labeled", "unlabeled"):
|
||||
label = request.json["label"]["name"]
|
||||
notify(f"[{repository}] @{user} {action} {label} on issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} {label} on issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
|
||||
elif action == "milestoned":
|
||||
milestone = request.json["issue"]["milestone"]["title"]
|
||||
notify(f"[{repository}] @{user} set {milestone} on issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} set {milestone} on issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
|
||||
elif action == "demilestoned":
|
||||
notify(f"[{repository}] @{user} {action} issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} issue #{issue_number}: {issue_title} {url}", repository=repository)
|
||||
|
||||
else:
|
||||
notify(f"[{repository}] WARNING: unknown 'issues' action: {action}", repository=repository)
|
||||
await notify(f"[{repository}] WARNING: unknown 'issues' action: {action}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#labelevent
|
||||
elif hook_type == "label":
|
||||
|
@ -201,7 +201,7 @@ async def github(request):
|
|||
repository = request.json["repository"]["name"]
|
||||
user = request.json["sender"]["login"]
|
||||
|
||||
notify(f"[{repository}] @{user} {action} label {label}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} label {label}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#milestoneevent
|
||||
elif hook_type == "milestone":
|
||||
|
@ -210,7 +210,7 @@ async def github(request):
|
|||
user = request.json["sender"]["login"]
|
||||
milestone = request.json["milestone"]["title"]
|
||||
|
||||
notify(f"[{repository}] @{user} {action} milestone {milestone}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} milestone {milestone}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent
|
||||
elif hook_type == "pull_request_review_comment":
|
||||
|
@ -226,9 +226,9 @@ async def github(request):
|
|||
comment = comment[:120] + "..."
|
||||
|
||||
if action == "created":
|
||||
notify(f"[{repository}] @{user} commented on pull request #{pull_request_number} {pull_request_title}: {comment} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} commented on pull request #{pull_request_number} {pull_request_title}: {comment} {url}", repository=repository)
|
||||
else:
|
||||
notify(f"[{repository}] @{user} {action} a comment on pull request #{pull_request_number} {pull_request_title}: {comment} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} a comment on pull request #{pull_request_number} {pull_request_title}: {comment} {url}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#pullrequestreviewevent
|
||||
elif hook_type == "pull_request_review":
|
||||
|
@ -253,10 +253,10 @@ async def github(request):
|
|||
if state == "commented" and not comment:
|
||||
pass
|
||||
else:
|
||||
notify(f"[{repository}] @{user} {state} pull request #{pull_request_number} {pull_request_title}{comment} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {state} pull request #{pull_request_number} {pull_request_title}{comment} {url}", repository=repository)
|
||||
|
||||
else:
|
||||
notify(f"[{repository}] @{user} {action} review pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} review pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#pullrequestevent
|
||||
elif hook_type == "pull_request":
|
||||
|
@ -277,34 +277,34 @@ async def github(request):
|
|||
|
||||
if action in ("opened", "edited", "deleted", "transferred", "pinned",
|
||||
"unpinned", "reopened"):
|
||||
notify(f"[{repository}] @{user} {action} pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
|
||||
elif action in ("labeled", "unlabeled"):
|
||||
label = request.json["label"]["name"]
|
||||
notify(f"[{repository}] @{user} {action} {label} on pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} {label} on pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
|
||||
elif action == "closed":
|
||||
if request.json["pull_request"]["merged"]:
|
||||
action = "merged"
|
||||
notify(f"[{repository}] @{user} {action} pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
|
||||
elif action == "ready_for_review":
|
||||
notify(f"[{repository}] @{user} just made pull request #{pull_request_number} ready for review: {pull_request_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} just made pull request #{pull_request_number} ready for review: {pull_request_title} {url}", repository=repository)
|
||||
|
||||
# super weird, this action is not supposed to be possible for pull_request :|
|
||||
elif action == "milestoned":
|
||||
milestone = request.json["pull_request"]["milestone"]
|
||||
notify(f"[{repository}] @{user} set {milestone} pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} set {milestone} pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
|
||||
# super weird, this action is not supposed to be possible for pull_request :|
|
||||
elif action == "demilestoned":
|
||||
notify(f"[{repository}] @{user} {action} pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
await notify(f"[{repository}] @{user} {action} pull request #{pull_request_number}: {pull_request_title} {url}", repository=repository)
|
||||
|
||||
elif action in ("review_requested", "review_request_removed", "synchronize"):
|
||||
pass # we don't care about those...
|
||||
|
||||
else:
|
||||
notify(f"WARNING: unknown 'pull_request' action: {action}", repository=repository)
|
||||
await notify(f"WARNING: unknown 'pull_request' action: {action}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#repositoryevent
|
||||
elif hook_type == "repository":
|
||||
|
@ -319,7 +319,7 @@ async def github(request):
|
|||
else:
|
||||
description = ": " + description
|
||||
|
||||
notify(f"@{user} {action} repository {repository}{description} {url}", repository=repository)
|
||||
await notify(f"@{user} {action} repository {repository}{description} {url}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#releaseevent
|
||||
elif hook_type == "release":
|
||||
|
@ -330,7 +330,7 @@ async def github(request):
|
|||
release_tag = request.json["release"]["tag_name"]
|
||||
release_title = request.json["release"]["name"]
|
||||
|
||||
notify(f"[repository] @{user} {action} new release #{release_tag} {release_title} {url}", repository=repository)
|
||||
await notify(f"[repository] @{user} {action} new release #{release_tag} {release_title} {url}", repository=repository)
|
||||
|
||||
# https://developer.github.com/v3/activity/events/types/#statusevent
|
||||
elif hook_type == "status":
|
||||
|
@ -342,7 +342,7 @@ async def github(request):
|
|||
url = request.json["commit"]["html_url"]
|
||||
|
||||
if state not in ("success", "pending"):
|
||||
notify(f"[{repository}] {description} {target_url} on commit {url}")
|
||||
await notify(f"[{repository}] {description} {target_url} on commit {url}")
|
||||
else:
|
||||
print(f"Status weird stuff: [{repository}] @{user} state: {state}, description: {description}, target_url: {target_url} - {url}")
|
||||
|
||||
|
@ -351,7 +351,7 @@ async def github(request):
|
|||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
notify(f"Error in Webhooks: exception {e} on {hook_type} webhooks, please see logs")
|
||||
await notify(f"Error in Webhooks: exception {e} on {hook_type} webhooks, please see logs")
|
||||
abort(500)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue