Skip to content

Commit b716833

Browse files
author
Bernhard Grünewaldt
committed
insert options from payload file nearly working
1 parent ff51b8a commit b716833

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

customfield_editor_plugin_client/cli_client.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def main():
2323
#
2424
parser = argparse.ArgumentParser(description='Customfield Editor Plugin REST API CLI Client.')
2525

26-
parser.add_argument("-a", "--action", help='Which action to execute.', choices=['adminListFields', 'adminGrantPermission', 'userListFields', 'userListOptions'])
26+
parser.add_argument("-a", "--action", help='Which action to execute.', choices=['adminListFields', 'adminGrantPermission', 'userListFields', 'userListOptions', 'userInsertOptions'])
2727
parser.add_argument("-url", "--baseUrl", help='baseUrl to JIRA instance e.g. http://server:port/jira/')
2828
parser.add_argument("-user", "--authUsername", help='username for basic auth.')
2929
parser.add_argument("-pass", "--authPassword", help='password for basic auth.')
@@ -35,6 +35,7 @@ def main():
3535
parser.add_argument("-ctx", "--contextId", type=int, help='The ID of the custom field context.')
3636
parser.add_argument("-ulist", "--userList", nargs='+', help='space separated user names to grant permission')
3737
parser.add_argument("-glist", "--groupList", nargs='+', help='space separated group names to grant permission')
38+
parser.add_argument("-f", "--payloadFile", help='Payload JSON file.')
3839

3940
args = parser.parse_args()
4041
if not args.action:
@@ -55,6 +56,10 @@ def main():
5556
print (' list options:')
5657
print (' $> cep-client -a userListOptions --customFieldId 10001 -url http://localhost:2990/jira/ -user admin -pass admin')
5758
print (' OPTIONAL: --contextId specify if you want another context other than default context')
59+
print ('')
60+
print (' insert options:')
61+
print (' $> cep-client -a userInsertOptions --customFieldId 10001 -f ./test-data/options-to-insert.json -url http://localhost:2990/jira/ -user admin -pass admin')
62+
print (' FILE FORMAT: https://github.com/codeclou/customfield-editor-plugin/tree/cep-client/test-data/options-to-insert.json ')
5863

5964
sys.exit(1)
6065

@@ -98,8 +103,6 @@ def main():
98103
#
99104
if args.action == 'adminListFields':
100105
adminOperations.list_fields()
101-
102-
103106
if args.action == 'adminGrantPermission':
104107
adminOperations.grant_permission(args.customFieldId, args.userList, args.groupList)
105108

@@ -109,9 +112,15 @@ def main():
109112
#
110113
if args.action == 'userListFields':
111114
userOperations.list_fields()
112-
113115
if args.action == 'userListOptions':
114116
userOperations.list_options(args.customFieldId, args.contextId)
117+
if args.action == 'userInsertOptions':
118+
try:
119+
with open(args.payloadFile) as payloadFile:
120+
userOperations.insert_options(args.customFieldId, args.contextId, payloadFile)
121+
except FileNotFoundError as ex:
122+
printHelper.error('payloadFile not found: {0}'.format(args.payloadFile))
123+
raise ApiHelperException('payloadFile not found')
115124

116125

117126
except requests.ConnectionError as ex:

customfield_editor_plugin_client/helper/api_helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ def _handle_response_errors(self, response):
6666
elif response.status_code == 204:
6767
self.printHelper.success('request successful')
6868
return
69+
elif response.status_code == 400:
70+
self.printHelper.error(response.json())
71+
raise ApiHelperException(401)
6972
elif response.status_code == 401:
7073
self.printHelper.error('UNAUTHORIZED (401). Authorization failed (wrong or no credentials).')
7174
raise ApiHelperException(401)

customfield_editor_plugin_client/modules/user_operations.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import json
12
from customfield_editor_plugin_client.helper.print_helper import PrintHelper
23
from customfield_editor_plugin_client.helper.api_helper import ApiHelper
4+
from customfield_editor_plugin_client.api_helper_exception import ApiHelperException
35

46
class UserOperations:
57

@@ -18,3 +20,23 @@ def list_options(self, customfield_id, context_id):
1820
response_json = self._api.get('/user/customfields/{0}/contexts/default/options'.format(customfield_id))
1921

2022
self._print.table(response_json)
23+
24+
25+
def insert_options(self, customfield_id, context_id, options):
26+
optionsJson = json.load(options)
27+
print (optionsJson)
28+
if context_id:
29+
context_infix = context_id
30+
else:
31+
context_infix = 'default'
32+
33+
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)
42+

test-data/options-to-insert.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"optionvalue" : "windows"
4+
},
5+
{
6+
"optionvalue" : "linux"
7+
},
8+
{
9+
"optionvalue" : "mac",
10+
"default": true
11+
}
12+
]

0 commit comments

Comments
 (0)