aboutsummaryrefslogtreecommitdiff
path: root/modules/github.py
blob: 789b8c00d5704b61b366452b05d43a623f6a8f26 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import json
from src import ModuleManager, utils

@utils.export("channelset", {"setting": "github-hook",
    "help": ("Disable/Enable showing BitBot's github commits in the "
    "current channel"), "validate": utils.bool_or_none})
class Module(ModuleManager.BaseModule):
    @utils.hook("api.post.github")
    def github(self, event):
        data = event["data"]
        try:
            data = json.loads(data)
        except:
            return

        if "commits" in data:
            full_name = data["repository"]["full_name"]

            for commit in data["commits"]:
                id = commit["id"]

                message = commit["message"].split("\n")
                message = [line.strip() for line in message]
                message = " ".join(message)

                author = "%s <%s>" % (commit["author"]["username"],
                    commit["author"]["email"])
                modified_count = len(commit["modified"])
                added_count = len(commit["added"])
                removed_count = len(commit["removed"])

                line = ("(%s) [files: %d/%d/%d mod/add/del] commit by %s: "
                    "'%s'") % (full_name, modified_count, added_count,
                    removed_count, author, message)
                hooks = self.bot.database.channel_settings.find_by_setting(
                    "github-hook")
                hooks = [hook for hook in hooks if hook[2]]
                for server_id, channel_name, _ in hooks:
                    server = self.bot.get_server(server_id)
                    channel = server.get_channel(channel_name)

                    self.events.on("send.stdout").call(target=channel,
                        module_name="Github", server=server, message=line)
                    self.bot.register_both(server)