-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteamcitycli.py
More file actions
290 lines (225 loc) · 7.71 KB
/
teamcitycli.py
File metadata and controls
290 lines (225 loc) · 7.71 KB
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
#!/usr/bin/env python
import json
import click
from colorclass import Color
import pygments.formatters
import pygments.lexers
from pyteamcity import TeamCity, HTTPError
import terminaltables
lexer = pygments.lexers.get_lexer_by_name('json')
formatter = pygments.formatters.TerminalFormatter()
default_build_list_columns = 'status,id,number,buildTypeId,branchName'
default_project_list_columns = 'name,id,parentProjectId'
def output_json_data(data):
output = json.dumps(data, indent=4)
output = pygments.highlight(output, lexer, formatter).strip()
click.echo(output)
@click.group()
@click.pass_context
def cli(ctx):
"""CLI for interacting with TeamCity"""
ctx.obj = TeamCity()
@cli.group()
def build():
"""Commands related to builds"""
@cli.group()
def project():
"""Commands related to projects"""
@cli.group()
def change():
"""Commands related to changes"""
@cli.group()
def server():
"""Commands related to the server instance"""
@cli.group()
def user():
"""Commands related to users"""
@server.command(name='info')
@click.pass_context
def server_info(ctx):
"""Display info about TeamCity server"""
data = ctx.obj.get_server_info()
output_json_data(data)
@project.command(name='list')
@click.option('--parent-project-id', default=None,
help='parent_project_id to filter on')
@click.option('--output-format', default='table',
type=click.Choice(['table', 'json']),
help='Output format')
@click.option('--columns', default=default_project_list_columns,
help='comma-separated list of columns to show in table')
@click.pass_context
def project_list(ctx, parent_project_id, output_format, columns):
"""Display list of projects"""
data = ctx.obj.get_projects(parent_project_id=parent_project_id)
if output_format == 'table':
column_names = columns.split(',')
output_table(column_names, data)
elif output_format == 'json':
output_json_data(data)
@project.command(name='show')
@click.pass_context
@click.argument('args', nargs=-1)
def project_show(ctx, args):
"""Display info for selected projects"""
for project_id in args:
data = ctx.obj.get_project_by_project_id(project_id)
output_json_data(data)
@build.command(name='list')
@click.option('--show-url/--no-show-url', default=False,
help='Show URL for request')
@click.option('--show-data/--no-show-data', default=True,
help='Show data retrieved from request')
@click.option('--start', default=0, help='Start index')
@click.option('--count', default=100, help='Max number of items to show')
@click.option('--build-type-id', default=None, help='buildTypeId to filter on')
@click.option('--branch', default=None, help='branch to filter on')
@click.option('--status', default=None, help='filter on build status',
type=click.Choice(['success', 'failure', 'error']))
@click.option('--tags', default=None,
help='comma-delimited list of build tags '
'(only builds containing all the specified tags '
'are returned)')
@click.option('--user', default=None,
help='limit builds to only those triggered '
'by the user specified')
@click.option('--output-format', default='table',
type=click.Choice(['table', 'json']),
help='Output format')
@click.option('--columns', default=default_build_list_columns,
help='comma-separated list of columns to show in table')
@click.pass_context
def build_list(ctx, show_url, show_data,
start, count,
build_type_id, branch, status, tags, user,
output_format, columns):
"""Display list of builds"""
kwargs = {'start': start,
'count': count}
if build_type_id:
kwargs['build_type_id'] = build_type_id
if branch:
kwargs['branch'] = branch
if status:
kwargs['status'] = status
if tags:
kwargs['tags'] = tags
if user:
kwargs['user'] = user
func = ctx.obj.get_builds
if show_url:
kwargs['return_type'] = 'url'
url = func(**kwargs)
del kwargs['return_type']
click.echo(url)
if not show_data:
return
try:
data = func(**kwargs)
except HTTPError as e:
click.echo('url: %s' % e.url)
click.echo('status_code: %s' % e.status_code)
click.echo()
click.echo(e)
return
click.echo('count: %d' % data['count'])
if data['count'] == 0:
return
if output_format == 'table':
column_names = columns.split(',')
output_table(column_names, data['build'])
elif output_format == 'json':
output_json_data(data)
def output_table(column_names, data):
table_data = [column_names]
for item in data:
row = [str(item.get(column_name, 'N/A'))
for column_name in column_names]
colorize_row(row)
table_data.append(row)
table = terminaltables.SingleTable(table_data)
click.echo(table.table)
def colorize_row(row):
for idx, value in enumerate(row):
if value == 'SUCCESS':
row[idx] = colorize(value, 'green')
elif value == 'FAILURE':
row[idx] = colorize(value, 'red')
def colorize(s, color, auto=True):
tag = '%s%s' % ('auto' if auto else '', color)
return Color('{%s}%s{/%s}' % (tag, s, tag))
@build.group(name='show')
def build_show():
"""Show statistics/tags/etc. for builds"""
@build_show.command(name='statistics')
@click.pass_context
@click.argument('args', nargs=-1)
def build_show_statistics(ctx, args):
"""Display info for selected build(s)"""
for build_id in args:
data = ctx.obj.get_build_statistics_by_build_id(build_id)
output_json_data(data)
@build_show.command(name='tags')
@click.pass_context
@click.argument('args', nargs=-1)
def build_show_tags(ctx, args):
"""Display info for selected build(s)"""
for build_id in args:
data = ctx.obj.get_build_tags_by_build_id(build_id)
output_json_data(data)
@user.command(name='list')
@click.pass_context
def user_list(ctx):
"""Display list of users"""
data = ctx.obj.get_all_users()
output_json_data(data)
@user.command(name='show')
@click.pass_context
@click.argument('args', nargs=-1)
def user_show(ctx, args):
"""Display info for selected users"""
for user_id in args:
data = ctx.obj.get_user_by_username(user_id)
output_json_data(data)
@server.group(name='plugin')
def server_plugin():
"""Show info about server plugins"""
@server_plugin.command(name='list')
@click.pass_context
def server_plugin_list(ctx):
"""Display list of plugins"""
data = ctx.obj.get_all_plugins()
output_json_data(data)
@server.group(name='agent')
def server_agent():
"""Show info about agents"""
@server_agent.command(name='list')
@click.pass_context
def server_agent_list(ctx):
"""Display list of agents"""
data = ctx.obj.get_agents()
output_json_data(data)
@server_agent.command(name='show')
@click.pass_context
@click.argument('args', nargs=-1)
def server_agent_show(ctx, args):
"""Display info for selected agent(s)"""
for agent_id in args:
data = ctx.obj.get_agent_by_agent_id(agent_id)
output_json_data(data)
@change.command(name='list')
@click.pass_context
def change_list(ctx):
"""Display list of changes"""
data = ctx.obj.get_all_changes()
output_json_data(data)
@change.command(name='show')
@click.pass_context
@click.argument('args', nargs=-1)
def change_show(ctx, args):
"""Display info for selected changes"""
for change_id in args:
data = ctx.obj.get_change_by_change_id(change_id)
output_json_data(data)
if __name__ == '__main__':
cli()