diff options
Diffstat (limited to 'modules/git_webhooks')
| -rw-r--r-- | modules/git_webhooks/__init__.py | 18 | ||||
| -rw-r--r-- | modules/git_webhooks/github.py | 5 | ||||
| -rw-r--r-- | modules/git_webhooks/gitlab.py | 30 |
3 files changed, 35 insertions, 18 deletions
diff --git a/modules/git_webhooks/__init__.py b/modules/git_webhooks/__init__.py index 0dc03310..b87f0665 100644 --- a/modules/git_webhooks/__init__.py +++ b/modules/git_webhooks/__init__.py @@ -13,6 +13,10 @@ DEFAULT_EVENT_CATEGORIES = [ "ping", "code", "pr", "issue", "repo" ] +PRIVATE_SETTING_NAME = "git-show-private" +PRIVATE_SETTING = utils.BoolSetting(PRIVATE_SETTING_NAME, + "Whether or not to show git activity for private repositories") + @utils.export("channelset", utils.BoolSetting("git-prevent-highlight", "Enable/disable preventing highlights")) @utils.export("channelset", utils.BoolSetting("git-hide-organisation", @@ -21,8 +25,8 @@ DEFAULT_EVENT_CATEGORIES = [ "Hide/show command-like prefix on git webhook outputs")) @utils.export("channelset", utils.BoolSetting("git-shorten-urls", "Weather or not git webhook URLs should be shortened")) -@utils.export("botset", utils.BoolSetting("git-show-private", - "Whether or not to show git activity for private repositories")) +@utils.export("botset", PRIVATE_SETTING) +@utils.export("channelset", PRIVATE_SETTING) class Module(ModuleManager.BaseModule): _name = "Webhooks" @@ -54,8 +58,8 @@ class Module(ModuleManager.BaseModule): "payload"][0]) data = json.loads(payload) - if handler.is_private(data, headers) and not self.bot.get_setting( - "git-show-private", False): + is_private = handler.is_private(data, headers) + if is_private and not self.bot.get_setting(PRIVATE_SETTING_NAME, True): return {"state": "success", "deliveries": 0} full_name, repo_username, repo_name, organisation = handler.names( @@ -96,6 +100,10 @@ class Module(ModuleManager.BaseModule): repo_hooked = bool(unfiltered_targets) targets = [] for server, channel, hook in unfiltered_targets: + if is_private and not channel.get_setting( + PRIVATE_SETTING_NAME, False): + continue + if (branch and hook["branches"] and not branch in hook["branches"]): @@ -192,7 +200,7 @@ class Module(ModuleManager.BaseModule): """ :help: Add/remove/modify a git webhook :require_mode: high - :require_access: git-webhook + :require_access: admin,git-webhook :permission: gitoverride :usage: list :usage: add <hook> diff --git a/modules/git_webhooks/github.py b/modules/git_webhooks/github.py index 96dc26c3..a3f51c89 100644 --- a/modules/git_webhooks/github.py +++ b/modules/git_webhooks/github.py @@ -183,7 +183,7 @@ class GitHub(object): return url def _iso8601(self, s): - return utils.datetime.iso8601_parse(s) + return utils.datetime.parse.iso8601(s) def ping(self, data): return ["Received new webhook"] @@ -444,7 +444,8 @@ class GitHub(object): completed_at = self._iso8601(data["check_run"]["completed_at"]) if completed_at > started_at: seconds = (completed_at-started_at).total_seconds() - duration = " in %s" % utils.datetime.to_pretty_time(seconds) + duration = " in %s" % utils.datetime.format.to_pretty_time( + seconds) status = data["check_run"]["status"] status_str = "" diff --git a/modules/git_webhooks/gitlab.py b/modules/git_webhooks/gitlab.py index 9f481384..c602eb61 100644 --- a/modules/git_webhooks/gitlab.py +++ b/modules/git_webhooks/gitlab.py @@ -55,7 +55,10 @@ class GitLab(object): return False def names(self, data, headers): - full_name = data["project"]["path_with_namespace"] + if "project" in data: + full_name = data["project"]["path_with_namespace"] + else: + full_name = data["project_name"].replace(" ", "") repo_username, repo_name = full_name.split("/", 1) organisation = None @@ -72,8 +75,8 @@ class GitLab(object): def event(self, data, headers): event = headers["X-GitLab-Event"].rsplit(" ", 1)[0].lower() - event = event.replace(" ", "_") + action = None event_action = None @@ -152,7 +155,7 @@ class GitLab(object): number = utils.irc.color("!%s" % data["object_attributes"]["iid"], colors.COLOR_ID) action = data["object_attributes"]["action"] - action_desc = "%s %s" % (ISSUE_ACTIONS[action], number) + action_desc = "%s %s" % (ISSUE_ACTIONS.get(action, action), number) branch = data["object_attributes"]["target_branch"] colored_branch = utils.irc.color(branch, colors.COLOR_BRANCH) @@ -173,9 +176,13 @@ class GitLab(object): return [["[MR] %s %s: %s" % (author, action_desc, pr_title), url]] def issues(self, full_name, data): + if not "action" in data["object_attributes"]: + return + number = utils.irc.color("#%s" % data["object_attributes"]["iid"], colors.COLOR_ID) - action = ISSUE_ACTIONS[data["object_attributes"]["action"]] + action = data["object_attributes"]["action"] + action = ISSUE_ACTIONS.get(action, action) issue_title = data["object_attributes"]["title"] author = utils.irc.bold(data["user"]["username"]) url = data["object_attributes"]["url"] @@ -185,17 +192,18 @@ class GitLab(object): def note(self, full_name, data): type = data["object_attributes"]["noteable_type"] - if type in ["Issue", "MergeRequest"]: - return self.issue_note(full_name, data) + if type == "Issue": + return self._note(full_name, data, data["issue"]) + elif type == "MergeRequest": + return self._note(full_name, data, data["merge_request"]) - def issue_note(self, full_name, data): - number = utils.irc.color("#%s" % data["issue"]["iid"], - colors.COLOR_ID) + def _note(self, full_name, data, object): + number = utils.irc.color("#%s" % object["iid"], colors.COLOR_ID) type = data["object_attributes"]["noteable_type"] type == "issue" if type == "Issue" else "MR" - issue_title = data["issue"]["title"] + title = object["title"] commenter = utils.irc.bold(data["user"]["username"]) url = data["object_attributes"]["url"] return [["[%s] %s commented on %s: %s" % - (type, commenter, number, issue_title), url]] + (type, commenter, number, title), url]] |
