2019-02-07 22:30:50 +00:00
|
|
|
import datetime, itertools, json, math, urllib.parse
|
2018-10-05 22:29:59 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2018-11-30 21:53:47 +00:00
|
|
|
FORM_ENCODED = "application/x-www-form-urlencoded"
|
|
|
|
|
2018-10-26 10:25:28 +00:00
|
|
|
COMMIT_URL = "https://github.com/%s/commit/%s"
|
2018-11-17 22:07:32 +00:00
|
|
|
COMMIT_RANGE_URL = "https://github.com/%s/compare/%s...%s"
|
2018-11-17 20:59:24 +00:00
|
|
|
CREATE_URL = "https://github.com/%s/tree/%s"
|
2018-10-26 10:25:28 +00:00
|
|
|
|
2019-01-09 22:30:11 +00:00
|
|
|
API_ISSUE_URL = "https://api.github.com/repos/%s/%s/issues/%s"
|
2019-01-09 22:45:33 +00:00
|
|
|
API_PULL_URL = "https://api.github.com/repos/%s/%s/pulls/%s"
|
2019-01-09 22:30:11 +00:00
|
|
|
|
2019-01-16 22:26:31 +00:00
|
|
|
DEFAULT_EVENT_CATEGORIES = [
|
|
|
|
"ping", "code", "pr", "issue", "repo"
|
2018-11-17 22:32:44 +00:00
|
|
|
]
|
2019-01-16 22:26:31 +00:00
|
|
|
EVENT_CATEGORIES = {
|
2019-01-18 08:08:39 +00:00
|
|
|
"ping": [
|
2019-01-17 14:31:23 +00:00
|
|
|
"ping" # new webhook received
|
|
|
|
],
|
2019-01-18 08:08:39 +00:00
|
|
|
"code": [
|
2019-01-17 14:31:23 +00:00
|
|
|
"push", "commit_comment"
|
|
|
|
],
|
2019-01-18 08:08:39 +00:00
|
|
|
"pr-minimal": [
|
|
|
|
"pull_request/opened", "pull_request/closed", "pull_request/reopened"
|
|
|
|
],
|
|
|
|
"pr": [
|
|
|
|
"pull_request/opened", "pull_request/closed", "pull_request/reopened",
|
|
|
|
"pull_request/edited", "pull_request/assigned",
|
|
|
|
"pull_request/unassigned", "pull_request_review",
|
|
|
|
"pull_request_review_comment"
|
|
|
|
],
|
|
|
|
"pr-all": [
|
|
|
|
"pull_request", "pull_request_review", "pull_request_review_comment"
|
|
|
|
],
|
|
|
|
"issue-minimal": [
|
|
|
|
"issues/opened", "issues/closed", "issues/reopened", "issues/deleted"
|
2019-01-17 14:31:23 +00:00
|
|
|
],
|
|
|
|
"issue": [
|
2019-01-18 08:08:39 +00:00
|
|
|
"issues/opened", "issues/closed", "issues/reopened", "issues/deleted",
|
|
|
|
"issues/edited", "issues/assigned", "issues/unassigned", "issue_comment"
|
|
|
|
],
|
|
|
|
"issue-all": [
|
2019-01-17 14:31:23 +00:00
|
|
|
"issues", "issue_comment"
|
|
|
|
],
|
2019-01-18 08:08:39 +00:00
|
|
|
"repo": [
|
2019-02-15 07:49:07 +00:00
|
|
|
"create", # a repository, branch or tag has been created
|
2019-01-17 14:31:23 +00:00
|
|
|
"delete", # same as above but deleted
|
|
|
|
"release",
|
|
|
|
"fork"
|
|
|
|
],
|
2019-01-18 08:08:39 +00:00
|
|
|
"team": [
|
2019-01-17 14:31:23 +00:00
|
|
|
"membership"
|
2019-01-22 20:49:31 +00:00
|
|
|
],
|
|
|
|
"star": [
|
|
|
|
# "watch" is a misleading name for this event so this add "star" as an
|
|
|
|
# alias for "watch"
|
|
|
|
"watch"
|
2019-01-17 14:31:23 +00:00
|
|
|
]
|
2019-01-16 22:26:31 +00:00
|
|
|
}
|
2018-11-17 22:32:44 +00:00
|
|
|
|
2018-11-06 17:23:27 +00:00
|
|
|
COMMENT_ACTIONS = {
|
2018-11-06 17:05:40 +00:00
|
|
|
"created": "commented",
|
|
|
|
"edited": "edited a comment",
|
|
|
|
"deleted": "deleted a comment"
|
|
|
|
}
|
|
|
|
|
2019-02-07 22:30:50 +00:00
|
|
|
CHECK_RUN_CONCLUSION = {
|
|
|
|
"success": "passed",
|
|
|
|
"failure": "failed",
|
|
|
|
"neutral": "finished",
|
|
|
|
"cancelled": "was cancelled",
|
|
|
|
"timed_out": "timed out",
|
|
|
|
"action_required": "requires action"
|
|
|
|
}
|
|
|
|
CHECK_RUN_FAILURES = ["failure", "cancelled", "timed_out", "action_required"]
|
|
|
|
|
2018-11-08 17:48:23 +00:00
|
|
|
@utils.export("channelset", {"setting": "github-hide-prefix",
|
|
|
|
"help": "Hide/show command-like prefix on Github hook outputs",
|
|
|
|
"validate": utils.bool_or_none})
|
2019-01-09 23:01:27 +00:00
|
|
|
@utils.export("channelset", {"setting": "github-default-repo",
|
|
|
|
"help": "Set the default github repo for the current channel"})
|
2019-01-26 10:49:31 +00:00
|
|
|
@utils.export("channelset", {"setting": "github-prevent-highlight",
|
|
|
|
"help": "Enable/disable preventing highlights",
|
|
|
|
"validate": utils.bool_or_none})
|
2018-10-05 22:29:59 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-01-09 23:01:27 +00:00
|
|
|
def _parse_ref(self, channel, ref):
|
2019-01-10 13:11:35 +00:00
|
|
|
repo, _, number = ref.rpartition("#")
|
2019-01-09 23:01:27 +00:00
|
|
|
if not repo:
|
|
|
|
repo = channel.get_setting("github-default-repo", None)
|
|
|
|
|
2019-01-22 17:16:04 +00:00
|
|
|
username, repository = None, None
|
|
|
|
if repo:
|
|
|
|
username, _, repository = repo.partition("/")
|
2019-01-09 22:30:11 +00:00
|
|
|
|
|
|
|
if not username or not repository or not number:
|
|
|
|
raise utils.EventError("Please provide username/repo#number")
|
|
|
|
if not number.isdigit():
|
|
|
|
raise utils.EventError("Issue number must be a number")
|
2019-01-09 23:01:27 +00:00
|
|
|
return username, repository, number
|
|
|
|
|
2019-01-10 13:36:13 +00:00
|
|
|
def _gh_issue(self, event, page, username, repository, number):
|
2019-01-10 13:32:19 +00:00
|
|
|
labels = [label["name"] for label in page.data["labels"]]
|
|
|
|
url = self._short_url(page.data["html_url"])
|
|
|
|
|
|
|
|
event["stdout"].write("(%s/%s issue#%s, %s) %s [%s] %s" % (
|
|
|
|
username, repository, number, page.data["state"],
|
|
|
|
page.data["title"], ", ".join(labels), url))
|
|
|
|
def _gh_get_issue(self, username, repository, number):
|
|
|
|
return utils.http.request(
|
|
|
|
API_ISSUE_URL % (username, repository, number),
|
|
|
|
json=True)
|
|
|
|
|
2019-01-09 23:01:27 +00:00
|
|
|
@utils.hook("received.command.ghissue", min_args=1)
|
|
|
|
def github_issue(self, event):
|
|
|
|
username, repository, number = self._parse_ref(
|
2019-01-09 23:02:39 +00:00
|
|
|
event["target"], event["args_split"][0])
|
2019-01-09 22:30:11 +00:00
|
|
|
|
2019-01-10 13:32:19 +00:00
|
|
|
page = self._gh_get_issue(username, repository, number)
|
2019-01-10 13:18:58 +00:00
|
|
|
if page and page.code == 200:
|
2019-01-10 13:36:13 +00:00
|
|
|
self._gh_issue(event, page, username, repository, number)
|
2019-01-10 13:18:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Could not find issue")
|
2019-01-09 22:30:11 +00:00
|
|
|
|
2019-01-10 13:36:13 +00:00
|
|
|
def _gh_pull(self, event, page, username, repository, number):
|
2019-01-10 13:32:19 +00:00
|
|
|
repo_from = page.data["head"]["label"]
|
|
|
|
repo_to = page.data["base"]["label"]
|
|
|
|
added = self._added(page.data["additions"])
|
|
|
|
removed = self._removed(page.data["deletions"])
|
|
|
|
url = self._short_url(page.data["html_url"])
|
|
|
|
|
|
|
|
event["stdout"].write(
|
2019-02-16 09:06:29 +00:00
|
|
|
"(%s/%s pull#%s, %s) [%s/%s] %s → %s - %s %s" % (
|
2019-01-10 13:32:19 +00:00
|
|
|
username, repository, number, page.data["state"],
|
|
|
|
added, removed, repo_from, repo_to, page.data["title"], url))
|
|
|
|
def _gh_get_pull(self, username, repository, number):
|
|
|
|
return utils.http.request(
|
|
|
|
API_PULL_URL % (username, repository, number),
|
|
|
|
json=True)
|
2019-01-09 22:45:33 +00:00
|
|
|
@utils.hook("received.command.ghpull", min_args=1)
|
|
|
|
def github_pull(self, event):
|
2019-01-09 23:01:27 +00:00
|
|
|
username, repository, number = self._parse_ref(
|
2019-01-09 23:02:39 +00:00
|
|
|
event["target"], event["args_split"][0])
|
2019-01-10 13:32:19 +00:00
|
|
|
page = self._gh_get_pull(username, repository, number)
|
2019-01-09 22:45:33 +00:00
|
|
|
|
2019-01-10 13:18:58 +00:00
|
|
|
if page and page.code == 200:
|
2019-01-10 13:36:13 +00:00
|
|
|
self._gh_pull(event, page, username, repository, number)
|
2019-01-10 13:18:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Could not find pull request")
|
2019-01-09 22:45:33 +00:00
|
|
|
|
2019-01-13 22:06:41 +00:00
|
|
|
@utils.hook("received.command.gh", alias_of="github")
|
|
|
|
@utils.hook("received.command.github", min_args=1)
|
2019-01-10 13:32:19 +00:00
|
|
|
def github(self, event):
|
|
|
|
username, repository, number = self._parse_ref(
|
|
|
|
event["target"], event["args_split"][0])
|
|
|
|
page = self._gh_get_issue(username, repository, number)
|
|
|
|
if page and page.code == 200:
|
|
|
|
if "pull_request" in page.data:
|
|
|
|
pull = self._gh_get_pull(username, repository, number)
|
2019-01-10 13:36:13 +00:00
|
|
|
self._gh_pull(event, pull, username, repository, number)
|
2019-01-10 13:32:19 +00:00
|
|
|
else:
|
2019-01-10 13:36:13 +00:00
|
|
|
self._gh_issue(event, page, username, repository, number)
|
2019-01-10 13:32:19 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Issue/PR not found")
|
|
|
|
|
2019-02-07 14:01:24 +00:00
|
|
|
@utils.hook("received.command.ghwebhook", min_args=1, channel_only=True)
|
2019-01-18 12:05:56 +00:00
|
|
|
def github_webhook(self, event):
|
|
|
|
"""
|
|
|
|
:help: Add/remove/modify a github webhook
|
|
|
|
:require_mode: high
|
2019-02-21 21:18:07 +00:00
|
|
|
:require_access: github-webhook
|
2019-01-29 08:06:59 +00:00
|
|
|
:permission: githuboverride
|
2019-01-20 13:01:31 +00:00
|
|
|
:usage: list
|
2019-01-18 12:05:56 +00:00
|
|
|
:usage: add <hook>
|
|
|
|
:usage: remove <hook>
|
2019-01-19 07:48:24 +00:00
|
|
|
:usage: events <hook> [category [category ...]]
|
|
|
|
:usage: branches <hook> [branch [branch ...]]
|
2019-01-18 12:05:56 +00:00
|
|
|
"""
|
2019-02-27 14:53:03 +00:00
|
|
|
if event["target"].get_setting("github-hide-prefix", False):
|
2019-02-27 14:52:10 +00:00
|
|
|
event["stdout"].hide_prefix()
|
|
|
|
event["stderr"].hide_prefix()
|
|
|
|
|
2019-01-18 12:05:56 +00:00
|
|
|
all_hooks = event["target"].get_setting("github-hooks", {})
|
2019-02-07 14:01:24 +00:00
|
|
|
hook_name = None
|
2019-01-18 12:05:56 +00:00
|
|
|
existing_hook = None
|
2019-02-07 14:01:24 +00:00
|
|
|
if len(event["args_split"]) > 1:
|
|
|
|
hook_name = event["args_split"][1]
|
|
|
|
for existing_hook_name in all_hooks.keys():
|
|
|
|
if existing_hook_name.lower() == hook_name.lower():
|
|
|
|
existing_hook = existing_hook_name
|
|
|
|
break
|
2019-01-18 12:05:56 +00:00
|
|
|
|
2019-01-20 13:03:36 +00:00
|
|
|
subcommand = event["args_split"][0].lower()
|
2019-01-20 13:01:31 +00:00
|
|
|
if subcommand == "list":
|
|
|
|
event["stdout"].write("Registered web hooks: %s" %
|
|
|
|
", ".join(all_hooks.keys()))
|
|
|
|
elif subcommand == "add":
|
2019-01-18 12:05:56 +00:00
|
|
|
if existing_hook:
|
2019-02-07 14:01:24 +00:00
|
|
|
event["stderr"].write("There's already a hook for %s" %
|
|
|
|
hook_name)
|
2019-01-18 12:05:56 +00:00
|
|
|
return
|
|
|
|
|
2019-02-07 14:01:24 +00:00
|
|
|
all_hooks[hook_name] = {
|
2019-01-18 12:05:56 +00:00
|
|
|
"events": DEFAULT_EVENT_CATEGORIES.copy(),
|
|
|
|
"branches": []
|
|
|
|
}
|
|
|
|
event["target"].set_setting("github-hooks", all_hooks)
|
2019-02-07 14:01:24 +00:00
|
|
|
event["stdout"].write("Added hook for %s" % hook_name)
|
2019-01-20 13:01:31 +00:00
|
|
|
elif subcommand == "remove":
|
2019-01-18 12:05:56 +00:00
|
|
|
if not existing_hook:
|
2019-02-07 14:01:24 +00:00
|
|
|
event["stderr"].write("No hook found for %s" % hook_name)
|
2019-01-18 12:05:56 +00:00
|
|
|
return
|
|
|
|
del all_hooks[existing_hook]
|
2019-01-18 13:25:53 +00:00
|
|
|
if all_hooks:
|
|
|
|
event["target"].set_setting("github-hooks", all_hooks)
|
|
|
|
else:
|
|
|
|
event["target"].del_setting("github-hooks")
|
2019-02-07 14:01:24 +00:00
|
|
|
event["stdout"].write("Removed hook for %s" % hook_name)
|
2019-01-20 13:01:31 +00:00
|
|
|
elif subcommand == "events":
|
2019-01-18 12:05:56 +00:00
|
|
|
if not existing_hook:
|
2019-02-07 14:01:24 +00:00
|
|
|
event["stderr"].write("No hook found for %s" % hook_name)
|
2019-01-18 12:05:56 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if len(event["args_split"]) < 3:
|
|
|
|
event["stdout"].write("Events for hook %s: %s" %
|
2019-02-07 14:01:24 +00:00
|
|
|
(hook_name, " ".join(all_hooks[existing_hook]["events"])))
|
2019-01-18 12:05:56 +00:00
|
|
|
else:
|
|
|
|
new_events = [e.lower() for e in event["args_split"][2:]]
|
|
|
|
all_hooks[existing_hook]["events"] = new_events
|
2019-01-18 12:13:01 +00:00
|
|
|
event["target"].set_setting("github-hooks", all_hooks)
|
2019-02-07 14:01:24 +00:00
|
|
|
event["stdout"].write("Updated events for hook %s" % hook_name)
|
2019-01-20 13:01:31 +00:00
|
|
|
elif subcommand == "branches":
|
2019-01-19 07:48:24 +00:00
|
|
|
if not existing_hook:
|
2019-02-07 14:01:24 +00:00
|
|
|
event["stderr"].write("No hook found for %s" % hook_name)
|
2019-01-19 07:48:24 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if len(event["args_split"]) < 3:
|
2019-02-07 14:01:24 +00:00
|
|
|
branches = ",".join(all_hooks[existing_hook]["branches"])
|
2019-01-19 07:48:24 +00:00
|
|
|
event["stdout"].write("Branches shown for hook %s: %s" %
|
2019-02-07 14:01:24 +00:00
|
|
|
(hook_name, branches))
|
2019-01-19 07:48:24 +00:00
|
|
|
else:
|
|
|
|
all_hooks[existing_hook]["branches"] = event["args_split"][2:]
|
|
|
|
event["target"].set_setting("github-hooks", all_hooks)
|
|
|
|
event["stdout"].write("Updated shown branches for hook %s" %
|
2019-02-07 14:01:24 +00:00
|
|
|
hook_name)
|
2019-01-18 14:21:53 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown command '%s'" %
|
|
|
|
event["args_split"][0])
|
2019-01-18 12:05:56 +00:00
|
|
|
|
2018-10-05 22:29:59 +00:00
|
|
|
@utils.hook("api.post.github")
|
2019-01-09 22:47:20 +00:00
|
|
|
def webhook(self, event):
|
2018-11-30 21:53:47 +00:00
|
|
|
payload = event["data"].decode("utf8")
|
2018-12-06 12:01:23 +00:00
|
|
|
if event["headers"]["Content-Type"] == FORM_ENCODED:
|
2019-01-03 21:55:21 +00:00
|
|
|
payload = urllib.parse.unquote(urllib.parse.parse_qs(payload)[
|
|
|
|
"payload"][0])
|
2018-11-30 21:53:47 +00:00
|
|
|
data = json.loads(payload)
|
2018-10-05 22:29:59 +00:00
|
|
|
|
2018-12-08 09:00:12 +00:00
|
|
|
github_event = event["headers"]["X-GitHub-Event"]
|
2018-11-06 16:08:02 +00:00
|
|
|
|
2019-01-16 22:13:14 +00:00
|
|
|
full_name = None
|
2019-01-16 22:28:12 +00:00
|
|
|
repo_username = None
|
|
|
|
repo_name = None
|
2019-01-16 22:32:17 +00:00
|
|
|
if "repository" in data:
|
2019-01-16 22:13:14 +00:00
|
|
|
full_name = data["repository"]["full_name"]
|
2019-01-16 22:28:12 +00:00
|
|
|
repo_username, repo_name = full_name.split("/", 1)
|
2019-01-16 22:13:14 +00:00
|
|
|
|
|
|
|
organisation = None
|
|
|
|
if "organization" in data:
|
|
|
|
organisation = data["organization"]["login"]
|
|
|
|
|
2019-01-18 08:08:39 +00:00
|
|
|
event_action = None
|
|
|
|
if "action" in data:
|
|
|
|
event_action = "%s/%s" % (github_event, data["action"])
|
|
|
|
|
2019-01-18 12:05:56 +00:00
|
|
|
branch = None
|
|
|
|
if "ref" in data:
|
2019-01-23 08:56:36 +00:00
|
|
|
_, _, branch = data["ref"].rpartition("/")
|
2019-01-18 12:05:56 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
hooks = self.bot.database.channel_settings.find_by_setting(
|
2019-01-18 12:05:56 +00:00
|
|
|
"github-hooks")
|
2018-11-17 22:32:44 +00:00
|
|
|
targets = []
|
|
|
|
|
2018-11-18 10:25:32 +00:00
|
|
|
repo_hooked = False
|
2019-02-04 21:35:31 +00:00
|
|
|
for server_id, channel_name, hooked_repos in hooks:
|
2019-01-18 12:05:56 +00:00
|
|
|
found_hook = None
|
2019-02-04 10:17:27 +00:00
|
|
|
if full_name and full_name in hooked_repos:
|
2019-01-18 12:05:56 +00:00
|
|
|
found_hook = hooked_repos[full_name]
|
2019-02-04 10:17:27 +00:00
|
|
|
elif repo_username and repo_username in hooked_repos:
|
|
|
|
found_hook = hooked_repos[repo_username]
|
2019-01-18 12:05:56 +00:00
|
|
|
elif organisation and organisation in hooked_repos:
|
|
|
|
found_hook = hooked_repos[organisation]
|
|
|
|
|
|
|
|
if found_hook:
|
2018-11-18 10:25:32 +00:00
|
|
|
repo_hooked = True
|
2018-11-17 22:32:44 +00:00
|
|
|
server = self.bot.get_server(server_id)
|
2018-11-17 22:33:31 +00:00
|
|
|
if server and channel_name in server.channels:
|
2019-01-19 07:48:24 +00:00
|
|
|
if (branch and
|
|
|
|
found_hook["branches"] and
|
|
|
|
not branch in found_hook["branches"]):
|
|
|
|
continue
|
2019-01-16 22:26:31 +00:00
|
|
|
|
2019-01-18 08:18:47 +00:00
|
|
|
github_events = []
|
2019-01-18 12:05:56 +00:00
|
|
|
for event in found_hook["events"]:
|
2019-01-18 08:20:03 +00:00
|
|
|
github_events.append(EVENT_CATEGORIES.get(
|
2019-01-18 12:05:56 +00:00
|
|
|
event, [event]))
|
2019-01-18 08:18:47 +00:00
|
|
|
github_events = list(itertools.chain(*github_events))
|
2019-01-16 22:26:31 +00:00
|
|
|
|
2019-01-19 07:48:24 +00:00
|
|
|
channel = server.channels.get(channel_name)
|
2019-01-18 08:08:39 +00:00
|
|
|
if (github_event in github_events or
|
|
|
|
(event_action and event_action in github_events)):
|
2018-11-17 22:35:32 +00:00
|
|
|
targets.append([server, channel])
|
2018-11-17 22:32:44 +00:00
|
|
|
|
|
|
|
if not targets:
|
2019-02-08 23:01:45 +00:00
|
|
|
if not repo_hooked:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return {"state": "success", "deliveries": 0}
|
2018-11-06 13:43:06 +00:00
|
|
|
|
|
|
|
outputs = None
|
|
|
|
if github_event == "push":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.push(full_name, data)
|
2018-11-06 13:43:06 +00:00
|
|
|
elif github_event == "commit_comment":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.commit_comment(full_name, data)
|
2018-11-06 13:43:06 +00:00
|
|
|
elif github_event == "pull_request":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.pull_request(full_name, data)
|
2018-11-06 13:43:06 +00:00
|
|
|
elif github_event == "pull_request_review":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.pull_request_review(full_name, data)
|
2018-11-06 13:43:06 +00:00
|
|
|
elif github_event == "pull_request_review_comment":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.pull_request_review_comment(full_name, data)
|
2018-11-06 13:43:06 +00:00
|
|
|
elif github_event == "issue_comment":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.issue_comment(full_name, data)
|
2018-11-06 13:43:06 +00:00
|
|
|
elif github_event == "issues":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.issues(full_name, data)
|
2018-11-17 20:59:24 +00:00
|
|
|
elif github_event == "create":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.create(full_name, data)
|
2018-11-17 21:10:03 +00:00
|
|
|
elif github_event == "delete":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.delete(full_name, data)
|
2018-11-17 21:21:32 +00:00
|
|
|
elif github_event == "release":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.release(full_name, data)
|
2019-02-07 22:30:50 +00:00
|
|
|
elif github_event == "check_run":
|
|
|
|
outputs = self.check_run(data)
|
2018-12-10 18:47:54 +00:00
|
|
|
elif github_event == "fork":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.fork(full_name, data)
|
2019-01-10 22:14:40 +00:00
|
|
|
elif github_event == "ping":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.ping(data)
|
2019-01-16 22:13:38 +00:00
|
|
|
elif github_event == "membership":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.membership(organisation, data)
|
2019-01-22 20:49:31 +00:00
|
|
|
elif github_event == "watch":
|
2019-01-26 10:33:45 +00:00
|
|
|
outputs = self.watch(data)
|
2018-11-06 13:43:06 +00:00
|
|
|
|
|
|
|
if outputs:
|
2018-11-17 22:39:14 +00:00
|
|
|
for server, channel in targets:
|
2018-11-06 14:02:03 +00:00
|
|
|
for output in outputs:
|
2019-01-16 22:13:14 +00:00
|
|
|
source = full_name or organisation
|
|
|
|
output = "(%s) %s" % (source, output)
|
2019-01-26 10:49:31 +00:00
|
|
|
if channel.get_setting("github-prevent-highlight", False):
|
2019-01-26 10:53:14 +00:00
|
|
|
output = self._prevent_highlight(channel, output)
|
2019-01-26 10:49:31 +00:00
|
|
|
|
2018-11-27 14:25:12 +00:00
|
|
|
self.events.on("send.stdout").call(target=channel,
|
2018-11-27 15:07:22 +00:00
|
|
|
module_name="Github", server=server, message=output,
|
2018-11-27 14:25:12 +00:00
|
|
|
hide_prefix=channel.get_setting(
|
|
|
|
"github-hide-prefix", False))
|
2018-10-06 08:54:21 +00:00
|
|
|
|
2019-02-08 23:01:45 +00:00
|
|
|
return {"state": "success", "deliveries": len(targets)}
|
2018-11-06 14:09:13 +00:00
|
|
|
|
2019-01-26 10:49:31 +00:00
|
|
|
def _prevent_highlight(self, channel, s):
|
|
|
|
for user in channel.users:
|
2019-01-26 10:53:28 +00:00
|
|
|
while user.nickname.lower() in s.lower():
|
|
|
|
index = s.lower().index(user.nickname.lower())
|
|
|
|
length = len(user.nickname.lower())
|
2019-01-26 10:49:31 +00:00
|
|
|
|
|
|
|
original = s[index:index+length]
|
|
|
|
original = utils.prevent_highlight(original)
|
|
|
|
|
|
|
|
s = s[:index] + original + s[index+length:]
|
|
|
|
return s
|
|
|
|
|
2018-12-11 22:27:04 +00:00
|
|
|
def _short_url(self, url):
|
2018-12-14 08:49:37 +00:00
|
|
|
try:
|
|
|
|
page = utils.http.request("https://git.io", method="POST",
|
|
|
|
post_data={"url": url})
|
|
|
|
return page.headers["Location"]
|
|
|
|
except utils.http.HTTPTimeoutException:
|
2019-01-22 22:06:25 +00:00
|
|
|
self.log.warn(
|
|
|
|
"HTTPTimeoutException while waiting for github short URL")
|
2018-12-14 08:49:37 +00:00
|
|
|
return url
|
2018-12-11 22:27:04 +00:00
|
|
|
|
2019-02-07 22:30:50 +00:00
|
|
|
def _iso8601(self, s):
|
2019-02-17 14:15:40 +00:00
|
|
|
return datetime.datetime.strptime(s, utils.ISO8601_PARSE)
|
2019-02-07 22:30:50 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def ping(self, data):
|
2019-01-10 22:24:04 +00:00
|
|
|
return ["Received new webhook"]
|
2019-01-10 22:14:40 +00:00
|
|
|
|
2018-11-16 20:39:37 +00:00
|
|
|
def _change_count(self, n, symbol, color):
|
2018-11-17 08:21:40 +00:00
|
|
|
return utils.irc.color("%s%d" % (symbol, n), color)+utils.irc.bold("")
|
2018-11-16 20:39:37 +00:00
|
|
|
def _added(self, n):
|
2018-11-17 08:20:34 +00:00
|
|
|
return self._change_count(n, "+", utils.consts.GREEN)
|
2018-11-16 20:39:37 +00:00
|
|
|
def _removed(self, n):
|
2018-11-17 08:20:34 +00:00
|
|
|
return self._change_count(n, "-", utils.consts.RED)
|
2018-11-16 20:39:37 +00:00
|
|
|
def _modified(self, n):
|
2018-11-26 14:47:47 +00:00
|
|
|
return self._change_count(n, "~", utils.consts.PURPLE)
|
2018-11-16 20:39:37 +00:00
|
|
|
|
|
|
|
def _short_hash(self, hash):
|
|
|
|
return hash[:8]
|
|
|
|
|
2018-11-17 08:15:28 +00:00
|
|
|
def _flat_unique(self, commits, key):
|
|
|
|
return set(itertools.chain(*(commit[key] for commit in commits)))
|
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def push(self, full_name, data):
|
2018-11-06 13:43:06 +00:00
|
|
|
outputs = []
|
2018-11-18 10:27:19 +00:00
|
|
|
branch = data["ref"].split("/", 2)[2]
|
2018-11-20 12:40:26 +00:00
|
|
|
branch = utils.irc.color(branch, utils.consts.LIGHTBLUE)
|
2018-11-18 10:27:19 +00:00
|
|
|
|
2019-02-10 15:06:26 +00:00
|
|
|
forced = ""
|
|
|
|
if data["forced"]:
|
|
|
|
forced = "%s " % utils.irc.color("force", utils.consts.RED)
|
|
|
|
|
2018-11-17 20:59:24 +00:00
|
|
|
if len(data["commits"]) <= 3:
|
2018-11-16 20:23:34 +00:00
|
|
|
for commit in data["commits"]:
|
2018-11-16 20:39:37 +00:00
|
|
|
id = self._short_hash(commit["id"])
|
2018-11-17 20:33:23 +00:00
|
|
|
message = commit["message"].split("\n")[0].strip()
|
2019-01-19 08:02:34 +00:00
|
|
|
author = utils.irc.bold(data["pusher"]["name"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(COMMIT_URL % (full_name, id))
|
2018-11-06 13:43:06 +00:00
|
|
|
|
2018-11-16 20:39:37 +00:00
|
|
|
added = self._added(len(commit["added"]))
|
|
|
|
removed = self._removed(len(commit["removed"]))
|
2018-11-17 08:22:39 +00:00
|
|
|
modified = self._modified(len(commit["modified"]))
|
2018-11-12 08:44:15 +00:00
|
|
|
|
2019-02-10 15:06:26 +00:00
|
|
|
outputs.append(
|
2019-02-11 11:17:07 +00:00
|
|
|
"%s %spushed to %s [%s/%s/%s files] %s - %s"
|
|
|
|
% (author, forced, branch, added, removed, modified,
|
2019-02-10 15:06:26 +00:00
|
|
|
message, url))
|
2018-11-16 20:39:37 +00:00
|
|
|
else:
|
2018-11-17 08:28:48 +00:00
|
|
|
first_id = self._short_hash(data["before"])
|
2018-11-16 20:39:37 +00:00
|
|
|
last_id = self._short_hash(data["commits"][-1]["id"])
|
2018-11-17 09:40:44 +00:00
|
|
|
pusher = utils.irc.bold(data["pusher"]["name"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(
|
|
|
|
COMMIT_RANGE_URL % (full_name, first_id, last_id))
|
2018-11-16 20:39:37 +00:00
|
|
|
|
2018-11-17 08:15:28 +00:00
|
|
|
commits = data["commits"]
|
|
|
|
added = self._added(len(self._flat_unique(commits, "added")))
|
|
|
|
removed = self._removed(len(self._flat_unique(commits, "removed")))
|
2018-11-17 08:24:31 +00:00
|
|
|
modified = self._modified(len(self._flat_unique(commits,
|
2018-11-17 08:15:28 +00:00
|
|
|
"modified")))
|
|
|
|
|
2019-02-11 11:17:07 +00:00
|
|
|
outputs.append("%s %spushed %d commits to %s [%s/%s/%s files] - %s"
|
|
|
|
% (pusher, forced, len(data["commits"]), branch,
|
|
|
|
added, removed, modified, url))
|
2018-11-12 08:35:16 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
return outputs
|
|
|
|
|
2018-11-06 17:05:40 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def commit_comment(self, full_name, data):
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
2019-02-07 15:46:07 +00:00
|
|
|
commit = self._short_hash(data["comment"]["commit_id"])
|
2018-11-12 17:16:17 +00:00
|
|
|
commenter = utils.irc.bold(data["comment"]["user"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["comment"]["html_url"])
|
2018-11-17 21:44:52 +00:00
|
|
|
return ["[commit/%s] %s commented" % (commit, commenter, action)]
|
2018-11-06 13:43:06 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def pull_request(self, full_name, data):
|
2018-12-29 19:05:14 +00:00
|
|
|
number = data["pull_request"]["number"]
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
2018-11-07 08:23:07 +00:00
|
|
|
action_desc = action
|
2018-11-20 10:27:28 +00:00
|
|
|
branch = data["pull_request"]["base"]["ref"]
|
|
|
|
colored_branch = utils.irc.color(branch, utils.consts.LIGHTBLUE)
|
|
|
|
|
|
|
|
if action == "opened":
|
|
|
|
action_desc = "requested merge into %s" % colored_branch
|
|
|
|
elif action == "closed":
|
2018-11-07 11:47:54 +00:00
|
|
|
if data["pull_request"]["merged"]:
|
2018-11-19 20:56:42 +00:00
|
|
|
action_desc = "%s into %s" % (
|
|
|
|
utils.irc.color("merged", utils.consts.GREEN),
|
2018-11-20 10:27:28 +00:00
|
|
|
colored_branch)
|
2018-11-07 11:47:54 +00:00
|
|
|
else:
|
|
|
|
action_desc = utils.irc.color("closed without merging",
|
2018-11-13 16:02:26 +00:00
|
|
|
utils.consts.RED)
|
2018-11-15 07:06:20 +00:00
|
|
|
elif action == "synchronize":
|
|
|
|
action_desc = "committed to"
|
2018-11-07 08:23:07 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
pr_title = data["pull_request"]["title"]
|
2018-11-12 17:16:17 +00:00
|
|
|
author = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["pull_request"]["html_url"])
|
2018-12-29 19:05:14 +00:00
|
|
|
return ["[pr #%d] %s %s: %s - %s" % (
|
|
|
|
number, author, action_desc, pr_title, url)]
|
2018-11-06 13:43:06 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def pull_request_review(self, full_name, data):
|
2018-11-07 19:21:59 +00:00
|
|
|
if data["review"]["state"] == "commented":
|
|
|
|
return []
|
2019-02-21 11:19:36 +00:00
|
|
|
if not "submitted_at" in data["review"]:
|
|
|
|
return []
|
2018-11-07 19:21:59 +00:00
|
|
|
|
2018-12-29 19:05:14 +00:00
|
|
|
number = data["pull_request"]["number"]
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
|
|
|
pr_title = data["pull_request"]["title"]
|
2018-12-06 18:35:46 +00:00
|
|
|
reviewer = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["review"]["html_url"])
|
2018-12-29 19:05:14 +00:00
|
|
|
return ["[pr #%d] %s %s a review on: %s - %s" % (
|
|
|
|
number, reviewer, action, pr_title, url)]
|
2018-11-06 13:43:06 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def pull_request_review_comment(self, full_name, data):
|
2018-12-29 19:05:14 +00:00
|
|
|
number = data["pull_request"]["number"]
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
|
|
|
pr_title = data["pull_request"]["title"]
|
2019-01-09 22:33:02 +00:00
|
|
|
sender = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["comment"]["html_url"])
|
2018-12-29 19:05:14 +00:00
|
|
|
return ["[pr #%d] %s %s on a review: %s - %s" %
|
|
|
|
(number, sender, COMMENT_ACTIONS[action], pr_title, url)]
|
2018-11-06 13:43:06 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def issues(self, full_name, data):
|
2018-12-29 19:05:14 +00:00
|
|
|
number = data["issue"]["number"]
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
2018-12-09 20:57:29 +00:00
|
|
|
action_desc = action
|
2019-01-17 17:01:12 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
issue_title = data["issue"]["title"]
|
2018-11-12 17:16:17 +00:00
|
|
|
author = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["issue"]["html_url"])
|
2018-12-29 19:05:14 +00:00
|
|
|
return ["[issue #%d] %s %s: %s - %s" %
|
|
|
|
(number, author, action_desc, issue_title, url)]
|
2019-01-26 10:33:45 +00:00
|
|
|
def issue_comment(self, full_name, data):
|
2019-01-13 01:34:07 +00:00
|
|
|
if "changes" in data:
|
|
|
|
# don't show this event when nothing has actually changed
|
|
|
|
if data["changes"]["body"]["from"] == data["comment"]["body"]:
|
|
|
|
return
|
|
|
|
|
2018-12-29 19:05:14 +00:00
|
|
|
number = data["issue"]["number"]
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
2018-11-06 15:01:02 +00:00
|
|
|
issue_title = data["issue"]["title"]
|
|
|
|
type = "pr" if "pull_request" in data["issue"] else "issue"
|
2019-02-26 21:55:34 +00:00
|
|
|
commenter = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["comment"]["html_url"])
|
2018-12-29 19:05:14 +00:00
|
|
|
return ["[%s #%d] %s %s on: %s - %s" %
|
2018-12-29 21:15:50 +00:00
|
|
|
(type, number, commenter, COMMENT_ACTIONS[action], issue_title,
|
2018-11-07 19:27:22 +00:00
|
|
|
url)]
|
2018-11-17 20:59:24 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def create(self, full_name, data):
|
2018-11-19 21:36:24 +00:00
|
|
|
ref = data["ref"]
|
2018-11-20 12:40:26 +00:00
|
|
|
ref_color = utils.irc.color(ref, utils.consts.LIGHTBLUE)
|
2018-11-19 21:34:36 +00:00
|
|
|
type = data["ref_type"]
|
2018-11-17 21:06:27 +00:00
|
|
|
sender = utils.irc.bold(data["sender"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(CREATE_URL % (full_name, ref))
|
2018-11-19 21:36:24 +00:00
|
|
|
return ["%s created a %s: %s - %s" % (sender, type, ref_color, url)]
|
2018-11-17 21:10:03 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def delete(self, full_name, data):
|
2018-11-17 21:10:03 +00:00
|
|
|
ref = data["ref"]
|
|
|
|
type = data["ref_type"]
|
|
|
|
sender = utils.irc.bold(data["sender"]["login"])
|
2018-11-17 21:44:52 +00:00
|
|
|
return ["%s deleted a %s: %s" % (sender, type, ref)]
|
2018-11-17 21:21:32 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def release(self, full_name, data):
|
2018-11-17 21:21:32 +00:00
|
|
|
action = data["action"]
|
2018-11-17 22:17:14 +00:00
|
|
|
tag = data["release"]["tag_name"]
|
2018-11-17 21:21:32 +00:00
|
|
|
name = data["release"]["name"] or ""
|
|
|
|
if name:
|
|
|
|
name = ": %s"
|
|
|
|
author = utils.irc.bold(data["release"]["author"]["login"])
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["release"]["html_url"])
|
2018-11-17 21:44:52 +00:00
|
|
|
return ["%s %s a release%s - %s" % (author, action, name, url)]
|
2018-12-10 17:17:40 +00:00
|
|
|
|
2019-02-07 22:30:50 +00:00
|
|
|
def check_run(self, data):
|
|
|
|
name = data["check_run"]["name"]
|
|
|
|
commit = self._short_hash(data["check_run"]["head_sha"])
|
|
|
|
commit = utils.irc.color(commit, utils.consts.LIGHTBLUE)
|
|
|
|
|
|
|
|
url = ""
|
|
|
|
if data["check_run"]["details_url"]:
|
|
|
|
url = data["check_run"]["details_url"]
|
|
|
|
url = " - %s" % self.exports.get_one("shortlink")(url)
|
|
|
|
|
|
|
|
duration = ""
|
|
|
|
if data["check_run"]["completed_at"]:
|
|
|
|
started_at = self._iso8601(data["check_run"]["started_at"])
|
|
|
|
completed_at = self._iso8601(data["check_run"]["completed_at"])
|
2019-02-07 22:59:50 +00:00
|
|
|
if completed_at > started_at:
|
|
|
|
seconds = (completed_at-started_at).total_seconds()
|
|
|
|
duration = " in %s" % utils.to_pretty_time(seconds)
|
2019-02-07 22:30:50 +00:00
|
|
|
|
|
|
|
status = data["check_run"]["status"]
|
|
|
|
status_str = ""
|
|
|
|
if status == "queued":
|
2019-02-07 23:05:29 +00:00
|
|
|
status_str = utils.irc.bold("queued")
|
2019-02-07 22:30:50 +00:00
|
|
|
elif status == "in_progress":
|
|
|
|
status_str = utils.irc.bold("started")
|
|
|
|
elif status == "completed":
|
|
|
|
conclusion = data["check_run"]["conclusion"]
|
|
|
|
conclusion_color = utils.consts.GREEN
|
|
|
|
if conclusion in CHECK_RUN_FAILURES:
|
|
|
|
conclusion_color = utils.consts.RED
|
|
|
|
if conclusion == "neutral":
|
|
|
|
conclusion_color = utils.consts.LIGHTGREY
|
|
|
|
|
|
|
|
status_str = utils.irc.color(
|
|
|
|
CHECK_RUN_CONCLUSION[conclusion], conclusion_color)
|
|
|
|
|
|
|
|
return ["[build @%s] %s: %s%s%s" % (
|
|
|
|
commit, name, status_str, duration, url)]
|
2018-12-10 18:47:54 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def fork(self, full_name, data):
|
2018-12-10 18:47:54 +00:00
|
|
|
forker = utils.irc.bold(data["sender"]["login"])
|
|
|
|
fork_full_name = utils.irc.color(data["forkee"]["full_name"],
|
|
|
|
utils.consts.LIGHTBLUE)
|
2018-12-11 22:27:04 +00:00
|
|
|
url = self._short_url(data["forkee"]["html_url"])
|
2018-12-10 18:47:54 +00:00
|
|
|
return ["%s forked into %s - %s" %
|
|
|
|
(forker, fork_full_name, url)]
|
2019-01-16 22:13:38 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def membership(self, organisation, data):
|
2019-01-16 22:13:38 +00:00
|
|
|
return ["%s %s %s to team %s" %
|
2019-01-16 22:14:26 +00:00
|
|
|
(data["sender"]["login"], data["action"], data["member"]["login"],
|
2019-01-16 22:13:38 +00:00
|
|
|
data["team"]["name"])]
|
2019-01-22 20:49:31 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def watch(self, data):
|
2019-01-22 20:50:44 +00:00
|
|
|
return ["%s starred the repository" % data["sender"]["login"]]
|