-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgh-email-comments.py
executable file
·151 lines (122 loc) · 3.52 KB
/
gh-email-comments.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
143
144
145
146
147
148
149
150
151
#!/usr/bin/python
# encoding=utf8
#
# Cron script to validate patches in open pull requests.
# 1. Validation skipped if "checkpatch" label is set.
# 2. Run both checkpatch.pl and odp agreement checks.
# Result is status on odp pull request github web page and
# label "checkpatch" set.
from github import Github
import github
import os
import re
import glob
import sys
import pickle
import imaplib
import email
import smtplib
from email.mime.text import MIMEText
reload(sys)
sys.setdefaultencoding('utf8')
configfile = '~/gscripts_config.py'
sys.path.append(os.path.dirname(os.path.expanduser(configfile)))
import gscripts_config as gcfg
gh_login = gcfg.gcfg['gh']['login']
gh_password = gcfg.gcfg['gh']['pass']
g = Github(gh_login, password=gh_password)
for repo in g.get_user().get_repos():
if repo.full_name == "Linaro/odp":
break
if not repo:
exit(1)
# get Message-Id to reply with correct In-Reply-To
def get_email_id_by_title(title):
ret_msg_id = None
imap_login = gcfg.gcfg['imap']['login']
imap_password = gcfg.gcfg['imap']['pass']
mail = imaplib.IMAP4_SSL('imap.yandex.ru')
mail.login(imap_login, imap_password)
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
mail.select(readonly=1) # Select inbox or default namespace
# imap truncates subject search
m = re.search(r'\[(PATCH.*v[0-9]+)\](.*)', title[0:52])
if not m:
mail.logout()
return None
prefix = m.group(1).lstrip() + " 0/" #look for PATCH v1 0/X
subject = m.group(2).lstrip()
(retcode, messages) = mail.uid('search', '(SUBJECT "%s")' % subject)
for num in messages[0].split():
typ, data = mail.fetch(num,'(RFC822)')
msg_str = email.message_from_string(data[0][1])
m = re.search(prefix, msg_str['Subject'])
if not m:
continue
ret_msg_id = msg_str['Message-Id']
break
mail.logout()
return ret_msg_id
def smtp_send_comment(pull, rvc, id):
if rvc.user.name:
user = "%s(%s)" % (rvc.user.name, rvc.user.login)
else:
user = "%s" % (rvc.user.login)
text = "%s replied on github web page:\n\n" % (user)
text += "%s\n" % (rvc.path)
if rvc.position:
text += "line %s\n" % (rvc.position)
text += "%s\n" % rvc.diff_hunk
text += "\n\n"
text += "Comment:\n"
text += rvc.body
text += "\n\n"
text += "%s\n" % rvc.html_url
text += "updated_at %s\n" % rvc.updated_at
msg = MIMEText(text.encode("utf-8"))
msg['Subject'] = "Re: %s" % pull.title
msg['From'] = "Github ODP bot <odpbot@yandex.ru>"
msg['To'] = "lng-odp@lists.linaro.org"
msg['In-Reply-To'] = id
#msg['To'] = "maxim.uvarov@linaro.org"
smtp_server = gcfg.gcfg['smtp']['server']
smtp_login = gcfg.gcfg['smtp']['login']
smtp_password = gcfg.gcfg['smtp']['pass']
s = smtplib.SMTP_SSL(smtp_server)
s.login(smtp_login, smtp_password)
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()
def email_new_comment(pull, rvc):
id = get_email_id_by_title(pull.title)
smtp_send_comment(pull, rvc, id)
### MAIN
os.system("mkdir -p gh-ec-cache")
for pull in repo.get_pulls():
try:
issue = repo.get_issue(pull.number)
except:
# timeout ssl http error can happen. Process this pr
# on next run.
continue
pull_cache = {}
filename = "gh-ec-cache/%d.cache" % pull.number
try:
f = open(filename, "rb" );
pull_cache = pickle.load(f)
f.close()
except:
print "return at 1"
break
try:
for rvc in pull.get_review_comments():
if rvc.id not in pull_cache:
email_new_comment(pull, rvc)
pull_cache[rvc.id] = { rvc.body }
except:
print "return at 2"
break
f = open(filename, 'w')
pickle.dump(pull_cache, f)
f.close()