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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
import itertools, json, urllib.parse
from src import ModuleManager, utils
FORM_ENCODED = "application/x-www-form-urlencoded"
COMMIT_URL = "https://github.com/%s/commit/%s"
COMMIT_RANGE_URL = "https://github.com/%s/compare/%s...%s"
CREATE_URL = "https://github.com/%s/tree/%s"
DEFAULT_EVENTS = [
"push",
"commit_comment",
"pull_request",
"pull_request_review",
"pull_request_review_comment",
"issue_comment",
"issues",
"create",
"delete",
"release",
"fork"
]
COMMENT_ACTIONS = {
"created": "commented",
"edited": "edited a comment",
"deleted": "deleted a comment"
}
@utils.export("channelset", {"setting": "github-hook",
"help": ("Disable/Enable showing BitBot's github commits in the "
"current channel"), "array": True})
@utils.export("channelset", {"setting": "github-hide-prefix",
"help": "Hide/show command-like prefix on Github hook outputs",
"validate": utils.bool_or_none})
class Module(ModuleManager.BaseModule):
@utils.hook("api.post.github")
def github(self, event):
payload = event["data"].decode("utf8")
if event["headers"]["Content-Type"] == FORM_ENCODED:
payload = urllib.parse.unquote(urllib.parse.parse_qs(payload)[
"payload"][0])
data = json.loads(payload)
github_event = event["headers"]["X-GitHub-Event"]
if github_event == "ping":
return ""
full_name = data["repository"]["full_name"]
repo_username, repo_name = full_name.split("/", 1)
hooks = self.bot.database.channel_settings.find_by_setting(
"github-hook")
targets = []
repo_hooked = False
for i, (server_id, channel_name, hooked_repos) in list(
enumerate(hooks))[::-1]:
if repo_username in hooked_repos or full_name in hooked_repos:
repo_hooked = True
server = self.bot.get_server(server_id)
if server and channel_name in server.channels:
channel = server.channels.get(channel_name)
github_events = channel.get_setting("github-events",
DEFAULT_EVENTS)
if github_event in github_events:
targets.append([server, channel])
if not targets:
return "" if repo_hooked else None
outputs = None
if github_event == "push":
outputs = self.push(event, full_name, data)
elif github_event == "commit_comment":
outputs = self.commit_comment(event, full_name, data)
elif github_event == "pull_request":
outputs = self.pull_request(event, full_name, data)
elif github_event == "pull_request_review":
outputs = self.pull_request_review(event, full_name, data)
elif github_event == "pull_request_review_comment":
outputs = self.pull_request_review_comment(event, full_name, data)
elif github_event == "issue_comment":
outputs = self.issue_comment(event, full_name, data)
elif github_event == "issues":
outputs = self.issues(event, full_name, data)
elif github_event == "create":
outputs = self.create(event, full_name, data)
elif github_event == "delete":
outputs = self.delete(event, full_name, data)
elif github_event == "release":
outputs = self.release(event, full_name, data)
elif github_event == "status":
outputs = self.status(event, full_name, data)
elif github_event == "fork":
outputs = self.fork(event, full_name, data)
if outputs:
for server, channel in targets:
for output in outputs:
output = "(%s) %s" % (full_name, output)
self.events.on("send.stdout").call(target=channel,
module_name="Github", server=server, message=output,
hide_prefix=channel.get_setting(
"github-hide-prefix", False))
return ""
def _short_url(self, url):
try:
page = utils.http.request("https://git.io", method="POST",
post_data={"url": url})
return page.headers["Location"]
except utils.http.HTTPTimeoutException:
return url
def _change_count(self, n, symbol, color):
return utils.irc.color("%s%d" % (symbol, n), color)+utils.irc.bold("")
def _added(self, n):
return self._change_count(n, "+", utils.consts.GREEN)
def _removed(self, n):
return self._change_count(n, "-", utils.consts.RED)
def _modified(self, n):
return self._change_count(n, "~", utils.consts.PURPLE)
def _short_hash(self, hash):
return hash[:8]
def _flat_unique(self, commits, key):
return set(itertools.chain(*(commit[key] for commit in commits)))
def push(self, event, full_name, data):
outputs = []
branch = data["ref"].split("/", 2)[2]
branch = utils.irc.color(branch, utils.consts.LIGHTBLUE)
if len(data["commits"]) <= 3:
for commit in data["commits"]:
id = self._short_hash(commit["id"])
message = commit["message"].split("\n")[0].strip()
author = commit["author"]["name"] or commit["author"]["login"]
author = utils.irc.bold(author)
url = self._short_url(COMMIT_URL % (full_name, id))
added = self._added(len(commit["added"]))
removed = self._removed(len(commit["removed"]))
modified = self._modified(len(commit["modified"]))
outputs.append("[%s/%s/%s files] commit by %s to %s: %s - %s"
% (added, removed, modified, author, branch, message, url))
else:
first_id = self._short_hash(data["before"])
last_id = self._short_hash(data["commits"][-1]["id"])
pusher = utils.irc.bold(data["pusher"]["name"])
url = self._short_url(
COMMIT_RANGE_URL % (full_name, first_id, last_id))
commits = data["commits"]
added = self._added(len(self._flat_unique(commits, "added")))
removed = self._removed(len(self._flat_unique(commits, "removed")))
modified = self._modified(len(self._flat_unique(commits,
"modified")))
outputs.append("[%s/%s/%s files] %s pushed %d commits to %s - %s"
% (added, removed, modified, pusher, len(data["commits"]),
branch, url))
return outputs
def commit_comment(self, event, full_name, data):
action = data["action"]
commit = data["commit_id"][:8]
commenter = utils.irc.bold(data["comment"]["user"]["login"])
url = self._short_url(data["comment"]["html_url"])
return ["[commit/%s] %s commented" % (commit, commenter, action)]
def pull_request(self, event, full_name, data):
number = data["pull_request"]["number"]
action = data["action"]
action_desc = action
branch = data["pull_request"]["base"]["ref"]
colored_branch = utils.irc.color(branch, utils.consts.LIGHTBLUE)
if action == "opened":
action_desc = "requested merge into %s" % colored_branch
elif action == "closed":
if data["pull_request"]["merged"]:
action_desc = "%s into %s" % (
utils.irc.color("merged", utils.consts.GREEN),
colored_branch)
else:
action_desc = utils.irc.color("closed without merging",
utils.consts.RED)
elif action == "synchronize":
action_desc = "committed to"
elif action == "labeled":
action_desc = "labeled as '%s'" % data["label"]["name"]
elif action == "unlabeled":
action_desc = "unlabeled as '%s'" % data["label"]["name"]
pr_title = data["pull_request"]["title"]
author = utils.irc.bold(data["sender"]["login"])
url = self._short_url(data["pull_request"]["html_url"])
return ["[pr #%d] %s %s: %s - %s" % (
number, author, action_desc, pr_title, url)]
def pull_request_review(self, event, full_name, data):
if data["review"]["state"] == "commented":
return []
number = data["pull_request"]["number"]
action = data["action"]
pr_title = data["pull_request"]["title"]
reviewer = utils.irc.bold(data["sender"]["login"])
url = self._short_url(data["review"]["html_url"])
return ["[pr #%d] %s %s a review on: %s - %s" % (
number, reviewer, action, pr_title, url)]
def pull_request_review_comment(self, event, full_name, data):
number = data["pull_request"]["number"]
action = data["action"]
pr_title = data["pull_request"]["title"]
sender = data["sender"]["login"]
url = self._short_url(data["comment"]["html_url"])
return ["[pr #%d] %s %s on a review: %s - %s" %
(number, sender, COMMENT_ACTIONS[action], pr_title, url)]
def issues(self, event, full_name, data):
number = data["issue"]["number"]
action = data["action"]
action_desc = action
if action == "labeled":
action_desc = "labeled as '%s'" % data["label"]["name"]
elif action == "unlabeled":
action_desc = "unlabeled as '%s'" % data["label"]["name"]
issue_title = data["issue"]["title"]
author = utils.irc.bold(data["sender"]["login"])
url = self._short_url(data["issue"]["html_url"])
return ["[issue #%d] %s %s: %s - %s" %
(number, author, action_desc, issue_title, url)]
def issue_comment(self, event, full_name, data):
number = data["issue"]["number"]
action = data["action"]
issue_title = data["issue"]["title"]
type = "pr" if "pull_request" in data["issue"] else "issue"
commenter = utils.irc.bold(data["comment"]["user"]["login"])
url = self._short_url(data["comment"]["html_url"])
return ["[%s #%d] %s %s on: %s - %s" %
(type, number, commenter, COMMENT_ACTIONS[action], issue_title,
url)]
def create(self, event, full_name, data):
ref = data["ref"]
ref_color = utils.irc.color(ref, utils.consts.LIGHTBLUE)
type = data["ref_type"]
sender = utils.irc.bold(data["sender"]["login"])
url = self._short_url(CREATE_URL % (full_name, ref))
return ["%s created a %s: %s - %s" % (sender, type, ref_color, url)]
def delete(self, event, full_name, data):
ref = data["ref"]
type = data["ref_type"]
sender = utils.irc.bold(data["sender"]["login"])
return ["%s deleted a %s: %s" % (sender, type, ref)]
def release(self, event, full_name, data):
action = data["action"]
tag = data["release"]["tag_name"]
name = data["release"]["name"] or ""
if name:
name = ": %s"
author = utils.irc.bold(data["release"]["author"]["login"])
url = self._short_url(data["release"]["html_url"])
return ["%s %s a release%s - %s" % (author, action, name, url)]
def status(self, event, full_name, data):
context = data["context"]
state = data["state"]
url = data["target_url"]
commit = self._short_id(data["sha"])
return ["[%s status] %s is '%s' - %s" %
(commit, context, state, url)]
def fork(self, event, full_name, data):
forker = utils.irc.bold(data["sender"]["login"])
fork_full_name = utils.irc.color(data["forkee"]["full_name"],
utils.consts.LIGHTBLUE)
url = self._short_url(data["forkee"]["html_url"])
return ["%s forked into %s - %s" %
(forker, fork_full_name, url)]
|