forked from NaN-tic/tryton-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
274 lines (211 loc) · 7.61 KB
/
project.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
import os
import ssl
import sys
import hgapi
from invoke import run, task, Collection
from .config import get_config
from . import reviewboard
from .scm import hg_pull, hg_clone, _module_version
from .utils import t
import logging
# from .bucket import pullrequests
import choice
try:
from proteus import config as pconfig, Model
except ImportError as e:
print("trytond importation error: ", e, file=sys.stderr)
os.environ['TZ'] = "Europe/Madrid"
settings = get_config()
logger = logging.getLogger("nan-tasks")
def get_tryton_connection():
tryton = settings['tryton']
try:
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
return pconfig.set_xmlrpc(tryton['server'], context=ssl_context)
except AttributeError:
# If python is older than 2.7.9 it doesn't have
# ssl.create_default_context() but it neither verify certificates
return pconfig.set_xmlrpc(tryton['server'])
@task
def ct(log_file):
get_tryton_connection()
create_test_task(log_file)
def create_test_task(log_file):
get_tryton_connection()
settings = get_config()
tryton = settings['tryton']
Project = Model.get('project.work')
Employee = Model.get('company.employee')
Party = Model.get('party.party')
Tracker = Model.get('project.work.tracker')
employee = Employee(int(tryton.get('default_employee_id')))
parent = Project(int(tryton.get('default_project_id')))
party = Party(int(tryton.get('default_party_id')))
tracker = Tracker(int(tryton.get('default_tracker_id')))
f = open(log_file, 'r')
lines = []
for line in f.readlines():
if 'init' in line or 'modules' in line:
continue
lines.append(line)
f.close()
work = Project()
work.type = 'task'
work.product = None
work.timesheet_work_name = 'Test Exception'
work.parent = parent
work.tracker = tracker
work.party = party
work.problem = "\n".join(lines)
work.assigned_employee = employee
work.save()
@task()
def fetch_review(ctx, work):
get_tryton_connection()
Review = Model.get('project.work.codereview')
reviews = Review.find([('work.code', '=', work), ('state', '=', 'opened')])
for review in reviews:
if review.component:
path = os.path.join('modules', review.component.name)
else:
path = ''
if not os.path.exists(path):
cl = review.url.split('/')[:-2]
clone_url = "/".join(cl)
hg_clone(clone_url, path, review.branch)
hg_pull(review.component.name, path, update=True,
branch=review.branch)
def get_request_info(url):
rs = url.split('/')
owner, repo, request_id = rs[-4], rs[-3], rs[-1]
return owner, repo, request_id
def show_review(review):
print("{id} - {name} - {url}".format(
id=review.id, name=review.name, url=review.url))
@task()
def components(ctx, database):
get_tryton_connection()
DBComponent = Model.get('nantic.database.component')
components = DBComponent.find([('database.name', '=', database),
('state', '=', 'accepted')])
for component in components:
print(component.component.name)
@task()
def check_migration(ctx, database, version=3.4):
module_table = 'ir_module'
if version == 3.4:
module_table = 'ir_module_module'
output = run('psql -A -d %s -c "select name from %s'
' where state=\'installed\'"' % (database, module_table), hide='both')
modules = [x.strip() for x in output.stdout.split('\n')]
_module_version(modules[1:-1])
@task()
def decline_review(ctx, work, review_id=None, message=None):
get_tryton_connection()
Review = Model.get('project.work.codereview')
Task = Model.get('project.work')
tasks = Task.find([('code', '=', work)])
if not tasks:
print(t.red('Error: Task %s was not found.' % work), file=sys.stderr)
sys.exit(1)
w = tasks[0]
reviews = Review.find([('work', '=', w.id), ('state', '=', 'opened')])
for review in reviews:
if review_id and str(review.id) != review_id:
print(review_id, review.id)
continue
show_review(review)
if not review_id:
continue
confirm = choice.Binary('Are you sure you want to decline?',
False).ask()
if confirm:
owner, repo, request_id = get_request_info(review.url)
res = pullrequests.decline(owner, repo, request_id, message)
if res and res['state'] == 'MERGED':
review.state = 'closed'
review.save()
@task()
def merge_review(ctx, work, review_id=None, message=None):
get_tryton_connection()
Review = Model.get('project.work.codereview')
Task = Model.get('project.work')
tasks = Task.find([('code', '=', work)])
if not tasks:
print(t.red('Error: Task %s was not found.' % work), file=sys.stderr)
sys.exit(1)
w = tasks[0]
reviews = Review.find([('work', '=', w.id), ('state', '=', 'opened')])
for review in reviews:
if review_id and str(review.id) != review_id:
print(review_id, review.id)
continue
show_review(review)
if not review_id:
continue
confirm = choice.Binary('Are you sure you want to merge?', False).ask()
if confirm:
owner, repo, request_id = get_request_info(review.url)
res = pullrequests.merge(owner, repo, request_id, message)
if res and res['state'] == 'MERGED':
review.state = 'closed'
review.save()
@task()
def upload_review(ctx, work, path, branch='default', module=None):
get_tryton_connection()
Review = Model.get('project.work.codereview')
Task = Model.get('project.work')
Component = Model.get('project.work.component')
tasks = Task.find([('code', '=', work)])
if not tasks:
print(t.red('Error: Task %s was not found.' % work), file=sys.stderr)
sys.exit(1)
work = tasks[0]
if not module:
module = os.path.realpath(path).split('/')[-1]
components = Component.find([('name', '=', module)], limit=1)
if not components:
component = Component(name=module)
component.save()
else:
component, = components
repo = hgapi.Repo(path)
url = repo.config('paths', 'default')
if url:
url_list = url.split('/')
owner, repo_name = (url_list[-2], url_list[-1])
else:
owner = 'nantic'
repo_name, = path.split('/')[-1:]
review = reviewboard.create(ctx, path, module, work.rec_name,
(work.problem or work.rec_name) + '\n' + (work.solution or ''), work.code)
review_id = review
review = Review.find([
('review_id', '=', str(review_id)),
('work', '=', work.id),
])
if not review:
review = Review()
else:
review = review[0]
review.name = "[{module}]-{task_name}".format(
module=module, task_name=work.rec_name.encode('utf-8'))
review.review_id = str(review_id)
review.url = ('http://reviews.nan-tic.com/r/{id}').format(
owner=owner,
repo=repo_name,
id=review_id)
review.work = work
review.branch = branch
review.component = component
review.save()
ProjectCollection = Collection()
ProjectCollection.add_task(ct)
ProjectCollection.add_task(components)
ProjectCollection.add_task(check_migration)
ProjectCollection.add_task(upload_review)
# ProjectCollection.add_task(merge_review)
# ProjectCollection.add_task(fetch_review)