Maxim Uvarov | c656cfc | 2017-09-04 15:41:53 +0300 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Cron script to validate patches in open pull requests. |
| 4 | # 1. Validation skipped if "checkpatch" label is set. |
| 5 | # 2. Run both checkpatch.pl and odp agreement checks. |
| 6 | # Result is status on odp pull request github web page and |
| 7 | # label "checkpatch" set. |
| 8 | |
| 9 | from github import Github |
| 10 | import github |
| 11 | import os |
| 12 | import re |
| 13 | import glob |
| 14 | import sys |
| 15 | import pickle |
| 16 | import imaplib |
| 17 | import email |
| 18 | import smtplib |
| 19 | from email.mime.text import MIMEText |
| 20 | |
| 21 | configfile = '/home/muvarov/gscripts_config.py' |
| 22 | sys.path.append(os.path.dirname(os.path.expanduser(configfile))) |
| 23 | import gscripts_config as gcfg |
| 24 | |
| 25 | |
| 26 | gh_login = gcfg.gcfg['gh']['login'] |
| 27 | gh_password = gcfg.gcfg['gh']['pass'] |
| 28 | |
| 29 | g = Github(gh_login, password=gh_password) |
| 30 | |
| 31 | for repo in g.get_user().get_repos(): |
| 32 | if repo.full_name == "Linaro/odp": |
| 33 | break |
| 34 | if not repo: |
| 35 | exit(1) |
| 36 | |
| 37 | # get Message-Id to reply with correct In-Reply-To |
| 38 | def get_email_id_by_title(title): |
| 39 | ret_msg_id = None |
| 40 | |
| 41 | imap_login = gcfg.gcfg['imap']['login'] |
| 42 | imap_password = gcfg.gcfg['imap']['pass'] |
| 43 | |
| 44 | mail = imaplib.IMAP4_SSL('imap.yandex.ru') |
| 45 | mail.login(imap_login, imap_password) |
| 46 | mail.list() |
| 47 | |
| 48 | # Out: list of "folders" aka labels in gmail. |
| 49 | mail.select("inbox") # connect to inbox. |
| 50 | mail.select(readonly=1) # Select inbox or default namespace |
| 51 | |
| 52 | # imap truncates subject search |
| 53 | m = re.search(r'\[(PATCH.*v[0-9]+)\](.*)', title[0:52]) |
| 54 | if not m: |
| 55 | mail.logout() |
| 56 | return None |
| 57 | |
| 58 | prefix = m.group(1).lstrip() + " 0/" #look for PATCH v1 0/X |
| 59 | subject = m.group(2).lstrip() |
| 60 | |
| 61 | (retcode, messages) = mail.uid('search', '(SUBJECT "%s")' % subject) |
| 62 | for num in messages[0].split(): |
| 63 | typ, data = mail.fetch(num,'(RFC822)') |
| 64 | msg_str = email.message_from_string(data[0][1]) |
| 65 | m = re.search(prefix, msg_str['Subject']) |
| 66 | if not m: |
| 67 | continue |
| 68 | ret_msg_id = msg_str['Message-Id'] |
| 69 | break |
| 70 | mail.logout() |
| 71 | return ret_msg_id |
| 72 | |
| 73 | def smtp_send_comment(pull, rvc, id): |
| 74 | if rvc.user.name: |
| 75 | user = "%s(%s)" % (rvc.user.name, rvc.user.login) |
| 76 | else: |
| 77 | user = "%s" % (rvc.user.login) |
| 78 | |
| 79 | text = "%s replied on github web page:\n\n" % (user) |
| 80 | |
| 81 | text += "%s\n" % (rvc.path) |
| 82 | if rvc.position: |
| 83 | text += "line %s\n" % (rvc.position) |
| 84 | text += "%s\n" % rvc.diff_hunk |
| 85 | text += "\n\n" |
| 86 | text += "Comment:\n" |
| 87 | text += rvc.body |
| 88 | text += "\n\n" |
| 89 | |
| 90 | text += "%s\n" % rvc.html_url |
| 91 | text += "updated_at %s\n" % rvc.updated_at |
| 92 | msg = MIMEText(text.encode("utf-8")) |
| 93 | |
| 94 | msg['Subject'] = "Re: %s" % pull.title |
| 95 | msg['From'] = "Github ODP bot <odpbot@yandex.ru>" |
| 96 | msg['To'] = "lng-odp@lists.linaro.org" |
| 97 | msg['In-Reply-To'] = id |
| 98 | #msg['To'] = "maxim.uvarov@linaro.org" |
| 99 | |
| 100 | smtp_server = gcfg.gcfg['smtp']['server'] |
| 101 | smtp_login = gcfg.gcfg['smtp']['login'] |
| 102 | smtp_password = gcfg.gcfg['smtp']['pass'] |
| 103 | |
| 104 | s = smtplib.SMTP_SSL(smtp_server) |
| 105 | s.login(smtp_login, smtp_password) |
| 106 | s.sendmail(msg['From'], msg['To'], msg.as_string()) |
| 107 | s.quit() |
| 108 | |
| 109 | def email_new_comment(pull, rvc): |
| 110 | id = get_email_id_by_title(pull.title) |
| 111 | smtp_send_comment(pull, rvc, id) |
| 112 | |
| 113 | ### MAIN |
| 114 | |
| 115 | os.system("mkdir -p gh-ec-cache") |
| 116 | |
| 117 | |
| 118 | for pull in repo.get_pulls(): |
| 119 | try: |
| 120 | issue = repo.get_issue(pull.number) |
| 121 | except: |
| 122 | # timeout ssl http error can happen. Process this pr |
| 123 | # on next run. |
| 124 | continue |
| 125 | |
| 126 | pull_cache = {} |
| 127 | filename = "gh-ec-cache/%d.cache" % pull.number |
| 128 | try: |
| 129 | f = open(filename, "rb" ); |
| 130 | pull_cache = pickle.load(f) |
| 131 | f.close() |
| 132 | except: |
| 133 | print "return at 1" |
| 134 | break |
| 135 | |
| 136 | try: |
| 137 | for rvc in pull.get_review_comments(): |
| 138 | if rvc.id not in pull_cache: |
| 139 | email_new_comment(pull, rvc) |
| 140 | pull_cache[rvc.id] = { rvc.body } |
| 141 | except: |
| 142 | print "return at 2" |
| 143 | break |
| 144 | |
| 145 | f = open(filename, 'w') |
| 146 | pickle.dump(pull_cache, f) |
| 147 | f.close() |