forked from SurveyMonkey/pyteamcity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyteamcity.py
executable file
·586 lines (476 loc) · 18.3 KB
/
pyteamcity.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
"""
RESTful api definition: http://${TeamCity}/guestAuth/app/rest/application.wadl
"""
import collections
import inspect
import os
import re
import textwrap
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup
import requests
class ConnectionError(Exception):
def __init__(self, host, port, orig_exception=None):
self.host = host
self.port = port
self.orig_exception = orig_exception
def __str__(self):
return 'Failed to connect to %s:%s' % (self.host, self.port)
class HTTPError(Exception):
url = None
status_code = None
def __init__(self, msg, url, status_code):
super(HTTPError, self).__init__(msg)
self.url = url
self.status_code = status_code
def _underscore_to_camel_case(s):
words = s.split('_')
words = [words[0].lower()] + [w.title() for w in words[1:]]
return ''.join(words)
def _build_url(*args, **kwargs):
"""Builds a new API url from scratch."""
parts = [kwargs.get('base_url')]
parts.extend(args)
parts = [str(p) for p in parts]
return '/'.join(parts)
def get_default_kwargs(func):
"""Returns a sequence of tuples (kwarg_name, default_value) for func"""
argspec = inspect.getargspec(func)
if not argspec.defaults:
return []
return zip(argspec.args[-len(argspec.defaults):],
argspec.defaults)
def endpoint(url_pattern, method='GET'):
def wrapped_func(f):
def get_url(*args, **kwargs):
# kwargs with default values declared by function
all_kwargs = dict(get_default_kwargs(f))
# kwargs for positional arguments passed by caller
groups = re.findall('{(\w+)}', url_pattern)
all_kwargs.update(dict(zip(groups, args)))
# kwargs for keyword arguments passed by caller
all_kwargs.update(kwargs)
return _build_url(url_pattern.format(*args, **all_kwargs),
**all_kwargs)
def inner_func(self, *args, **kwargs):
kwargs['base_url'] = self.base_url
url = get_url(*args, **kwargs)
request = self._get_request('GET', url)
return_type = kwargs.get('return_type', 'data')
if return_type == 'url':
return url
if return_type == 'request':
return request
if method == 'GET':
response = self._get(url)
elif method == 'POST':
response = self._post(url)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
raise HTTPError(response.text,
url=url,
status_code=response.status_code)
try:
return response.json()
except Exception as e:
return response.text
return inner_func
return wrapped_func
def GET(url_pattern):
return endpoint(url_pattern, method='GET')
def POST(url_pattern):
return endpoint(url_pattern, method='POST')
class TeamCity:
username = None
password = None
server = None
port = None
error_handler = None
def __init__(self, username=None, password=None, server=None, port=None,
session=None):
self.username = username or os.getenv('TEAMCITY_USER')
self.password = password or os.getenv('TEAMCITY_PASSWORD')
self.host = server or os.getenv('TEAMCITY_HOST')
self.port = port or int(os.getenv('TEAMCITY_PORT', 0)) or 80
self.base_base_url = "http://%s:%d" % (self.host, self.port)
self.guest_auth_base_url = "http://%s:%d/guestAuth" % (
self.host, self.port)
if self.username and self.password:
self.base_url = "http://%s:%d/httpAuth/app/rest" % (
self.host, self.port)
self.auth = (self.username, self.password)
else:
self.base_url = "http://%s:%d/guestAuth/app/rest" % (
self.host, self.port)
self.auth = None
self.session = session or requests.Session()
self._agent_cache = {}
def get_url(self, path):
return '/'.join([self.base_base_url, path])
def _get_request(self, verb, url, headers=None, **kwargs):
if headers is None:
headers = {'Accept': 'application/json'}
return requests.Request(
verb,
url,
auth=self.auth,
headers=headers,
**kwargs).prepare()
def _get(self, url, **kwargs):
request = self._get_request('GET', url, **kwargs)
return self._send_request(request)
def _post(self, url, **kwargs):
request = self._get_request('POST', url, **kwargs)
return self._send_request(request)
def _put(self, url, **kwargs):
request = self._get_request('PUT', url, **kwargs)
return self._send_request(request)
def _send_request(self, request):
try:
return self.session.send(request)
except requests.exceptions.ConnectionError as e:
new_exception = ConnectionError(self.host, self.port, e)
if self.error_handler:
self.error_handler(new_exception)
else:
raise new_exception
@GET('server')
def get_server_info(self):
"""
Gets server info of the TeamCity server pointed to by this instance of
the Client.
"""
@GET('server/plugins')
def get_all_plugins(self):
"""
Gets all plugins in the TeamCity server pointed to by this instance of
the Client.
"""
def get_builds(self,
build_type_id='', branch='', status='', running='',
tags=None,
user=None, project='',
start=0, count=100, **kwargs):
_get_locator_kwargs = {}
if branch:
_get_locator_kwargs['branch'] = branch
if build_type_id:
_get_locator_kwargs['build_type'] = build_type_id
if status:
_get_locator_kwargs['status'] = status
if running:
_get_locator_kwargs['running'] = running
if tags:
_get_locator_kwargs['tags'] = tags
if user:
_get_locator_kwargs['user'] = user
if project:
_get_locator_kwargs['project'] = project
locator = self._get_locator(**_get_locator_kwargs)
if locator:
return self._get_all_builds_locator(
locator=locator,
start=start, count=count,
**kwargs)
else:
return self._get_all_builds(
start=start, count=count,
**kwargs)
def _get_locator(self, **kwargs):
if not kwargs:
return ''
# Sort kwargs.items() so that ordering is deterministic, which is very
# handy for automated tests
return ','.join('%s:%s' % (_underscore_to_camel_case(k), v)
for k, v in sorted(kwargs.items()) if v)
@GET('builds/?start={start}&count={count}')
def _get_all_builds(self, start=0, count=100):
"""
Gets all builds in the TeamCity server pointed to by this instance of
the Client.
This can be very large since it is historic data. Therefore the count
can be limited.
:param start: what build number to start from
:param count: how many builds to return
"""
@GET('builds/?locator={locator}&start={start}&count={count}')
def _get_all_builds_locator(self, locator='', start=0, count=100):
"""
Gets all builds in the TeamCity server pointed to by this instance of
the Client.
This can be very large since it is historic data. Therefore the count
can be limited.
:param start: what build number to start from
:param count: how many builds to return
"""
@GET('builds/id:{build_id}')
def get_build_by_build_id(self, build_id):
"""
Gets a build with build ID `bId`.
:param build_id: the build to get, in the format [0-9]+
"""
@GET('changes?start={start}&count={count}')
def get_all_changes(self, start=0, count=10):
"""
Gets all changes made in the TeamCity server pointed to by this
instance of the Client.
"""
@GET('changes/id:{change_id}')
def get_change_by_change_id(self, change_id):
"""
Gets a particular change with change ID `cId`.
:param change_id: the change to get, in the format [0-9]+
"""
@GET('changes/build:id:{build_id}')
def get_changes_by_build_id(self, build_id):
"""
Gets changes in a build for a build ID `build_id`.
:param build_id: the build to get changes of in the format [0-9]+
"""
def get_build_types(self, project='', affected_project='',
template_flag=None,
**kwargs):
"""
Gets all build types in the TeamCity server pointed to by this instance
of the Client.
"""
_get_locator_kwargs = {}
if project:
_get_locator_kwargs['project'] = project
if affected_project:
_get_locator_kwargs['affected_project'] = affected_project
if template_flag:
_get_locator_kwargs['template_flag'] = template_flag
locator = self._get_locator(**_get_locator_kwargs)
if locator:
return self._get_all_build_types_locator(
locator=locator,
**kwargs)
else:
return self._get_all_build_types(**kwargs)
@GET('buildTypes/?locator={locator}')
def _get_all_build_types_locator(self, locator=''):
"""
Gets all build types in the TeamCity server pointed to by this instance
of the Client.
"""
@GET('buildTypes/')
def _get_all_build_types(self):
"""
Gets all build types in the TeamCity server pointed to by this instance
of the Client.
"""
@GET('buildTypes/id:{build_type_id}')
def get_build_type(self, build_type_id):
"""
Gets details for a build type with id `build_type_id`.
:param build_type_id: the build type to get, in format bt[0-9]+
"""
def get_projects(self, parent_project_id=None, **kwargs):
return_type = kwargs.get('return_type')
all_projects_data = self._get_all_projects(**kwargs)
if parent_project_id is None or return_type in ('url', 'request'):
return all_projects_data
projects = [project for project in all_projects_data['project']
if parent_project_id == project.get('parentProjectId')]
ret = {'count': len(projects),
'project': projects}
return ret
@GET('buildQueue')
def get_queued_builds(self):
"""
Gets queued builds
"""
@GET('buildQueue/id:{build_id}')
def get_queued_build_by_build_id(self, build_id):
"""
Gets a queued build with build ID `build_id`.
:param build_id: the build to get, in the format [0-9]+
"""
def trigger_build(
self,
build_type_id, branch=None,
comment=None, parameters=None, agent_id=None):
"""
Trigger a new build
"""
url = _build_url('buildQueue', base_url=self.base_url)
data = self._get_build_node(
build_type_id, branch,
comment, parameters, agent_id)
response = self._post(
url,
headers={'Content-Type': 'application/xml'},
data=data)
root = ET.fromstring(response.text)
new_build_attributes = root.findall('.')[0].attrib
return new_build_attributes
def _get_build_node(
self,
build_type_id, branch=None,
comment=None, parameters=None, agent_id=None):
build_attributes = ''
if branch:
build_attributes = 'branchName="%s"' % branch
if build_attributes:
data = '<build %s>\n' % build_attributes
else:
data = '<build>\n'
data += ' <buildType id="%s"/>\n' % build_type_id
if agent_id:
data += ' <agent id="%s"/>\n' % agent_id
if comment:
data += ' <comment><text>%s</text></comment>\n' % comment
if parameters:
data += ' <properties>\n'
data += ''.join([
' <property name="%s" value="%s"/>\n' % (name, value)
for name, value in parameters.items()])
data += ' </properties>\n'
data += '</build>\n'
return data
@GET('projects')
def _get_all_projects(self):
"""
Gets all projects in the TeamCity server pointed to by this instance of
the Client.
"""
def create_project(self, name):
"""
Create project
"""
url = _build_url('projects', base_url=self.base_url)
data = {'name': name}
headers = {'Content-Type': 'application/json',
'Accept': 'application/json'}
return self.session.post(url, json=data, headers=headers)
@GET('projects/id:{project_id}')
def get_project_by_project_id(self, project_id):
"""
Gets details for a project with ID `project_id`.
:param project_id: the project ID to get, in format project[0-9]+
"""
@GET('agents')
def get_agents(self):
"""
Gets all agents in the TeamCity server pointed to by this instance of
the Client.
"""
@GET('agents/id:{agent_id}')
def get_agent_by_agent_id(self, agent_id):
"""
Gets details for an agent with ID `agent_id`.
:param agent_id: the agent ID to get, in format [0-9]+
"""
def get_agent_statistics(self):
counters = collections.Counter()
counters['by_build_type'] = collections.Counter()
for agent in self.get_agents()['agent']:
counters['num_total'] += 1
build_text = self.get_agent_build_type(agent['id'])
counters['by_build_type'][build_text] += 1
if 'Idle' in build_text:
counters['num_idle'] += 1
else:
counters['num_busy'] += 1
return counters
def get_agent_build_type(self, agent_id):
data = self._fetch_agent_details(agent_id)
return data['build_type']
def get_agent_build_text(self, agent_id):
data = self._fetch_agent_details(agent_id)
return data['build_text']
def _fetch_agent_details(self, agent_id):
data = self._agent_cache.get(agent_id)
if data:
return data
url = self.get_url('/agentDetails.html?id=%s' % agent_id)
agent_details_response = self._get(url)
html_doc = agent_details_response.text
if 'Running build' not in html_doc:
build_type = build_text = 'Idle'
else:
soup = BeautifulSoup(html_doc)
build_type_node = soup.find(class_='buildTypeName')
build_text_node = soup.find(id=re.compile('build:(?P<build_type>\d+):text'))
build_type = build_type_node.text.replace('\n', '').replace('\r', '')
build_text = build_text_node.text
data = {
'build_type': build_type,
'build_text': build_text,
}
self._agent_cache[agent_id] = data
return data
@GET('builds/id:{build_id}/statistics')
def get_build_statistics_by_build_id(self, build_id):
"""
Gets statistics for a build with ID `build_id`.
Statistics include `BuildDuration`, `FailedTestCount`,
`TimeSpentInQueue`, and more.
:param build_id: the build ID to get, in format [0-9]+
"""
@GET('builds/id:{build_id}/tags')
def get_build_tags_by_build_id(self, build_id):
"""
Gets tags for a build with ID `build_id`.
:param build_id: the build ID to get, in format [0-9]+
"""
@GET('builds/id:{build_id}/resulting-properties')
def get_build_parameters_by_build_id(self, build_id):
"""
Gets parameters for a build with ID `build_id`.
:param build_id: the build ID to get, in format [0-9]+
"""
@GET('builds/id:{build_id}/artifacts/{data_type}/{artifact_relative_name}')
def get_build_artifacts_by_build_id(self, build_id, data_type, artifact_relative_name):
"""
Gets artifacts for a build with ID `build_id`.
:param build_id: the build ID to get, in format [0-9]+
"""
def get_build_log_by_build_id(self, build_id):
"""
Gets log for a build with ID `build_id`.
:param build_id: the build ID to get, in format [0-9]+
"""
url = _build_url(
'downloadBuildLog.html?buildId={build_id}'.format(
build_id=build_id),
base_url=self.guest_auth_base_url)
return self._get(url)
@GET('vcs-roots')
def get_all_vcs_roots(self):
"""
Gets all VCS roots in the TeamCity server pointed to by this instance
of the Client.
"""
@GET('vcs-roots/id:{vcs_root_id}')
def get_vcs_root_by_vcs_root_id(self, vcs_root_id):
"""
Gets a VCS root with the specified ID `vcs_root_id`.
:param vcs_root_id: the VCS root to get
"""
@GET('users')
def get_all_users(self):
"""
Gets all users in the TeamCity server pointed to by this instance of
the Client.
"""
@GET('users/username:{username}')
def get_user_by_username(self, username):
"""
Gets user details for a given username.
:param username: the username to get details for.
"""
def get_project_params(self, proj_id):
"""Returns project parameters dictionary with values """
proj_params = dict([(x['name'],x.get('value')) for x in self.get_project_by_project_id(proj_id)['parameters']['property']])
return proj_params
def reset_build_counter(self, build_type_id):
""" Resets the build types build counter """
url = _build_url('buildTypes',
'id:{build_type_id}'.format(build_type_id = build_type_id),
'settings',
'buildNumberCounter',
base_url= self.base_url)
return self.session.put(url=url, auth=(self.username, self.password),data='0')