Skip to content

Commit 567c73a

Browse files
author
Bernhard Grünewaldt
committed
better usage and input validation depending on action.
user list options implemented
1 parent 1b74528 commit 567c73a

File tree

4 files changed

+84
-17
lines changed

4 files changed

+84
-17
lines changed

customfield_editor_plugin_client/cli_client.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,55 @@ def main():
1616
#
1717
colorama.init(autoreset=True)
1818
printHelper = PrintHelper()
19-
print(colorama.Fore.CYAN + '\n~~ REST API Client for Customfield Editor Plugin ~~ v1.2rc1')
19+
print(colorama.Fore.CYAN + '\n~~ REST API Client for Customfield Editor Plugin ~~ v1.2rc1\n')
2020

2121
#
2222
# CLI PARAMS
2323
#
2424
parser = argparse.ArgumentParser(description='Customfield Editor Plugin REST API CLI Client.')
25+
26+
parser.add_argument("-a", "--action", help='Which action to execute.', choices=['adminListFields', 'adminGrantPermission', 'userListFields', 'userListOptions'])
27+
parser.add_argument("-url", "--baseUrl", help='baseUrl to JIRA instance e.g. http://server:port/jira/')
28+
parser.add_argument("-user", "--authUsername", help='username for basic auth.')
29+
parser.add_argument("-pass", "--authPassword", help='password for basic auth.')
30+
31+
#
32+
# CLI PARAMS depending on action
33+
#
2534
parser.add_argument("-cid", "--customFieldId", type=int, help='The ID of the custom field.')
35+
parser.add_argument("-ctx", "--contextId", type=int, help='The ID of the custom field context.')
2636
parser.add_argument("-ulist", "--userList", nargs='+', help='space separated user names to grant permission')
2737
parser.add_argument("-glist", "--groupList", nargs='+', help='space separated group names to grant permission')
28-
parser.add_argument("-a", "--action", help='Which action to execute.', choices=['adminListFields', 'adminGrantPermission', 'userListFields'], required=True)
29-
parser.add_argument("-url", "--baseUrl", help='baseUrl to JIRA instance e.g. http://server:port/jira/', required=True)
30-
parser.add_argument("-user", "--authUsername", help='username for basic auth.', required=True)
31-
parser.add_argument("-pass", "--authPassword", help='password for basic auth.', required=True)
38+
3239
args = parser.parse_args()
40+
if not args.action:
41+
printHelper.headline('USAGE')
42+
printHelper.step('admin operations')
43+
print (' list fields:')
44+
print (' $> cep-client -a adminListFields -url http://localhost:2990/jira/ -user admin -pass admin')
45+
print ('')
46+
print (' grant permissions:')
47+
print (' $> cep-client -a adminGrantPermission --customFieldId 10100 --userList bob steve \ \n'
48+
' -url http://localhost:2990/jira/ -user admin -pass admin')
49+
print (' OPTIONAL: --groupList with one or more groupnames')
50+
51+
printHelper.step('user operations')
52+
print (' list fields:')
53+
print (' $> cep-client -a userListFields -url http://localhost:2990/jira/ -user admin -pass admin')
54+
print ('')
55+
print (' list options:')
56+
print (' $> cep-client -a userListOptions --customFieldId 10001 -url http://localhost:2990/jira/ -user admin -pass admin')
57+
# $> cep-client -a adminGrantPermission --customFieldId 10100 --userList bob steve -url http://localhost:2990/jira -user admin -pass admin
58+
# (optional: --groupList)
59+
sys.exit(1)
3360

3461

3562
#
3663
# INITIALIZE HELPERS
3764
#
3865
printHelper.step('initializing')
3966
try:
40-
userInput = UserInput(args.baseUrl, args.authUsername, args.authPassword)
67+
userInput = UserInput(args)
4168
except UserInputException as ex:
4269
printHelper.error('UserInput is invalid.')
4370
print (ex)
@@ -68,13 +95,11 @@ def main():
6895
#
6996
# ADMIN ACTIONS
7097
#
71-
# $> cep-client -a adminListFields -url http://localhost:2990/jira/ -user admin -pass admin
98+
#
7299
if args.action == 'adminListFields':
73-
74100
adminOperations.list_fields()
75101

