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 | |
Maxim Uvarov | a9c4fdd | 2017-08-11 14:34:49 +0300 | [diff] [blame] | 23 | configfile = '/home/muvarov/gscripts_config.py' |
| 24 | sys.path.append(os.path.dirname(os.path.expanduser(configfile))) |
| 25 | import gscripts_config as gcfg |
| 26 | |
| 27 | gh_login = gcfg.gcfg['gh']['login'] |
| 28 | gh_password = gcfg.gcfg['gh']['pass'] |
| 29 | |
Maxim Uvarov | 1acb99b | 2017-05-08 23:40:58 +0300 | [diff] [blame] | 30 | |
| 31 | qin = sys.stdin.read() |
| 32 | #f = open('mr_%s.dump' % time.time(), 'w') |
| 33 | #pickle.dump(qin, f) |
| 34 | #f.close() |
| 35 | |
| 36 | #fname = "mr_1493307805.62.dump" |
| 37 | #qin = pickle.load( open(fname, "rb" ) ) |
| 38 | |
| 39 | print("Content-type: text/html\n") |
| 40 | print("""<!DOCTYPE HTML> |
| 41 | <html> |
| 42 | <head> |
| 43 | <meta charset="utf-8"> |
| 44 | <title>some title</title> |
| 45 | </head> |
| 46 | <body>""") |
| 47 | |
| 48 | io = StringIO(qin) |
| 49 | js = json.load(io) |
| 50 | |
Maxim Uvarov | a9c4fdd | 2017-08-11 14:34:49 +0300 | [diff] [blame] | 51 | gh = login(gh_login, password=gh_password) |
Maxim Uvarov | 1acb99b | 2017-05-08 23:40:58 +0300 | [diff] [blame] | 52 | me = gh.user() |
| 53 | print me |
| 54 | |
| 55 | repo = 0 |
| 56 | for r in gh.iter_repos(): |
| 57 | print r.full_name |
| 58 | if r.full_name == "Linaro/odp": |
| 59 | #if r.full_name == "muvarov/odp": |
| 60 | repo = r |
| 61 | break |
| 62 | |
| 63 | if not repo: |
| 64 | print "Repo not found" |
| 65 | sys.exit(1) |
| 66 | |
| 67 | #for key, value in js['pull_request'].iteritems() : |
| 68 | # print "\n\n\n\n----------------" |
| 69 | # print key |
| 70 | # print value |
| 71 | |
| 72 | action = js['action'] |
| 73 | if action == "synchronize" or action == "opened": |
| 74 | print("<h1>action is %s, process</h1>" % action) |
| 75 | else: |
| 76 | print("<h1>action is %s, do nothing</h1>" % action) |
| 77 | print("</body></html>") |
| 78 | sys.exit(0) |
| 79 | |
| 80 | pr_num = js['pull_request']['number'] |
| 81 | pr = repo.pull_request(pr_num) |
| 82 | issue = repo.issue(pr_num) |
| 83 | |
| 84 | |
| 85 | branch = js['pull_request']['base']['ref'] |
| 86 | print "branch = %s\n" % branch |
| 87 | |
| 88 | title = issue.title |
| 89 | |
| 90 | version = 0 |
| 91 | for m in re.finditer(r'PATCH.*v([0-9]+)', title): |
| 92 | version = int(m.group(1)) |
| 93 | |
| 94 | version += 1 |
| 95 | |
| 96 | m = re.search(r"\[PATCH.*\] (.*)", title) |
| 97 | if m: |
| 98 | title = m.group(1) |
| 99 | |
| 100 | if branch == "api-next": |
| 101 | issue.edit(title="[PATCH API-NEXT v%d] %s" % (version, title)) |
Yi He | 36f2934 | 2017-05-18 15:19:28 +0800 | [diff] [blame] | 102 | elif branch == "cloud-dev": |
| 103 | issue.edit(title="[PATCH CLOUD-DEV v%d] %s" % (version, title)) |
Maxim Uvarov | 1acb99b | 2017-05-08 23:40:58 +0300 | [diff] [blame] | 104 | else: |
| 105 | issue.edit(title="[PATCH v%d] %s" % (version, title)) |
| 106 | print issue.title |
| 107 | |
| 108 | commits = js['pull_request']['commits'] |
| 109 | if commits > 20: |
| 110 | issue.add_labels("No_Email_sent") |
| 111 | else: |
| 112 | # return code does not reflect if event was actually |
| 113 | # removed |
| 114 | issue.remove_label("Email_sent") |
| 115 | |
Maxim Uvarov | a9c4fdd | 2017-08-11 14:34:49 +0300 | [diff] [blame] | 116 | try: |
| 117 | issue.remove_label("checkpatch") |
| 118 | except: |
| 119 | pass |
| 120 | |
Maxim Uvarov | 1acb99b | 2017-05-08 23:40:58 +0300 | [diff] [blame] | 121 | print "body_text %s\n" % issue.body_text |
| 122 | |
| 123 | |
| 124 | print("<h1>all ok!</h1>") |
| 125 | print("</body></html>") |