Skip to content

Commit ebb7cbb

Browse files
author
Bernhard Grünewaldt
committed
insert options and setting default value working
1 parent b716833 commit ebb7cbb

File tree

7 files changed

+69
-43
lines changed

7 files changed

+69
-43
lines changed

customfield_editor_plugin_client/cli_client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import colorama
44
import requests
55
from .helper.api_helper import ApiHelper
6-
from .helper.api_helper_exception import ApiHelperException
6+
from .helper.api_helper_exceptions import ApiHelperException
7+
from .helper.api_helper_exceptions import ValidationErrorsException
78
from .helper.print_helper import PrintHelper
89
from .model.user_input import UserInput
910
from .model.user_input_exception import UserInputException
@@ -119,7 +120,7 @@ def main():
119120
with open(args.payloadFile) as payloadFile:
120121
userOperations.insert_options(args.customFieldId, args.contextId, payloadFile)
121122
except FileNotFoundError as ex:
122-
printHelper.error('payloadFile not found: {0}'.format(args.payloadFile))
123+
printHelper.error('payloadFile not found')
123124
raise ApiHelperException('payloadFile not found')
124125

125126

@@ -129,7 +130,7 @@ def main():
129130
print (ex)
130131
sys.exit(1)
131132
except ApiHelperException as ex:
132-
print ('')
133+
printHelper.pretty(ex.args[0])
133134
printHelper.error('There seemed to a be problem with your request. Check errors above. EXIT')
134135
sys.exit(1)
135136

customfield_editor_plugin_client/helper/api_helper.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import requests
22
import textwrap
33
import tabulate
4-
from .api_helper_exception import ApiHelperException
4+
from .api_helper_exceptions import ApiHelperException
5+
from .api_helper_exceptions import ValidationErrorsException
6+
57
from .print_helper import PrintHelper
68

79
class ApiHelper:
@@ -15,22 +17,30 @@ def __init__(self, userInput):
1517
def post(self, urlpart, payload):
1618
request_url = self._rest_url(urlpart)
1719
self.printHelper.step('POST ' + request_url)
18-
response = requests.post(request_url, json=payload, auth=(self.userInput.authUserName, self.userInput.authPassword))
19-
self._handle_response_errors(response)
2020
try:
21-
return response.json()
22-
except ValueError as ex:
23-
return {}
21+
response = requests.post(request_url, json=payload, auth=(self.userInput.authUserName, self.userInput.authPassword))
22+
self._handle_response_errors(response)
23+
try:
24+
return response.json()
25+
except ValueError as ex:
26+
return {}
27+
except ValidationErrorsException as ex:
28+
self.printHelper.warn('Validation Errors')
29+
self.printHelper.table(ex.args[0]['errors'])
2430

2531
def put(self, urlpart, payload):
2632
request_url = self._rest_url(urlpart)
2733
self.printHelper.step('PUT ' + request_url)
28-
response = requests.put(request_url, json=payload, auth=(self.userInput.authUserName, self.userInput.authPassword))
29-
self._handle_response_errors(response)
3034
try:
31-
return response.json()
32-
except ValueError as ex:
33-
return {}
35+
response = requests.put(request_url, json=payload, auth=(self.userInput.authUserName, self.userInput.authPassword))
36+
self._handle_response_errors(response)
37+
try:
38+
return response.json()
39+
except ValueError as ex:
40+
return {}
41+
except ValidationErrorsException as ex:
42+
self.printHelper.warn('Validation Errors')
43+
self.printHelper.table(ex.args[0]['errors'])
3444

3545
def get(self, urlpart):
3646
request_url = self._rest_base_url() + urlpart
@@ -61,20 +71,14 @@ def _rest_url(self, url_part):
6171

