File: _format.py

package info (click to toggle)
azure-devops-cli-extension 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,384 kB
  • sloc: python: 160,782; xml: 198; makefile: 56; sh: 51
file content (400 lines) | stat: -rw-r--r-- 12,557 bytes parent folder | download | duplicates (4)
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
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from collections import OrderedDict
import dateutil

_VALUE_TRUNCATION_LENGTH = 80


def transform_builds_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_build_row(item))
    return table_output


def transform_build_table_output(result):
    table_output = [_transform_build_row(result)]
    return table_output


def _transform_build_row(row):
    from azext_devops.dev.common.git import REF_HEADS_PREFIX
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    table_row['Number'] = row['buildNumber']
    table_row['Status'] = row['status']
    if row['result']:
        table_row['Result'] = row['result']
    else:
        table_row['Result'] = ' '
    table_row['Definition ID'] = row['definition']['id']
    table_row['Definition Name'] = row['definition']['name']

    if row['sourceBranch']:
        source_branch = row['sourceBranch']
        if source_branch[0:len(REF_HEADS_PREFIX)] == REF_HEADS_PREFIX:
            source_branch = source_branch[len(REF_HEADS_PREFIX):]
        table_row['Source Branch'] = source_branch
    else:
        table_row['Source Branch'] = ' '

    queued_time = dateutil.parser.parse(row['queueTime']).astimezone(dateutil.tz.tzlocal())
    table_row['Queued Time'] = str(queued_time.date()) + ' ' + str(queued_time.time())
    table_row['Reason'] = row['reason']
    return table_row


def transform_build_tags_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_build_tags_row(item))
    return table_output


def _transform_build_tags_row(row):
    table_row = OrderedDict()
    table_row['Tags'] = row
    return table_row


def transform_definitions_table_output(result):
    table_output = []
    include_draft_column = False
    for item in result:
        if item['quality'] == 'draft':
            include_draft_column = True
            break
    for item in result:
        table_output.append(_transform_definition_row(item, include_draft_column))
    return table_output


def transform_definition_table_output(result):
    table_output = [_transform_definition_row(result, result['quality'] == 'draft')]
    return table_output


def _transform_definition_row(row, include_draft_column=False):
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    table_row['Name'] = row['name']
    if include_draft_column:
        if row['quality'] == 'draft':
            table_row['Draft'] = True
        else:
            table_row['Draft'] = ' '
    if row['queueStatus']:
        table_row['Status'] = row['queueStatus']
    else:
        table_row['Status'] = ' '
    if row['queue']:
        table_row['Default Queue'] = row['queue']['name']
    else:
        table_row['Default Queue'] = ' '
    return table_row


def _get_task_key(row):
    return row['name'].lower()


def transform_releases_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_release_row(item))
    return table_output


def transform_release_table_output(result):
    table_output = [_transform_release_row(result)]
    return table_output


def _transform_release_row(row):
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    table_row['Name'] = row['name']
    table_row['Definition Name'] = row['releaseDefinition']['name']
    table_row['Created By'] = row['createdBy']['displayName']
    created_on = dateutil.parser.parse(row['createdOn']).astimezone(dateutil.tz.tzlocal())
    table_row['Created On'] = str(created_on.date()) + ' ' + str(created_on.time())
    table_row['Status'] = row['status']
    table_row['Description'] = row['description']
    return table_row


def transform_release_definitions_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_release_definition_row(item))
    return table_output


def transform_release_definition_table_output(result):
    table_output = [_transform_release_definition_row(result)]
    return table_output


def _transform_release_definition_row(row):
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    table_row['Name'] = row['name']
    table_row['CreatedBy'] = row['createdBy']['displayName']
    table_row['Created On'] = row['createdOn']
    return table_row


def transform_runs_artifact_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_runs_artifact_row(item))
    return table_output


def _transform_runs_artifact_row(row):
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    table_row['Name'] = row['name']
    table_row['Type'] = row['resource']['type']
    return table_row


def transform_pipelines_table_output(result):
    table_output = []
    include_draft_column = False
    for item in result:
        if item['quality'] == 'draft':
            include_draft_column = True
            break
    for item in result:
        table_output.append(_transform_pipeline_row(item, include_draft_column))
    return table_output


def transform_pipeline_table_output(result):
    table_output = [_transform_pipeline_row(result, result['quality'] == 'draft')]
    return table_output


def _transform_pipeline_row(row, include_draft_column=False):
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    path = row['path']
    table_row['Path'] = path[:50] + '..' if len(path) > 50 else path
    name = row['name']
    table_row['Name'] = name[:50] + '..' if len(name) > 50 else name
    if include_draft_column:
        if row['quality'] == 'draft':
            table_row['Draft'] = True
        else:
            table_row['Draft'] = ' '
    if row['queueStatus']:
        table_row['Status'] = row['queueStatus']
    else:
        table_row['Status'] = ' '
    if row['queue']:
        table_row['Default Queue'] = row['queue']['name']
    else:
        table_row['Default Queue'] = ' '
    return table_row


def transform_pipeline_runs_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_pipeline_run_row(item))
    return table_output


def transform_pipeline_or_run_table_output(result):
    table_output = [_transform_pipeline_or_run_row(result)]
    return table_output


