2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on channel_access
|
|
|
|
#--depends-on check_mode
|
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
|
|
|
#--depends-on permissions
|
|
|
|
#--depends-on rest_api
|
|
|
|
|
2019-05-16 13:17:04 +00:00
|
|
|
import datetime, itertools, json, math, re, urllib.parse
|
2019-05-18 10:05:38 +00:00
|
|
|
from src import EventManager, ModuleManager, utils
|
2018-10-05 22:29:59 +00:00
|
|
|
|
2019-02-28 17:09:52 +00:00
|
|
|
COLOR_BRANCH = utils.consts.ORANGE
|
|
|
|
COLOR_REPO = utils.consts.GREY
|
2019-02-28 17:18:47 +00:00
|
|
|
COLOR_POSITIVE = utils.consts.GREEN
|
2019-03-02 21:32:08 +00:00
|
|
|
COLOR_NEUTRAL = utils.consts.LIGHTGREY
|
2019-02-28 17:18:47 +00:00
|
|
|
COLOR_NEGATIVE = utils.consts.RED
|
2019-03-02 08:38:45 +00:00
|
|
|
COLOR_ID = utils.consts.PINK
|
2019-02-28 17:09:52 +00:00
|
|
|
|
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"
|
|
|
|
],
|
2019-03-15 21:13:39 +00:00
|
|
|
"pr-review-minimal": [
|
|
|
|
"pull_request_review/submitted", "pull_request_review/dismissed"
|
|
|
|
],
|
|
|
|
"pr-review-comment-minimal": [
|
|
|
|
"pull_request_review_comment/created",
|
|
|
|
"pull_request_review_comment/deleted"
|
|
|
|
],
|
2019-01-18 08:08:39 +00:00
|
|
|
"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-03-15 21:13:39 +00:00
|
|
|
"issue-comment-minimal": [
|
|
|
|
"issue_comment/created", "issue_comment/deleted"
|
|
|
|
],
|
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",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2019-03-01 21:20:19 +00:00
|
|
|
@utils.export("channelset", {"setting": "github-hide-organisation",
|
|
|
|
"help": "Hide/show organisation in repository names",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2019-01-09 23:01:27 +00:00
|
|
|
@utils.export("channelset", {"setting": "github-default-repo",
|
2019-05-23 09:28:14 +00:00
|
|
|
"help": "Set the default github repo for the current channel",
|
|
|
|
"example": "jesopo/bitbot"})
|
2019-01-26 10:49:31 +00:00
|
|
|
@utils.export("channelset", {"setting": "github-prevent-highlight",
|
|
|
|
"help": "Enable/disable preventing highlights",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2019-05-16 13:17:04 +00:00
|
|
|
@utils.export("channelset", {"setting": "auto-github",
|
|
|
|
"help": "Enable/disable automatically getting github issue/PR info",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2019-06-16 16:05:40 +00:00
|
|
|
@utils.export("channelset", {"setting": "auto-github-cooldown",
|
2019-06-16 15:57:22 +00:00
|
|
|
"help": "Set amount of seconds between auto-github duplicates",
|
|
|
|
"validate": utils.int_or_none, "example": "300"})
|
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-05-17 09:10:27 +00:00
|
|
|
org, _, repo = repo.partition("/")
|
2019-01-09 23:01:27 +00:00
|
|
|
|
2019-05-17 09:10:27 +00:00
|
|
|
default_repo = channel.get_setting("github-default-repo", "")
|
|
|
|
default_org, _, default_repo = default_repo.partition("/")
|
2019-01-09 22:30:11 +00:00
|
|
|
|
2019-05-17 09:14:49 +00:00
|
|
|
if org and not repo:
|
|
|
|
repo = org or default_repo
|
2019-05-17 09:28:34 +00:00
|
|
|
org = default_org
|
2019-05-17 09:14:49 +00:00
|
|
|
else:
|
|
|
|
org = org or default_org
|
|
|
|
repo = repo or default_repo
|
2019-05-17 09:10:27 +00:00
|
|
|
|
|
|
|
if not org or not repo or not number:
|
2019-01-09 22:30:11 +00:00
|
|
|
raise utils.EventError("Please provide username/repo#number")
|
|
|
|
if not number.isdigit():
|
|
|
|
raise utils.EventError("Issue number must be a number")
|
2019-05-17 09:10:27 +00:00
|
|
|
return org, repo, number
|
2019-01-09 23:01:27 +00:00
|
|
|
|
2019-05-16 13:20:50 +00:00
|
|
|
def _parse_issue(self, page, username, repository, number):
|
2019-03-02 21:32:08 +00:00
|
|
|
repo = utils.irc.color("%s/%s" % (username, repository), COLOR_REPO)
|
|
|
|
number = utils.irc.color("#%s" % number, COLOR_ID)
|
2019-01-10 13:32:19 +00:00
|
|
|
labels = [label["name"] for label in page.data["labels"]]
|
2019-03-02 21:14:51 +00:00
|
|
|
labels_str = ""
|
|
|
|
if labels:
|
|
|
|
labels_str = "[%s] " % ", ".join(labels)
|
|
|
|
|
2019-01-10 13:32:19 +00:00
|
|
|
url = self._short_url(page.data["html_url"])
|
|
|
|
|
2019-03-02 21:32:08 +00:00
|
|
|
state = page.data["state"]
|
|
|
|
if state == "open":
|
|
|
|
state = utils.irc.color("open", COLOR_NEUTRAL)
|
|
|
|
elif state == "closed":
|
|
|
|
state = utils.irc.color("closed", COLOR_NEGATIVE)
|
|
|
|
|
2019-05-16 13:17:04 +00:00
|
|
|
return "(%s issue%s, %s) %s %s%s" % (
|
|
|
|
repo, number, state, page.data["title"], labels_str, url)
|
|
|
|
def _get_issue(self, username, repository, number):
|
2019-01-10 13:32:19 +00:00
|
|
|
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):
|
2019-02-27 14:56:20 +00:00
|
|
|
if event["target"].get_setting("github-hide-prefix", False):
|
|
|
|
event["stdout"].hide_prefix()
|
|
|
|
event["stderr"].hide_prefix()
|
|
|
|
|
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-09 22:30:11 +00:00
|
|
|
|
2019-05-16 13:18:42 +00:00
|
|
|
page = self._get_issue(username, repository, number)
|
2019-01-10 13:18:58 +00:00
|
|
|
if page and page.code == 200:
|
2019-05-16 13:20:50 +00:00
|
|
|
self._parse_issue(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-05-16 13:20:50 +00:00
|
|
|
def _parse_pull(self, page, username, repository, number):
|
2019-03-02 08:29:45 +00:00
|
|
|
repo = utils.irc.color("%s/%s" % (username, repository), COLOR_REPO)
|
2019-03-02 08:44:32 +00:00
|
|
|
number = utils.irc.color("#%s" % number, COLOR_ID)
|
2019-03-02 08:29:45 +00:00
|
|
|
branch_from = page.data["head"]["label"]
|
|
|
|
branch_to = page.data["base"]["label"]
|
2019-01-10 13:32:19 +00:00
|
|
|
added = self._added(page.data["additions"])
|
|
|
|
removed = self._removed(page.data["deletions"])
|
|
|
|
url = self._short_url(page.data["html_url"])
|
|
|
|
|
2019-03-02 08:38:56 +00:00
|
|
|
state = page.data["state"]
|
|
|
|
if page.data["merged"]:
|
2019-03-02 21:32:08 +00:00
|
|
|
state = utils.irc.color("merged", COLOR_POSITIVE)
|
2019-03-02 08:38:56 +00:00
|
|
|
elif state == "open":
|
2019-03-02 21:32:08 +00:00
|
|
|
state = utils.irc.color("open", COLOR_NEUTRAL)
|
2019-03-02 08:38:56 +00:00
|
|
|
elif state == "closed":
|
2019-03-02 21:32:08 +00:00
|
|
|
state = utils.irc.color("closed", COLOR_NEGATIVE)
|
2019-03-02 08:38:56 +00:00
|
|
|
|
2019-05-16 13:17:04 +00:00
|
|
|
return "(%s PR%s, %s) %s → %s [%s/%s] %s %s" % (
|
2019-03-02 21:32:08 +00:00
|
|
|
repo, number, state, branch_from, branch_to, added, removed,
|
2019-05-16 13:17:04 +00:00
|
|
|
page.data["title"], url)
|
|
|
|
def _get_pull(self, username, repository, number):
|
2019-01-10 13:32:19 +00:00
|
|
|
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-02-27 14:56:20 +00:00
|
|
|
if event["target"].get_setting("github-hide-prefix", False):
|
|
|
|
event["stdout"].hide_prefix()
|
|
|
|
event["stderr"].hide_prefix()
|
|
|
|
|
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-05-16 13:18:42 +00:00
|
|
|
page = self._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-05-16 13:20:50 +00:00
|
|
|
self._parse_pull(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-05-16 13:17:04 +00:00
|
|
|
def _get_info(self, target, ref):
|
|
|
|
username, repository, number = self._parse_ref(target, ref)
|
2019-05-16 13:18:42 +00:00
|
|
|
page = self._get_issue(username, repository, number)
|
2019-05-16 13:17:04 +00:00
|
|
|
if page and page.code == 200:
|
|
|
|
if "pull_request" in page.data:
|
2019-05-16 13:18:42 +00:00
|
|
|
pull = self._get_pull(username, repository, number)
|
2019-05-16 13:20:50 +00:00
|
|
|
return self._parse_pull(pull, username, repository, number)
|
2019-05-16 13:17:04 +00:00
|
|
|
else:
|
2019-05-16 13:20:50 +00:00
|
|
|
return self._parse_issue(page, username, repository, number)
|
2019-05-16 13:17:04 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
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):
|
2019-02-27 14:55:28 +00:00
|
|
|
if event["target"].get_setting("github-hide-prefix", False):
|
|
|
|
event["stdout"].hide_prefix()
|
|
|
|
event["stderr"].hide_prefix()
|
2019-05-16 13:17:04 +00:00
|
|
|
result = self._get_info(event["target"], event["args_split"][0])
|
|
|
|
if not result == None:
|
|
|
|
event["stdout"].write(result)
|
2019-01-10 13:32:19 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Issue/PR not found")
|
|
|
|
|
2019-06-16 15:57:22 +00:00
|
|
|
def _cache_ref(self, ref):
|
|
|
|
return "auto-github-%s" % ref.lower()
|
2019-06-16 16:05:40 +00:00
|
|
|
def _auto_github_cooldown(self, channel, ref):
|
|
|
|
cooldown = channel.get_setting("auto-github-cooldown", None)
|
|
|
|
if not cooldown == None:
|
2019-06-16 15:57:22 +00:00
|
|
|
cache = self._cache_ref(ref)
|
|
|
|
if not self.bot.cache.has_item(cache):
|
2019-06-16 16:05:40 +00:00
|
|
|
self.bot.cache.temporary_cache(cache, cooldown)
|
2019-06-16 15:57:22 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2019-06-16 17:40:25 +00:00
|
|
|
@utils.hook("command.regex", ignore_action=False)
|
2019-05-21 08:54:18 +00:00
|
|
|
def url_regex(self, event):
|
|
|
|
"""
|
|
|
|
:command: github
|
|
|
|
:pattern: https?://github.com/([^/]+)/([^/]+)/(pull|issues)/(\d+)
|
|
|
|
"""
|
2019-05-21 12:53:36 +00:00
|
|
|
if event["target"].get_setting("auto-github", False):
|
2019-05-22 21:31:03 +00:00
|
|
|
event.eat()
|
2019-05-22 18:03:51 +00:00
|
|
|
ref = "%s/%s#%s" % (event["match"].group(1),
|
|
|
|
event["match"].group(2), event["match"].group(4))
|
2019-06-16 16:05:40 +00:00
|
|
|
if self._auto_github_cooldown(event["target"], ref):
|
2019-06-16 15:57:22 +00:00
|
|
|
try:
|
|
|
|
result = self._get_info(event["target"], ref)
|
|
|
|
except utils.EventError:
|
|
|
|
return
|
|
|
|
if result:
|
|
|
|
if event["target"].get_setting("github-hide-prefix", False):
|
|
|
|
event["stdout"].hide_prefix()
|
|
|
|
event["stdout"].write(result)
|
2019-05-21 08:54:18 +00:00
|
|
|
|
2019-06-16 17:40:25 +00:00
|
|
|
@utils.hook("command.regex", ignore_action=False)
|
2019-05-21 08:54:18 +00:00
|
|
|
def ref_regex(self, event):
|
|
|
|
"""
|
|
|
|
:command: github
|
|
|
|
:pattern: (?:\S+(?:\/\S+)?)?#\d+
|
|
|
|
"""
|
2019-05-21 12:53:36 +00:00
|
|
|
if event["target"].get_setting("auto-github", False):
|
2019-05-22 21:31:03 +00:00
|
|
|
event.eat()
|
2019-06-16 15:57:22 +00:00
|
|
|
ref = event["match"].group(0)
|
2019-06-16 16:05:40 +00:00
|
|
|
if self._auto_github_cooldown(event["target"], ref):
|
2019-06-16 15:57:22 +00:00
|
|
|
try:
|
|
|
|
result = self._get_info(event["target"],
|
|
|
|
event["match"].group(0))
|
|
|
|
except utils.EventError:
|
|
|
|
return
|
|
|
|
if result:
|
|
|
|
if event["target"].get_setting("github-hide-prefix", False):
|
|
|
|
event["stdout"].hide_prefix()
|
|
|
|
event["stdout"].write(result)
|
2019-05-16 13:17:04 +00:00
|
|
|
|
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
|
|
|
"""
|
|
|
|
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
|
2019-04-24 11:07:30 +00:00
|
|
|
server = self.bot.get_server_by_id(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-03-01 21:47:00 +00:00
|
|
|
for hooked_event in found_hook["events"]:
|
2019-01-18 08:20:03 +00:00
|
|
|
github_events.append(EVENT_CATEGORIES.get(
|
2019-03-01 21:47:00 +00:00
|
|
|
hooked_event, [hooked_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
|
|
|
|
2019-03-01 21:20:19 +00:00
|
|
|
|
2018-11-06 13:43:06 +00:00
|
|
|
if outputs:
|
2018-11-17 22:39:14 +00:00
|
|
|
for server, channel in targets:
|
2019-03-01 22:21:07 +00:00
|
|
|
source = full_name or organisation
|
|
|
|
hide_org = channel.get_setting(
|
|
|
|
"github-hide-organisation", False)
|
|
|
|
if repo_name and hide_org:
|
|
|
|
source = repo_name
|
|
|
|
|
2018-11-06 14:02:03 +00:00
|
|
|
for output in outputs:
|
2019-02-28 17:10:31 +00:00
|
|
|
output = "(%s) %s" % (
|
|
|
|
utils.irc.color(source, COLOR_REPO), output)
|
|
|
|
|
2019-01-26 10:49:31 +00:00
|
|
|
if channel.get_setting("github-prevent-highlight", False):
|
2019-05-14 09:24:31 +00:00
|
|
|
output = self._prevent_highlight(server, 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-05-14 09:24:31 +00:00
|
|
|
def _prevent_highlight(self, server, channel, s):
|
2019-01-26 10:49:31 +00:00
|
|
|
for user in channel.users:
|
2019-05-14 09:50:47 +00:00
|
|
|
if len(user.nickname) == 1:
|
2019-05-14 10:01:31 +00:00
|
|
|
# if we don't ignore 1-letter nicknames, the below while loop
|
|
|
|
# will fire indefininitely.
|
2019-05-14 09:50:47 +00:00
|
|
|
continue
|
|
|
|
|
2019-05-24 20:33:01 +00:00
|
|
|
regex = re.compile(r"(.)\b(%s)(%s)" % (
|
2019-05-28 07:18:42 +00:00
|
|
|
re.escape(user.nickname[0]), re.escape(user.nickname[1:])),
|
|
|
|
re.I)
|
2019-05-24 20:33:01 +00:00
|
|
|
s = regex.sub("\\1\\2\u200c\\3", s)
|
2019-01-26 10:49:31 +00:00
|
|
|
|
|
|
|
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(
|
2019-05-04 07:26:27 +00:00
|
|
|
"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):
|
2019-02-28 17:09:52 +00:00
|
|
|
return self._change_count(n, "+", COLOR_POSITIVE)
|
2018-11-16 20:39:37 +00:00
|
|
|
def _removed(self, n):
|
2019-02-28 17:09:52 +00:00
|
|
|
return self._change_count(n, "-", COLOR_NEGATIVE)
|
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]
|
2019-02-28 17:09:52 +00:00
|
|
|
branch = utils.irc.color(branch, COLOR_BRANCH)
|
2019-04-16 09:20:11 +00:00
|
|
|
author = utils.irc.bold(data["pusher"]["name"])
|
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)
|
|
|
|
|
2019-04-16 09:24:55 +00:00
|
|
|
if len(data["commits"]) == 0 and data["forced"]:
|
2019-04-16 09:20:11 +00:00
|
|
|
outputs.append(
|
|
|
|
"%s %spushed to %s" % (author, forced, branch))
|
|
|
|
elif len(data["commits"]) <= 3:
|
2018-11-16 20:23:34 +00:00
|
|
|
for commit in data["commits"]:
|
2019-03-14 13:58:42 +00:00
|
|
|
hash = commit["id"]
|
|
|
|
hash_colored = utils.irc.color(self._short_hash(hash), COLOR_ID)
|
2018-11-17 20:33:23 +00:00
|
|
|
message = commit["message"].split("\n")[0].strip()
|
2019-03-14 13:58:42 +00:00
|
|
|
url = self._short_url(COMMIT_URL % (full_name, hash))
|
2018-11-06 13:43:06 +00:00
|
|
|
|
2019-02-10 15:06:26 +00:00
|
|
|
outputs.append(
|
2019-03-10 10:31:32 +00:00
|
|
|
"%s %spushed %s to %s: %s - %s"
|
2019-03-14 13:58:42 +00:00
|
|
|
% (author, forced, hash_colored, branch, message, url))
|
2018-11-16 20:39:37 +00:00
|
|
|
else:
|
2019-02-28 17:21:19 +00:00
|
|
|
first_id = data["before"]
|
|
|
|
last_id = data["commits"][-1]["id"]
|
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
|
|
|
|
2019-03-03 22:49:06 +00:00
|
|
|
outputs.append("%s %spushed %d commits to %s - %s"
|
2019-04-16 09:20:11 +00:00
|
|
|
% (author, forced, len(data["commits"]), branch, 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"])
|
2019-06-05 10:40:23 +00:00
|
|
|
return ["[commit/%s] %s %s a comment - %s" % (commit, commenter,
|
|
|
|
action, url)]
|
2018-11-06 13:43:06 +00:00
|
|
|
|
2019-01-26 10:33:45 +00:00
|
|
|
def pull_request(self, full_name, data):
|
2019-02-28 17:10:42 +00:00
|
|
|
number = utils.irc.color("#%s" % data["pull_request"]["number"],
|
|
|
|
COLOR_ID)
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
2019-03-07 08:35:29 +00:00
|
|
|
action_desc = "%s %s" % (action, number)
|
2018-11-20 10:27:28 +00:00
|
|
|
branch = data["pull_request"]["base"]["ref"]
|
2019-02-28 17:09:52 +00:00
|
|
|
colored_branch = utils.irc.color(branch, COLOR_BRANCH)
|
2018-11-20 10:27:28 +00:00
|
|
|
|
|
|
|
if action == "opened":
|
2019-03-07 08:35:29 +00:00
|
|
|
action_desc = "requested %s merge into %s" % (number,
|
|
|
|
colored_branch)
|
2018-11-20 10:27:28 +00:00
|
|
|
elif action == "closed":
|
2018-11-07 11:47:54 +00:00
|
|
|
if data["pull_request"]["merged"]:
|
2019-03-07 08:35:29 +00:00
|
|
|
action_desc = "%s %s into %s" % (
|
|
|
|
utils.irc.color("merged", COLOR_POSITIVE), number,
|
2018-11-20 10:27:28 +00:00
|
|
|
colored_branch)
|
2018-11-07 11:47:54 +00:00
|
|
|
else:
|
2019-04-11 12:09:30 +00:00
|
|
|
action_desc = "%s %s" % (
|
|
|
|
utils.irc.color("closed", COLOR_NEGATIVE), number)
|
2019-06-07 10:29:11 +00:00
|
|
|
elif action == "ready_for_review":
|
|
|
|
action_desc = "marked %s ready for review" % number
|
2018-11-15 07:06:20 +00:00
|
|
|
elif action == "synchronize":
|
2019-03-07 08:35:29 +00:00
|
|
|
action_desc = "committed to %s" % number
|
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"])
|
2019-03-07 08:35:29 +00:00
|
|
|
return ["[PR] %s %s: %s - %s" % (
|
|
|
|
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):
|
2019-04-17 09:33:12 +00:00
|
|
|
if not data["action"] == "submitted":
|
|
|
|
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
|
|
|
|
2019-03-15 10:33:02 +00:00
|
|
|
state = data["review"]["state"]
|
2019-03-19 16:10:59 +00:00
|
|
|
if state == "commented":
|
|
|
|
return []
|
|
|
|
|
2019-02-28 17:10:42 +00:00
|
|
|
number = utils.irc.color("#%s" % data["pull_request"]["number"],
|
|
|
|
COLOR_ID)
|
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"])
|
2019-03-15 10:33:02 +00:00
|
|
|
|
|
|
|
state_desc = state
|
|
|
|
if state == "approved":
|
|
|
|
state_desc = "approved changes"
|
|
|
|
elif state == "changes_requested":
|
|
|
|
state_desc = "requested changes"
|
|
|
|
elif state == "dismissed":
|
|
|
|
state_desc = "dismissed a review"
|
|
|
|
|
|
|
|
return ["[PR] %s %s on %s: %s - %s" %
|
2019-03-15 19:28:37 +00:00
|
|
|
(reviewer, state_desc, number, 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):
|
2019-02-28 17:10:42 +00:00
|
|
|
number = utils.irc.color("#%s" % data["pull_request"]["number"],
|
|
|
|
COLOR_ID)
|
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"])
|
2019-02-28 23:16:37 +00:00
|
|
|
return ["[PR] %s %s on a review on %s: %s - %s" %
|
2019-02-28 23:15:13 +00:00
|
|
|
(sender, COMMENT_ACTIONS[action], number, 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):
|
2019-02-28 17:10:42 +00:00
|
|
|
number = utils.irc.color("#%s" % data["issue"]["number"], COLOR_ID)
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
|
|
|
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"])
|
2019-02-28 23:15:13 +00:00
|
|
|
return ["[issue] %s %s %s: %s - %s" %
|
|
|
|
(author, action, number, 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
|
|
|
|
|
2019-02-28 17:09:52 +00:00
|
|
|
number = utils.irc.color("#%s" % data["issue"]["number"], COLOR_ID)
|
2018-11-06 13:43:06 +00:00
|
|
|
action = data["action"]
|
2018-11-06 15:01:02 +00:00
|
|
|
issue_title = data["issue"]["title"]
|
2019-03-01 21:12:46 +00:00
|
|
|
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"])
|
2019-02-28 23:15:13 +00:00
|
|
|
return ["[%s] %s %s on %s: %s - %s" %
|
|
|
|
(type, commenter, COMMENT_ACTIONS[action], number, 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"]
|
2019-02-28 17:09:52 +00:00
|
|
|
ref_color = utils.irc.color(ref, COLOR_BRANCH)
|
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"]
|
2019-06-22 21:42:52 +00:00
|
|
|
ref_color = utils.irc.color(ref, COLOR_BRANCH)
|
2018-11-17 21:10:03 +00:00
|
|
|
type = data["ref_type"]
|
|
|
|
sender = utils.irc.bold(data["sender"]["login"])
|
2019-06-22 21:42:52 +00:00
|
|
|
return ["%s deleted a %s: %s" % (sender, type, ref_color)]
|
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:
|
2019-05-12 21:09:19 +00:00
|
|
|
name = ": %s" % name
|
2018-11-17 21:21:32 +00:00
|
|
|
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"]
|
2019-02-28 17:09:52 +00:00
|
|
|
conclusion_color = COLOR_POSITIVE
|
2019-02-07 22:30:50 +00:00
|
|
|
if conclusion in CHECK_RUN_FAILURES:
|
2019-02-28 17:09:52 +00:00
|
|
|
conclusion_color = COLOR_NEGATIVE
|
2019-02-07 22:30:50 +00:00
|
|
|
if conclusion == "neutral":
|
2019-03-02 21:32:08 +00:00
|
|
|
conclusion_color = COLOR_NEUTRAL
|
2019-02-07 22:30:50 +00:00
|
|
|
|
|
|
|
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"]]
|