-
Notifications
You must be signed in to change notification settings - Fork 17
/
ticketbot.py
142 lines (115 loc) · 4.07 KB
/
ticketbot.py
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
"""
The #django-dev ticket bot.
"""
import os
import re
from collections import namedtuple
import irc3
import requests
ticket_re = re.compile(r'(?<!build)(?:^|\s)#(\d+)')
ticket_url = "https://code.djangoproject.com/ticket/%s"
github_sha_re = re.compile(r'(?:\s|^)([A-Fa-f0-9]{7,40})(?=\s|$)')
github_changeset_url = "https://github.com/django/django/commit/%s"
github_PR_re = re.compile(r'(?:\bPR|\B!)(\d+)\b')
github_PR_url = "https://github.com/django/django/pull/%s"
MatchSet = namedtuple('MatchSet',
['tickets', 'github_changesets', 'github_PRs'])
def get_matches(message):
"""
Given a message, return a tuple of various interesting things in it:
* ticket ids
* git commit ids
* github PR ids
"""
tickets = set(map(int, ticket_re.findall(message))).difference(
set(range(0, 11))
) # #1-10 are ignored.
github_changesets = set(github_sha_re.findall(message))
github_PRs = set(github_PR_re.findall(message))
return MatchSet(tickets, github_changesets, github_PRs)
def validate_sha_github(sha):
"""
Make sure the given SHA belong to the Django tree.
Works by making a request to the github repo.
"""
r = requests.head(github_changeset_url % sha)
return r.status_code == 200
def get_links(match_set, sha_validation=validate_sha_github):
"""
Given a match_set (a tuple of matches returned by get_matches),
return a list of links to show back to the user.
The sha_validation argument is a callable that's used to validate
the commit ids. Passing None skips the validation.
"""
links = []
for ticket in match_set.tickets:
links.append(ticket_url % ticket)
for PR in match_set.github_PRs:
links.append(github_PR_url % PR)
# validate github changeset SHA's
for c in match_set.github_changesets:
if sha_validation and sha_validation(c):
links.append(github_changeset_url % c)
return links
@irc3.plugin
class Plugin:
def __init__(self, bot):
self.bot = bot
@irc3.event(irc3.rfc.PRIVMSG)
def process_msg_or_privmsg(self, mask, event, target, data, **kw):
"""Detect special markers and reply with their respective links."""
# Don't send automatic replies to notices
if event == 'NOTICE':
return
is_privmsg = target == self.bot.nick
user = mask.nick
matches = get_matches(data)
# No content? Send helptext.
if not any(matches) and (is_privmsg or data.startswith(self.bot.nick)):
self.bot.privmsg(
user,
"Hi, I'm Django's ticketbot. I know how to linkify tickets "
"like \"#12345\", github changesets like \"a00cf3d\" (minimum "
"7 characters), and github pull requests like \"PR12345\" or \"!12345\"."
)
self.bot.privmsg(
user,
"Suggestions? Problems? Help make me better: "
"https://github.com/django/ticketbot"
)
return
# Produce links
links = get_links(matches)
# Check to see if they're sending me a private message
if is_privmsg:
to = user
else:
to = target
self.bot.privmsg(to, ' '.join(links))
def main():
password = os.environ['NICKSERV_PASS']
username = os.environ['NICKSERV_USER']
host = os.environ['IRC_HOST']
port = int(os.environ['IRC_PORT'])
channels = os.environ['CHANNELS'].split(',')
bot = irc3.IrcBot.from_config(dict(
nick=username,
username=username,
realname='Django project development helper bot',
sasl_username=username,
sasl_password=password,
url='https://github.com/django/ticketbot',
autojoins=channels,
host=host, port=port, ssl=True,
includes=[
'irc3.plugins.core',
'irc3.plugins.sasl',
__name__, # this registers our plugin
],
# debug=True,
# verbose=True,
# raw=True,
))
bot.run(forever=True)
if __name__ == '__main__':
main()