def _transform_pipeline_or_run_row(row):
    if row.get("buildNumber", None):  # Hack to detect the json object is definition or run
        return _transform_pipeline_run_row(row)
    return _transform_pipeline_row(row)


def transform_pipeline_run_table_output(result):
    table_output = [_transform_pipeline_run_row(result)]
    return table_output


def _transform_pipeline_run_row(row):
    from azext_devops.dev.common.git import REF_HEADS_PREFIX
    table_row = OrderedDict()
    table_row['Run ID'] = row['id']
    table_row['Number'] = row['buildNumber']
    table_row['Status'] = row['status']
    if row['result']:
        table_row['Result'] = row['result']
    else:
        table_row['Result'] = ' '
    table_row['Pipeline ID'] = row['definition']['id']
    table_row['Pipeline Name'] = row['definition']['name']

    if row['sourceBranch']:
        source_branch = row['sourceBranch']
        if source_branch[0:len(REF_HEADS_PREFIX)] == REF_HEADS_PREFIX:
            source_branch = source_branch[len(REF_HEADS_PREFIX):]
        table_row['Source Branch'] = source_branch
    else:
        table_row['Source Branch'] = ' '

    queued_time = dateutil.parser.parse(row['queueTime']).astimezone(dateutil.tz.tzlocal())
    table_row['Queued Time'] = str(queued_time.date()) + ' ' + str(queued_time.time())
    table_row['Reason'] = row['reason']
    return table_row


def transform_pipelines_pools_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_pipeline_pool_row(item))
    return table_output


def transform_pipelines_pool_table_output(result):
    table_output = [_transform_pipeline_pool_row(result)]
    return table_output


def _transform_pipeline_pool_row(row):
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    table_row['Name'] = row['name']
    table_row['Is Hosted'] = row['isHosted']
    table_row['Pool Type'] = row['poolType']
    return table_row


def transform_pipelines_agents_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_pipeline_agent_row(item))
    return table_output


def transform_pipelines_agent_table_output(result):
    table_output = [_transform_pipeline_agent_row(result)]
    return table_output


def _transform_pipeline_agent_row(row):
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    table_row['Name'] = row['name']
    table_row['Is Enabled'] = row['enabled']
    table_row['Status'] = row['status']
    table_row['Version'] = row['version']
    return table_row


def transform_pipelines_queues_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_pipeline_queue_row(item))
    return table_output


def transform_pipelines_queue_table_output(result):
    table_output = [_transform_pipeline_queue_row(result)]
    return table_output


def _transform_pipeline_queue_row(row):
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    table_row['Name'] = row['name']
    table_row['Pool IsHosted'] = row['pool']['isHosted']
    table_row['Pool Type'] = row['pool']['poolType']
    return table_row


def transform_pipelines_variable_groups_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_pipeline_variable_group_row(item))
    return table_output


def transform_pipelines_variable_group_table_output(result):
    table_output = [_transform_pipeline_variable_group_row(result)]
    return table_output


def _transform_pipeline_variable_group_row(row):
    table_row = OrderedDict()
    table_row['ID'] = row['id']
    table_row['Name'] = row['name']
    table_row['Type'] = row['type']
    table_row['Description'] = row['description']
    if row.get('authorized', None) is not None:
        table_row['Is Authorized'] = row['authorized']
    table_row['Number of Variables'] = len(row['variables'])
    return table_row


def transform_pipelines_variables_table_output(result):
    table_output = []
    for key, value in result.items():
        table_output.append(_transform_pipeline_variable_row(key, value))
    return table_output


def _transform_pipeline_variable_row(key, value):
    table_row = OrderedDict()
    table_row['Name'] = key
    table_row['Allow Override'] = 'True' if value['allowOverride'] else 'False'
    table_row['Is Secret'] = 'True' if value['isSecret'] else 'False'
    val = value['value'] if value['value'] is not None else ''
    if len(val) > _VALUE_TRUNCATION_LENGTH:
        val = val[0:_VALUE_TRUNCATION_LENGTH] + '...'
    table_row['Value'] = val
    return table_row


def transform_pipelines_var_group_variables_table_output(result):
    table_output = []
    for key, value in result.items():
        table_output.append(_transform_pipeline_var_group_variable_row(key, value))
    return table_output


def _transform_pipeline_var_group_variable_row(key, value):
    table_row = OrderedDict()
    table_row['Name'] = key
    table_row['Is Secret'] = 'True' if value['isSecret'] else 'False'
    val = value['value'] if value['value'] is not None else ''
    if len(val) > _VALUE_TRUNCATION_LENGTH:
        val = val[0:_VALUE_TRUNCATION_LENGTH] + '...'
    table_row['Value'] = val
    return table_row


def transform_pipelines_folders_table_output(result):
    table_output = []
    for item in result:
        table_output.append(_transform_pipeline_folder_row(item))
    return table_output


def transform_pipelines_folder_table_output(result):
    table_output = [_transform_pipeline_folder_row(result)]
    return table_output


def _transform_pipeline_folder_row(row):
    table_row = OrderedDict()
    table_row['Path'] = row['path']
    val = row['description'] if row['description'] is not None else ''
    if len(val) > _VALUE_TRUNCATION_LENGTH:
        val = val[0:_VALUE_TRUNCATION_LENGTH] + '...'
    table_row['Description'] = val
    return table_row