forked from gperciva/git-cl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projecthosting_upload.py
executable file
·200 lines (178 loc) · 6.95 KB
/
projecthosting_upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
import sys
import re
import os.path
# API docs:
# http://code.google.com/p/support/wiki/IssueTrackerAPIPython
import gdata.projecthosting.client
import gdata.projecthosting.data
import gdata.gauth
import gdata.client
import gdata.data
import atom.http_core
import atom.core
def string_is_number(case):
try :
int(case)
return True
except :
return False
class PatchBot():
client = gdata.projecthosting.client.ProjectHostingClient()
# you can use mewes for complete junk testing
#PROJECT_NAME = "mewes"
PROJECT_NAME = "lilypond"
username = None
password = None
def __init__(self):
# both of these bail if they fail
self.get_credentials()
self.login()
def get_credentials(self):
# TODO: can we use the coderview cookie for this?
#filename = os.path.expanduser("~/.codereview_upload_cookies")
filename = os.path.expanduser("~/.lilypond-project-hosting-login")
try:
login_data = open(filename).readlines()
self.username = login_data[0]
self.password = login_data[1]
except:
print "Could not find stored credentials"
print " %(filename)s" % locals()
print "Please enter login details manually"
print
import getpass
print "Username (google account name):"
self.username = raw_input().strip()
self.password = getpass.getpass()
def login(self):
try:
self.client.client_login(
self.username, self.password,
source='lilypond-patch-handler', service='code')
except:
print "Incorrect username or password"
sys.exit(1)
def create_issue(self, subject, description):
"""Create an issue."""
issue = self.client.add_issue(
self.PROJECT_NAME,
"Patch: " + subject,
description,
self.username,
owner = self.username,
status = "Started",
labels = ["Type-Enhancement", "Patch-new"])
# get the issue number extracted from the URL
issue_id = int(re.search("[0-9]+", issue.id.text).group(0))
# update the issue to set the owner
return self.update_issue(issue_id, "")
def generate_devel_error(self, issue_id) :
print "WARNING: could not change issue labels;"
if issue_id is None:
print "please email lilypond-devel with a general",
print "description of the problem"
else:
print "please email lilypond-devel with the issue",
print "number: %s" % issue_id
def update_issue(self, issue_id, description):
try:
issue = self.client.update_issue(
self.PROJECT_NAME,
issue_id,
self.username,
comment = description,
owner = self.username,
status = "Started",
labels = ["Patch-new"])
# TODO: this is a bit hack-ish, but I'm new to exceptions
except gdata.client.RequestError as err:
if err.body == "No permission to edit issue":
if description != "":
issue = self.client.update_issue(
self.PROJECT_NAME,
issue_id,
self.username,
comment = description)
self.generate_devel_error(issue_id)
print err.body
elif err.body == "There were no updates performed.":
pass
else:
self.generate_devel_error(issue_id)
print err.body
return None
return issue_id
def find_fix_issue_id(self, text):
splittext = re.findall(r'\w+', text)
issue_id = None
# greedy search for the issue id
for i, word in enumerate(splittext):
if word in ["fix", "issue", "Fix", "Issue"]:
try:
maybe_number = splittext[i+1]
if maybe_number[-1] == ")":
maybe_number = maybe_number[:-1]
issue_id = int(maybe_number)
break
except:
pass
if not issue_id:
try:
maybe_number = re.findall(r'\([0-9]+\)', text)
issue_id = int(maybe_number[0][1:-1])
except:
pass
return issue_id
def query_user(self, issue = None) :
query_string1 = "We were not able to associate this patch with a google tracker issue." if issue == None else str(issue)+" will not be used as a google tracker number."
print query_string1
info = raw_input("Please enter a valid google tracker issue number\n"
"(or enter nothing to create a new issue): ")
while (info != '') and (not string_is_number(info)) :
info = raw_input("This is an invalid entry. Please enter either an issue number (just digits, no spaces) or nothing to create an issue: ")
if info == '' :
info = -1
return int(info)
def upload(self, issue, patchset, subject="", description="", issue_id=None):
if not subject:
subject = "new patch"
description = description + "\n\n" + "http://codereview.appspot.com/" + issue
# update or create?
if not issue_id:
issue_id = self.find_fix_issue_id(subject+' '+description)
if issue_id:
print "This has been identified with code.google.com issue "+str(issue_id)+"."
correct = raw_input("Is this correct? [y/n (y)]")
if correct != 'n' :
issue_id = self.update_issue(issue_id, description)
else :
issue_id = self.query_user(issue_id)
if issue_id > 0 :
issue_id = self.update_issue(issue_id, description)
else :
issue_id = self.create_issue(subject, description)
else:
issue_id = self.query_user(issue_id)
if issue_id > 0 :
issue_id = self.update_issue(issue_id, description)
else :
issue_id = self.create_issue(subject, description)
return issue_id
# hacky integration
def upload(issue, patchset, subject="", description="", issue_id=None):
patchy = PatchBot()
status = patchy.upload(issue, patchset, subject, description, issue_id)
if status:
print "Tracker issue done: %s" % status
else:
print "Problem with the tracker issue"
return status
def test_find_number():
patchy = PatchBot()
print patchy.find_fix_issue_id("Fix 123")
print patchy.find_fix_issue_id("(Issue 123)")
print patchy.find_fix_issue_id("(123)")
##test_find_number()
#upload("rietveld_issue_id", None, "test issue", "blah")
#upload("rietveld_issue_id", None, "test fix 1", "blah")