-
Notifications
You must be signed in to change notification settings - Fork 1
/
configlib.py
135 lines (108 loc) · 4.55 KB
/
configlib.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
"""
This module offers helper functions to be used in ``config.py``.
Functions in this module operate on JSON objects, which are a whole response or
part of a response from one of JIRA REST API methods.
- `Official Atlassian documentation of JIRA REST API
<https://docs.atlassian.com/software/jira/docs/api/REST/7.1.6>`_
"""
def get_jira_field(issue_json, field_key: str,
default_value: str = None) -> str:
"""Get a value from a simple predefined field of an issue.
:param issue_json:
The JSON object to examine, which represents one issue (ticket),
returned by method `issue` of JIRA REST API.
:param field_key:
JSON key of the predefined field.
:param default_value:
String to return, when the field is absent or its value is not defined.
"""
return _get_field(issue_json, field_key, default_value, 'name')
def get_custom_field(issue_json, field_key: str,
default_value: str = None) -> str:
"""Get a value from a simple custom field of an issue.
:param issue_json:
The JSON object to examine, which represents one issue (ticket),
returned by method `issue` of JIRA REST API.
:param field_key:
JSON key of the custom field.
:param default_value:
String to return, when the field is absent or its value is not defined.
"""
return _get_field(issue_json, field_key, default_value, 'value')
def get_compound_jira_field(issue_json, field_key: str,
default_value: str = None,
several_name: str = "Several") -> str:
"""Get a value from a compound predefined field of an issue.
:param issue_json:
The JSON object to examine, which represents one issue (ticket),
returned by method `issue` of JIRA REST API.
:param field_key:
JSON key of the predefined field.
:param default_value:
String to return, when the field is absent or its value is not defined.
:param several_name:
String to return, when the field has more than one value.
"""
return _get_compound_field(issue_json, field_key, default_value,
several_name, 'name')
def get_compound_custom_field(issue_json, field_key: str,
default_value: str = None,
several_name: str = "Several") -> str:
"""Get a value from a compound custom field of an issue.
:param issue_json:
The JSON object to examine, which represents one issue (ticket),
returned by method `issue` of JIRA REST API.
:param field_key:
JSON key of the custom field.
:param default_value:
String to return, when the field is absent or its value is not defined.
:param several_name:
String to return, when the field has more than one value.
"""
return _get_compound_field(issue_json, field_key, default_value,
several_name, 'value')
def is_field_change(changelog_entry, field_key: str) -> bool:
"""Check if changelog entry contains field value change.
:param changelog_entry:
Changelog item to examime.
:param field_key:
Field to check.
:return:
"""
def is_field_change_item(changelog_item):
return _is_field_change_item(changelog_item, field_key)
if 'items' in changelog_entry:
return any(map(is_field_change_item, changelog_entry['items']))
return False
def _is_field_change_item(changelog_item, field_key: str) -> bool:
return 'field' in changelog_item and changelog_item['field'] == field_key
def _get_field(issue_json, field_key: str, default_value: str = None,
subscript: str = None) -> str:
field = _extract_field(issue_json, field_key)
if field is None:
return default_value
if subscript:
return field[subscript]
return field
def _get_compound_field(issue_json, field_key: str, default_value: str = None,
several_name: str = "Several",
subscript: str = None) -> str:
field = _extract_field(issue_json, field_key)
if field is None:
return default_value
if len(field) == 1:
if subscript:
return field[0][subscript]
return field[0]
else:
return several_name
def _extract_field(issue_json, field_key: str):
if 'fields' not in issue_json:
return None
fields = issue_json['fields']
if field_key not in fields:
return None
field = fields[field_key]
if not field:
return None
return field