Maxim Uvarov | 1acb99b | 2017-05-08 23:40:58 +0300 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # github pull request update script |
| 4 | # |
| 5 | # Script changes patch version and remove label Email_sent |
| 6 | # Note: version changed only on pull request update event. |
| 7 | |
| 8 | import cgi |
| 9 | import pickle |
| 10 | import sys |
| 11 | import time |
| 12 | import json |
| 13 | from StringIO import StringIO |
| 14 | import sys, urllib |
| 15 | from cgi import parse_qs, escape |
| 16 | import re |
| 17 | |
| 18 | from github3 import login |
| 19 | from github3 import pulls |
| 20 | from github3 import issues |
| 21 | import os |
| 22 | |
| 23 | |
| 24 | qin = sys.stdin.read() |
| 25 | #f = open('mr_%s.dump' % time.time(), 'w') |
| 26 | #pickle.dump(qin, f) |
| 27 | #f.close() |
| 28 | |
| 29 | #fname = "mr_1493307805.62.dump" |
| 30 | #qin = pickle.load( open(fname, "rb" ) ) |
| 31 | |
| 32 | print("Content-type: text/html\n") |
| 33 | print("""<!DOCTYPE HTML> |
| 34 | <html> |
| 35 | <head> |
| 36 | <meta charset="utf-8"> |
| 37 | <title>some title</title> |
| 38 | </head> |
| 39 | <body>""") |
| 40 | |
| 41 | io = StringIO(qin) |
| 42 | js = json.load(io) |
| 43 | |
| 44 | gh = login('login-github@domain.com', password='password') |
| 45 | me = gh.user() |
| 46 | print me |
| 47 | |
| 48 | repo = 0 |
| 49 | for r in gh.iter_repos(): |
| 50 | print r.full_name |
| 51 | if r.full_name == "Linaro/odp": |
| 52 | #if r.full_name == "muvarov/odp": |
| 53 | repo = r |
| 54 | break |
| 55 | |
| 56 | if not repo: |
| 57 | print "Repo not found" |
| 58 | sys.exit(1) |
| 59 | |
| 60 | #for key, value in js['pull_request'].iteritems() : |
| 61 | # print "\n\n\n\n----------------" |
| 62 | # print key |
| 63 | # print value |
| 64 | |
| 65 | action = js['action'] |
| 66 | if action == "synchronize" or action == "opened": |
| 67 | print("<h1>action is %s, process</h1>" % action) |
| 68 | else: |
| 69 | print("<h1>action is %s, do nothing</h1>" % action) |
| 70 | print("</body></html>") |
| 71 | sys.exit(0) |
| 72 | |
| 73 | pr_num = js['pull_request']['number'] |
| 74 | pr = repo.pull_request(pr_num) |
| 75 | issue = repo.issue(pr_num) |
| 76 | |
| 77 | |
| 78 | branch = js['pull_request']['base']['ref'] |
| 79 | print "branch = %s\n" % branch |
| 80 | |
| 81 | title = issue.title |
| 82 | |
| 83 | version = 0 |
| 84 | for m in re.finditer(r'PATCH.*v([0-9]+)', title): |
| 85 | version = int(m.group(1)) |
| 86 | |
| 87 | version += 1 |
| 88 | |
| 89 | m = re.search(r"\[PATCH.*\] (.*)", title) |
| 90 | if m: |
| 91 | title = m.group(1) |
| 92 | |
| 93 | if branch == "api-next": |
| 94 | issue.edit(title="[PATCH API-NEXT v%d] %s" % (version, title)) |
| 95 | else: |
| 96 | issue.edit(title="[PATCH v%d] %s" % (version, title)) |
| 97 | print issue.title |
| 98 | |
| 99 | commits = js['pull_request']['commits'] |
| 100 | if commits > 20: |
| 101 | issue.add_labels("No_Email_sent") |
| 102 | else: |
| 103 | # return code does not reflect if event was actually |
| 104 | # removed |
| 105 | issue.remove_label("Email_sent") |
| 106 | |
| 107 | print "body_text %s\n" % issue.body_text |
| 108 | |
| 109 | |
| 110 | print("<h1>all ok!</h1>") |
| 111 | print("</body></html>") |