76-
#> cep-client -a adminGrantPermission --customFieldId 10100 --userList bob steve -url http://localhost:2990/jira -user admin -pass admin
77-
# (optional: --groupList)
102+
78103
if args.action == 'adminGrantPermission':
79104
adminOperations.grant_permission(args.customFieldId, args.userList, args.groupList)
80105

@@ -85,6 +110,8 @@ def main():
85110
if args.action == 'userListFields':
86111
userOperations.list_fields()
87112

113+
if args.action == 'userListOptions':
114+
userOperations.list_options(args.customFieldId, args.contextId)
88115

89116

90117
except requests.ConnectionError as ex:

customfield_editor_plugin_client/helper/print_helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def error(self, text):
2323
def step(self, text):
2424
print(colorama.Fore.CYAN + '\n> ' + text)
2525

26+
def headline(self, text):
27+
print(colorama.Fore.MAGENTA + '\n# ' + text)
28+
2629
def action(self, text):
2730
print(colorama.Fore.MAGENTA + '\n# Action: ' + text)
2831

customfield_editor_plugin_client/model/user_input.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,49 @@
33

44
class UserInput:
55

6-
def __init__(self, baseurl, username, password):
7-
self.baseUrl = baseurl
8-
self.authUserName = username
9-
self.authPassword = password
6+
def __init__(self, cli_args):
7+
self.baseUrl = cli_args.baseUrl
8+
self.authUserName = cli_args.authUsername
9+
self.authPassword = cli_args.authPassword
10+
self.validate_cli_args(cli_args)
1011

1112
@property
12-
def baseUrl(self):
13-
return self.__baseUrl
13+
def authUserName(self):
14+
return self._auth_username
15+
@authUserName.setter
16+
def authUserName(self, value):
17+
if not value:
18+
raise UserInputException('authUserName needs to be set.')
19+
self._auth_username = value
20+
21+
@property
22+
def authPassword(self):
23+
return self._auth_password
24+
@authPassword.setter
25+
def authPassword(self, value):
26+
if not value:
27+
raise UserInputException('authPassword needs to be set.')
28+
self._auth_password = value
1429

30+
@property
31+
def baseUrl(self):
32+
return self._base_url
1533
@baseUrl.setter
1634
def baseUrl(self, value):
1735
if not validators.url(value, require_tld=False):
1836
raise UserInputException('baseUrl needs to be valid URL.')
1937
if not value.endswith('/'):
2038
value = value + '/'
39+
self._base_url = value
40+
41+
42+
def validate_cli_args(self, cli_args):
43+
if cli_args.action == 'adminGrantPermission':
44+
self._validate_customfield_id(cli_args.customFieldId)
45+
46+
if cli_args.action == 'userListOptions':
47+
self._validate_customfield_id(cli_args.customFieldId)
2148

22-
self.__baseUrl = value
49+
def _validate_customfield_id(self, id):
50+
if id <= 0:
51+
raise UserInputException('customFieldId needs to be positive integer.')

customfield_editor_plugin_client/modules/user_operations.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,11 @@ def __init__(self, api_helper):
1010
def list_fields(self):
1111
response_json = self._api.get('/user/customfields')
1212
self._print.table(response_json)
13+
14+
def list_options(self, customfield_id, context_id):
15+
if context_id:
16+
response_json = self._api.get('/user/customfields/{0}/contexts/{1}/options'.format(customfield_id, context_id))
17+
else:
18+
response_json = self._api.get('/user/customfields/{0}/contexts/default/options'.format(customfield_id))
19+
20+
self._print.table(response_json)

0 commit comments

Comments
 (0)