aboutsummaryrefslogtreecommitdiff
path: root/modules/git_webhooks/gitlab.py
blob: c20dfdd0318c0982390ce79fc68254d9342881bb (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from src import ModuleManager, utils
from . import colors

EVENT_CATEGORIES = {
    "ping": [
        "ping" # new webhook received
    ],
    "code": ["push"],
    "pr-minimal": [
        "merge_request/open", "merge_request/close", "merge_request/reopen",
        "merge_request/merge"
    ],
    "pr": [
        "merge_request/open", "merge_request/close", "merge_request/reopen",
        "merge_request/update", "merge_request/merge"
    ],
    "pr-all": ["merge_request"],
    "issue-minimal": [
        "issue/open", "issue/close", "issue/reopen", "confidential_issue/open",
        "confidential_issue/close", "confidential_issue/reopen"
    ],
    "issue": [
        "issue/open", "issue/close", "issue/reopen", "issue/update",
        "confidential_issue/open", "confidential_issue/close",
        "confidential_issue/reopen", "confidential_issue/update",
    ],
    "issue-all": [
        "issue", "issue_comment", "confidential_issue"
    ],
    "issue-comment-minimal": [
        "issue_comment/created", "issue_comment/deleted"
    ],
    "repo": ["tag_push"]
}

COMMENT_ACTIONS = {
    "created": "commented",
    "edited":  "edited a comment",
    "deleted": "deleted a comment"
}

ISSUE_ACTIONS = {
    "open": "opened",
    "close": "closed",
    "reopen": "reopened",
    "update": "updated",
    "merge": "merged"
}

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"]
        repo_username, repo_name = full_name.split("/", 1)

        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

    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()

        event = event.replace(" ", "_")
        event_action = None

        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)
                category_action = "%s/%s" % (category, event_action)

        return list(filter([event, event_action, category, category_action]))

    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)
        elif event == "merge_request":
            return self.merge_request(full_name, data)
        elif event in ["issue", "confidential_issue"]:
            return self.issues(full_name, data)
        elif event == "note":
            return self.note(full_name, data)
        elif event == "tag_push":
            return self.tag_push(full_name, data)

    def _short_hash(self, hash):
        return hash[:8]

    def tag_push(self, full_name, data):
        create = not data["after"].strip("0") == ""
        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"

        return [["%s %s a tag: %s" % (author, action, tag), None]]

    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"]

                outputs.append(["%s pushed %s to %s: %s"
                    % (author, hash_colored, branch, message), url])
        else:
            first_id = data["before"]
            last_id = data["after"]
            url = data["compare_url"]

            outputs.append(["%s pushed %d commits to %s"
                % (author, len(data["commits"]), branch), None])

        return outputs

    def merge_request(self, full_name, data):
        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)
        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":
            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)

        pr_title = data["object_attributes"]["title"]
        author = utils.irc.bold(data["user"]["username"])
        url = data["object_attributes"]["url"]
        return [["[MR] %s %s: %s" % (author, action_desc, pr_title), url]]

    def issues(self, full_name, data):
        number = utils.irc.color("#%s" % data["object_attributes"]["iid"],
            colors.COLOR_ID)
        action = ISSUE_ACTIONS[data["object_attributes"]["action"]]
        issue_title = data["object_attributes"]["title"]
        author = utils.irc.bold(data["user"]["username"])
        url = data["object_attributes"]["url"]

        return [["[issue] %s %s %s: %s" %
            (author, action, number, issue_title), url]]

    def note(self, full_name, data):
        type = data["object_attributes"]["noteable_type"]
        if type in ["Issue", "MergeRequest"]:
            return self.issue_note(full_name, data)

    def issue_note(self, full_name, data):
        number = utils.irc.color("#%s" % data["issue"]["iid"],
            colors.COLOR_ID)
        type = data["object_attributes"]["noteable_type"]
        type == "issue" if type == "Issue" else "MR"

        issue_title = data["issue"]["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]]