Maxim Uvarov | 1acb99b | 2017-05-08 23:40:58 +0300 | [diff] [blame] | 1 | from github3 import login |
| 2 | from github3 import pulls |
| 3 | from github3 import issues |
| 4 | import os |
| 5 | import re |
| 6 | import glob |
| 7 | |
| 8 | gh = login('github-login@domain.com', password='password') |
| 9 | me = gh.user() |
| 10 | print me |
| 11 | |
| 12 | repo = gh.repository(me, "Linaro\/odp") |
| 13 | print repo |
| 14 | |
| 15 | for r in gh.iter_repos(): |
| 16 | print r.full_name |
| 17 | print r.name |
| 18 | if r.full_name == "Linaro/odp": |
| 19 | repo = r |
| 20 | break |
| 21 | |
| 22 | if not repo: |
| 23 | exit(1) |
| 24 | |
| 25 | def my_system(cmd): |
| 26 | ret = os.system(cmd) |
| 27 | if ret: |
| 28 | print "Error: %s" % cmd |
| 29 | |
| 30 | def fix_patch(f, hdr, i, tlen): |
| 31 | fin = open(f, "r+") |
| 32 | data = fin.read() |
| 33 | fin.close() |
| 34 | |
| 35 | new_data = re.sub("\[PATCH.*\]", "[%s %d/%d]" % (hdr, i, tlen), data) |
| 36 | |
| 37 | fout = open(f, "w") |
| 38 | fout.writelines(new_data) |
| 39 | fout.close() |
| 40 | |
| 41 | |
| 42 | def fix_headers(hdr): |
| 43 | |
| 44 | print "Fix hdr with hdr %s\n" % hdr |
Dmitry Eremin-Solenikov | 791a542 | 2017-05-08 23:46:36 +0300 | [diff] [blame] | 45 | pfiles = sorted(glob.glob("./to_send-p*.patch")) |
Maxim Uvarov | 1acb99b | 2017-05-08 23:40:58 +0300 | [diff] [blame] | 46 | i = 0 |
| 47 | for f in pfiles: |
| 48 | i = i + 1 |
| 49 | fix_patch(f, hdr, i, len(pfiles)) |
| 50 | |
| 51 | return i |
| 52 | |
| 53 | def email_patches(edata): |
| 54 | #print "Email:", edata['patch_url'] |
| 55 | #print "Email:", edata['base_sha'] |
| 56 | my_system("wget -O pr.patch -o /dev/null %s" % edata['patch_url']) |
| 57 | fin = open("pr.patch","r+") |
| 58 | fout = open("pr_mod.patch","w") |
| 59 | data = fin.readlines() |
| 60 | efrom = "" |
| 61 | |
| 62 | my_system("rm -rf to_send*.patch") |
| 63 | |
| 64 | for l in data: |
| 65 | if l[0:9] == "Subject: ": |
| 66 | fout.write("Github-pr-num: %d\n" % edata['number']) |
| 67 | fout.write(l) |
| 68 | continue |
| 69 | |
| 70 | #print "line=%s" % l |
| 71 | if l.rstrip("\n") != "---": |
| 72 | fout.write(l) |
| 73 | else: |
| 74 | fout.write("---\n") |
| 75 | fout.write("/** Email created from pull request %d (%s)\n" % (edata['number'], edata['head_label'])) |
| 76 | fout.write(" ** %s\n" % edata['url']) |
| 77 | fout.write(" ** Patch: %s\n" % edata['patch_url']) |
| 78 | fout.write(" ** Base sha: %s\n" % edata['base_sha']) |
| 79 | fout.write(" ** Merge commit sha: %s\n" % edata['merge_commit_sha']) |
| 80 | fout.write(" **/\n") |
| 81 | if l[0:6] == "From: ": |
| 82 | efrom = l[6:].rstrip("\n") |
| 83 | fout.close() |
| 84 | fin.close() |
| 85 | |
| 86 | my_system("cat pr_mod.patch | formail -ds sh -c 'cat > to_send-p-$FILENO.patch'") |
| 87 | |
| 88 | num = fix_headers(edata['prefix']) |
| 89 | |
| 90 | c = open("to_send-c-0000.patch","w") |
| 91 | c.write("From %s %s\n" % (edata['base_sha'], "<date>")) |
| 92 | c.write("From: %s\n" % "Github ODP bot <odpbot@yandex.ru>") |
| 93 | c.write("Date: %s\n" % "<date>") |
| 94 | |
| 95 | m = re.search("\[(.*)\](.*)", edata['title']) |
| 96 | c.write("Subject: [%s 0/%d]%s\n" % (m.group(1), num, m.group(2))) |
| 97 | |
| 98 | c.write("\n") |
| 99 | c.write(edata['body_text']) |
| 100 | c.write("\n") |
| 101 | c.write("\n") |
| 102 | |
| 103 | c.write("----------------github------------------------\n") |
| 104 | c.write("/** Email created from pull request %d (%s)\n" % (edata['number'], edata['head_label'])) |
| 105 | c.write(" ** %s\n" % edata['url']) |
| 106 | c.write(" ** Patch: %s\n" % edata['patch_url']) |
| 107 | c.write(" ** Base sha: %s\n" % edata['base_sha']) |
| 108 | c.write(" ** Merge commit sha: %s\n" % edata['merge_commit_sha']) |
| 109 | c.write(" **/\n") |
| 110 | c.write("----------------/github------------------------\n") |
| 111 | c.write("\n") |
| 112 | |
| 113 | c.write("----------------checkpatch.pl------------------------\n") |
| 114 | c.close() |
| 115 | my_system("perl ./scripts/checkpatch.pl to_send-p-*.patch | grep -v \"Ignored message types\">> to_send-c-0000.patch") |
| 116 | c = open("to_send-c-0000.patch","a") |
| 117 | c.write("----------------/checkpatch.pl------------------------\n") |
| 118 | c.close() |
| 119 | |
| 120 | my_system("rm -rf pr_mod.patch") |
| 121 | my_system("rm -rf pr.patch") |
| 122 | |
| 123 | options = "--smtp-server=\"smtp.server\" --smtp-ssl --smtp-pass=\"password\" --smtp-encryption=tls --smtp-user=\"odpbot\" --from=\"Github ODP bot <odpbot@yandex.ru>\"" |
| 124 | |
| 125 | my_system("git send-email --confirm=never --to ml-listp@lists.real --suppress-cc=all %s to_send*.patch" % options) |
| 126 | #my_system("git send-email --confirm=never --to muvarov@gmail.com --suppress-cc=all %s to_send*.patch" % options) |
| 127 | |
| 128 | |
| 129 | #for x in repo.iter_issues(): |
| 130 | # print"----------------", x.number, "--------" |
| 131 | # print x.pull_request |
| 132 | # print "Label:" |
| 133 | # for l in x.iter_labels(): |
| 134 | # print l |
| 135 | # print "Comments:" |
| 136 | # for c in x.iter_comments(): |
| 137 | # print c.body_text |
| 138 | # x.add_labels("Maxlabel") |
| 139 | # email_patches(x.pull_request['patch_url']) |
| 140 | |
| 141 | for p in repo.iter_pulls(): |
| 142 | edata= {} |
| 143 | #print p |
| 144 | #print p.base.sha |
| 145 | #print p.head.label |
| 146 | #print p.body |
| 147 | #print p.body_text |
| 148 | print "Pull request %d" % p.number |
| 149 | |
| 150 | edata['url'] = p.html_url |
| 151 | edata['head_label'] = p.head.label |
| 152 | edata['number'] = p.number |
| 153 | edata['base_sha'] = p.base.sha |
| 154 | edata['patch_url'] = p.patch_url |
| 155 | edata['merge_commit_sha'] = p.merge_commit_sha |
| 156 | edata['title'] = p.title |
| 157 | edata['body_text'] = p.body_text |
| 158 | edata['branch'] = p.base.ref |
| 159 | |
| 160 | |
| 161 | if p.state != "open": |
| 162 | continue |
| 163 | |
| 164 | m = re.search(r"\[(PATCH.*)\] (.*)", p.title) |
| 165 | if not m: |
| 166 | print("no PATCH in head, skipping\n") |
| 167 | continue |
| 168 | |
| 169 | edata['prefix'] = m.group(1) |
| 170 | |
| 171 | issue = repo.issue(p.number) |
| 172 | |
| 173 | #for c in p.iter_commits(): |
| 174 | # print c.sha |
| 175 | |
| 176 | print "Label:" |
| 177 | skip = 0 |
| 178 | for l in issue.iter_labels(): |
| 179 | if l.name == "Email_sent": |
| 180 | print l.name |
| 181 | #if p.number == 21: |
| 182 | # break |
| 183 | skip = 1 |
| 184 | break |
| 185 | if l.name == "No_Email_sent": |
| 186 | print l.name |
| 187 | skip = 1 |
| 188 | break |
| 189 | if skip: |
| 190 | print "skipping sending email for PR %d\n" % p.number |
| 191 | continue |
| 192 | |
| 193 | |
| 194 | #print "Comments:" |
| 195 | #for c in p.iter_issue_comments(): |
| 196 | # print c.body_text |
| 197 | |
| 198 | email_patches(edata) |
| 199 | issue.add_labels("Email_sent") |