Skip to content

Commit

Permalink
jira: Render list items with commas in quotes
Browse files Browse the repository at this point in the history
When you have a component or name with a comma, simply joining
the list together does not provide output you could quote and use
as input, and makes it confusing where the component name begins
and ends without looking at the component list.

So, for example, one can see:

   "component1, desc1", component2, component3

... in the output instead of:

   component1, desc1, component2, component3

... when looking at issues, in addition to:

$ jirate field TEST-1 set components '"component1, desc1", component2'
  • Loading branch information
lhh committed Jan 22, 2024
1 parent e1cad6a commit d63c502
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
10 changes: 10 additions & 0 deletions jirate/decor.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ def parse_params(arg):
return ret


def comma_separated(item_list):
out = []
for item in item_list:
if item and ',' in item:
out.append(f'"{item}"')
else:
out.append(item)
return ', '.join(out)


def truncate(arg, maxlen):
if arg and maxlen and len(arg) > maxlen:
arg = arg[:maxlen - 1] + '…'
Expand Down
6 changes: 3 additions & 3 deletions jirate/jira_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from jirate.args import ComplicatedArgs, GenericArgs
from jirate.jboard import JiraProject, get_jira
from jirate.decor import md_print, pretty_date, color_string, hbar_under, hbar, hbar_over, nym, vsep_print, vseparator, parse_params, truncate, render_matrix
from jirate.decor import md_print, pretty_date, color_string, hbar_under, hbar, hbar_over, nym, vsep_print, vseparator, parse_params, truncate, render_matrix, comma_separated
from jirate.decor import pretty_print # NOQA
from jirate.config import get_config
from jirate.jira_fields import apply_field_renderers, render_issue_fields, max_field_width, render_field_data
Expand Down Expand Up @@ -290,7 +290,7 @@ def issue_fields(args):
values.append(val['value'])
else:
values.append(val['id'])
fvalue = ', '.join(values)
fvalue = comma_separated(values)
vsep_print(' ', fname, nlen, fvalue)
return (0, False)

Expand Down Expand Up @@ -399,7 +399,7 @@ def print_creation_fields(metadata):
values.append(val['value'])
else:
values.append(val['id'])
fvalue = ', '.join(values)
fvalue = comma_separated(values)
vsep_print(' ', fname, nlen, req, 1, fvalue)


Expand Down
8 changes: 4 additions & 4 deletions jirate/jira_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import re # NOQA
from collections import OrderedDict
from jirate.decor import pretty_date, vsep_print
from jirate.decor import pretty_date, vsep_print, comma_separated


#
# Field rendering functions. Return a string, or None if you want the field
# suppressed.
#
def _list_of_key(field, key):
return ', '.join([item[key] for item in field])
return comma_separated([item[key] for item in field])


def string(field, fields):
Expand All @@ -21,7 +21,7 @@ def auto_field(field, fields):
if isinstance(field, str):
return field
if isinstance(field, list):
return ', '.join([str(item) for item in field])
return comma_separated([str(item) for item in field])
if isinstance(field, dict):
for key in ['name', 'value']:
if key in field:
Expand Down Expand Up @@ -61,7 +61,7 @@ def user_list(field, fields):


def array(field, fields):
return ', '.join(field)
return comma_separated(field)


def value_list(field, fields):
Expand Down
6 changes: 3 additions & 3 deletions jirate/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@
'timeZone': 'America/New_York'},
'attachment': [],
'comment': [],
'components': [],
'components': [{'name': 'food, pork'}, {'name': 'food, carrot'}],
'created': '2023-08-03T10:28:48.366+0000',
'description': 'Test Description 1',
'duedate': '2024-01-30',
Expand Down Expand Up @@ -358,7 +358,7 @@
{'name': 'Version2', 'value': 'version_two'}],
'customfield_1234571': [{'key': 'user1', 'name': 'user-one', 'displayName': 'One', 'emailAddress': '[email protected]'},
{'key': 'user2', 'name': '[email protected]', 'displayName': 'Two', 'emailAddress': '[email protected]'}],
'customfield_1234572': ['one', 'two', 'three'],
'customfield_1234572': ['one', 'two', 'three, and four'],
'customfield_1234573': [{'name': 'group1'}, {'name': 'group2'}],
'customfield_1234574': ['one', 2.0],
'customfield_1234575': '2022-08-01',
Expand Down Expand Up @@ -389,7 +389,7 @@
'assignee': None,
'attachment': [],
'comment': [],
'components': ['porkchop'],
'components': [],
'created': '2023-08-03T10:28:48.366+0000',
'customfield_1234567': None,
'customfield_1234568': None,
Expand Down
19 changes: 17 additions & 2 deletions jirate/tests/test_decor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3

from jirate.decor import truncate, parse_params
from jirate.decor import truncate, parse_params, comma_separated


def test_parse_simple():
Expand All @@ -9,7 +9,7 @@ def test_parse_simple():

def test_parse_commas():
assert parse_params('arg,barg') == ['arg', 'barg']
assert parse_params('"Comma,Test, ", 2') == ['Comma,Test,', "2"]
assert parse_params('"Comma, Test, ", 2') == ['Comma, Test,', "2"]


def test_parse_spaces():
Expand All @@ -30,3 +30,18 @@ def test_truncate_identity():
def test_truncate_exceed():
assert truncate('abc', 3) == 'abc'
assert truncate('abcd', 3) == 'ab…'


def test_comma_separated():
str_val = 'one, two, "three, and four"'
list_val = ['one', 'two', 'three, and four']

assert comma_separated(list_val) == str_val


def test_parse_and_unparse():
str_val = 'one, two, "three, and four"'
list_val = ['one', 'two', 'three, and four']

assert parse_params(comma_separated(list_val)) == list_val
assert comma_separated(parse_params(str_val)) == str_val
3 changes: 2 additions & 1 deletion jirate/tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def test_render_code_override():

field_test_params = 'field_id,field_name,value'
field_test_info = [
pytest.param('components', 'Component/s', '"food, pork", "food, carrot"'),
# Fixed in build (string)
pytest.param('customfield_1234567', 'Fixed in Build', 'test-build-1'),

Expand All @@ -86,7 +87,7 @@ def test_render_code_override():
pytest.param('customfield_1234571', 'Array of Users', 'user-one, [email protected]'),

# Array of strings
pytest.param('customfield_1234572', 'Array of Strings', 'one, two, three'),
pytest.param('customfield_1234572', 'Array of Strings', 'one, two, "three, and four"'),

# Array of groups (name?),
pytest.param('customfield_1234573', 'Array of Groups', 'group1, group2'),
Expand Down

0 comments on commit d63c502

Please sign in to comment.