2019-10-15 15:04:21 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
from . import colors
|
|
|
|
|
|
|
|
EVENT_CATEGORIES = {
|
|
|
|
"ping": [
|
|
|
|
"ping" # new webhook received
|
|
|
|
],
|
|
|
|
"code": ["push"],
|
|
|
|
"pr-minimal": [
|
2019-10-18 12:28:24 +00:00
|
|
|
"merge_request/open", "merge_request/close", "merge_request/reopen",
|
|
|
|
"merge_request/merge"
|
2019-10-15 15:04:21 +00:00
|
|
|
],
|
|
|
|
"pr": [
|
2019-10-18 10:58:10 +00:00
|
|
|
"merge_request/open", "merge_request/close", "merge_request/reopen",
|
2019-10-18 12:28:24 +00:00
|
|
|
"merge_request/update", "merge_request/merge"
|
2019-10-15 15:04:21 +00:00
|
|
|
],
|
2019-10-18 02:52:59 +00:00
|
|
|
"pr-all": ["merge_request"],
|
2019-10-15 15:04:21 +00:00
|
|
|
"issue-minimal": [
|
2019-10-28 11:17:56 +00:00
|
|
|
"issue/open", "issue/close", "issue/reopen", "confidential_issue/open",
|
|
|
|
"confidential_issue/close", "confidential_issue/reopen"
|
2019-10-15 15:04:21 +00:00
|
|
|
],
|
2019-10-18 10:58:10 +00:00
|
|
|
"issue": [
|
|
|
|
"issue/open", "issue/close", "issue/reopen", "issue/update",
|
2019-10-28 11:17:56 +00:00
|
|
|
"confidential_issue/open", "confidential_issue/close",
|
|
|
|
"confidential_issue/reopen", "confidential_issue/update",
|
2019-10-15 15:04:21 +00:00
|
|
|
],
|
|
|
|
"issue-all": [
|
2019-10-28 11:17:56 +00:00
|
|
|
"issue", "issue_comment", "confidential_issue"
|
2019-10-15 15:04:21 +00:00
|
|
|
],
|
|
|
|
"issue-comment-minimal": [
|
|
|
|
"issue_comment/created", "issue_comment/deleted"
|
|
|
|
],
|
2019-10-20 11:51:11 +00:00
|
|
|
"repo": ["tag_push"]
|
2019-10-15 15:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
COMMENT_ACTIONS = {
|
|
|
|
"created": "commented",
|
|
|
|
"edited": "edited a comment",
|
|
|
|
"deleted": "deleted a comment"
|
|
|
|
}
|
|
|
|
|
2019-10-18 10:19:16 +00:00
|
|
|
ISSUE_ACTIONS = {
|
|
|
|
"open": "opened",
|
|
|
|
"close": "closed",
|
|
|
|
"reopen": "reopened",
|
2019-10-18 12:28:24 +00:00
|
|
|
"update": "updated",
|
|
|
|
"merge": "merged"
|
2019-10-18 10:19:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 15:04:21 +00:00
|
|
|
class GitLab(object):
|
|
|
|
def is_private(self, data, headers):
|
|
|
|
if "project" in data:
|
|
|
|
return not data["project"]["visibility_level"] == 20
|
|
|
|
return False
|
|
|
|
|
|
|
|
def names(self, data, headers):
|
|
|
|
full_name = data["project"]["path_with_namespace"]
|
2019-10-25 21:46:02 +00:00
|
|
|
repo_username, repo_name = full_name.split("/", 1)
|
2019-10-15 15:04:21 +00:00
|
|
|
|
2019-10-25 21:36:27 +00:00
|
|
|
organisation = None
|
|
|
|
if full_name.count("/") == 2:
|
|
|
|
organisation = repo_username
|
|
|
|
repo_username = full_name.rsplit("/", 1)[0]
|
|
|
|
|
|
|
|
return full_name, repo_username, repo_name, organisation
|
2019-10-15 15:04:21 +00:00
|
|
|
|
|
|
|
def branch(self, data, headers):
|
|
|
|
if "ref" in data:
|
|
|
|
return data["ref"].rpartition("/")[2]
|
|
|
|
return None
|
|
|
|
|
|
|
|
def event(self, data, headers):
|
|
|
|
event = headers["X-GitLab-Event"].rsplit(" ", 1)[0].lower()
|
2019-10-28 14:10:42 +00:00
|
|
|
|
2019-10-15 15:04:21 +00:00
|
|
|
event = event.replace(" ", "_")
|
|
|
|
event_action = None
|
2019-10-28 14:10:42 +00:00
|
|
|
|
|
|
|
category = None
|
|
|
|
category_action = None
|
|
|
|
|
|
|
|
if "object_attributes" in data:
|
|
|
|
if "action" in data["object_attributes"]:
|
|
|
|
event_action = "%s/%s" % (
|
|
|
|
event, data["object_attributes"]["action"])
|
|
|
|
if "noteable_type" in data["object_attributes"]:
|
|
|
|
category = data["object_attributes"]["noteable_type"].lower()
|
|
|
|
category = "%s+%s" % (event, category)
|
2019-10-28 14:12:20 +00:00
|
|
|
category_action = "%s/%s" % (category, event_action)
|
2019-10-28 14:10:42 +00:00
|
|
|
|
|
|
|
return list(filter([event, event_action, category, category_action]))
|
2019-10-15 15:04:21 +00:00
|
|
|
|
|
|
|
def event_categories(self, event):
|
|
|
|
return EVENT_CATEGORIES.get(event, [event])
|
|
|
|
|
|
|
|
def webhook(self, full_name, event, data, headers):
|
|
|
|
if event == "push":
|
|
|
|
return self.push(full_name, data)
|
2019-10-15 15:14:15 +00:00
|
|
|
elif event == "merge_request":
|
2019-10-18 02:52:59 +00:00
|
|
|
return self.merge_request(full_name, data)
|
2019-10-28 11:20:28 +00:00
|
|
|
elif event in ["issue", "confidential_issue"]:
|
2019-10-15 15:04:21 +00:00
|
|
|
return self.issues(full_name, data)
|
|
|
|
elif event == "note":
|
|
|
|
return self.note(full_name, data)
|
2019-10-20 11:51:11 +00:00
|
|
|
elif event == "tag_push":
|
|
|
|
return self.tag_push(full_name, data)
|
2019-10-15 15:04:21 +00:00
|
|
|
|
|
|
|
def _short_hash(self, hash):
|
|
|
|
return hash[:8]
|
|
|
|
|
2019-10-20 11:51:11 +00:00
|
|
|
def tag_push(self, full_name, data):
|
2019-10-20 12:39:04 +00:00
|
|
|
create = not data["after"].strip("0") == ""
|
2019-10-20 11:51:11 +00:00
|
|
|
tag = utils.irc.color(data["ref"].rsplit("/", 1)[-1],
|
|
|
|
colors.COLOR_BRANCH)
|
|
|
|
author = utils.irc.bold(data["user_username"])
|
|
|
|
action = "created" if create else "deleted"
|
|
|
|
|
2019-10-20 12:36:26 +00:00
|
|
|
return [["%s %s a tag: %s" % (author, action, tag), None]]
|
2019-10-20 11:51:11 +00:00
|
|
|
|
2019-10-15 15:04:21 +00:00
|
|
|
def push(self, full_name, data):
|
|
|
|
outputs = []
|
|
|
|
branch = data["ref"].rpartition("/")[2]
|
|
|
|
branch = utils.irc.color(branch, colors.COLOR_BRANCH)
|
|
|
|
author = utils.irc.bold(data["user_username"])
|
|
|
|
|
|
|
|
if len(data["commits"]) <= 3:
|
|
|
|
for commit in data["commits"]:
|
|
|
|
hash = commit["id"]
|
|
|
|
hash_colored = utils.irc.color(self._short_hash(hash),
|
|
|
|
colors.COLOR_ID)
|
|
|
|
message = commit["message"].split("\n")[0].strip()
|
|
|
|
url = commit["url"]
|
|
|
|
|
2019-10-18 13:07:34 +00:00
|
|
|
outputs.append(["%s pushed %s to %s: %s"
|
2019-10-18 13:03:20 +00:00
|
|
|
% (author, hash_colored, branch, message), url])
|
2019-10-15 15:04:21 +00:00
|
|
|
else:
|
|
|
|
first_id = data["before"]
|
|
|
|
last_id = data["after"]
|
|
|
|
url = data["compare_url"]
|
|
|
|
|
2019-10-18 13:03:20 +00:00
|
|
|
outputs.append(["%s pushed %d commits to %s"
|
|
|
|
% (author, len(data["commits"]), branch), None])
|
2019-10-15 15:04:21 +00:00
|
|
|
|
|
|
|
return outputs
|
|
|
|
|
|
|
|
def merge_request(self, full_name, data):
|
2019-10-18 11:20:23 +00:00
|
|
|
number = utils.irc.color("!%s" % data["object_attributes"]["iid"],
|
2019-10-15 15:04:21 +00:00
|
|
|
colors.COLOR_ID)
|
2019-10-18 12:31:21 +00:00
|
|
|
action = data["object_attributes"]["action"]
|
|
|
|
action_desc = "%s %s" % (ISSUE_ACTIONS[action], number)
|
2019-10-15 15:04:21 +00:00
|
|
|
branch = data["object_attributes"]["target_branch"]
|
|
|
|
colored_branch = utils.irc.color(branch, colors.COLOR_BRANCH)
|
|
|
|
|
|
|
|
if action == "open":
|
|
|
|
action_desc = "requested %s merge into %s" % (number,
|
|
|
|
colored_branch)
|
|
|
|
elif action == "close":
|
2019-10-18 12:31:21 +00:00
|
|
|
action_desc = "%s %s" % (
|
|
|
|
utils.irc.color("closed", colors.COLOR_NEGATIVE), number)
|
|
|
|
elif action == "merge":
|
|
|
|
action_desc = "%s %s into %s" % (
|
|
|
|
utils.irc.color("merged", colors.COLOR_POSITIVE), number,
|
|
|
|
colored_branch)
|
2019-10-15 15:04:21 +00:00
|
|
|
|
|
|
|
pr_title = data["object_attributes"]["title"]
|
|
|
|
author = utils.irc.bold(data["user"]["username"])
|
|
|
|
url = data["object_attributes"]["url"]
|
2019-10-18 13:03:20 +00:00
|
|
|
return [["[MR] %s %s: %s" % (author, action_desc, pr_title), url]]
|
2019-10-15 15:04:21 +00:00
|
|
|
|
|
|
|
def issues(self, full_name, data):
|
|
|
|
number = utils.irc.color("#%s" % data["object_attributes"]["iid"],
|
|
|
|
colors.COLOR_ID)
|
2019-10-18 10:19:16 +00:00
|
|
|
action = ISSUE_ACTIONS[data["object_attributes"]["action"]]
|
2019-10-17 10:50:57 +00:00
|
|
|
issue_title = data["object_attributes"]["title"]
|
2019-10-15 15:04:21 +00:00
|
|
|
author = utils.irc.bold(data["user"]["username"])
|
|
|
|
url = data["object_attributes"]["url"]
|
|
|
|
|
2019-10-18 13:07:34 +00:00
|
|
|
return [["[issue] %s %s %s: %s" %
|
2019-10-18 13:03:20 +00:00
|
|
|
(author, action, number, issue_title), url]]
|
2019-10-15 15:04:21 +00:00
|
|
|
|
|
|
|
def note(self, full_name, data):
|
|
|
|
type = data["object_attributes"]["noteable_type"]
|
|
|
|
if type in ["Issue", "MergeRequest"]:
|
2019-10-28 12:37:21 +00:00
|
|
|
return self.issue_note(full_name, data)
|
2019-10-15 15:04:21 +00:00
|
|
|
|
|
|
|
def issue_note(self, full_name, data):
|
2019-10-28 12:38:29 +00:00
|
|
|
number = utils.irc.color("#%s" % data["issue"]["iid"],
|
2019-10-15 15:04:21 +00:00
|
|
|
colors.COLOR_ID)
|
|
|
|
type = data["object_attributes"]["noteable_type"]
|
2019-10-18 11:02:56 +00:00
|
|
|
type == "issue" if type == "Issue" else "MR"
|
2019-10-15 15:04:21 +00:00
|
|
|
|
|
|
|
issue_title = data["issue"]["title"]
|
|
|
|
commenter = utils.irc.bold(data["user"]["username"])
|
|
|
|
url = data["object_attributes"]["url"]
|
2019-10-18 13:03:20 +00:00
|
|
|
return [["[%s] %s commented on %s: %s" %
|
|
|
|
(type, commenter, number, issue_title), url]]
|