6272
def _handle_response_errors(self, response):
6373
if response.status_code == 200:
64-
self.printHelper.success('request successful')
6574
return
6675
elif response.status_code == 204:
67-
self.printHelper.success('request successful')
6876
return
6977
elif response.status_code == 400:
70-
self.printHelper.error(response.json())
71-
raise ApiHelperException(401)
78+
raise ValidationErrorsException(response.json())
7279
elif response.status_code == 401:
73-
self.printHelper.error('UNAUTHORIZED (401). Authorization failed (wrong or no credentials).')
74-
raise ApiHelperException(401)
80+
raise ApiHelperException('UNAUTHORIZED (401). Authorization failed (wrong or no credentials).')
7581
elif response.status_code == 403:
76-
self.printHelper.error('FORBIDDEN (403). Insufficient rights - OR - too many failed login attempts. (Log into JIRA in the browser to solve CAPTCHA).')
77-
raise ApiHelperException(403)
82+
raise ApiHelperException('FORBIDDEN (403). Insufficient rights - OR - too many failed login attempts. (Log into JIRA in the browser to solve CAPTCHA).')
7883
else:
79-
self.printHelper.error('request failed with HTTP {0}'.format(response.status_code))
80-
raise ApiHelperException(response.status_code)
84+
raise ApiHelperException('request failed with HTTP {0}'.format(response.status_code))

customfield_editor_plugin_client/helper/api_helper_exception.py

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class ApiHelperException(Exception):
2+
def __init__(self, value):
3+
self.value = value
4+
def __str__(self):
5+
return repr(self.value)
6+
7+
class ValidationErrorsException(Exception):
8+
def __init__(self, value):
9+
self.value = value
10+
def __str__(self):
11+
return repr(self.value)

customfield_editor_plugin_client/helper/print_helper.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import colorama
33
import textwrap
44
import tabulate
5+
import pprint
6+
57
class PrintHelper:
68

79
def _success_prefix(self):
@@ -29,5 +31,13 @@ def headline(self, text):
2931
def action(self, text):
3032
print(colorama.Fore.MAGENTA + '\n# Action: ' + text)
3133

34+
def warn(self, text):
35+
print(colorama.Fore.YELLOW + ' > ' + text)
36+
3237
def table(self, json):
3338
print(textwrap.indent(tabulate.tabulate(json, headers="keys"), ' '))
39+
40+
def pretty(self, json):
41+
self._pp = pprint.PrettyPrinter()
42+
self._pp.pprint(json)
43+
#print(textwrap.indent(, ' '))

customfield_editor_plugin_client/modules/user_operations.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from customfield_editor_plugin_client.helper.print_helper import PrintHelper
33
from customfield_editor_plugin_client.helper.api_helper import ApiHelper
4-
from customfield_editor_plugin_client.api_helper_exception import ApiHelperException
4+
from customfield_editor_plugin_client.helper.api_helper_exceptions import ApiHelperException
55

66
class UserOperations:
77

@@ -24,19 +24,24 @@ def list_options(self, customfield_id, context_id):
2424

2525
def insert_options(self, customfield_id, context_id, options):
2626
optionsJson = json.load(options)
27-
print (optionsJson)
27+
self._print.table(optionsJson)
2828
if context_id:
2929
context_infix = context_id
3030
else:
3131
context_infix = 'default'
3232

3333
for option in optionsJson:
34-
try:
35-
response_json = self._api.post('/user/customfields/{0}/contexts/{1}/options'.format(customfield_id, context_infix),
36-
{
37-
"optionvalue": option['optionvalue']
38-
})
39-
#self._print.table(response_json)
40-
except ApiHelperException as ex:
41-
print (ex)
34+
created_option = self._api.post('/user/customfields/{0}/contexts/{1}/options'.format(customfield_id, context_infix),
35+
{
36+
"optionvalue": option['optionvalue']
37+
})
38+
if created_option and 'id' in created_option:
39+
self._print.pretty(created_option)
40+
if 'default' in option:
41+
self._api.post('/user/customfields/{0}/contexts/{1}/options/default'.format(customfield_id, context_infix),
42+
{
43+
"optionId": created_option['id']
44+
})
45+
46+
4247

test-data/options-to-insert.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[
22
{
3-
"optionvalue" : "windows"
3+
"optionvalue" : "windows7"
44
},
55
{
6-
"optionvalue" : "linux"
6+
"optionvalue" : "linux7"
77
},
88
{
9-
"optionvalue" : "mac",
10-
"default": true
9+
"optionvalue" : "mac7",
10+
"default": "true"
1111
}
1212
]

0 commit comments

Comments
 (0)