blob: 96277c236c479b031efe19c0ae7827eb9a709e4a [file] [log] [blame]
#!/usr/bin/python
import imaplib
import email
import re
import sys
from github3 import login
from github3 import pulls
from github3 import issues
mail = imaplib.IMAP4_SSL('imap.yandex.ru')
mail.login('imap-login@domain.com', 'imap-password')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.
pull_rq_db = {}
gh = login('github-login@domai.com', password='github-password')
me = gh.user()
print me
repo = gh.repository(me, "Linaro\/odp")
print repo
for r in gh.iter_repos():
print r.full_name
print r.name
if r.full_name == "Linaro/odp":
repo = r
break
if not repo:
exit(1)
print "Using github repo %s" % r.full_name
# get new messages
def is_patch(msg):
m = re.search("\nSubject.*Re:.*PATCH.*\n", msg)
if m:
#print m.group(0)
return 1
return 0
def get_find_pull_req(msg):
m = re.search(r'https://github.com/Linaro/odp/pull/.*\n', msg)
if m:
return m.group(0)[35:]
return ""
def process_email(i, efrom, msg, pr):
#f = open("eml/em-%d.dat" % i, "w")
#f.write(msg)
#f.close()
#for ic in pr.iter_issue_comments():
# print ic
issue = repo.issue(pr.number)
pre_msg = "<pre>From: %s\n %s </pre>" % (efrom, msg)
issue.create_comment(pre_msg)
return
def get_all_prs():
mail.select(readonly=1) # Select inbox or default namespace
(retcode, messages) = mail.search(None, '(SEEN)')
for num in messages[0].split():
typ, data = mail.fetch(num,'(RFC822)')
msg_str = email.message_from_string(data[0][1])
msg = str(msg_str)
pr = get_find_pull_req(msg)
if pr != "":
pull_rq_db[msg_str['Message-ID']] = pr
print pr,
sys.stdout.flush()
return
get_all_prs()
mail.select(readonly=0) # Select inbox or default namespace
(retcode, messages) = mail.search(None, '(UNSEEN)')
#(retcode, messages) = mail.search(None, '(SEEN)')
print retcode
print messages
if retcode != 'OK':
exit (1)
i = 0
for num in messages[0].split():
typ, data = mail.fetch(num,'(RFC822)')
msg_str = email.message_from_string(data[0][1])
msg = str(msg_str)
if not is_patch(msg):
continue
if msg_str.is_multipart():
continue
#print "Message-ID=", msg_str['Message-ID']
#print "In-Reply-To=", msg_str['In-Reply-To']
#print "Pull request =", pr
if not 'In-Reply-To' in msg_str:
print "x",
sys.stdout.flush()
i = i + 1
continue
pr = get_find_pull_req(msg)
if pr != "":
pull_rq_db[msg_str['Message-ID']] = pr
else:
if msg_str['In-Reply-To'] in pull_rq_db:
pr = pull_rq_db[msg_str['In-Reply-To']]
print "Found pr from in replay"
if pr == "":
print ".",
sys.stdout.flush()
i = i + 1
continue
efrom = msg_str['From']
body = msg_str.get_payload(decode=True)
print "Message-ID=", msg_str['Message-ID']
print "In-Reply-To=", msg_str['In-Reply-To']
print "Pull request =", pr
mail.store(num, '+FLAGS', '\\SEEN')
process_email(i, efrom, body, repo.pull_request(pr))
f = open("eml/em-%d.eml" % i, "w")
f.write(msg)
f.close()
i = i + 1